【问题标题】:Why does "digits" argument not affect output of print(summary(fit))?为什么“digits”参数不会影响 print(summary(fit)) 的输出?
【发布时间】:2018-12-23 23:18:29
【问题描述】:

我从here 找到了解决方案。如果我运行 mtcars 示例,数字将按预期显示。
但是当我使用Avertising dataset 和以下脚本时,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

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

【问题讨论】:

  • 我尝试使用 digits=2、1 和 0 并且输出发生了变化。但实际上它并不会在每个数字上都发生变化。奇怪的行为。
  • 更多,当你看?summary.lm时,digits应该是有效位数,就像你使用signif()时一样,但在fat中它是小数位数,就像你使用@时一样987654329@
  • 我怀疑它默认打印的位数超过了指定的位数,因为您的 TVnewspaper 变量的影响非常小/小数点后包含零。如果您仅使用 radio 作为解释变量来估计模型,digits = 2 将按预期工作

标签: r lm summary


【解决方案1】:

在底层,这是调用printCoefMat 来打印系数矩阵很好digits 被传递给帮助状态的这个函数

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

注意“大多数数字”。

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

来自format的帮助

数字

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

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

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

【讨论】:

  • 好的,但是对于 mtcars 示例,我可以使用 print(summary(fit),digits=0),我希望 print(summary(advertising_fit), digits = 3) 可以工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 2014-05-16
  • 2022-11-06
  • 2015-11-26
  • 1970-01-01
  • 2016-04-05
相关资源
最近更新 更多