1

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

我知道如何使用以下代码从综合方差分析中提取 MS:

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

首先产生受试者间 MS (PopColor$Pop),然后分别产生受试者间 MS (残差):

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

下面是我的数据的一个子集,有六个人口:

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

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

4

1 回答 1

0

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

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

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可能的配对。

于 2013-06-23T22:34:30.743 回答