Create a gist now

Instantly share code, notes, and snippets.

Annotated version of myboot
myboot<-function(data, stat, nreps, hist = TRUE) {
# Where 'data' is the original data set to be paste into 'myboot' program.
# 'stat' is the function that will generate the desired statistic such as "standard errors", "confidence intervals" etc.
# 'nreps' is the number of repetitions we want in the simulation.
estimates<-get(stat)(data)# Compute the number of estimates needed:
# Make a container object matrix for store the bootstrap results:
container<-matrix(NA, ncol = len , nrow = nreps)
# The length of "estimates" is the number of coefficients we will estimate standard errors for.
nobs<-nrow(data)#Compute the number of observations to resample
for(i in 1:nreps) {
# Now let's start our bootstrapping resampling loop
# Draw nobs uniform integers draws from 1 to nobs
# This will sample a random sample from the original data
# This apply the function 'stat' to the new data table and store it as a row in 'container' object
### 1.2 ### This will compute standard deviation for each column
if(hist==T) {
frame() # alias for
if(len<= 3) par(mfrow=c(len,1))
if((len> 3)&(len<= 6)) par(mfrow=c(3,2))
for(j in 1:len) hist(container[,j],
main=paste("Estimates for ", names(estimates)[j]), xlab="")
return(list(estimation=container, sds=sds))
###@ 2 @###
model<-lm(lw~s+iq, data=griliches76)
# This function generates a vector with the boostrap estimates.
#mod1<-function(griliches76)lm(lw~s+iq, data=griliches76)[[1]]
mod1.res<-myboot(griliches76, "mod", 10000, hist=T)
# This will run the bootstrap function on the estimates 10.000 times and plot a histogram for the found distribution.

Here's a remix of your code. The resampling is done with sample, and the function will show a progress bar. I have used the foreach package to make the computation parallelizable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment