Skip to content

Instantly share code, notes, and snippets.

@dmarcelinobr
Last active December 18, 2015 21:09
Show Gist options
  • Save dmarcelinobr/5845715 to your computer and use it in GitHub Desktop.
Save dmarcelinobr/5845715 to your computer and use it in GitHub Desktop.
myboot<-function(data, stat, nreps, hist = TRUE) {
estimates<-get(stat)(data)
len<-length(estimates)
container<-matrix(NA, ncol = len , nrow = nreps)
nobs<-nrow(data)
for(i in 1:nreps) {
posdraws<-ceiling(runif(nobs)*nobs)
resample<-data[posdraws,]
container[i,]<-get(stat)(resample)
}
sds<-apply(container,2,sd)
if(hist==T) {
mfrow=c(1,1)
frame()
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="")
}
print(rbind(estimates,sds))
return(list(estimation=container, sds=sds))
}
### 2 ###
mod<-function(griliches76)lm(lw~s+iq, data=griliches76)[[1]]
mod1.res<-myboot(griliches76, "mod", 10000, hist=T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment