有没有办法格式化系数估计值。值不是很小。我正在使用 LM 功能
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 ***
Run Code Online (Sandbox Code Playgroud)
我喜欢specify_decimal我多次从这个问题借用的功能。
我specify_decimal在下面的函数中使用来创建一个“更好”的输出,指定我喜欢的位数。请注意,这只是美化输出,因为它将数字转换为字符(我假设您希望将其用于演示目的,否则您将只使用科学记数法)。
职能:
#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
}
Run Code Online (Sandbox Code Playgroud)
lm 文档中的示例:
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
Run Code Online (Sandbox Code Playgroud)