我喜歡specify_decimal
函數,我有很多次從this借來的問題。
我在下面的函數中使用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
}
從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
格式化成什麼?我不明白你在問什麼。請給出一個具有預期結果的可重複的例子。你可能只想要'圓(coef(model))' –