[…] all models are approximations. Essentially, all models are wrong, but some are useful. However, the approximate nature of the model must always be borne in mind […]

— George Box (1987, Empirical Model-Building and Response Surfaces, p. 424)


t5.16 <- Table5.16 %>% 
  as_tibble() %>% 
  mutate(
    A = factor(TotalAngle),
    B = factor(CuttingSpeed)
  )


modelo <- lm(ToolLife ~ A * B, data = t5.16)

print(anova(modelo))
## Analysis of Variance Table
## 
## Response: ToolLife
##           Df Sum Sq Mean Sq F value   Pr(>F)   
## A          2 24.333 12.1667  8.4231 0.008676 **
## B          2 25.333 12.6667  8.7692 0.007703 **
## A:B        4 61.333 15.3333 10.6154 0.001844 **
## Residuals  9 13.000  1.4444                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
print(summary(modelo))
## 
## Call:
## lm(formula = ToolLife ~ A * B, data = t5.16)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##   -1.5   -0.5    0.0    0.5    1.5 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept) -1.500e+00  8.498e-01  -1.765  0.11137   
## A20          2.500e+00  1.202e+00   2.080  0.06725 . 
## A25          1.000e+00  1.202e+00   0.832  0.42691   
## B150        -1.776e-15  1.202e+00   0.000  1.00000   
## B175         4.000e+00  1.202e+00   3.328  0.00882 **
## A20:B150     1.000e+00  1.700e+00   0.588  0.57077   
## A25:B150     6.000e+00  1.700e+00   3.530  0.00641 **
## A20:B175     3.535e-15  1.700e+00   0.000  1.00000   
## A25:B175    -4.000e+00  1.700e+00  -2.353  0.04306 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.202 on 9 degrees of freedom
## Multiple R-squared:  0.8952, Adjusted R-squared:  0.802 
## F-statistic: 9.606 on 8 and 9 DF,  p-value: 0.001337

Examinando os gráficos de diagnísticos não encontramos evidência de problemas severos:

t5.16_mdl <- t5.16 %>% 
  mutate(Resíduos = resid(modelo),
         Ajustado = fitted(modelo))


p1 <- t5.16_mdl %>% ggplot(aes(sample = Resíduos)) +
  stat_qq() +
  stat_qq_line(color = "red")  +
  ggtitle("Gráfico de probabilidade Normal") +
  labs(x = "Quantis Teórico", y  = 'Resíduos') 


p2 <- t5.16_mdl %>% ggplot() +
  geom_point(aes(x = Ajustado, y = Resíduos)) +
  ggtitle("Ajustados vs. Residuos") +
  labs(x = "Ajustado", y = "Resíduos")

Como o modelo tem fatores quantitativos, podemos ajustar um modelo de segunda ordem

plot_grid(p1, p2)

modelo2 <- t5.16 %>% 
  lm(ToolLife ~ 
       TotalAngle + 
       CuttingSpeed + 
       I((TotalAngle-20)^2) + 
       I((CuttingSpeed-150)^2) +
       I((TotalAngle-20)*(CuttingSpeed-150)) +
       I((TotalAngle-20)^2*(CuttingSpeed-150)) +
       I((TotalAngle-20)*(CuttingSpeed-150)^2) +
       I((TotalAngle-20)^2*(CuttingSpeed-150)^2), data = .)



base <- expand_grid(TotalAngle = seq(from = 15, to = 25, length.ou = 100), 
            CuttingSpeed = seq(from = 125, to = 175, length.out = 100)) %>% 
    mutate(
      `I((TotalAngle-20)^2)` = (TotalAngle-20)^2,
      `I((CuttingSpeed-150)^2)` = (CuttingSpeed-150)^2,
        `I((TotalAngle-20)*(CuttingSpeed-150))` = (TotalAngle-20)*(CuttingSpeed-150),
        `I((TotalAngle-20)^2*(CuttingSpeed-150))` = (TotalAngle-20)^2*(CuttingSpeed-150),
        `I((TotalAngle-20)*(CuttingSpeed-150)^2)` = (TotalAngle-20)*(CuttingSpeed-150)^2,
      `I((TotalAngle-20)^2*(CuttingSpeed-150)^2)` = (TotalAngle-20)^2*(CuttingSpeed-150)^2,
        `I((TotalAngle-20)^2*(CuttingSpeed-150)^2)` =  (TotalAngle-20)^2*(CuttingSpeed-150)^2) 

Além disso, podemos construir uma superficie de resposta:

p1 <- base %>% 
  mutate(z = predict(modelo2, base)) %>% 
  mutate(x = TotalAngle, y = CuttingSpeed) %>% 
  select(x, y, z) %>% 
  ggplot() + 
    theme_bw() +
    geom_contour(aes(x = x, y = y, z = z)) + 
  geom_point(aes(TotalAngle, CuttingSpeed), data = t5.16) 

p1 %>% ggplotly()
base %>% 
  mutate(z = predict(modelo2, base)) %>% 
  mutate(x = TotalAngle, y = CuttingSpeed) %>% 
  select(x, y, z) %>% 
  plot_ly(z = ~ matrix(z, nrow = 100)) %>% 
  add_surface(
    showscale = FALSE,
    contours = list(
      z = list(
        show=TRUE,
        usecolormap=TRUE,
        highlightcolor="#ff0000",
        project=list(z=TRUE)
      )
    )
  ) %>% 
  layout(showlegend = FALSE,
         scene = list(xaxis = list(title = "Angle"),
                      yaxis = list(title = "Speed"),
                      zaxis = list(title = "ToolLife")
                      )
         )

Referencias

  1. Montgomery, D. C. (2020). Design and analysis of experiments. New York: John Wiley.