简体   繁体   English

如何使用huxtable()在小标题中使用回归模型的打印列表列

[英]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. 通常,将变量命名为name1name2等是您应该首先使用列表的标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 tibble::tribble() 创建列表列? - How to create a list-column with tibble::tribble()? 如何将ggplots的列表列打印为pdf? - how to print a list-column of ggplots to pdf? 在 R 中,如何在不依赖任何先前列的小标题中创建一个新的列表列变量? - In R, how can I create a new list-column variable in a tibble that doesn't depend on any prior columns? 如何在嵌套列表列中的 tibble 的字符列和行之间粘贴字符串 - How to paste strings between tibble's character column and rows in a nested list-column 将列表列添加到每个元素都是另一个列表列的第一个元素的 tibble - Adding a list-column to a tibble in which each element is the first element of another list-column 如何重新编码<null>单元格嵌套 NA (<lgl [1]> ) 在 tibble 的列表列中?</lgl></null> - How to recode <NULL> cells to nested NA (<lgl [1]>) in a tibble's list-column? 如何创建一个列表列,它是 R 中另一个列表列的子集? - How can I make a list-column that is a subset of another list-column in R? 当列表中有一个向量时,如何将列表列变异为一个普通的列,只留下最后一个值? - How do I mutate a list-column to a common one leaving only the last value when there is a vector in the list? purrr:在列表列中使用%in% - purrr: using %in% with a list-column 如何“传播”列表列? - how to “spread” a list-column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM