如何在modelsummary包中对指标进行四舍五入?

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

How can I round metrics in modelsummary package?

问题

I enjoy the option metrics="all" in modelsummary package that gives excellent summary statistics of instrument variable models (e.g. ivreg), namely the Wu-Hausman test.

data(mtcars)
library(ivreg)

iv_model <- ivreg(mpg ~ qsec + cyl + drat | disp | wt, data = mtcars)
summary(iv_model, diagnostics = TRUE)

library(modelsummary)
modelsummary(iv_model, metrics = "all")

[![enter image description here][1]][1]

How can I round the metrics? Regression coefficients are rounded to 3 digits whereas Wu Hausman is 13 digits.

英文:

I enjoy the option metrics="all" in modelsummary package that gives excellent summary statistics of instrument variable models (e.g. ivreg), namely the Wu-Hausman test.

data(mtcars)
library(ivreg)

iv_model <- ivreg(mpg ~ qsec + cyl + drat | disp | wt, data = mtcars)
summary(iv_model, diagnostics = TRUE)

library(modelsummary)
modelsummary(iv_model, metrics = "all")

如何在modelsummary包中对指标进行四舍五入?

How can I round the metrics? Regression coefficients are rounded to 3 digits whereas Wu Hausman is 13 digits.

答案1

得分: 4

你可以使用gof_map为非标准统计量添加映射,例如将两者都舍入到小数点后3位:

library(tibble)
gm <- tribble(
  ~raw,           ~clean,        ~fmt,
  "wu.hausman", "wu.hausman", 3,
  "wu.hausman.p", "wu.hausman.p", 3)
modelsummary(iv_model, metrics = "all", gof_map = gm)

这将格式化最后两行如下:

如何在modelsummary包中对指标进行四舍五入?

如果需要,您还可以更改gm的第二列以提供更漂亮的格式化名称。

英文:

You can add a mapping for non-standard statistics using gof_map e.g. to round both to 3 decimal places:

library(tibble)
gm &lt;- tribble(
  ~raw,        ~clean,      ~fmt,
  &quot;wu.hausman&quot;, &quot;wu.hausman&quot;, 3,
  &quot;wu.hausman.p&quot;, &quot;wu.hausman.p&quot;, 3)
modelsummary(iv_model, metrics = &quot;all&quot;, gof_map = gm)  

which formats the last two rows as:

如何在modelsummary包中对指标进行四舍五入?

You could also change the second column of gm to give nicer formatted names if you want.

huangapple
  • 本文由 发表于 2023年3月15日 19:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744308.html
匿名

发表评论

匿名网友

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

确定