我在mtcars
数据集中运行回归模型并将结果导出到数据框
library(tidyverse)
library(broom)
outcomes <- c("wt", "mpg", "hp", "disp")
exposures <- c("gear", "vs", "am")
covariates <- c("drat", "qsec")
expand.grid(outcomes, exposures, covariates) %>%
group_by(Var1, Var2) %>%
summarise(Var3 = paste0(Var3, collapse = "+")) %>%
rowwise() %>%
summarise(frm = paste0(Var1, "~factor(", Var2, ")+", Var3)) %>%
group_by(model_id = row_number(),
frm, samplesize=nrow(mtcars)) %>%
do(tidy(lm(.$frm, data = mtcars))) %>%
ungroup()
我知道mtcars
没有丢失的数据,但如果我的数据框包含缺失值,那么列samplesize
是否会显示每个模型的样本大小,即frm
的每个组合?