Search code examples
rstatistics-bootstrap

Bootstrapping in R: Predict


I am running a program where I conduct an OLS regression and then I subtract the coefficients from the actual observations to keep the residuals.

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

I want to bootstrap my model and then do the same with the bootstrapped coefficients. I try doing this the following way:

lboot <- lm.boot(model1, R = 1000)
predfit = predict(lboot, final)

residuals = data.frame(final$obs - predfit) # obtain residuals

However, that does not work. I also try:

boot_predict(model1, final,  R = 1000, condense = T, comparison = "difference")

and that also does not work.

How can I bootstrap my model and then predict based of that?


Solution

  • If you're trying to fit the best OLS using bootstrap, I'd use the caret package.

    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