简体   繁体   中英

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. 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.

Just do

huxreg(model.list)

with the list object. From ?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM