如何进行迭代方差分析并从R中的lm对象中提取均方值
我有一个数据集,其中有18个群体。每个群体中都有几个个体,每个个体都有一个“颜色”的称呼。我只想在单因素方差分析中同时比较两个群体,以群体为主要因素,以获得内部和之间的成对MS 我知道如何使用以下代码从综合方差分析中提取MS:如何进行迭代方差分析并从R中的lm对象中提取均方值,r,statistics,anova,do-loops,mean-square-error,R,Statistics,Anova,Do Loops,Mean Square Error,我有一个数据集,其中有18个群体。每个群体中都有几个个体,每个个体都有一个“颜色”的称呼。我只想在单因素方差分析中同时比较两个群体,以群体为主要因素,以获得内部和之间的成对MS 我知道如何使用以下代码从综合方差分析中提取MS: mylm <- lm(Color ~ Pop, data=PopColor) anova(mylm)[["Mean Sq"]] 有没有一种方法可以让我创建一个do循环,在所有人群之间进行所有成对的单向方差分析,然后在MS之间和MS内提取出数据 然后,我想将两个MS
mylm <- lm(Color ~ Pop, data=PopColor)
anova(mylm)[["Mean Sq"]]
dput(dat)
structure(list(Pop = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("pop1001", "pop1026",
"pop252", "pop254a", "pop311", "pop317"), class = "factor"),
Color = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L,
3L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 3L, 3L, 2L, 3L, 2L,
3L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 3L, 3L, 2L, 3L, 3L, 2L,
3L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 4L,
2L, 3L, 2L, 4L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 4L, 3L, 2L, 4L,
4L, 1L, 2L, 2L, 2L, 2L, 1L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L,
2L, 3L, 4L, 2L, 2L, 4L, 3L)), .Names = c("Pop", "Color"), class = "data.frame", row.names = c(NA,
-94L))
任何帮助都将不胜感激!谢谢 我不明白你的第2点(对于不像我这样的统计学家来说有点技术性)。对于第一点,我理解为您希望在所有对人群中应用lm/Anova。您可以使用
combn
:
combn:一次生成x和m元素的所有组合
pops欢迎来到SO。我对你的问题提出了一点看法。请阅读SO关于如何设置问题格式的帮助。格式良好的问题通常会得到最好的答案。感谢您重新格式化我的问题!您最好使用multcomp
软件包,该软件包还可以更正多次比较。@hadley是的,我将向general/omnibus ANOVA模型添加bonferroni更正。现在我只需要MS,多次比较修正只会改变F重要性的临界值,而不会改变实际MS值。但是谢谢-我会调查multcomp软件包。我需要15点声誉积分,所以我无法撤销否决票!谢谢你的回答,这真的很有帮助!不客气。我不是专家,但就个人而言,我会尝试@hadley建议使用multicmp
软件包
dput(dat)
structure(list(Pop = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("pop1001", "pop1026",
"pop252", "pop254a", "pop311", "pop317"), class = "factor"),
Color = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L,
3L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 3L, 3L, 2L, 3L, 2L,
3L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 3L, 3L, 2L, 3L, 3L, 2L,
3L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 4L,
2L, 3L, 2L, 4L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 4L, 3L, 2L, 4L,
4L, 1L, 2L, 2L, 2L, 2L, 1L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L,
2L, 3L, 4L, 2L, 2L, 4L, 3L)), .Names = c("Pop", "Color"), class = "data.frame", row.names = c(NA,
-94L))
pops <- unique(PopColor$Pop)
ll <- combn(pops,2,function(x){
dat.pair <- PopColor[PopColor$Pop %in% pops[x],]
mylm <- lm(Color ~ Pop, data=dat.pair)
c(as.character(pops[x]),anova(mylm)[["Mean Sq"]])
},simplify=FALSE)
do.call(rbind,ll)
[,1] [,2] [,3] [,4]
[1,] "pop1026" "pop254a" "0.210291858678956" "0.597865353037767"
[2,] "pop1026" "pop1001" "0.52409988385598" "0.486874236874237"
[3,] "pop1026" "pop317" "15.7296466973886" "0.456486042692939"
[4,] "pop1026" "pop311" "1.34392057218144" "0.631962930099576"
[5,] "pop1026" "pop252" "0.339324116743472" "0.528899835796388"
[6,] "pop254a" "pop1001" "0.0166666666666669" "0.351785714285714"
[7,] "pop254a" "pop317" "14.45" "0.227777777777778"
[8,] "pop254a" "pop311" "1.92898550724637" "0.561430575035063"
[9,] "pop254a" "pop252" "0.8" "0.344444444444444"
[10,] "pop1001" "pop317" "20.4166666666667" "0.205357142857143"
[11,] "pop1001" "pop311" "3.55030333670374" "0.46474019088017"
[12,] "pop1001" "pop252" "1.35" "0.280357142857143"
[13,] "pop317" "pop311" "9.60474308300398" "0.429172510518934"
[14,] "pop317" "pop252" "8.45" "0.116666666666667"
[15,] "pop311" "pop252" "0.110803689064557" "0.496914446002805"