I found the solution from here. If I run the mtcars
example, digits are displayed as expected.
But when I use the Avertising dataset and the follwing script, the digits
argument does not have any effect:
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)
Output:
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
Do I miss something obvious?
Under the hood, this is calling printCoefMat
to print the coefficient matrix nicely. digits
is passed to this function where the help states
digits minimum number of significant digits to be used for most numbers.
Note 'most numbers'.
Looking at the source, this will eventually call format
on a vector containing the rounded values absolute value of coefficients and their standard errors with the passing same digits
argument value.
From the help for format
digits
how many significant digits are to be used for numeric and complex x. The default, NULL, uses getOption("digits"). This is a suggestion: enough decimal places will be used so that the smallest (in magnitude) number has this many significant digits, and also to satisfy nsmall. (For the interpretation for complex numbers see signif.) see signif.)
Therefore, as you have enough decimal points for the smallest of the coefficients and their standard errors to have sufficient significant digits.
In this case it is the coefficient for newspaper
.