在 R 中引导:预测
Bootstrapping in R: Predict
我是 运行 一个程序,我在其中执行 OLS 回归,然后从实际观察中减去系数以保留残差。
model1 = lm(data = final, obs ~ day + poly(temp,2) + prpn + school + lag1) # linear model
predfit = predict(model1, final) # predicted values
residuals = data.frame(final$obs - predfit) # obtain residuals
我想 bootstrap 我的模型,然后对 bootstrapped 系数做同样的事情。我尝试按以下方式执行此操作:
lboot <- lm.boot(model1, R = 1000)
predfit = predict(lboot, final)
residuals = data.frame(final$obs - predfit) # obtain residuals
然而,那是行不通的。我也试试:
boot_predict(model1, final, R = 1000, condense = T, comparison = "difference")
那也行不通。
我如何 bootstrap 我的模型然后基于它进行预测?
如果您尝试使用 bootstrap 拟合最佳 OLS,我会使用 caret
包。
library(caret)
#separate indep and dep variables
indepVars = final[,-final$obs]
depVar = final$obs
#train model
ols.train = train(indepVars, depVar, method='lm',
trControl = trainControl(method='boot', number=1000))
#make prediction and get residuals
ols.pred = predict(ols.train, indepVars)
residuals = ols.pred - final$obs
我是 运行 一个程序,我在其中执行 OLS 回归,然后从实际观察中减去系数以保留残差。
model1 = lm(data = final, obs ~ day + poly(temp,2) + prpn + school + lag1) # linear model
predfit = predict(model1, final) # predicted values
residuals = data.frame(final$obs - predfit) # obtain residuals
我想 bootstrap 我的模型,然后对 bootstrapped 系数做同样的事情。我尝试按以下方式执行此操作:
lboot <- lm.boot(model1, R = 1000)
predfit = predict(lboot, final)
residuals = data.frame(final$obs - predfit) # obtain residuals
然而,那是行不通的。我也试试:
boot_predict(model1, final, R = 1000, condense = T, comparison = "difference")
那也行不通。
我如何 bootstrap 我的模型然后基于它进行预测?
如果您尝试使用 bootstrap 拟合最佳 OLS,我会使用 caret
包。
library(caret)
#separate indep and dep variables
indepVars = final[,-final$obs]
depVar = final$obs
#train model
ols.train = train(indepVars, depVar, method='lm',
trControl = trainControl(method='boot', number=1000))
#make prediction and get residuals
ols.pred = predict(ols.train, indepVars)
residuals = ols.pred - final$obs