0

plyr 是否会跳过某个因子的缺失级别[即分组变量]?这是我诊断问题的第一个问题。

我有一个数据集,其中患者位于strata=ruralstrata=city。我想将ageintreatment=Atreatment=B.

例如,我正在尝试做:

ddply(data.c, .(strata), function(x) t.test(age~treatment, data=x, na.rm=TRUE)

但它告诉我

t.test.formula 中的错误(年龄 ~ 治疗,数据 = x,na.rm = TRUE):分组因子必须恰好有 2 个级别

如果我运行factor(data$strata)并且factor(data$treatment)我分别只看到两个级别(它们分别是两个标签;这不是问题,对吗?)。

plyr 是否认为 NA 是分组因素中的一个级别?错误消息可能是什么问题?

我一直在谷歌搜索并查看 stackoverflow 来回答这个问题。我对 R 很陌生,但我找不到答案。任何帮助深表感谢。


所以我有一些示例代码。Chase 的例子效果很好,但是当我使用我的数据时,我仍然得到同样的错误。

dlply(data.c, .(strata), function(x) t.test(age~treatment, data=x, na.rm=TRUE产生了同样的错误。

我不明白为什么会这样。

下面是我的data.c

        strata  age treatment
1   1   57.8630136986301    p_3
2   0   52.958904109589 p_3
3   NA  97.2438356164384    p12
4   0   88.2027397260274    p12
5   1   77.5890410958904    p12
6   1   43.9123287671233    p12
7   1   63.5260273972603    p_3
8   1   42.1890410958904    p12
9   0   52.1753424657534    p12
10  1   65.6493150684932    p12
11  0   44.9835616438356    p12
12  1   64.8849315068493    p12
13  1   57.5835616438356    p12
14  0   47.3013698630137    p12
15  0   74.0356164383562    NA
16  0   65.4986301369863    p12
17  1   83.986301369863 p12
18  0   47.4904109589041    p12
19  1   47.8630136986301    p12
20  1   58.8520547945205    p12
21  1   61.3342465753425    p12
22  1   66.841095890411 p12
23  1   55.6383561643836    p12
24  1   52.7178082191781    p12
25  1   71.4630136986301    p12
26  1   NA  p12
27  1   59.2082191780822    p12
28  1   69.8575342465753    p12
29  1   46.7397260273973    p12
30  1   53.5013698630137    p_3
31  1   41.3205479452055    p12
32  0   51.3917808219178    p_3
33  1   47.8684931506849    p12
34  1   87.654794520548 p12
35  0   75.558904109589 p12
36  1   71.2520547945205    p12
37  1   44.9808219178082    p_3
38  1   52  p_3
39  1   54.7643835616438    p_3
40  1   85.6630136986301    p_3
41  1   74.1835616438356    p_3
42  1   56.8684931506849    p_3
43  1   87.572602739726 p_3
44  1   85.0109589041096    p_3
45  1   70.0767123287671    p_3
46  0   47.2328767123288    p12
47  1   63.7972602739726    p12
48  1   85.8054794520548    p12
49  1   67.027397260274 p12
50  1   60.7342465753425    p12
51  0   61.9479452054794    p_3
52  0   86.8712328767123    p12
53  1   87.8219178082192    p12
54  1   49.9424657534247    p12
55  0   83.386301369863 p12
56  1   88.3013698630137    p12
57  1   55.7890410958904    p12
58  1   63.7616438356164    p12
59  1   55.5041095890411    p12
60  1   43.5232876712329    p12
61  1   58.8246575342466    p12
62  0   46.7397260273973    p12
63  0   74.2027397260274    p_3
64  0   51.9205479452055    p_3
65  0   78.1890410958904    p_3
66  0   78.9917808219178    p_3
4

2 回答 2

2

我认为您需要向我们提供一些示例数据,因为我无法复制您的错误。我确实收到有关ddply()抱怨以下内容的错误:

 Error in list_to_dataframe(res, attr(.data, "split_labels")) : 
  Results must be all atomic, or all data frames

这是因为输出t.test()的不是 data.frame 而是 class 的列表htest。因此,只需告诉 plyr 返回一个列表对象即可解决问题。如果你想从列表中提取一个特定的值,你可能会得到一个 data.frame 回来。

以下是我设置您的数据的方式:

x <- data.frame(strata = sample(letters[1:2], 100, TRUE),
                age = sample(18:65, 100, TRUE),
                treatment = sample(LETTERS[1:2], 100, TRUE))
x[sample(nrow(x), 10, FALSE), "strata"] <- NA
x[sample(nrow(x), 10, FALSE), "treatment"] <- NA

然后只需调用函数即可dlply()

out <- dlply(x, .(strata), function(x) t.test(age~treatment, data = x, na.rm=TRUE))

为了回答你的问题,是的 - plyr 似乎将 NA 视为一个有效组,这可以从长度和out列出列表对象的名称中看出:

> names(out)
[1] "a"  "b"  "NA"

正如我所说,更好地描述您的问题可能会导致更好的答案 - 但希望这能让您走上正确的道路。

于 2012-07-12T03:37:57.540 回答
1

plyr为了对变量进行分组,确实将缺失视为单独的组。因此,在您的示例中,有三组:01NA.inform您可以通过添加参数来查看哪些组引发了错误:

dlply(data.c, 
      .(strata), 
      function(x) t.test(age~treatment, data=x, na.rm=TRUE), 
      .inform=TRUE)

报告:

Error in t.test.formula(age ~ treatment, data = x, na.rm = TRUE) : 
  grouping factor must have exactly 2 levels
Error: with piece 3: 
  strata      age treatment
3     NA 97.24384       p12

事实上,只有一行带有strataof NA,并且t.test对单个数据点(或者更确切地说,所有数据点都在同一组中)执行 a 将给出您看到的错误:

> t.test(age~treatment, data=data.c[is.na(data.c$strata),], na.rm=TRUE)
Error in t.test.formula(age ~ treatment, data = data.c[is.na(data.c$strata),  : 
  grouping factor must have exactly 2 levels

要解决此问题,您可以data.cdlply调用中设置子集:

dlply(data.c[!is.na(data.c$strata),],
      .(strata), 
      function(x) t.test(age~treatment, data=x, na.rm=TRUE))

(假设您知道问题将在哪里解决)并且给出:

$`0`

        Welch Two Sample t-test

data:  age by treatment 
t = 0.2653, df = 15.729, p-value = 0.7942
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -13.41565  17.24792 
sample estimates:
mean in group p_3 mean in group p12 
         64.22896          62.31283 


$`1`

        Welch Two Sample t-test

data:  age by treatment 
t = 0.6606, df = 18.612, p-value = 0.517
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -6.998471 13.440397 
sample estimates:
mean in group p_3 mean in group p12 
         65.50091          62.27995 


attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
  strata
1      0
2      1

或者您可以通过错误捕获来保护呼叫。plyr为此目的提供了一个方便的包装器try_default

dlply(data.c, 
      .(strata), 
      function(x) try_default(t.test(age~treatment, data=x, na.rm=TRUE),
                              NULL))

这使

Error in t.test.formula(age ~ treatment, data = x, na.rm = TRUE) : 
  grouping factor must have exactly 2 levels
$`0`

        Welch Two Sample t-test

data:  age by treatment 
t = 0.2653, df = 15.729, p-value = 0.7942
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -13.41565  17.24792 
sample estimates:
mean in group p_3 mean in group p12 
         64.22896          62.31283 


$`1`

        Welch Two Sample t-test

data:  age by treatment 
t = 0.6606, df = 18.612, p-value = 0.517
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -6.998471 13.440397 
sample estimates:
mean in group p_3 mean in group p12 
         65.50091          62.27995 


$`NA`
NULL

attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
  strata
1      0
2      1
3     NA

请注意,此输出有 3 个元素,其中NA一个NULLtry_default.

于 2012-07-12T17:45:25.470 回答