Midterm Q2: Olamide Gab-Opadokun 3/6/2020
Midterm Q2: Olamide Gab-Opadokun 3/6/2020
Olamide Gab-Opadokun
3/6/2020
library(AlgDesign)
gen.factorial(c(2,2,2,2), center=TRUE)
## X1 X2 X3 X4
## 1 -1 -1 -1 -1
## 2 1 -1 -1 -1
## 3 -1 1 -1 -1
## 4 1 1 -1 -1
## 5 -1 -1 1 -1
## 6 1 -1 1 -1
## 7 -1 1 1 -1
## 8 1 1 1 -1
## 9 -1 -1 -1 1
## 10 1 -1 -1 1
## 11 -1 1 -1 1
## 12 1 1 -1 1
## 13 -1 -1 1 1
## 14 1 -1 1 1
## 15 -1 1 1 1
## 16 1 1 1 1
food<-read.csv('C:/Users/Olamide/Downloads/PecosFood.csv')
head(food)
1
fit<-lm(Log.Microbial.Count~.,data=food)
anova(fit)
effect<-fit$coefficients[-1]
qq<-qqnorm(effect)
### use identify(qq) and click on graph to identify outliers
### identify(qq) does not work in R studio
text(qq$x[8], qq$y[8]+0.15, names(effect)[8])
text(qq$x[2], qq$y[2]+0.15, names(effect)[2])
text(qq$x[3], qq$y[3]-0.15, names(effect)[3])
2
Normal Q−Q Plot
1.5
Moisture
1.0
Sample Quantiles
0.5
−0.5 0.0
Preservative
X23
−1.5
−1 0 1
Theoretical Quantiles
#a)
#############################################
#to obtain proportion of variability associated to X15
SSX15<-anova(fit)[15,2]
SSTO<-sum(anova(fit)[,2])
P15<-SSX15/SSTO
P15
3
## [1] 0.003036787
b)
for (i in 1:15)
SSX<-as.numeric(anova(fit)[,2][-16])
SSTO<-sum(as.numeric(anova(fit)[,2]))
Proportions<-SSX/SSTO
prop<-as.numeric(Proportions)
prop
HalfNormalPlot.p<-function(lm.object,B=20,alpha=0.05,...)
{
## temporary lm object
lm.object.temp<-lm.object
## design matrix
X<-model.matrix(fit)
## n and p
n<-nrow(X)
## sum of squares cross products matrix
SSCP<- t(X) %*% X
## invert SSCP
A<-eigen(SSCP)
SSCP.inv<- A$vectors %*% diag(1/A$values) %*% t(A$vectors)
## hat matrix
H<-X %*% SSCP.inv %*% t(X)
## scale matrix for residuals (sigma=1)
A<-eigen(diag(n)-H)
## Because of rounding error some of the eigenvalues might be negative
## but essentially equal to zero. So we compute the absolute value before
## the square root
scale<- A$vectors %*% diag(sqrt(abs(A$values))) %*% t(A$vectors)
t<-prop
## order statistics
order.stats<-t[order(t)]
## theoretical quantiles
z<-qnorm((1:(n-1)+(n-1)-.125)/(2*(n-1)+0.5))
plot(z,order.stats,
xlab="Half Normal Scores",
ylab="Proportions",...)
effect1<-fit$effects[-1]
text(z,order.stats)
# initialize a matrix
MC.Sample<-matrix(NA,ncol=15,nrow=B)
4
for (k in 1:B)
{
food.temp<-read.csv('C:/Users/Olamide/Downloads/PecosFood.csv')
food.temp$Log.Microbial.Count<-rnorm(16)
fit.temp<-lm(Log.Microbial.Count~.,data=food.temp)
anova(fit.temp)
effect<-fit.temp$coefficients[-1]
#extra sum of squares
as.numeric(anova(fit.temp)[,2])[-16]
## again without the warnings
options(warn = -1)
as.numeric(anova(fit.temp)[,2])[-16]
#b)
for (i in 1:15)
SSX<-as.numeric((anova(fit.temp)[,2])[-16])
SSTO<-sum(as.numeric(anova(fit.temp)[,2]))
Proportions<-SSX/SSTO
prop2<-as.numeric(Proportions)
prop2
t<-prop2
## order statistics
MC.Sample[k,]<-t[order(t)]
}
## enveloppe
upper<-function(x) { quantile(x,prob=1-alpha,type=9) }
lower.limits<-apply(MC.Sample,MARGIN=2,min)
median.values<-apply(MC.Sample,MARGIN=2,median)
upper.limits<-apply(MC.Sample,MARGIN=2,upper)
points(z,lower.limits,lty=3,type="l",col="blue",lwd=2)
points(z,median.values,lty=5,type="l",col="blue",lwd=2)
points(z,upper.limits,lty=3,type="l",col="blue",lwd=2)
HalfNormalPlot.p(fit)
5
14 15
0.30
13
Proportions
0.20
0.10
0.00
1 2 3 4 5 6 7 8 9 10 11 12
c) According to the plot, the active effect are X12, X23 and X123