C2MV commited on
Commit
c799bb2
1 Parent(s): 44eec3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -22
app.py CHANGED
@@ -350,13 +350,13 @@ class RSM_BoxBehnken:
350
  if self.model_simplified is None:
351
  print("Error: Ajusta el modelo simplificado primero.")
352
  return None
353
-
354
  # ANOVA del modelo simplificado
355
  anova_table = sm.stats.anova_lm(self.model_simplified, typ=2)
356
-
357
  # Suma de cuadrados total
358
  ss_total = anova_table['sum_sq'].sum()
359
-
360
  # Crear tabla de contribución
361
  contribution_table = pd.DataFrame({
362
  'Fuente de Variación': [],
@@ -370,24 +370,58 @@ class RSM_BoxBehnken:
370
 
371
  # Calcular estadísticos F y porcentaje de contribución para cada factor
372
  ms_error = anova_table.loc['Residual', 'sum_sq'] / anova_table.loc['Residual', 'df']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
 
 
374
  for index, row in anova_table.iterrows():
375
  if index != 'Residual':
376
  factor_name = index
377
  if factor_name == f'I({self.x1_name} ** 2)':
378
- factor_name = f'{self.x1_name}^2'
379
  elif factor_name == f'I({self.x2_name} ** 2)':
380
- factor_name = f'{self.x2_name}^2'
381
  elif factor_name == f'I({self.x3_name} ** 2)':
382
- factor_name = f'{self.x3_name}^2'
383
-
384
  ss_factor = row['sum_sq']
385
  df_factor = row['df']
386
  ms_factor = ss_factor / df_factor
387
  f_stat = ms_factor / ms_error
388
  p_value = f.sf(f_stat, df_factor, anova_table.loc['Residual', 'df'])
389
  contribution_percentage = (ss_factor / ss_total) * 100
390
-
391
  contribution_table = pd.concat([contribution_table, pd.DataFrame({
392
  'Fuente de Variación': [factor_name],
393
  'Suma de Cuadrados': [ss_factor],
@@ -397,22 +431,32 @@ class RSM_BoxBehnken:
397
  'Valor p': [p_value],
398
  '% Contribución': [contribution_percentage]
399
  })], ignore_index=True)
400
-
401
- # Calcular estadístico F global y su valor p
402
- f_global = anova_table['sum_sq'][:-1].sum() / anova_table['df'][:-1].sum() / ms_error
403
- p_global = f.sf(f_global, anova_table['df'][:-1].sum(), anova_table.loc['Residual', 'df'])
404
-
405
- # Agregar fila para el estadístico F global
406
  contribution_table = pd.concat([contribution_table, pd.DataFrame({
407
- 'Fuente de Variación': ['F global'],
408
- 'Suma de Cuadrados': [np.nan],
409
- 'Grados de Libertad': [np.nan],
410
- 'Cuadrado Medio': [np.nan],
411
- 'F': [f_global],
412
- 'Valor p': [p_global],
413
- '% Contribución': [np.nan]
414
  })], ignore_index=True)
415
-
 
 
 
 
 
 
 
 
 
 
 
416
  return contribution_table.round(3)
417
 
418
  def calculate_detailed_anova(self):
 
350
  if self.model_simplified is None:
351
  print("Error: Ajusta el modelo simplificado primero.")
352
  return None
353
+
354
  # ANOVA del modelo simplificado
355
  anova_table = sm.stats.anova_lm(self.model_simplified, typ=2)
356
+
357
  # Suma de cuadrados total
358
  ss_total = anova_table['sum_sq'].sum()
359
+
360
  # Crear tabla de contribución
361
  contribution_table = pd.DataFrame({
362
  'Fuente de Variación': [],
 
370
 
371
  # Calcular estadísticos F y porcentaje de contribución para cada factor
372
  ms_error = anova_table.loc['Residual', 'sum_sq'] / anova_table.loc['Residual', 'df']
373
+
374
+ # Agregar Block (si está disponible en los datos)
375
+ block_ss = self.data.groupby('Block')['AIA_ppm'].sum().var() if 'Block' in self.data.columns else 0
376
+ if block_ss > 0:
377
+ block_df = len(self.data['Block'].unique()) - 1 if 'Block' in self.data.columns else 1
378
+ block_ms = block_ss / block_df
379
+ block_f = block_ms / ms_error
380
+ block_p = f.sf(block_f, block_df, anova_table.loc['Residual', 'df'])
381
+ contribution_table = pd.concat([contribution_table, pd.DataFrame({
382
+ 'Fuente de Variación': ['Block'],
383
+ 'Suma de Cuadrados': [block_ss],
384
+ 'Grados de Libertad': [block_df],
385
+ 'Cuadrado Medio': [block_ms],
386
+ 'F': [block_f],
387
+ 'Valor p': [block_p],
388
+ '% Contribución': [(block_ss / ss_total) * 100]
389
+ })], ignore_index=True)
390
+
391
+ # Agregar Model (suma de todos los términos del modelo excepto el residual)
392
+ model_ss = anova_table['sum_sq'][:-1].sum() # Excluir residual
393
+ model_df = anova_table['df'][:-1].sum()
394
+ model_ms = model_ss / model_df
395
+ model_f = model_ms / ms_error
396
+ model_p = f.sf(model_f, model_df, anova_table.loc['Residual', 'df'])
397
+ contribution_table = pd.concat([contribution_table, pd.DataFrame({
398
+ 'Fuente de Variación': ['Model'],
399
+ 'Suma de Cuadrados': [model_ss],
400
+ 'Grados de Libertad': [model_df],
401
+ 'Cuadrado Medio': [model_ms],
402
+ 'F': [model_f],
403
+ 'Valor p': [model_p],
404
+ '% Contribución': [(model_ss / ss_total) * 100]
405
+ })], ignore_index=True)
406
 
407
+ # Agregar factores individuales y sus interacciones
408
  for index, row in anova_table.iterrows():
409
  if index != 'Residual':
410
  factor_name = index
411
  if factor_name == f'I({self.x1_name} ** 2)':
412
+ factor_name = f'{self.x1_name}²'
413
  elif factor_name == f'I({self.x2_name} ** 2)':
414
+ factor_name = f'{self.x2_name}²'
415
  elif factor_name == f'I({self.x3_name} ** 2)':
416
+ factor_name = f'{self.x3_name}²'
417
+
418
  ss_factor = row['sum_sq']
419
  df_factor = row['df']
420
  ms_factor = ss_factor / df_factor
421
  f_stat = ms_factor / ms_error
422
  p_value = f.sf(f_stat, df_factor, anova_table.loc['Residual', 'df'])
423
  contribution_percentage = (ss_factor / ss_total) * 100
424
+
425
  contribution_table = pd.concat([contribution_table, pd.DataFrame({
426
  'Fuente de Variación': [factor_name],
427
  'Suma de Cuadrados': [ss_factor],
 
431
  'Valor p': [p_value],
432
  '% Contribución': [contribution_percentage]
433
  })], ignore_index=True)
434
+
435
+ # Agregar Residual
436
+ residual_ss = anova_table.loc['Residual', 'sum_sq']
437
+ residual_df = anova_table.loc['Residual', 'df']
438
+ residual_ms = residual_ss / residual_df
 
439
  contribution_table = pd.concat([contribution_table, pd.DataFrame({
440
+ 'Fuente de Variación': ['Residual'],
441
+ 'Suma de Cuadrados': [residual_ss],
442
+ 'Grados de Libertad': [residual_df],
443
+ 'Cuadrado Medio': [residual_ms],
444
+ 'F': [None],
445
+ 'Valor p': [None],
446
+ '% Contribución': [(residual_ss / ss_total) * 100]
447
  })], ignore_index=True)
448
+
449
+ # Agregar Correlation Total
450
+ contribution_table = pd.concat([contribution_table, pd.DataFrame({
451
+ 'Fuente de Variación': ['Cor Total'],
452
+ 'Suma de Cuadrados': [ss_total],
453
+ 'Grados de Libertad': [len(self.data) - 1],
454
+ 'Cuadrado Medio': [None],
455
+ 'F': [None],
456
+ 'Valor p': [None],
457
+ '% Contribución': [100]
458
+ })], ignore_index=True)
459
+
460
  return contribution_table.round(3)
461
 
462
  def calculate_detailed_anova(self):