Created
December 7, 2016 16:16
-
-
Save jtleek/e968aef6937248db02003e12004dafde to your computer and use it in GitHub Desktop.
bootstrapping residuals - the dumb way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f1 = formula(log(cost) ~ date) | |
lm1 = lm(f1,data=nuclear) | |
stat = lm1$coeff[2] | |
resid = lm1$residuals | |
fit = lm1$fitted.values | |
n = dim(nuclear)[1] | |
B = 500 | |
stat0 = rep(0,B) | |
set.seed(1262016) | |
for(i in 1:B){ | |
resid0 = resid[sample(1:n,replace=T)] | |
nuclear$y0 = fit + resid0 | |
lm0 = lm(y0 ~ date,data=nuclear) | |
stat0[i] = lm0$coeff[2] | |
} | |
hist(stat0) | |
abline(v=stat,col="blue") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works (I think?) and is very hacky, but feels like you should just use
replicate()
. I'm not sure it makes sense to use purrr or base equivalents unless you want to hold on to the bootstrap data and/or fits for downstream work.I think if you want to hold on to all that intermediate stuff, a pipe-y purrr workflow is handy.