#part 1
library(lsmeans)
## Warning: package 'lsmeans' was built under R version 3.6.2
## Loading required package: emmeans
## Welcome to emmeans.
## NOTE -- Important change from versions <= 1.41:
## Indicator predictors are now treated as 2-level factors by default.
## To revert to old behavior, use emm_options(cov.keep = character(0))
## The 'lsmeans' package is now basically a front end for 'emmeans'.
## Users are encouraged to switch the rest of the way.
## See help('transition') for more information, including how to
## convert old 'lsmeans' objects and scripts to work with 'emmeans'.
library(car)
## Warning: package 'car' was built under R version 3.6.2
## Loading required package: carData
brand =as.factor(rep(c(1,2,3),each=5))
lives =c(100,96,92,96,92,76,80,75,84,82,108,100,96,98,100)
battery=data.frame(lives,brand)
mod1=aov(lives~brand, data=battery);anova(mod1)
plot(mod1,which=2)
shapiro.test(mod1$residuals)
##
## Shapiro-Wilk normality test
##
## data: mod1$residuals
## W = 0.92696, p-value = 0.2457
plot(mod1,which=1)
leveneTest(lives~brand,data=battery)
lsmbrand=lsmeans(mod1,~brand);lsmbrand
## brand lsmean SE df lower.CL upper.CL
## 1 95.2 1.77 12 91.4 99.0
## 2 79.4 1.77 12 75.6 83.2
## 3 100.4 1.77 12 96.6 104.2
##
## Confidence level used: 0.95
tk=summary(contrast(lsmbrand,method='pairwise',adjust = 'tukey'),infer = c(T,T), level=0.95, side = 'two-sided');tk
from d, brand2 has the shortest life.
The company expected to replace 56.037% of batteries of brand2.
pnorm((80-79.4)/sqrt(15.6))
## [1] 0.5603714
#Part 2 6.1 (a) Since no clear patterns observed in the QQ plot and P value of shapiro test is 0.3454 > 0.05,assumption of normality is satisfied. Since no clear patterns observed in residuals vs fitted plot and P value of Levene’s Test is 0.3332 greater than 0.05, assumption of constant variance is satisfied. The data doesn’t need a transformation.
dat<-read.table("pr6.1",header = TRUE)
dat$ftrt<- as.factor(dat$trt)
mod61=aov(y~ftrt,data=dat);plot(mod61,which=c(1,2))
shapiro.test(mod61$residuals)
##
## Shapiro-Wilk normality test
##
## data: mod61$residuals
## W = 0.95494, p-value = 0.3454
leveneTest(y~ftrt,data=dat)
summary(mod61)
## Df Sum Sq Mean Sq F value Pr(>F)
## ftrt 5 6398 1280 71.2 3.2e-11 ***
## Residuals 18 323 18
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(emmeans)
lsmTrt<-lsmeans(mod61,~ftrt);lsmTrt
## ftrt lsmean SE df lower.CL upper.CL
## 1 95.0 2.12 18 90.5 99.5
## 2 82.2 2.12 18 77.8 86.7
## 3 81.5 2.12 18 77.0 86.0
## 4 68.2 2.12 18 63.8 72.7
## 5 50.5 2.12 18 46.0 55.0
## 6 52.0 2.12 18 47.5 56.5
##
## Confidence level used: 0.95
#Subset data set excluding trt=2 and trt=6 groups
dat2<-dat[which(dat$trt!=2 & dat$trt!=6),]
#Add nitrogen level
nlev_f<-function(x){
if(x==1){1}else if(x==3){2} else if(x==4){3} else {4}
}
dat2$nlevel<- sapply(dat2$trt,nlev_f)
plot(dat2$nlevel, dat2$y)
summary( contrast(lsmTrt,list("quadratic effec"=c(1,0,-1,-1,1,0))), infer=c(T,T), level=0.95, side="two-sided" )
reg_irri<- lm(y~nlevel+I(nlevel^2),data = dat2)
summary(reg_irri)
##
## Call:
## lm(formula = y ~ nlevel + I(nlevel^2), data = dat2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.7375 -3.2938 0.7375 2.3750 6.4625
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 105.188 5.768 18.235 1.21e-10 ***
## nlevel -9.362 5.262 -1.779 0.0986 .
## I(nlevel^2) -1.063 1.036 -1.026 0.3238
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.144 on 13 degrees of freedom
## Multiple R-squared: 0.9509, Adjusted R-squared: 0.9434
## F-statistic: 125.9 on 2 and 13 DF, p-value: 3.099e-09
summary( contrast(lsmTrt,list("irre effect"=c(1/2,-1/2,0,0,1/2,-1/2))), infer=c(T,T), level=0.95, side="two-sided" )
summary(contrast(lsmTrt, method="trt.vs.ctrl", adjust="mvt",ref=1), infer=c(T,T), level=0.95, side="two-sided")
7.2 Sample size 77 we would need for each group.
groupmeans <- c(10, 11, 11)
p <- power.anova.test(groups = length(groupmeans), between.var = var(groupmeans), within.var = 4, power=0.9,sig.level=0.05);p
##
## Balanced one-way analysis of variance power calculation
##
## groups = 3
## n = 76.93183
## between.var = 0.3333333
## within.var = 4
## sig.level = 0.05
## power = 0.9
##
## NOTE: n is number in each group
7.4 false;false