#Question 1a

data<-data.frame(
  Ammonium=c(2,2,30,30,2,2,30,30,2,2,30,30,2,2,30,30),
  StirRate=c(100,100,100,100,150,150,150,150,100,100,100,100,150,150,150,150),
  Temperature=c(8,8,8,8,8,8,8,8,40,40,40,40,40,40,40,40),
  Density=c(14.68,15.18,15.12,17.48,7.54,6.66,12.46,12.62,10.95,17.68,12.65,15.96,8.03,8.84,
            14.96,14.96)
)
library(ggplot2)
ggplot(data,aes(x=factor(Ammonium), y=Density, fill=factor(StirRate)))+
  geom_boxplot()+
  facet_wrap(~Temperature)

labs(title = "Data Density by Ammonium, StirRate, and Temperature",
     x="Ammonium(%)",y="Density(g/cm^2)",fill="StirRate(RPM)")+
  theme_minimal()
## NULL
model<-aov(Density~Ammonium*StirRate*Temperature, data=data)
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

The model equation for a full 2^3 factorial model is as follows

Yijk= Observed Density from factors M= Mean Ai= Effect of Ammonium Bj= Effect of StirRate Ck= Effect of Temperature (ABij),(ACik)(BCjk)= Two interaction effects (ABC)ijk= Three way interaction of factors Eijk= Experimental Error

Equation: Yijk= M+Ai+Bj+Ck+(ABij)+(ACik)+(BCjk)+(ABC)ijk+Eijk

#Question 1b Using Alpha =.05 as the guide for the p-value we can conclude that the three way interaction is not significant. It is mainly the individual factors/variables that are significant. The most significant variable being Temperature and StirRate. The slower and lower the StirRate and Temperature are, it seems to create higher density. This was based on the chart, and analyzed data from the given instructions.

#Question 2

library(agricolae)
set.seed(123)

A<-c("A1","A2")
B<-c("B1","B2")
C<-c("C1","C2")

BC<-as.vector(outer(B,C, paste, sep=":"))
d<-try(agricolae::design.ab(trt=list(A,BC), r=3, design="rcbd", seed=123),silent=TRUE)

if (inherits(d,"try-error")){
  d<-agricolae::design.ab(trt=c(length(A),length(BC)),r=3, design="rcbd", seed = 123)
  book<-d$book
f1<-intersect(c("factor1","trt1"),names(book))  
f2<-intersect(c("factor2","trt2"),names(book))

if (is.na(f1)||is.na(f2) || length(f1)==0 || length(f2)==0) {
  message("Fallback to generic columns detectred, using first two columns instead.")
  f1<-names(book)[1]
  f2<-names(book)[2]
}

book$A<-A[book[[f1]]]
book$BC<-BC[book[[f2]]]
} else {
  book<-d$book
  if ("trt1"%in% names(book)) names(book)[names(book)=="trt1"]<-"A"
  if ("trt2"%in% names(book)) names(book)[names(book)=="trt2"]<-"BC"
  if ("factor1"%in% names(book)) names(book)[names(book)=="factor1"]<-"A"
  if ("factor2"%in% names(book)) names(book)[names(book)=="factor2"]<-"BC"
}
## Fallback to generic columns detectred, using first two columns instead.
split_BC<-do.call(rbind, strsplit(as.character(book$BC), ":",fixed=TRUE))
book$B<-split_BC[,1]
book$C<-split_BC[,2]
book$BC<-NULL
book<-book[,c("plots","block", "A","B","C")]
book
##    plots block    A  B  C
## 1    101     1 <NA> B1 C1
## 2    102     1 <NA> B1 C1
## 3    103     1 <NA> B1 C1
## 4    104     1 <NA> B1 C1
## 5    105     1 <NA> B1 C1
## 6    106     1 <NA> B1 C1
## 7    107     1 <NA> B1 C1
## 8    108     1 <NA> B1 C1
## 9    109     2 <NA> B2 C1
## 10   110     2 <NA> B2 C1
## 11   111     2 <NA> B2 C1
## 12   112     2 <NA> B2 C1
## 13   113     2 <NA> B2 C1
## 14   114     2 <NA> B2 C1
## 15   115     2 <NA> B2 C1
## 16   116     2 <NA> B2 C1
## 17   117     3 <NA> B1 C2
## 18   118     3 <NA> B1 C2
## 19   119     3 <NA> B1 C2
## 20   120     3 <NA> B1 C2
## 21   121     3 <NA> B1 C2
## 22   122     3 <NA> B1 C2
## 23   123     3 <NA> B1 C2
## 24   124     3 <NA> B1 C2