Amrita Sawant
Amrita Sawant

Reputation: 10913

R format regression output

Is there a way to format the Coefficient Estimates. The values are not very small. I am using LM function

                   Estimate Std. Error  t value Pr(>|t|) 

  (Intercept)      -1.041e+01  8.259e-02 -126.015  < 2e-16 ***
   QUANTITY         3.929e-01  1.684e-02   23.324  < 2e-16 ***
   WEIGHT           1.125e-04  3.650e-05    3.082  0.00206 ** 
   PACKAGES         5.702e+00  1.135e-01   50.228  < 2e-16 ***
   DEPT_COUNT       1.188e+01  8.834e-02  134.475  < 2e-16 ***
   PROD_CNT         1.062e+00  2.708e-02   39.220  < 2e-16 ***

Upvotes: 1

Views: 2710

Answers (2)

LyzandeR
LyzandeR

Reputation: 37889

I like the specify_decimal function that I have many times borrowed from this question.

I am using specify_decimal in my below function to create a 'nicer' output specifying the number of digits I like. NOTICE that this is only beautifying the output as it turns numbers into characters (I assume you want this for presentation purposes otherwise you would just use the scientific notation).

Functions:

#specify_decimal
specify_decimal <- function(x, k) format(round(x, k), nsmall=k)

#beautifying summary.lm
new_summary  <- function(lmcoef, digits) {

    coefs <- as.data.frame(lmcoef)
    coefs[] <- lapply(coefs, function(x) specify_decimal(x, digits))
    coefs

}

Example from lm documentation:

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)

#first argument is the summary(lmobject)$coefficients
#second argument is the number of digits
> new_summary(summary(lm.D9)$coefficients, 5)
            Estimate Std. Error  t value Pr(>|t|)
(Intercept)  5.03200    0.22022 22.85012  0.00000
groupTrt    -0.37100    0.31143 -1.19126  0.24902

Upvotes: 5

dacuna
dacuna

Reputation: 1096

I don't know if I understand you correctly but if I am I think you could use the digits option:

options(digits=4)

On digits option definition: "digits: controls the number of digits to print when printing numeric values.".

Upvotes: 0

Related Questions