R “为什么?”;“数字”;参数不影响打印输出(摘要(适合))?

R “为什么?”;“数字”;参数不影响打印输出(摘要(适合))?,r,lm,summary,R,Lm,Summary,我从中找到了解决办法。如果我运行mtcars示例,数字将按预期显示。 但是当我使用和下面的脚本时,digits参数没有任何效果: path <- "path_to_adverising_csv/" file <- "Advertising.csv" filename <- paste0(path, file) advertising <- read.csv(filename, header = TRUE) names(advertising) advertising_f

我从中找到了解决办法。如果我运行
mtcars
示例,数字将按预期显示。
但是当我使用和下面的脚本时,
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)

path在引擎盖下,这是调用
printCoefMat
来很好地打印系数矩阵<代码>数字
传递到此函数,其中帮助说明

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

注意“大多数数字”

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

格式的帮助中

数字

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

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


在这种情况下,它是我尝试使用数字=2、1和0的
报纸的系数,输出会发生变化。但事实上,并不是每个数字都会改变。奇怪的行为。此外,当您查看
?summary.lm
时,数字应该是有效位数,就像您使用
signif()
时一样,但在fat中,它是小数位数,就像您使用
round()时一样
我怀疑它默认打印的位数大于指定的位数,因为
TV
paper
变量的影响太小/小数点后包含零。如果您仅使用无线电作为解释变量来估计模型,
digits=2
可以正常工作,但对于mtcars示例,我可以做
print(summary(fit),digits=0)
,我希望
print(summary(advertising_fit),digits=3)
正常工作?
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