英文:
Skip a model in a modelsummary table
问题
I am trying to print a three-panel table of 3 regression models, in which instead of one of the results (the middle regression for the middle panel) it should be "NA" or "--".
For example, I can run:
library(modelsummary)
y <- rnorm(5)
models <- list()
for (i in 1:3) {
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
}
models <- list(models[1:3], models[4:6], models[7:9])
modelsummary(models, shape = 'rbind', gof_map = c(""),
coef_omit = "Intercept", estimate = "{estimate}", statistic = NULL)
to produce the following table:
I don't need the second regression from Panel B. I need the respective coefficient to be NA, or a dash. Is there a way to pass something to modelsummary to tell it to skip printing that particular model?
英文:
I am trying to print a three-panel table of 3 regression models, in which instead of one of the results (the middle regression for the middle panel) it should be "NA" or "--".
For example, I can run:
library(modelsummary)
y <- rnorm(5)
models <- list()
for (i in 1:3) {
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
}
models <- list(models[1:3], models[4:6], models[7:9])
modelsummary(models, shape = 'rbind', gof_map = c(""),
coef_omit = "Intercept", estimate = "{estimate}", statistic = NULL)
to produce the following table:
I don't need the second regression from Panel B. I need the respective coefficient to be NA, or a dash. Is there a way to pass something to modelsummary to tell it to skip printing that particular model?
答案1
得分: 1
你可以将modelsummary
保存为一个对象,然后编辑系数部分。这里我使用了随机种子123。
MS <- modelsummary(models, shape = 'rbind', gof_map = c(""),
coef_omit = "Intercept", estimate = "{estimate}", statistic = NULL)
MS[[5]] <- gsub("-1.150", " ", MS[[5]])
英文:
You can save the modelsummary
as an object then edit the coefficient out. Here I have used a random seed of 123.
MS <- modelsummary(models, shape = 'rbind', gof_map = c(""),
coef_omit = "Intercept", estimate = "{estimate}", statistic = NULL)
MS[[5]] <- gsub("-1.150", " ", MS[[5]])
答案2
得分: 1
这是与@TheN提出的类似想法:
- 将模型保存到中间的
modelsummary_list
表示形式中。 - 手动修改您想要的值。
- 将
modelsummary_list
输入到modelsummary()
。
这种方法的好处是它可以适用于任何“最终”输出格式,而 gsub
解决方案仅适用于某些输出类型。
library(modelsummary)
y <- rnorm(5)
models <- list()
for (i in 1:3) {
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
}
models <- list(models[1:3], models[4:6], models[7:9])
tmp <- lapply(models, modelsummary, output = "modelsummary_list")
tmp[[2]][[2]]$tidy$estimate[2] <- tmp[[2]][[2]]$tidy$std.error[2] <- "-"
modelsummary(
tmp, shape = 'rbind', gof_map = c(""),
coef_omit = "Intercept", estimate = "{estimate}", statistic = NULL)
英文:
This is a similar idea to the one proposed by @TheN:
- Save your models to an intermediate
modelsummary_list
representation. - Modify the values you want manually.
- Feed the
modelsummary_list
back tomodelsummary()
.
The benefit of this approach is that it will work with any “final” output format, whereas the gsub
solution only works for some output types.
library(modelsummary)
y <- rnorm(5)
models <- list()
for (i in 1:3) {
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
models[[length(models) + 1]] <- lm(y ~ rnorm(5))
}
models <- list(models[1:3], models[4:6], models[7:9])
tmp <- lapply(models, modelsummary, output = "modelsummary_list")
tmp[[2]][[2]]$tidy$estimate[2] <- tmp[[2]][[2]]$tidy$std.error[2] <- "-"
modelsummary(
tmp, shape = 'rbind', gof_map = c(""),
coef_omit = "Intercept", estimate = "{estimate}", statistic = NULL)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论