x <- c(2.1, 2.4, 1.9, 2.6)
mean(x)
## [1] 2.25
alfa <- sqrt(2)

biodiesel <- data.frame(
  Corrida = 1:13,
  Tipo    = c(rep("Factorial", 4), rep("Axial", 4), rep("Centro", 5)),
  x1      = c(-1,  1, -1,  1, -alfa, alfa,  0,     0,    0, 0, 0, 0, 0),
  x2      = c(-1, -1,  1,  1,  0,    0,    -alfa,  alfa, 0, 0, 0, 0, 0),
  y       = c(83.6, 89.8, 88.5, 90.5, 86.8, 91.7, 85.8, 90.1,
              94.0, 93.6, 93.7, 93.9, 93.5)
)

biodiesel$Temperatura <- 55.0 + 6.0  * biodiesel$x1   # °C
biodiesel$KOH         <- 1.00 + 0.35 * biodiesel$x2   # % m/m

biodiesel <- biodiesel[, c("Corrida", "Tipo", "x1", "x2",
                           "Temperatura", "KOH", "y")]

biodiesel
##    Corrida      Tipo        x1        x2 Temperatura       KOH    y
## 1        1 Factorial -1.000000 -1.000000    49.00000 0.6500000 83.6
## 2        2 Factorial  1.000000 -1.000000    61.00000 0.6500000 89.8
## 3        3 Factorial -1.000000  1.000000    49.00000 1.3500000 88.5
## 4        4 Factorial  1.000000  1.000000    61.00000 1.3500000 90.5
## 5        5     Axial -1.414214  0.000000    46.51472 1.0000000 86.8
## 6        6     Axial  1.414214  0.000000    63.48528 1.0000000 91.7
## 7        7     Axial  0.000000 -1.414214    55.00000 0.5050253 85.8
## 8        8     Axial  0.000000  1.414214    55.00000 1.4949747 90.1
## 9        9    Centro  0.000000  0.000000    55.00000 1.0000000 94.0
## 10      10    Centro  0.000000  0.000000    55.00000 1.0000000 93.6
## 11      11    Centro  0.000000  0.000000    55.00000 1.0000000 93.7
## 12      12    Centro  0.000000  0.000000    55.00000 1.0000000 93.9
## 13      13    Centro  0.000000  0.000000    55.00000 1.0000000 93.5
library(rsm)
## Warning: package 'rsm' was built under R version 4.6.2
packageVersion("rsm")
## [1] '2.10.6'
CR <- coded.data(biodiesel,
                 x1 ~ (Temperatura - 55)/6,
                 x2 ~ (KOH - 1.00)/0.35)

CR
##    Corrida      Tipo Temperatura       KOH Temperatura       KOH    y
## 1        1 Factorial    49.00000 0.6500000   -1.000000 -1.000000 83.6
## 2        2 Factorial    61.00000 0.6500000    1.000000 -1.000000 89.8
## 3        3 Factorial    49.00000 1.3500000   -1.000000  1.000000 88.5
## 4        4 Factorial    61.00000 1.3500000    1.000000  1.000000 90.5
## 5        5     Axial    46.51472 1.0000000   -1.414214  0.000000 86.8
## 6        6     Axial    63.48528 1.0000000    1.414214  0.000000 91.7
## 7        7     Axial    55.00000 0.5050253    0.000000 -1.414214 85.8
## 8        8     Axial    55.00000 1.4949747    0.000000  1.414214 90.1
## 9        9    Centro    55.00000 1.0000000    0.000000  0.000000 94.0
## 10      10    Centro    55.00000 1.0000000    0.000000  0.000000 93.6
## 11      11    Centro    55.00000 1.0000000    0.000000  0.000000 93.7
## 12      12    Centro    55.00000 1.0000000    0.000000  0.000000 93.9
## 13      13    Centro    55.00000 1.0000000    0.000000  0.000000 93.5
## 
## Data are stored in coded form using these coding formulas ...
## x1 ~ (Temperatura - 55)/6
## x2 ~ (KOH - 1)/0.35
par(mfrow = c(1,2))

varfcn(CR, ~ SO(x1, x2),
       main = "Varianza de predicción")

varfcn(CR, ~ SO(x1, x2),
       contour = TRUE,
       main = "Contornos")

par(mfrow = c(1,1))
m2a <- rsm(y ~ FO(x1, x2), data = CR)

summary(m2a)$lof
## Analysis of Variance Table
## 
## Response: y
##             Df Sum Sq Mean Sq  F value    Pr(>F)    
## FO(x1, x2)   2 45.669 22.8347   2.3689    0.1438    
## Residuals   10 96.394  9.6394                       
## Lack of fit  6 96.222 16.0370 372.9525 1.906e-05 ***
## Pure error   4  0.172  0.0430                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
m2 <- rsm(y ~ SO(x1, x2), data = CR)

summary(m2)$lof
## Analysis of Variance Table
## 
## Response: y
##             Df Sum Sq Mean Sq F value    Pr(>F)    
## FO(x1, x2)   2 45.669  22.835 177.080 1.014e-06 ***
## TWI(x1, x2)  1  4.410   4.410  34.199 0.0006319 ***
## PQ(x1, x2)   2 91.081  45.541 353.161 9.361e-08 ***
## Residuals    7  0.903   0.129                      
## Lack of fit  3  0.731   0.244   5.664 0.0635869 .  
## Pure error   4  0.172   0.043                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
can <- canonical(m2)

can$xs
##        x1        x2 
## 0.3592727 0.1792886
code2val(can$xs, codings(CR))
## Temperatura         KOH 
##   57.155636    1.062751
nd <- as.data.frame(t(can$xs))

predict(m2, newdata = nd)
##        1 
## 94.21062
par(mfrow = c(1,1))

contour(m2, ~ x1 + x2,
        image = TRUE,
        main = "Contornos del rendimiento")

persp(m2, ~ x1 + x2,
      col = terrain.colors(50),
      contours = "colors",
      zlab = "Rendimiento",
      theta = -40,
      phi = 25,
      main = "Superficie de respuesta")

par(mfrow = c(1,3))

plot(fitted(m2), resid(m2),
     pch = 19,
     xlab = "Ajustados",
     ylab = "Residuos")

abline(h = 0, lty = 2)

qqnorm(resid(m2))
qqline(resid(m2))

plot(resid(m2), type = "b",
     pch = 19,
     xlab = "Orden",
     ylab = "Residuos")

abline(h = 0, lty = 2)

par(mfrow = c(1,1))
shapiro.test(resid(m2))
## 
##  Shapiro-Wilk normality test
## 
## data:  resid(m2)
## W = 0.98726, p-value = 0.9983