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