I have previously written about bootstrap modeling with {purrr} and {modelr} here. What if you would like to do some simple bootstrap modeling without importing a library? This itself is easy too!
n <-2000df <- mtcarspred <-numeric(0)library(tictoc) # for timingtic()set.seed(123)for (i in1:n){ boot <-sample(nrow(df), n, replace =TRUE) fit <-lm(mpg ~ wt, data = df[boot,]) pred[i] <-predict(fit, newdata = df[boot,]) +sample(resid(fit), size =1)}toc()
6.8 sec elapsed
So we can see that the process ran pretty quickly and the loop itself is not a very difficult one. Let’s explain a little.
So the boot object is a sampling of df which in this case is the mtcars data set. We took a sample with replacement from this data set. We took 2000 samples and did this 2000 times.
Next we made the fit object by fitting a simple linear model to the data where mpg is a function of wt. Once this is done, we made out predictions.