#Question 1 Part A: Writing the equation

#Full Factorial Model Equation: \[ y_{ijk} = \mu + \tau_i + \beta_j + \gamma_k + (\tau\beta)_{ij} + (\tau\gamma)_{ik} + (\beta\gamma)_{jk} + (\tau\beta\gamma)_{ijk} + \epsilon_{ijkl} \]

Where,

\(\tau_i\) = Main Effect for Ammonium %
\(\beta_j\) = Main Effect for Stir Rate
\(\gamma_k\) = Main Effect for Temperature

\(i = 2, 30\) denoting two levels of ammonium
\(j = 100, 150\) denoting two levels of stir rate
\(k = 8, 40\) denoting two levels of temperature
\(l = 2\) replicates

#Part B #We need to identify which factors are significant sowe perform an iterative approach which is to perform hypothesis starting from testing for 3 factor interactions to testing for 2 factor. Starting with testing hypothesis for 3 Factor interaction.

Null:

\[ H_{0} : (\tau\beta\gamma)_{ijk} = 0 \; \forall \; "ijk" \]

Alternate:

\[ H_{a} : (\tau\beta\gamma)_{ijk} \neq 0 \; \exists \; "ijk" \]

#Reading the Data:

csvData <- read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/PowderProduction.csv")
print(csvData)
##    Ammonium StirRate Temperature 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 factors Ammonium, Stir Rate, and Temperature were treated as fixed effects, each having two levels with two replications.

library(GAD)
csvData$Ammonium <- as.fixed(csvData$Ammonium)
csvData$StirRate <- as.fixed(csvData$StirRate)
csvData$Temperature <- as.fixed(csvData$Temperature)
str(csvData)
## 'data.frame':    16 obs. of  4 variables:
##  $ Ammonium   : Factor w/ 2 levels "2","30": 1 1 2 2 1 1 2 2 1 1 ...
##  $ StirRate   : Factor w/ 2 levels "100","150": 1 1 1 1 2 2 2 2 1 1 ...
##  $ Temperature: Factor w/ 2 levels "8","40": 1 1 1 1 1 1 1 1 2 2 ...
##  $ Density    : num  14.68 15.18 15.12 17.48 7.54 ...
model<-aov(csvData$Density~csvData$Ammonium+csvData$StirRate+csvData$Temperature+csvData$Ammonium*csvData$StirRate+
             csvData$Ammonium*csvData$Temperature+csvData$StirRate*csvData$Temperature+
             csvData$Ammonium*csvData$StirRate*csvData$Temperature)
GAD::gad(model)
## $anova
## Analysis of Variance Table
## 
## Response: csvData$Density
##                                                       Df Sum Sq Mean Sq F value
## csvData$Ammonium                                       1 44.389  44.389 11.1803
## csvData$StirRate                                       1 70.686  70.686 17.8037
## csvData$Temperature                                    1  0.328   0.328  0.0826
## csvData$Ammonium:csvData$StirRate                      1 28.117  28.117  7.0817
## csvData$Ammonium:csvData$Temperature                   1  0.022   0.022  0.0055
## csvData$StirRate:csvData$Temperature                   1 10.128  10.128  2.5510
## csvData$Ammonium:csvData$StirRate:csvData$Temperature  1  1.519   1.519  0.3826
## Residuals                                              8 31.762   3.970        
##                                                         Pr(>F)   
## csvData$Ammonium                                      0.010175 * 
## csvData$StirRate                                      0.002918 **
## csvData$Temperature                                   0.781170   
## csvData$Ammonium:csvData$StirRate                     0.028754 * 
## csvData$Ammonium:csvData$Temperature                  0.942808   
## csvData$StirRate:csvData$Temperature                  0.148890   
## csvData$Ammonium:csvData$StirRate:csvData$Temperature 0.553412   
## Residuals                                                        
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#Since the p-value for the three-factor interaction (0.5534) is greater than 0.05, the null hypothesis is not rejected, indicating that the three-factor interaction is not statistically significant.

#After excluding the non-significant three-factor interaction from the model, hypothesis testing is conducted for the two-factor interactions in descending order of p-values, beginning with the interaction between Ammonium and Temperature

Null:

\[ H_{0} : (\tau\gamma)_{ik} = 0 \; \forall \; "i, k" \]

Alternate:

\[ H_{a} : (\tau\gamma)_{ik} \neq 0 \; \exists \; "i, k" \]

model<-aov(csvData$Density~csvData$Ammonium+csvData$StirRate+csvData$Temperature+csvData$Ammonium*csvData$StirRate+csvData$StirRate*csvData$Temperature)
GAD::gad(model)
## $anova
## Analysis of Variance Table
## 
## Response: csvData$Density
##                                      Df Sum Sq Mean Sq F value    Pr(>F)    
## csvData$Ammonium                      1 44.389  44.389 13.3287 0.0044560 ** 
## csvData$StirRate                      1 70.686  70.686 21.2250 0.0009696 ***
## csvData$Temperature                   1  0.328   0.328  0.0984 0.7601850    
## csvData$Ammonium:csvData$StirRate     1 28.117  28.117  8.4426 0.0156821 *  
## csvData$StirRate:csvData$Temperature  1 10.128  10.128  3.0412 0.1117751    
## Residuals                            10 33.303   3.330                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#Since the p-value for the Stir Rate–Temperature interaction (p = 0.111) exceeds 0.05, the null hypothesis is not rejected, indicating no significant interaction between these factors.

#After removing the (Stir Rate × Temperature) interaction from the model, the next step is to test the remaining two-factor interaction between Ammonium and Stir Rate.

Null:

\[ H_{0} : (\tau\beta)_{ij} = 0 \; \forall \; "i, j" \]

Alternate:

\[ H_{a} : (\tau\beta)_{ij} \neq 0 \; \exists \; "i, j" \]

model<-aov(csvData$Density~csvData$Ammonium+csvData$StirRate+csvData$Ammonium*csvData$StirRate)
GAD::gad(model)
## $anova
## Analysis of Variance Table
## 
## Response: csvData$Density
##                                   Df Sum Sq Mean Sq F value    Pr(>F)    
## csvData$Ammonium                   1 44.389  44.389 12.1727 0.0044721 ** 
## csvData$StirRate                   1 70.686  70.686 19.3841 0.0008612 ***
## csvData$Ammonium:csvData$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

#Since the P-value of temeprature is 0.778 which is larger tha 0.05, we will drop it.

#Plotting the Interaction Graph for Ammonium and StirRate Interaction:

interaction.plot(csvData$StirRate,csvData$Ammonium,csvData$Density,col=c("blue","red"))

#Since the lines are not parallel, the interaction between ammonium and stir rate is significant.

#Question 2 Part A: #Stating the hypothesis: NULL:

\[ H_{0} : (\alpha\beta)_{ij} = 0 \; \forall \; "i, j" \]

Alternate:

\[ H_{a} : (\alpha\beta)_{ij} \neq 0 \; \exists \; "i, j" \]

#Reading the data:

furnaceposition <- c(rep(1,9),rep(2,9))
temperatures<-c(800,825,850)
firingtemperature <- rep(temperatures,6)
bakeddensity <- c(570,1063,565,565,1080,510,583,1043,590,528,988,526,547,1026,538,521,1004,532)
data.frame(furnaceposition,firingtemperature,bakeddensity)
##    furnaceposition firingtemperature bakeddensity
## 1                1               800          570
## 2                1               825         1063
## 3                1               850          565
## 4                1               800          565
## 5                1               825         1080
## 6                1               850          510
## 7                1               800          583
## 8                1               825         1043
## 9                1               850          590
## 10               2               800          528
## 11               2               825          988
## 12               2               850          526
## 13               2               800          547
## 14               2               825         1026
## 15               2               850          538
## 16               2               800          521
## 17               2               825         1004
## 18               2               850          532

#A. Both fixed effects:

furnaceposition <- as.fixed(furnaceposition)
firingtemperature <- as.fixed(firingtemperature)
model_1 <- aov(bakeddensity~furnaceposition+firingtemperature+furnaceposition*firingtemperature)
GAD::gad(model_1)
## $anova
## Analysis of Variance Table
## 
## Response: bakeddensity
##                                   Df Sum Sq Mean Sq  F value   Pr(>F)    
## furnaceposition                    1   7160    7160   15.998 0.001762 ** 
## firingtemperature                  2 945342  472671 1056.117 3.25e-14 ***
## furnaceposition:firingtemperature  2    818     409    0.914 0.427110    
## Residuals                         12   5371     448                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#It can be observed that no two-factor interaction is present, and both Position and Temperature are significant at a significance level of α = 0.05.

#B. Both Random Effects:

furnaceposition <- as.random(furnaceposition)
firingtemperature <- as.random(firingtemperature)
model_2 <- aov(bakeddensity~furnaceposition+firingtemperature+furnaceposition*firingtemperature)
GAD::gad(model_2)
## $anova
## Analysis of Variance Table
## 
## Response: bakeddensity
##                                   Df Sum Sq Mean Sq  F value    Pr(>F)    
## furnaceposition                    1   7160    7160   17.504 0.0526583 .  
## firingtemperature                  2 945342  472671 1155.518 0.0008647 ***
## furnaceposition:firingtemperature  2    818     409    0.914 0.4271101    
## Residuals                         12   5371     448                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#It can be observed that no two-factor interaction is present, and only Temperature is significant at a significance level of α = 0.05.

#C.Mixwed Effects:

furnaceposition <- as.fixed(furnaceposition)
firingtemperature <- as.random(firingtemperature)
model_3 <- aov(bakeddensity~furnaceposition+firingtemperature+furnaceposition*firingtemperature)
GAD::gad(model_3)
## $anova
## Analysis of Variance Table
## 
## Response: bakeddensity
##                                   Df Sum Sq Mean Sq  F value   Pr(>F)    
## furnaceposition                    1   7160    7160   17.504  0.05266 .  
## firingtemperature                  2 945342  472671 1056.117 3.25e-14 ***
## furnaceposition:firingtemperature  2    818     409    0.914  0.42711    
## Residuals                         12   5371     448                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#It can be observed that no two-factor interaction is present, and Temperature remains the only significant factor at a significance level of α = 0.05.

#D. Comments:

#1. For all three models, there was no significant two-factor interaction observed. #2. The Temperature variable was found to be significant in all cases, regardless of whether it was treated as a random or fixed effect. #3. The p-value for Temperature remains the same in parts (a) and (c) — where Temperature is a fixed effect in (a) and a random effect in (c). However, the p-value increases in part (b) when Temperature is considered a random effect. #A similar trend is observed for the Position variable, which shows comparable p-values in parts (b) and (c) (where Position is random in (b) and fixed in (c)), but a smaller p-value in part (a). #4. The Position variable is significant only when it is modeled as a fixed effect (part a, with Temperature also fixed), and not significant when modeled as a random effect or when Temperature itself is treated as random.

#SOURCE CODE:

#Question:1
#PARTA:(Write the model equation for a full factorial model)

#PARTB:
csvData <- read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/PowderProduction.csv")
print(csvData)

library(GAD)
csvData$Ammonium <- as.fixed(csvData$Ammonium)
csvData$StirRate <- as.fixed(csvData$StirRate)
csvData$Temperature <- as.fixed(csvData$Temperature)
str(csvData)

model<-aov(csvData$Density~csvData$Ammonium+csvData$StirRate+csvData$Temperature+csvData$Ammonium*csvData$StirRate+
             csvData$Ammonium*csvData$Temperature+csvData$StirRate*csvData$Temperature+
             csvData$Ammonium*csvData$StirRate*csvData$Temperature)
GAD::gad(model)

interaction.plot(csvData$StirRate,csvData$Ammonium,csvData$Density,col=c("red","blue"))


#Question:2 ()
furnaceposition <- c(rep(1,9),rep(2,9))
temperatures<-c(800,825,850)
firingtemperature <- rep(temperatures,6)
bakeddensity <- c(570,1063,565,565,1080,510,583,1043,590,528,988,526,547,1026,538,521,1004,532)
data.frame(furnaceposition,firingtemperature,bakeddensity)

#PartA:(fixed effects)
furnaceposition <- as.fixed(furnaceposition)
firingtemperature <- as.fixed(firingtemperature)
model_1 <- aov(bakeddensity~furnaceposition+firingtemperature+furnaceposition*firingtemperature)
GAD::gad(model_1)


#PartB:(Random effects)
furnaceposition <- as.random(furnaceposition)
firingtemperature <- as.random(firingtemperature)
model_2 <- aov(bakeddensity~furnaceposition+firingtemperature+furnaceposition*firingtemperature)
GAD::gad(model_2)

#PartC:(Mixed Effect)
furnaceposition <- as.fixed(furnaceposition)
firingtemperature <- as.random(firingtemperature)
model_3 <- aov(bakeddensity~furnaceposition+firingtemperature+furnaceposition*firingtemperature)
GAD::gad(model_3)

```