[R] printCoefmat() and zap.ind

Shu Fai Cheung @hu|@|@cheung @end|ng |rom gm@||@com
Thu Jul 6 11:14:27 CEST 2023


Hi All,

I would like to ask two questions about printCoefmat().

First, I found a behavior of printCoefmat() that looks strange to me,
but I am not sure whether this is an intended behavior:

``` r
set.seed(5689417)
n <- 10000
x1 <- rnorm(n)
x2 <- rnorm(n)
y <- .5 * x1 + .6 * x2 + rnorm(n, -0.0002366, .2)
dat <- data.frame(x1, x2, y)
out <- lm(y ~ x1 + x2, dat)
out_summary <- summary(out)
printCoefmat(out_summary$coefficients)
#>               Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 1.7228e-08 1.9908e-03    0.00        1
#> x1          5.0212e-01 1.9715e-03  254.70   <2e-16 ***
#> x2          6.0016e-01 1.9924e-03  301.23   <2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
printCoefmat(out_summary$coefficients,
             zap.ind = 1,
             digits = 4)
#>             Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 0.000000   0.001991     0.0        1
#> x1          0.502100   0.001971   254.7   <2e-16 ***
#> x2          0.600200   0.001992   301.2   <2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
```

With zap.ind = 1, the values in "Estimate" were correctly
zapped using digits = 4. However, by default, "Estimate"
and "Std. Error" are formatted together. Because the
standard errors are small, with digits = 4, zero's were added
to values in "Estimate", resulting in "0.502100" and
"0.600200", which are misleading because, if rounded to
the 6th decimal place, the values to be displayed should
be "0.502122" and "0.600162".

Is this behavior of printCoefmat() intended/normal?

Second, how can I use zap without this behavior?
In cases like the one above, I need to use zap such that
the intercept will not be displayed in scientific notation.
Disabling scientific notation cannot achieve the desired
goal.

I tried adding cs.ind = 1:

```r
printCoefmat(out_summary$coefficients,
             zap.ind = 1,
             digits = 4,
             cs.ind = 1)
#>             Estimate Std. Error t value Pr(>|t|)
#> (Intercept)   0.0000   0.001991     0.0        1
#> x1            0.5021   0.001971   254.7   <2e-16 ***
#> x2            0.6002   0.001992   301.2   <2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
```

However, this solution is not ideal because the numbers
of decimal places of "Estimate" and "Std. Error" are
different. How can I get the output like this one?

```r
#>             Estimate Std. Error t value Pr(>|t|)
#> (Intercept)   0.0000   0.0020     0.0        1
#> x1            0.5021   0.0020   254.7   <2e-16 ***
#> x2            0.6002   0.0020   301.2   <2e-16 ***
```

Thanks for your attention.

Regards,
Shu Fai Cheung



More information about the R-help mailing list