如何进行迭代ANOVA并从R中的lm对象中提取均方值

时间:2013-06-23 21:20:09

标签: r statistics anova do-loops mean-square-error

我有一个数据集,其中我有18个人口。每个人口中都有几个人,每个人都有一个“颜色”电话。我只想在单因素方差分析中同时比较两个群体,以群体为主要因素,以获得成对的MS-within和MS-between。

我知道如何使用以下代码从综合ANOVA中提取MS:

mylm <- lm(Color ~ Pop, data=PopColor)
anova(mylm)[["Mean Sq"]]

首先产生主体间MS(PopColor $ Pop),然后分别产生主体间MS(残差):

[1] 3.7079911 0.4536985
  1. 有没有办法可以创建一个do-loop来在所有种群之间进行所有成对单向方差分析,然后提取MS中的内部和内部?
  2. 然后,我想将每次比较的两个MS值移动到它们自己的对称矩阵中:一个受群体标记的受试者MS矩阵,一个受群体标记的受试者内MS矩阵。这些列和行名称与人口名称相同。
  3. 以下是我的数据的一个子集,有六个人口:

    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))
    

    任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

我不明白你的观点2(对于像我这样的统计学家来说,有点技术性)。对于第一点,我理解它,因为你想在所有人群中应用lm / Anova。您可以使用combn

  

combn:生成一次取m的x元素的所有组合

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"

如您所见,我们有choose(6,2)=15个可能的对。