library(GAD)
## Loading required package: matrixStats
## Warning: package 'matrixStats' was built under R version 4.2.2
## Loading required package: R.methodsS3
## R.methodsS3 v1.8.2 (2022-06-13 22:00:14 UTC) successfully loaded. See ?R.methodsS3 for help.
pos <- c(rep(1,9),rep(2,9))
temp <- c(rep(seq(1,3),6))
obs <- c(570,1063,565,565,1080,510,583,1043,590,528,988,526,547,1026,538,521,1004,532)
dat <- data.frame(pos,temp,obs)
dat
##    pos temp  obs
## 1    1    1  570
## 2    1    2 1063
## 3    1    3  565
## 4    1    1  565
## 5    1    2 1080
## 6    1    3  510
## 7    1    1  583
## 8    1    2 1043
## 9    1    3  590
## 10   2    1  528
## 11   2    2  988
## 12   2    3  526
## 13   2    1  547
## 14   2    2 1026
## 15   2    3  538
## 16   2    1  521
## 17   2    2 1004
## 18   2    3  532

a)

pos <- as.fixed(pos)
temp <- as.fixed(temp)
dat1 <- data.frame(pos,temp,obs)
model1 <- aov(obs~pos+temp+pos*temp,data = dat1)
gad(model1)
## Analysis of Variance Table
## 
## Response: obs
##          Df Sum Sq Mean Sq  F value   Pr(>F)    
## pos       1   7160    7160   15.998 0.001762 ** 
## temp      2 945342  472671 1056.117 3.25e-14 ***
## pos:temp  2    818     409    0.914 0.427110    
## Residual 12   5371     448                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The p-value for factor A (position) and factor B (temperature) was respectively 0.001762 and 3.25e-14 which are low. However the p-value of the interaction term was rather high (0.427110).

b)

pos <- as.random(pos)
temp <- as.random(temp)
dat2 <- data.frame(pos,temp,obs)
model2 <- aov(obs~pos+temp+pos*temp,data = dat2)
gad(model2)
## Analysis of Variance Table
## 
## Response: obs
##          Df Sum Sq Mean Sq  F value    Pr(>F)    
## pos       1   7160    7160   17.504 0.0526583 .  
## temp      2 945342  472671 1155.518 0.0008647 ***
## pos:temp  2    818     409    0.914 0.4271101    
## Residual 12   5371     448                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The p-value for factor A (position) and factor B (temperature) was respectively 0.0526583 and 0.0008647 whereas the p-value of the interaction term was rather high (0.4271101).

c)

pos <- as.fixed(pos)
temp <- as.random(temp)
dat2 <- data.frame(pos,temp,obs)
model2 <- aov(obs~pos+temp+pos*temp,data = dat2)
gad(model2)
## Analysis of Variance Table
## 
## Response: obs
##          Df Sum Sq Mean Sq  F value   Pr(>F)    
## pos       1   7160    7160   17.504  0.05266 .  
## temp      2 945342  472671 1056.117 3.25e-14 ***
## pos:temp  2    818     409    0.914  0.42711    
## Residual 12   5371     448                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The p-value for factor A (position) and factor B (temperature) was respectively 0.0526583 and 3.25e-14 whereas the p-value of the interaction term was rather high (0.4271101).

d)

In the part a), where the both effects are fixed, we get very low p-values for both factors. However, in par b), both p-values do increase significantly. Finally in part c), the p-value of factor A remains the same as in part b), but the p-value of factor B reverts back to the part a) value. Meanwhile the p-value of the interaction term remains unchanged. As we know, when we have fixed effects for both factors, the f-statistic is calculated by dividing the MS of the factor by MSE, however in a random effect model, the factors MS will be divided by MS of interaction term (MSAB). As the MSAB here is larger than the MSE doing so will reduce the the f value and increase the p-value. As in a mixed effect model (like part c), only one of the factors MS is divided by MSAB. Furthermore, the f value for the interaction term is always calculated by diving it by the MSE, regardless of random/fixed/mixed effect model. That is why, the interaction terms p-value doesn’t change here.