我列出了一个模型列表:
library(nlme)
library(broom)
models <- lapply(1:5,function(i){
idx= sample(nrow(Orthodont),replace=TRUE)
lme(distance ~ age, random=~Sex,data = Orthodont[idx,])
})
model_list <- lapply(models,tidy,effects="fixed")
在这些模型中,有用的系数是第二个:
model_list[[1]]
# A tibble: 2 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 15.9 1.03 15.5 7.77e-26
2 age 0.739 0.0871 8.48 9.13e-13
您可以像这样在向量中获取 p 值,例如使用 p.value 1:
sapply(model_list,function(x)x$p.value[2])
跟踪模型而不用变量填充环境的更好方法是使用 purrr, dplyr(在此处查看更多信息):
library(purrr)
library(dplyr)
models = tibble(name=1:5,models=models) %>%
mutate(tidy_res = map(models,tidy,effects="fixed"))
models
# A tibble: 5 x 3
name models tidy_res
<int> <list> <list>
1 1 <lme> <tibble [2 × 5]>
2 2 <lme> <tibble [2 × 5]>
3 3 <lme> <tibble [2 × 5]>
4 4 <lme> <tibble [2 × 5]>
5 5 <lme> <tibble [2 × 5]>
models %>% unnest(tidy_res) %>% filter(term=="age")
# A tibble: 5 x 7
name models term estimate std.error statistic p.value
<int> <list> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 <lme> age 0.587 0.0601 9.77 2.44e-15
2 2 <lme> age 0.677 0.0663 10.2 3.91e-16
3 3 <lme> age 0.588 0.0603 9.74 3.05e-15
4 4 <lme> age 0.653 0.0529 12.3 2.74e-20
5 5 <lme> age 0.638 0.0623 10.2 3.34e-16