[英]How do I use print list-column of regression models in a tibble using huxtable()
I am trying to produce an Rmarkdown document that prints a series of regression models I currently have stored in list-column in a tibble. 我正在尝试生成一个Rmarkdown文档,该文档打印出我目前以小标题形式存储在列表列中的一系列回归模型。 The regression models look like this. 回归模型如下所示。
#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
I know that you can just do 我知道你可以做
huxreg(model1, model2, model3, model4)
But i have many more models, and many more list columns that I want to ignore. 但是我有更多的模型,还有许多我想忽略的列表列。 I was trying. 我在尝试
library(purrr)
map(model.list[,1], huxreg)
And that works, to a point, but it does not render properly in Rarkdown. 在某种程度上,这种方法行得通,但是在Rarkdown中无法正确渲染。
Just do 做就是了
huxreg(model.list)
with the list object. 与列表对象。 From ?huxreg
: 从?huxreg
:
... Models, or a single list of models. Names will be used as column headings.
You don't need to make a tibble. 您无需费心。
You could also cut out one step: 您还可以减少一个步骤:
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)
In general, naming your variables like name1
, name2
etc. is a sign you should be using a list in the first place. 通常,将变量命名为name1
, name2
等是您应该首先使用列表的标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.