我正在為線性模型進行引導,但如何編輯截距和 x 變數的列印輸出名稱?
以下是模擬資料
set.seed(42)
n <- 100
x <- rnorm(n)
e <- rnorm(n)
y <- as.numeric(50 25*x e)
dd <- data.frame(id=1:n, x=x, y=y)
這是模型:
mo <- lm(y ~ x, data=dd)
查找擬合和殘差:
fit <- fitted(mo)
resi <- residuals(mo)
基于殘差自舉檢索置信區間的函式:
FUN <- function() {
X <- model.matrix(mo)
ressampy <- fit sample(resi, length(resi), replace = TRUE)
bootmod <- lm(ressampy ~ X-1)
confint(bootmod, level = 0.95)
}
1 次運行的輸出(注意列印輸出是X(Intercept)andXx但我只希望它們是(Intercept)and x)
FUN()
2.5 % 97.5 %
X(Intercept) 49.74439 50.07817
Xx 24.92904 25.25103
這可能是一個簡單的修復,但我無法讓它作業。任何幫助將不勝感激!
uj5u.com熱心網友回復:
只需用于rownames()更改包含置信區間的矩陣的行名稱,如下所示:
FUN <- function() {
X <- model.matrix(mo)
ressampy <- fit sample(resi, length(resi), replace = TRUE)
bootmod <- lm(ressampy ~ X-1)
ci <- confint(bootmod, level = 0.95)
rownames(ci) <- c("(Intercept)", "x")
return(ci)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/383770.html
上一篇:數列的R函式
