Question No. 1 (a)

\(Y_{ijk} =\mu + \tau_{i}+\beta_{j}+\gamma_{k}+\left( \tau\beta \right)_{ij}+\left( \tau\gamma \right)_{ik}+\left( \beta\gamma \right)_{jk}+\left( \tau\beta\gamma \right)_{ijk}+\epsilon_{ijkl}\)

\(\tau\_{i}\)=Main effect for ammonium \(\beta\_{j}\)= Main effect for stir rate \(\gamma\_{k}\)= Main effect for temperature

i= levels of ammonium j= levels of stir rate k= levels of temperature

Question No: 1 B

Declare fixed factors for GAD and ensure categorical for aov

dat$ammonium <- as.fixed(as.factor(dat$ammonium))
dat$stirRate <- as.fixed(as.factor(dat$stirRate))
dat$temperture <- as.fixed(as.factor(dat$temperture))
dat$density <- as.numeric(dat$density)
#dat <- data.frame(dat$ammonium, dat$stirRate, dat$temperture, dat$density)
dat
##    ammonium stirRate temperture density
## 1         2      100          8   14.68
## 2         2      100          8   15.18
## 3        30      100          8   15.12
## 4        30      100          8   17.48
## 5         2      150          8    7.54
## 6         2      150          8    6.66
## 7        30      150          8   12.46
## 8        30      150          8   12.62
## 9         2      100         40   10.95
## 10        2      100         40   17.68
## 11       30      100         40   12.65
## 12       30      100         40   15.96
## 13        2      150         40    8.03
## 14        2      150         40    8.84
## 15       30      150         40   14.96
## 16       30      150         40   14.96

The Three Interactions

mod <- aov(dat$density ~ dat$ammonium * dat$stirRate * dat$temperture, data = dat)
summary(mod)
##                                          Df Sum Sq Mean Sq F value  Pr(>F)   
## dat$ammonium                              1  44.39   44.39  11.180 0.01018 * 
## dat$stirRate                              1  70.69   70.69  17.804 0.00292 **
## dat$temperture                            1   0.33    0.33   0.083 0.78117   
## dat$ammonium:dat$stirRate                 1  28.12   28.12   7.082 0.02875 * 
## dat$ammonium:dat$temperture               1   0.02    0.02   0.005 0.94281   
## dat$stirRate:dat$temperture               1  10.13   10.13   2.551 0.14889   
## dat$ammonium:dat$stirRate:dat$temperture  1   1.52    1.52   0.383 0.55341   
## Residuals                                 8  31.76    3.97                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
gad(mod)
## $anova
## Analysis of Variance Table
## 
## Response: dat$density
##                                          Df Sum Sq Mean Sq F value   Pr(>F)   
## dat$ammonium                              1 44.389  44.389 11.1803 0.010175 * 
## dat$stirRate                              1 70.686  70.686 17.8037 0.002918 **
## dat$temperture                            1  0.328   0.328  0.0826 0.781170   
## dat$ammonium:dat$stirRate                 1 28.117  28.117  7.0817 0.028754 * 
## dat$ammonium:dat$temperture               1  0.022   0.022  0.0055 0.942808   
## dat$stirRate:dat$temperture               1 10.128  10.128  2.5510 0.148890   
## dat$ammonium:dat$stirRate:dat$temperture  1  1.519   1.519  0.3826 0.553412   
## Residuals                                 8 31.762   3.970                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Comments: Here, ammonium : stirRate : temperture’s P value is 0.553412 is greater than .05 then Drop 3-way interaction we should need to test in 2 ways
model1 <- aov(density ~ ammonium*stirRate + ammonium*temperture + stirRate*temperture, data = dat)
summary(model1)
##                     Df Sum Sq Mean Sq F value  Pr(>F)   
## ammonium             1  44.39   44.39  12.004 0.00711 **
## stirRate             1  70.69   70.69  19.115 0.00179 **
## temperture           1   0.33    0.33   0.089 0.77268   
## ammonium:stirRate    1  28.12   28.12   7.603 0.02221 * 
## ammonium:temperture  1   0.02    0.02   0.006 0.94054   
## stirRate:temperture  1  10.13   10.13   2.739 0.13232   
## Residuals            9  33.28    3.70                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
gad(model1)
## $anova
## Analysis of Variance Table
## 
## Response: density
##                     Df Sum Sq Mean Sq F value   Pr(>F)   
## ammonium             1 44.389  44.389 12.0037 0.007109 **
## stirRate             1 70.686  70.686 19.1150 0.001792 **
## temperture           1  0.328   0.328  0.0886 0.772681   
## ammonium:stirRate    1 28.117  28.117  7.6033 0.022206 * 
## ammonium:temperture  1  0.022   0.022  0.0059 0.940538   
## stirRate:temperture  1 10.128  10.128  2.7389 0.132317   
## Residuals            9 33.281   3.698                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Here, ammonium: stir Rate’s P value, 0.02221 which is significant in compare with alpha = 0.05, We KEEP the ammonium:stirRate interaction, because p = 0.022 < 0.05. TTherefore, we cannot drop ammonium or stirRate in further simplifications. Next, we drop the OTHER two interactions (because THEIR p-values > 0.05).
model2 <- aov(density ~ temperture + ammonium * stirRate, data = dat)
gad(model2)
## $anova
## Analysis of Variance Table
## 
## Response: density
##                   Df Sum Sq Mean Sq F value   Pr(>F)   
## temperture         1  0.328   0.328  0.0830 0.778613   
## ammonium           1 44.389  44.389 11.2425 0.006443 **
## stirRate           1 70.686  70.686 17.9028 0.001410 **
## ammonium:stirRate  1 28.117  28.117  7.1211 0.021851 * 
## Residuals         11 43.431   3.948                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Temperature is not significant (p = 0.7786), so it is dropped and only the ammonium × stirRate interaction in the final model
model3 <- aov(density ~ ammonium + stirRate + temperture + ammonium*stirRate, data = dat)
gad(model3)
## $anova
## Analysis of Variance Table
## 
## Response: density
##                   Df Sum Sq Mean Sq F value   Pr(>F)   
## ammonium           1 44.389  44.389 11.2425 0.006443 **
## stirRate           1 70.686  70.686 17.9028 0.001410 **
## temperture         1  0.328   0.328  0.0830 0.778613   
## ammonium:stirRate  1 28.117  28.117  7.1211 0.021851 * 
## Residuals         11 43.431   3.948                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The ammonium × stirRate interaction is significant #while temperature is not, indicating that temperature has no meaningful effect on density and can be removed, leaving ammonium* stirRate as the final model structure
model4 <- aov(density ~ ammonium + stirRate + temperture, data = dat)
gad(model4)
## $anova
## Analysis of Variance Table
## 
## Response: density
##            Df Sum Sq Mean Sq F value   Pr(>F)   
## ammonium    1 44.389  44.389  7.4449 0.018316 * 
## stirRate    1 70.686  70.686 11.8554 0.004866 **
## temperture  1  0.328   0.328  0.0550 0.818581   
## Residuals  12 71.548   5.962                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Temperature shows no effect on density (p = 0.8186), so it should be removed, leaving only ammonium and stirRate as meaningful factors
model5  <- aov(density ~ ammonium * stirRate, data = dat)
gad(model5)
## $anova
## Analysis of Variance Table
## 
## Response: density
##                   Df Sum Sq Mean Sq F value    Pr(>F)    
## ammonium           1 44.389  44.389 12.1727 0.0044721 ** 
## stirRate           1 70.686  70.686 19.3841 0.0008612 ***
## ammonium:stirRate  1 28.117  28.117  7.7103 0.0167511 *  
## Residuals         12 43.759   3.647                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Both ammonium and stirRate significantly affect density, and their interaction is also significant p =0.0168, indicating that the effect of ammonium depends on the level of stirRate

Interaction plot

{r}
interaction.plot(dat$stirRate, dat$ammonium, dat$density, xlab = "Stir Rate", trace.label = "Ammonium",  ylab = "Mean Density")

Question 2

Position <- c(1, 2)
Temperature <- c(800, 825, 850)

df <- expand.grid(Position = Position, Temperature = Temperature)  
df <- rbind(df, df, df) 
df
##    Position Temperature
## 1         1         800
## 2         2         800
## 3         1         825
## 4         2         825
## 5         1         850
## 6         2         850
## 7         1         800
## 8         2         800
## 9         1         825
## 10        2         825
## 11        1         850
## 12        2         850
## 13        1         800
## 14        2         800
## 15        1         825
## 16        2         825
## 17        1         850
## 18        2         850
Observ <- c(570, 528, 1063, 988, 565, 526,565, 547, 1080, 1026, 510, 538, 583, 521, 1043, 1004, 590, 532)

Answer to the question No: 2(a)

dat2 <- df
dat2$Position <- as.fixed(as.factor(dat2$Position))   # make categorical first
dat2$Temperature <- as.fixed(as.factor(dat2$Temperature))
dat2$Observ <- as.numeric(Observ)
dat2
##    Position Temperature Observ
## 1         1         800    570
## 2         2         800    528
## 3         1         825   1063
## 4         2         825    988
## 5         1         850    565
## 6         2         850    526
## 7         1         800    565
## 8         2         800    547
## 9         1         825   1080
## 10        2         825   1026
## 11        1         850    510
## 12        2         850    538
## 13        1         800    583
## 14        2         800    521
## 15        1         825   1043
## 16        2         825   1004
## 17        1         850    590
## 18        2         850    532
str(dat2)
## 'data.frame':    18 obs. of  3 variables:
##  $ Position   : Factor w/ 2 levels "1","2": 1 2 1 2 1 2 1 2 1 2 ...
##  $ Temperature: Factor w/ 3 levels "800","825","850": 1 1 2 2 3 3 1 1 2 2 ...
##  $ Observ     : num  570 528 1063 988 565 ...
##  - attr(*, "out.attrs")=List of 2
##   ..$ dim     : Named int [1:2] 2 3
##   .. ..- attr(*, "names")= chr [1:2] "Position" "Temperature"
##   ..$ dimnames:List of 2
##   .. ..$ Position   : chr [1:2] "Position=1" "Position=2"
##   .. ..$ Temperature: chr [1:3] "Temperature=800" "Temperature=825" "Temperature=850"
gad(aov(Observ ~ dat2$Temperature * dat2$Position, data = dat2))
## $anova
## Analysis of Variance Table
## 
## Response: Observ
##                                Df Sum Sq Mean Sq  F value   Pr(>F)    
## dat2$Temperature                2 945342  472671 1056.117 3.25e-14 ***
## dat2$Position                   1   7160    7160   15.998 0.001762 ** 
## dat2$Temperature:dat2$Position  2    818     409    0.914 0.427110    
## Residuals                      12   5371     448                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Comments: When the temperature and position are fixed the P-value for the model are #P-Value for Temperature is 3.25e-14 #P-Value for Position is 0.001762 P-Value for two factor interaction position and temperature is 0.427110
##Answer to the question No: 2(b)


dat3 <-  df
dat3$Position <- as.random(as.factor(dat2$Position))   # make categorical first
dat3$Temperature <- as.random(as.factor(dat2$Temperature))
dat3$Observ <- as.numeric(Observ)

gad(aov(dat3$Observ ~ dat3$Temperature * dat3$Position, data = dat3))
## $anova
## Analysis of Variance Table
## 
## Response: dat3$Observ
##                                Df Sum Sq Mean Sq  F value    Pr(>F)    
## dat3$Temperature                2 945342  472671 1155.518 0.0008647 ***
## dat3$Position                   1   7160    7160   17.504 0.0526583 .  
## dat3$Temperature:dat3$Position  2    818     409    0.914 0.4271101    
## Residuals                      12   5371     448                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Comments: When the temperature and position both are random the P-value for the model are
P-Value for Temperature is 0.0008647
P-Value for Position is 0.0526583
P-Value for two factor interaction position and temperature is 0.4271101

Answer to the question No: 2(c)

dat4 <- df
dat4$Position  <- as.fixed(dat4$Position)
dat4$Temperature <- as.random(dat4$Temperature)
dat4$Observ <- as.numeric(Observ)
gad(aov(dat4$Observ ~ dat4$Temperature * dat4$Position, data = dat4))
## $anova
## Analysis of Variance Table
## 
## Response: dat4$Observ
##                                Df Sum Sq Mean Sq  F value   Pr(>F)    
## dat4$Temperature                2 945342  472671 1056.117 3.25e-14 ***
## dat4$Position                   1   7160    7160   17.504  0.05266 .  
## dat4$Temperature:dat4$Position  2    818     409    0.914  0.42711    
## Residuals                      12   5371     448                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Comments: When the temperature is fixed and position is random the P-value for the model are
P-Value for Temperature is 3.25e-14
P-Value for Position is 0.05266
P-Value for two factor interaction position and temperature is 0.4271101

Answer to the question No: 2(d)

We see the same p-value for interaction for part a, b & c. that means there is significant difference as fixed and random. The P-value of each differs for fixed and random effects.
#Question 1
library(GAD)
dat <- read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/PowderProduction.csv")
colnames(dat) <- c("ammonium","stirRate","temperture","density")

dat$ammonium <- as.fixed(as.factor(dat$ammonium))
dat$stirRate <- as.fixed(as.factor(dat$stirRate))
dat$temperture <- as.fixed(as.factor(dat$temperture))
dat$density <- as.numeric(dat$density)
dat <- data.frame(ammonium, stirRate, temperture, density)
dat
mod <- aov(density ~ ammonium * stirRate * temperture, data = dat)
summary(mod)
gad(mod)
model1 <- aov(density ~ ammonium*stirRate + ammonium*temperture + stirRate*temperture,data = dat)
summary(model1)
gad(model1)
model2 <- aov(density ~ temperture + ammonium * stirRate, data = dat)
gad(model2)
model3 <- aov(density ~ ammonium + stirRate + temperture + ammonium*stirRate, data = dat)
gad(model3)
model4 <- aov(density ~ ammonium + stirRate + temperture, data = dat)
gad(model4)
model5  <- aov(density ~ ammonium * stirRate, data = dat)
gad(model5)
interaction.plot(dat$stirRate, dat$ammonium, dat$density, xlab = "Stir Rate", trace.label = "Ammonium",  ylab = "Mean Density")

#Question2

Position <- c(1, 2)
Temperature <- c(800, 825, 850)

df <- expand.grid(Position = Position, Temperature = Temperature)  
df <- rbind(df, df, df) 
df

Observ <- c(570, 528, 1063, 988, 565, 526,565, 547, 1080, 1026, 510, 538, 583, 521, 1043, 1004, 590, 532)

##Answer to the question No: 2(a)

dat2 <- df
dat2$Position <- as.fixed(as.factor(dat2$Position))   # make categorical first
dat2$Temperature <- as.fixed(as.factor(dat2$Temperature))
dat2$Observ <- as.numeric(Observ)
dat2
str(dat2)
gad(aov(Observ ~ dat2$Temperature * dat2$Position, data = dat2))
dat3 <-  df
dat3$Position <- as.random(as.factor(dat2$Position))   # make categorical first
dat3$Temperature <- as.random(as.factor(dat2$Temperature))
dat3$Observ <- as.numeric(Observ)
gad(aov(dat3$Observ ~ dat3$Temperature * dat3$Position, data = dat3))
dat4 <- df
dat4$Position  <- as.fixed(dat4$Position)
dat4$Temperature <- as.random(dat4$Temperature)
dat4$Observ <- as.numeric(Observ)
gad(aov(dat4$Observ ~ dat4$Temperature * dat4$Position, data = dat4))