[R] Bootstrapped CIs of MSE for (G)AM model

varin sacha v@rin@@ch@ @ending from y@hoo@fr
Thu Nov 22 21:36:18 CET 2018


Dear R-experts,

I am trying to get the bootstrapped confidence intervals of Mean squared error (MSE) for a (G)AM model. I get an error message.
Here below the reproducible R code. Many thanks for your response.

####################


install.packages("ISLR")

library(ISLR)

install.packages("mgcv")

library(mgcv)

install.packages("boot")

library(boot)

#MSE calculation

n=dim(Wage)[1]

p=0.667

GAM1<-gam(wage ~education+s(age,bs="ps")+year,data=Wage)

sam=sample(1 :n,floor(p*n),replace=FALSE)

 

Training =Wage [sam,]

Testing = Wage [-sam,]

 

ypred=predict(GAM1,newdata=Testing)

y=Testing$wage

MSE = mean((y-ypred)^2)


# Bootstrap 95% CI for MSE

# function to obtain MSE
MSE <- function(formula, data, indices) {
  d <- data[indices,] # allows boot to select sample 
  fit <- gam(formula, data=d)
  return(MSE(fit)) 

} # bootstrapping with 1000 replications 
results <- boot(data=Wage, statistic=MSE, 
   R=1000, formula=gam(wage ~education+s(age,bs="ps")+year,data=Wage)

)
# get 95% confidence intervals 
boot.ci(results, type="bca")

##########################



More information about the R-help mailing list