# 1a) Model Equation (Full 2x2x2 factorial, all fixed)
# y_{ijkl} = μ + α_i + β_j + γ_k + (αβ)_{ij} + (αγ)_{ik} + (βγ)_{jk} + (αβγ)_{ijk} + ε_{ijkl}
# where i = Ammonium (2 levels), j = Stir Rate (2 levels),
# k = Temperature (2 levels), and l = replicate (1–2)
# All effects are fixed.


# 1b) Silver Powder Production (Fixed 2^3 factorial)

powder <- read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/PowderProduction.csv")

# Convert the variables to factors
powder$Ammonium <- as.factor(powder$Ammonium)
powder$StirRate <- as.factor(powder$StirRate)
powder$Temperature <- as.factor(powder$Temperature)

# Fit the full factorial model using aov
model1 <- aov(Density ~ Ammonium * StirRate * Temperature, data = powder)

# Show the ANOVA table
summary(model1)
##                               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
# Based on the p-values from the summary above,
# factors or interactions with p < 0.05 are considered significant.


# simple interaction plot if needed
interaction.plot(powder$StirRate, powder$Ammonium, powder$Density,
                 xlab="StirRate", ylab="Mean Density", trace.label="Ammonium")

# 1b:
# From the ANOVA output, Ammonium, StirRate, and their interaction (Ammonium:StirRate)
# had p-values < 0.05, indicating they significantly affect Density.
# Temperature was not significant (p = 0.78).



# 2a) both Temperature and Position FIXED

Temperature <- rep(c(800,825,850), each=6)
Position <- rep(rep(c(1,2), each=3), times=3)
Density <- c(570,1063,565,565,1080,510,
             583,1043,590,528,988,526,
             547,1026,538,521,1004,532)
anode <- data.frame(Temperature, Position, Density)

# Load GAD package
library(GAD)

# Treat both factors as fixed
Temperature <- as.fixed(anode$Temperature)
Position <- as.fixed(anode$Position)

# Run the ANOVA
model_a <- aov(Density ~ Temperature * Position)
gad(model_a)
## $anova
## Analysis of Variance Table
## 
## Response: Density
##                      Df Sum Sq Mean Sq F value Pr(>F)
## Temperature           2   2853    1426  0.0180 0.9822
## Position              1   4080    4080  0.0515 0.8242
## Temperature:Position  2   1760     880  0.0111 0.9890
## Residuals            12 949998   79167
# Comments for 2a:
# Both Temperature and Position were treated as fixed effects.
# From the gad() output, all p-values were large (> 0.05),
# so none of the factors significantly affected Density.
# Position and the interaction were not significant.


# 2b) Both Temperature and Position RANDOM

Temperature <- as.random(anode$Temperature)
Position <- as.random(anode$Position)
model_b <- aov(Density ~ Temperature * Position)
gad(model_b)
## $anova
## Analysis of Variance Table
## 
## Response: Density
##                      Df Sum Sq Mean Sq F value Pr(>F)
## Temperature           2   2853    1426  1.6208 0.3816
## Position              1   4080    4080  4.6361 0.1642
## Temperature:Position  2   1760     880  0.0111 0.9890
## Residuals            12 949998   79167
# 2b:
# With both factors random, all p-values were still > 0.05.
# Position had the smallest p-value (~0.16) but was not significant.
# Conclusions match 2a (no significant effects).


# 2c) Temperature RANDOM, Position FIXED (Mixed)

Temperature <- as.random(anode$Temperature)
Position <- as.fixed(anode$Position)
model_c <- aov(Density ~ Temperature * Position)
gad(model_c)
## $anova
## Analysis of Variance Table
## 
## Response: Density
##                      Df Sum Sq Mean Sq F value Pr(>F)
## Temperature           2   2853    1426  0.0180 0.9822
## Position              1   4080    4080  4.6361 0.1642
## Temperature:Position  2   1760     880  0.0111 0.9890
## Residuals            12 949998   79167
# Comments for 2c:
# Temperature is random and Position is fixed.
# The p-values were similar to 2a/2b and all > 0.05.
# No significant effects; conclusions did not change.


# 2d)
# Across the three cases (fixed, random, mixed) for the carbon anode data,
# all p-values were > 0.05, so no factors were significant in any model.
# Treating factors as fixed vs random did not change the conclusion.