[英]Confidence intervals for Caret package
我正在使用 Caret package 做一些回歸分析。 我在網上看過,但現在已經找到了解決方案。 我的代碼如下。 有沒有辦法計算這個 model 的置信區間?
set.seed(123)
# Set up repeated k-fold cross-validation
train.control <- trainControl(method = "cv", number = 10,
predictionBounds = c(0, NA), verboseIter = TRUE)
# Train the model
step.model <- train(`likes` ~., data = reduced_Data,
method = "leapSeq",
tuneGrid = data.frame(nvmax = 1:13),
trControl = train.control)
step.model$results
step.model$bestTune
summary(step.model$finalModel)
coef(step.model$finalModel, 2)
我嘗試使用來自基礎 package 的預測 function,但沒有運氣
predict(step.model$finalModel, my_data, interval = "confidence")
您擁有的是一個 regsubset class,有來自不同術語子集的結果,您需要一個標准來選擇哪個是最好的 model,重新調整 model,並為您的示例做預測:
library(caret)
train.control <- trainControl(method = "cv",number=3)
step.model <- train(mpg ~., data = mtcars,
method = "leapSeq",trControl = train.control)
假設我們使用最大 r 平方的 model:
res = summary(step.model$finalModel)
bestm = which.max(res$adjr2)
terms_to_use = names(which(res$which[bestm,]))[-1]
terms_to_use
1] "cyl" "hp" "wt"
安裝最終的 model:
final_form = as.formula(paste("mpg ~",paste(terms_to_use,collapse="+")))
fit = lm(final_form,data=mtcars)
要獲得合適的 se:
predict(fit,se=TRUE)
要獲得 95 ci 的系數:
confint(fit)
2.5 % 97.5 %
(Intercept) 35.0915623 42.412012412
cyl -2.0701179 0.186884238
hp -0.0423655 0.006289293
wt -4.6839740 -1.649972191
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.