r语言 - 如何使用 huxtable() 在 tibble 中使用回归模型的打印列表列



我正在尝试生成一个Rmarkdown文档,该文档打印了我当前存储在tibble列表列中的一系列回归模型。回归模型如下所示。

#generate data
covar1<-rnorm(100)
covar2<-rnorm(100)
depvar1<-rnorm(100)
depvar2<-rnorm(100)
#generate models
model1<-lm(depvar1~covar1)
model2<-lm(depvar1~covar1+covar2)
model3<-lm(depvar2~covar1)
model4<-lm(depvar2~covar1+covar2)
#list models
library(huxtable)
library(dplyr)
model.list<-list(model1, model2,model3, model4)
#make tibble
model.list<-tibble(model.list)
#name models by dependent variable
model.list$model_name<-c('Depvar1', 'Depvar1', 'Depvar2', 'Depvar2' )
#check
model.list

我知道你可以做

huxreg(model1, model2, model3, model4)

但是我有更多的模型,以及更多我想忽略的列表列。 我在努力。

library(purrr)
map(model.list[,1], huxreg)

这在一定程度上是有效的,但它在 Rarkdown 中无法正确呈现。

只是做

huxreg(model.list)

与列表对象。从?huxreg

...     Models, or a single list of models. Names will be used as column headings.

你不需要做一个嘟嘟。

你也可以省掉一个步骤:

models <- list()
models[[1]] <- lm(depvar1~covar1)
models[[2]] <- lm(depvar1~covar1+covar2)
models[[3]] <- lm(depvar2~covar1)
models[[4]] <- lm(depvar2~covar1+covar2)
huxreg(models)

一般来说,命名你的变量,如name1name2等,是你应该首先使用列表的标志。

最新更新