Skip a model in a modelsummary table.

huangapple go评论109阅读模式
英文:

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:

Skip a model in a modelsummary 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 &lt;- rnorm(5)

models &lt;- list()
for (i in 1:3) {
  models[[length(models) + 1]] &lt;- lm(y ~ rnorm(5))
  models[[length(models) + 1]] &lt;- lm(y ~ rnorm(5))
  models[[length(models) + 1]] &lt;- lm(y ~ rnorm(5))
}
models &lt;- list(models[1:3], models[4:6], models[7:9])

modelsummary(models, shape = &#39;rbind&#39;, gof_map = c(&quot;&quot;), 
              coef_omit = &quot;Intercept&quot;, estimate  = &quot;{estimate}&quot;, statistic = NULL)

to produce the following table:

Skip a model in a modelsummary 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 &lt;- modelsummary(models, shape = &#39;rbind&#39;, gof_map = c(&quot;&quot;), 
                   coef_omit = &quot;Intercept&quot;, estimate  = &quot;{estimate}&quot;, statistic = NULL)

MS[[5]] &lt;- gsub(&quot;-1.150&quot;, &quot;      &quot;, MS[[5]])

答案2

得分: 1

这是与@TheN提出的类似想法:

  1. 将模型保存到中间的 modelsummary_list 表示形式中。
  2. 手动修改您想要的值。
  3. 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)

Skip a model in a modelsummary table.

英文:

This is a similar idea to the one proposed by @TheN:

  1. Save your models to an intermediate modelsummary_list representation.
  2. Modify the values you want manually.
  3. Feed the modelsummary_list back to modelsummary().

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 &lt;- rnorm(5)

models &lt;- list()
for (i in 1:3) {
  models[[length(models) + 1]] &lt;- lm(y ~ rnorm(5))
  models[[length(models) + 1]] &lt;- lm(y ~ rnorm(5))
  models[[length(models) + 1]] &lt;- lm(y ~ rnorm(5))
}
models &lt;- list(models[1:3], models[4:6], models[7:9])

tmp &lt;- lapply(models, modelsummary, output = &quot;modelsummary_list&quot;)
tmp[[2]][[2]]$tidy$estimate[2] &lt;- tmp[[2]][[2]]$tidy$std.error[2] &lt;- &quot;-&quot;

modelsummary(
    tmp, shape = &#39;rbind&#39;, gof_map = c(&quot;&quot;), 
    coef_omit = &quot;Intercept&quot;, estimate  = &quot;{estimate}&quot;, statistic = NULL)

Skip a model in a modelsummary table.

huangapple
  • 本文由 发表于 2023年6月15日 05:40:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定