\[Y_{ijkl} = \mu + \alpha_i + \beta_j + \gamma_k + \alpha\beta_{ij} + \alpha\gamma_{ik} + \beta\gamma_{jk} + \alpha\beta\gamma_{ijk} + \epsilon_{ijkl}\] where: \(\mu\) is the overall mean.
\(\alpha_i\) is the main effect of Ammonium (\(i = 1, 2\)).
\(\beta_j\) is the main effect of Stir Rate (\(j = 1, 2\)).
\(\gamma_k\) is the main effect of Temperature (\(k = 1, 2\)).
\(\alpha\beta_{ij}, \alpha\gamma_{ik}, \beta\gamma_{jk}\) are the two-factor interactions.
\(\alpha\beta\gamma_{ijk}\) is the three-factor interaction.
\(\epsilon_{ijkl}\) is the random error component, \(\epsilon_{ijkl} \sim N(0, \sigma^2)\).
# Data
url <- "https://raw.githubusercontent.com/tmatis12/datafiles/main/PowderProduction.csv"
df <- read.csv(url)
# Variables to factors
df$Ammonium <- as.factor(df$Ammonium)
df$StirRate <- as.factor(df$StirRate)
df$Temperature <- as.factor(df$Temperature)
# Fit the full factorial ANOVA model
model <- aov(Density ~ Ammonium * StirRate * Temperature, data = df)
summary(model)
## Df Sum Sq Mean Sq F value Pr(>F)
## Ammonium 1 44.39 44.39 11.180 0.01018 *
## StirRate 1 70.69 70.69 17.804 0.00292 **
## Temperature 1 0.33 0.33 0.083 0.78117
## Ammonium:StirRate 1 28.12 28.12 7.082 0.02875 *
## Ammonium:Temperature 1 0.02 0.02 0.005 0.94281
## StirRate:Temperature 1 10.13 10.13 2.551 0.14889
## Ammonium:StirRate:Temperature 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
# Data
density <- c(
570, 565, 583,
1063, 1080, 1043,
565, 510, 590,
528, 547, 521,
988, 1026, 1004,
526, 538, 532
)
position <- as.factor(rep(1:2, each = 9))
temperature <- as.factor(rep(rep(c(800, 825, 850), each = 3), 2))
df <- data.frame(position, temperature, density)
library(GAD)
pos_fixed <- as.fixed(df$position)
temp_fixed <- as.fixed(df$temperature)
model_fixed <- gad(aov(density ~ pos_fixed * temp_fixed, data = df))
print(model_fixed)
## $anova
## Analysis of Variance Table
##
## Response: density
## Df Sum Sq Mean Sq F value Pr(>F)
## pos_fixed 1 7160 7160 15.998 0.001762 **
## temp_fixed 2 945342 472671 1056.117 3.25e-14 ***
## pos_fixed:temp_fixed 2 818 409 0.914 0.427110
## Residuals 12 5371 448
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(GAD)
pos_rand <- as.random(df$position)
temp_rand <- as.random(df$temperature)
model_random <- gad(aov(density ~ pos_rand * temp_rand, data = df))
print(model_random)
## $anova
## Analysis of Variance Table
##
## Response: density
## Df Sum Sq Mean Sq F value Pr(>F)
## pos_rand 1 7160 7160 17.504 0.0526583 .
## temp_rand 2 945342 472671 1155.518 0.0008647 ***
## pos_rand:temp_rand 2 818 409 0.914 0.4271101
## Residuals 12 5371 448
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(GAD)
pos_fixed <- as.fixed(df$position)
temp_rand <- as.random(df$temperature)
model_mix <- gad(aov(density ~ pos_fixed * temp_rand, data = df))
print(model_mix)
## $anova
## Analysis of Variance Table
##
## Response: density
## Df Sum Sq Mean Sq F value Pr(>F)
## pos_fixed 1 7160 7160 17.504 0.05266 .
## temp_rand 2 945342 472671 1056.117 3.25e-14 ***
## pos_fixed:temp_rand 2 818 409 0.914 0.42711
## Residuals 12 5371 448
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Alternative A (Both Fixed): Position (p = 0.00176) and Temperature (p = 3.25e-14) are significant and interaction is not significant (p = 0.427).
Alternative B (Both Random): Temperature is significant (p = 0.00087) and Position is marginally non-significant (p = 0.0527), and interaction is not significant (p = 0.427).
Alternative C (Position Fixed, Temperature Random): Position is marginally non-significant (p = 0.0527); Temperature is highly significant (p < 0.0001); interaction is not significant (p = 0.427).
Comparison: Changing factor between fixed and random effects alters the Expected Mean Squares (EMS) denominators. The interaction uses mean square error (MSE), while main effects in random/mixed use the interaction mean square (MSAB), because of that the p-values is altered.
The p-value for the interaction (Position vs Temperature) remains the same (p = 0.42711) across all options, because of the factorial designs interaction effect is always tested against the mean square error (MSE).
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message = FALSE)
# Data 1
url <- "https://raw.githubusercontent.com/tmatis12/datafiles/main/PowderProduction.csv"
df <- read.csv(url)
# Variables to factors
df$Ammonium <- as.factor(df$Ammonium)
df$StirRate <- as.factor(df$StirRate)
df$Temperature <- as.factor(df$Temperature)
# Fit the full factorial ANOVA model
model <- aov(Density ~ Ammonium * StirRate * Temperature, data = df)
summary(model)
# Data 2
density <- c(
570, 565, 583,
1063, 1080, 1043,
565, 510, 590,
528, 547, 521,
988, 1026, 1004,
526, 538, 532
)
position <- as.factor(rep(1:2, each = 9))
temperature <- as.factor(rep(rep(c(800, 825, 850), each = 3), 2))
df <- data.frame(position, temperature, density)
#a
library(GAD)
pos_fixed <- as.fixed(df$position)
temp_fixed <- as.fixed(df$temperature)
model_fixed <- gad(aov(density ~ pos_fixed * temp_fixed, data = df))
print(model_fixed)
#b
library(GAD)
pos_rand <- as.random(df$position)
temp_rand <- as.random(df$temperature)
model_random <- gad(aov(density ~ pos_rand * temp_rand, data = df))
print(model_random)
#c
library(GAD)
pos_fixed <- as.fixed(df$position)
temp_rand <- as.random(df$temperature)
model_mix <- gad(aov(density ~ pos_fixed * temp_rand, data = df))
print(model_mix)