R语言之方差分析篇( 二 )

install.packages("multcomp")library(multcomp)head(litter,n=21)data(litter,package="multcomp")attach(litter)options(digits=5)table(litter$dose)aggregate(weight,by=list(litter$dose),FUN=mean)fit<-aov(weight~gesttime+litter$dose)summary(fit)

R语言之方差分析篇

文章插图
因使用了协变量,短途运输 获取调整的组均值即去除协变量疚后的组均值,可使用 包中的()函数来计算调整的均值
library(effects)effect("dose",fit)
用户定义的对照的多重比较
library(multcomp)contrast<-rbind("no drug vs.drug"=c(3,-1,-1,-1))summary(glht(fit,linfct=mcp(dose=contrast)))
(1)评估检验的假设条件
与ANOVA相同,都城要正态性和同方差性假设
另还假定回归低低斜率相同,eg当模型饮食怀孕时间*剂量的交互项时,可对回归斜率的同质性进行检验 。
eg
检验回归斜率的同质性
library(multcomp)fit2<-aov(weight~gesttime*dose,data=http://www.kingceram.com/post/litter)summary(fit2)
R语言之方差分析篇

文章插图
(2)结果的可视化
install.packages("HH")library(HH)ancova(weight~gesttime+dose,data=http://www.kingceram.com/post/litter)
R语言之方差分析篇

文章插图

R语言之方差分析篇

文章插图
4、双因素方差分析
双因素ANOVA
attach(ToothGrowth)head(ToothGrowth)table(supp,ToothGrowth$dose)aggregate(len,by=list(supp,ToothGrowth$dose),FUN=mean)aggregate(len,by=list(supp,ToothGrowth$dose),FUN=sd)fit<-aov(len~supp*ToothGrowth$dose)summary(fit)detach(ToothGrowth)
R语言之方差分析篇

文章插图
可视化处理
interaction.plot(ToothGrowth$dose,supp,len,type="b",col=c("red","blue"),pch=c(16,18),main="Interaction between Dose and Supplement Type")
R语言之方差分析篇

文章插图
可用包中的()来展示交互效应
install.packages("gplots")library(gplots)plotmeans(len~interaction(supp,ToothGrowth$dose,sep=" "),connect=list(c(1,3,5),c(2,4,6)),col=c("red","darkgreen"),main="Interaciton Plot with 95% CIS",xlab="Treatment and Dose Combination")
R语言之方差分析篇

文章插图
用HH包中的()函数来可视化结果
library(HH)interaction2wt(len~supp*ToothGrowth$dose)
R语言之方差分析篇

文章插图
5、重复测量方差分析
R语言之方差分析篇

文章插图
所谓重复测量方差分析,即受试者被测量不止一次 。
w1b1<-subset(CO2,Treatment=="chilled")w1b1fit<-aov(uptake~conc*Type+Error(Plant/(conc)),w1b1)summary(fit)par(las=2)par(mar=c(10,4,4,2))with(w1b1,interaction.plot(conc,Type,uptake,type="b",col=c("red","blue"),pch=c(16,18),main="Interaction Plot for Plant Type and Concentration"))
R语言之方差分析篇

文章插图
boxplot(uptake~Type*conc,data=http://www.kingceram.com/post/w1b1,col=c("gold","green"),main="Chilled Quebec and Mississippi Plants",ylab="Carbon dioxide uptake rate umol/m^2 sec")