使用purrr :: map使用agricolae :: HSD.test分析lme4 :: lmer主题

时间:2017-07-13 11:29:18

标签: dictionary lme4 purrr

我无法理解如何使用map来应用分析数据中各个主题数据的函数

library(lme4)
library(tidyverse)
library(agricolae)

这是我开始的模型

fm <- lmer(Reaction ~ Days + (1 | Subject), data = sleepstudy)

我可以看一下个人的数据:

glimpse(ss <- sleepstudy %>%
    nest(-Subject) %>%
    mutate(fit = map(data, ~ lm(Reaction ~ Days, data = .)),
        results = map(fit, tidy)))

我可以使用ss $ fit元素并将HSD.test应用于它

glimpse(hsd <- HSD.test(ss$fit[[1]], trt = 'Days'))

但是当我尝试使用地图时,我会收到错误。以下是我使用的两种形式:

rm(ss)
glimpse(ss <- sleepstudy %>%
    nest(-Subject) %>%
    mutate(fit = map(data, ~ lm(Reaction ~ Days, data = .)),
        results = map(fit, HSD.test(., trt = 'Days'))))

glimpse(ss <- sleepstudy %>%
    nest(-Subject) %>%
    mutate(fit = map(data, ~ lm(Reaction ~ Days, data = .)),
        results = map(fit, HSD.test(fit, trt = 'Days'))))

1 个答案:

答案 0 :(得分:1)

您可以使用以下 -

library(lme4)
library(tidyverse)
library(agricolae)
library(broom)

sleepstudy %>%
  nest(data = -Subject) %>%
  mutate(fit = map(data, ~ lm(Reaction ~ Days, data = .)),
         results = map(fit, ~HSD.test(., trt = 'Days')))

#   Subject data              fit    results
#   <fct>   <list>            <list> <list> 
# 1 308     <tibble [10 × 2]> <lm>   <group>
# 2 309     <tibble [10 × 2]> <lm>   <group>
# 3 310     <tibble [10 × 2]> <lm>   <group>
# 4 330     <tibble [10 × 2]> <lm>   <group>
# 5 331     <tibble [10 × 2]> <lm>   <group>
# 6 332     <tibble [10 × 2]> <lm>   <group>
# 7 333     <tibble [10 × 2]> <lm>   <group>
# 8 334     <tibble [10 × 2]> <lm>   <group>
# 9 335     <tibble [10 × 2]> <lm>   <group>
#10 337     <tibble [10 × 2]> <lm>   <group>
#11 349     <tibble [10 × 2]> <lm>   <group>
#12 350     <tibble [10 × 2]> <lm>   <group>
#13 351     <tibble [10 × 2]> <lm>   <group>
#14 352     <tibble [10 × 2]> <lm>   <group>
#15 369     <tibble [10 × 2]> <lm>   <group>
#16 370     <tibble [10 × 2]> <lm>   <group>
#17 371     <tibble [10 × 2]> <lm>   <group>
#18 372     <tibble [10 × 2]> <lm>   <group>