【问题标题】:Problem with running paired t-test within nested dplyr dataset在嵌套 dplyr 数据集中运行配对 t 检验的问题
【发布时间】:2020-08-07 22:50:05
【问题描述】:

我已经完成了新 dplyr v1.0.0 的逐行操作的小插图,并且对 nest_by 函数在数据集的不同孤岛内建模的可能性很感兴趣。

但是,我很难让重复测量分析发挥作用。

这里有一个例子来说明它什么时候起作用

df1 <- data.frame(group = factor(rep(LETTERS[1:3],10)),
                  pred = factor(rep(letters[1:2],each=5,length.out=30)),
                  out = rnorm(30))

现在基于group 变量创建嵌套。

library(dplyr)
nest1 <- df1 %>% nest_by(group)
nest

我们可以查看这个新的特殊嵌套数据框

# A tibble: 3 x 2
# Rowwise:  group
# group               data
# <fct> <list<tbl_df[,2]>>
# a               [10 x 2]
# b               [10 x 2]
# c               [10 x 2]

现在我们可以对其执行操作,如线性回归,在原始组变量的每个级别内对pred 回归out

mods <- nest1 %>% mutate(mod = list(lm(out ~ pred, data = data)))

在这个新对象中,我们向包含 lm() 对象的原始嵌套数据集添加了一个新列

mods

#   # A tibble: 3 x 3
#   # Rowwise:  group
#   group               data mod   
#   <fct> <list<tbl_df[,2]>> <list>
#   1 A               [10 x 2] <lm>  
#   2 B               [10 x 2] <lm>  
#   3 C               [10 x 2] <lm>

我们可以查看这些模型的结果

library(broom)
mods %>% summarise(broom::tidy(mod))
#   A tibble: 6 x 6
#   Groups:   group [3]
#   group term        estimate std.error statistic  p.value
#   <fct> <chr>          <dbl>     <dbl>     <dbl>  <dbl>
# 1 A     (Intercept)   0.0684     0.295     0.232  0.823 
# 2 A     predb        -0.231      0.418    -0.553  0.595 
# 3 B     (Intercept)  -0.159      0.447    -0.356  0.731 
# 4 B     predb         0.332      0.633     0.524  0.615 
# 5 C     (Intercept)  -0.385      0.245    -1.57   0.154 
# 6 C     predb         0.891      0.346     2.58   0.0329

现在我希望能够做同样的事情,但需要重复测量 t 检验。

# dataset with grouping factor and two columns, each representing a measure at one of two timepoints
df2 <- data.frame(group = factor(rep(letters[1:3],10)),
                  t1 = rnorm(30),
                  t2 = rnorm(30))

# nest by grouping factor
nest2 <- df2 %>% nest_by(group)
nest2

# A tibble: 3 x 2

# Rowwise:  group
# group                 data
# <fct>   <list<tbl_df[,2]>>
# 1 a               [10 x 2]
# 2 b               [10 x 2]
# 3 c               [10 x 2]

现在,当我尝试在新嵌套数据集的每个级别执行配对 t 检验时,使用与线性模型类似的过程...

mods2 <- nest2 %>% mutate(t = list(t.test(t1, t2, data = data)))

...我收到以下错误消息

Error: Problem with `mutate()` input `t`.
x object 't1' not found
i Input `t` is `list(t.test(t1, t2, data = data))`.
i The error occured in row 1.
Run `rlang::last_error()` to see where the error occurred.

谁能帮助我?

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    data 选项与formula 方法一起使用,而's3' 方法以xy 作为参数,我们可以使用with 包装

    library(dplyr)
    library(purrr)
    nest2 %>%
          mutate(t = list(with(data, t.test(t1, t2))))
    # A tibble: 3 x 3
    # Rowwise:  group
    #  group               data t      
    #  <fct> <list<tbl_df[,2]>> <list> 
    #1 a               [10 × 2] <htest>
    #2 b               [10 × 2] <htest>
    #3 c               [10 × 2] <htest>     
    

    或使用提取器($[[

    nest2 %>% 
        mutate(t = list(t.test(data$t1, data$t2)))
    

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多