首页 > 解决方案 > 为什么“digits”参数不影响 print(summary(fit)) 的输出?

问题描述

我从这里找到了解决方案。如果我运行该mtcars示例,则会按预期显示数字。
但是当我使用Avertising 数据集和以下脚本时,该digits参数没有任何效果:

path <- "path_to_adverising_csv/"
file <- "Advertising.csv"
filename <- paste0(path, file)
advertising <- read.csv(filename, header = TRUE)
names(advertising)

advertising_fit <- lm(sales~TV+radio+newspaper, data = advertising)
print(summary(advertising_fit), digits = 2)

输出:

Call:
lm(formula = sales ~ TV + radio + newspaper, data = advertising)

Residuals:
   Min     1Q Median     3Q    Max 
-8.828 -0.891  0.242  1.189  2.829 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  2.93889    0.31191    9.42   <2e-16 ***
TV           0.04576    0.00139   32.81   <2e-16 ***
radio        0.18853    0.00861   21.89   <2e-16 ***
newspaper   -0.00104    0.00587   -0.18     0.86    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.69 on 196 degrees of freedom
Multiple R-squared:  0.897, Adjusted R-squared:  0.896 
F-statistic:  570 on 3 and 196 DF,  p-value: <2e-16

我错过了一些明显的东西吗?

标签: rlmsummary

解决方案


在引擎盖下,这是调用很好地printCoefMat打印系数矩阵。被传递给帮助状态的这个函数digits

数字用于大多数数字的最小有效数字数。

注意“大多数数字”。

查看源代码,这最终将调用format一个向量,其中包含系数的舍入值绝对值及其标准误差,并传递相同的digits参数值。

从帮助format

数字

数字和复数 x 将使用多少位有效数字。默认值 NULL 使用 getOption("digits")。这是一个建议:将使用足够的小数位,以便最小(数量级)的数字具有这么多有效数字,并满足 nsmall。(复数的解释见signif。)见signif。)

因此,因为您有足够的小数点来表示最小的系数及其标准误差,以便有足够的有效数字。

在这种情况下,它是 的系数newspaper


推荐阅读