RSM Tutorial
RSM Tutorial
2023-01-18
Introduction
This RMarkdown document is adapted from the package author’s publication (https://cran.r-
project.org/web/packages/rsm/vignettes/rsm.pdf) on rsm
rsm has built in functions for the generation of central composite and Box-Behnken designs
It provides functionality for the analysis for experimental data, estimation of response surface, testing the
model’s lack of fit, data visualization of contour plots of the fitted surface, and more
rsm covers standard first order (linear) and second order (quadratic) designs for one response variable
Functions provided by package:
functions/datatypes for coding and decoding factor levels
Functions to generate central composite and Box-Behnken designs (CCD and BBD, respectively)
Expands on base-R’s lm function to be compatible with standard response-surface models
Functions for data visualization
Provides guidance for further experimentation (i.e. path of steepest ascent)
Coding of data
Install package and load library:
library(rsm)
ChemReact1
Time and Temperature are continuous independent variables and Yield is a continuous dependent
variable
We will use coded.data to code them as x1 and x2, respectively:
CR1 <- coded.data(ChemReact1, x1 ~ (Time - 85)/5, #name of the coded variable ~ linear expres
sion fir the uncoded variable
x2 ~ (Temp - 175)/5)
CR1
## Time Temp Yield
## 1 80 170 80.5
## 2 80 180 81.5
## 3 90 170 82.0
## 4 90 180 83.5
## 5 85 175 83.9
## 6 85 175 84.3
## 7 85 175 84.0
##
## Data are stored in coded form using these coding formulas ...
## x1 ~ (Time - 85)/5
## x2 ~ (Temp - 175)/5
The dataframe looks very similar to how they were presented in the original, ChemReact1 dataframe, but
see they are internally Time and Temp are internally understood by R as x1 and x2. This can be
witnessed if CR1 is coerced into a standard dataframe:
as.data.frame(CR1)
## x1 x2 Yield
## 1 -1 -1 80.5
## 2 -1 1 81.5
## 3 1 -1 82.0
## 4 1 1 83.5
## 5 0 0 83.9
## 6 0 0 84.3
## 7 0 0 84.0
This is important so that the dataframe is compatible with downstream rsm package functions
Generating a design
Function for creating a Box-Behnken design - bbd
Function for creating a central composite design - ccd
Box-Behnken design
Create a 3-factor Box-Behnken design with two center points:
This example of an experiment has 3 variables (x1, x2, and x3). The experiment is randomized.
If there were a 4th or 5th variable, then the design would be blocked (default setting) and the blocks
would be individually randomized
Fit first order response-surface model to the data in the first block:
The summary of this model indicates that there is a significant lack of fit (p = 0.01034)
This suggests that it may be a good idea to test a higher order model
The p-value for the lack of fit test is still very small (p = 0.005221). To investigate further, more data is needed
so we can combine two blocks of the greater ChemReact experiment (ChemReact1 + ChemReact2):
The larger dataset allows us to fit a full second-order model to the data. This can be accomplished by using the
following code:
par(mfrow = c(2,3))
contour(CR2.rsm, ~ x1 + x2, image = TRUE, at = summary(CR2.rsm)$canonical$xs)
Another dataset example is a paper-helicopter experiment in which different variables such as wing area (A),
wing shape (R), body width (W), and body length (L) influence how long a paper helicopter can fly. Each
observation (row) represents the results of 10 replicated flights at each experimental condition.
This model studies average flight time with the variable name ‘ave’ using a second-order surface:
heli.rsm <- rsm(ave ~ block + SO(x1, x2, x3, x4), data = heli)
summary(heli.rsm)
##
## Call:
## rsm(formula = ave ~ block + SO(x1, x2, x3, x4), data = heli)
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 372.800000 1.506375 247.4815 < 2.2e-16 ***
## block2 -2.950000 1.207787 -2.4425 0.0284522 *
## x1 -0.083333 0.636560 -0.1309 0.8977075
## x2 5.083333 0.636560 7.9856 1.398e-06 ***
## x3 0.250000 0.636560 0.3927 0.7004292
## x4 -6.083333 0.636560 -9.5566 1.633e-07 ***
## x1:x2 -2.875000 0.779623 -3.6877 0.0024360 **
## x1:x3 -3.750000 0.779623 -4.8100 0.0002773 ***
## x1:x4 4.375000 0.779623 5.6117 6.412e-05 ***
## x2:x3 4.625000 0.779623 5.9324 3.657e-05 ***
## x2:x4 -1.500000 0.779623 -1.9240 0.0749257 .
## x3:x4 -2.125000 0.779623 -2.7257 0.0164099 *
## x1^2 -2.037500 0.603894 -3.3739 0.0045424 **
## x2^2 -1.662500 0.603894 -2.7530 0.0155541 *
## x3^2 -2.537500 0.603894 -4.2019 0.0008873 ***
## x4^2 -0.162500 0.603894 -0.2691 0.7917877
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Multiple R-squared: 0.9555, Adjusted R-squared: 0.9078
## F-statistic: 20.04 on 15 and 14 DF, p-value: 6.54e-07
##
## Analysis of Variance Table
##
## Response: ave
## Df Sum Sq Mean Sq F value Pr(>F)
## block 1 16.81 16.81 1.7281 0.209786
## FO(x1, x2, x3, x4) 4 1510.00 377.50 38.8175 1.965e-07
## TWI(x1, x2, x3, x4) 6 1114.00 185.67 19.0917 5.355e-06
## PQ(x1, x2, x3, x4) 4 282.54 70.64 7.2634 0.002201
## Residuals 14 136.15 9.72
## Lack of fit 10 125.40 12.54 4.6660 0.075500
## Pure error 4 10.75 2.69
##
## Stationary point of response surface:
## x1 x2 x3 x4
## 0.8607107 -0.3307115 -0.8394866 -0.1161465
##
## Stationary point in original units:
## A R W L
## 12.916426 2.434015 1.040128 1.941927
##
## Eigenanalysis:
## eigen() decomposition
## $values
## [1] 3.258222 -1.198324 -3.807935 -4.651963
##
## $vectors
## [,1] [,2] [,3] [,4]
## x1 0.5177048 0.04099358 0.7608371 -0.38913772
## x2 -0.4504231 0.58176202 0.5056034 0.45059647
## x3 -0.4517232 0.37582195 -0.1219894 -0.79988915
## x4 0.5701289 0.72015994 -0.3880860 0.07557783
## 1
## 372.1719
In the ANOVA table, we can see that many of the second order terms (i.e. a:b, ab^x terms) have
statistically significant (low) p-values
This means that they contribute significantly to the model and that this ‘canonical’ analysis is relevant
The stationary point seems to be close to the optimized region, but since the eigenvalues are of mixed
sign so this stationary point is a saddle point (neither a minimum or maximum). Further analysis is
requried
Visualize the behavior of the fitted surface around the stationary point:
## dist x1 x2 x3 x4 | A R W L | yhat
## 1 0.0 0.000 0.000 0.000 0.000 | 12.4000 2.52000 1.250 2.0000 | 372.800
## 2 0.5 -0.127 0.288 0.116 -0.371 | 12.3238 2.59488 1.279 1.8145 | 377.106
## 3 1.0 -0.351 0.538 0.312 -0.700 | 12.1894 2.65988 1.328 1.6500 | 382.675