Created
March 4, 2015 02:22
-
-
Save tengpeng/ee570348442b06ed2ef5 to your computer and use it in GitHub Desktop.
JK
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
pairedBootstrap <- function(yourData, responseName, numberBoot){ | |
#####CREATE USEFUL VARIABLES FOR THE BOOTSTRAP METHOD | |
originalSampleSize <- dim(yourData)[1] #the data sample size | |
numberExplanatory <- dim(yourData)[2]-1 #the number of explanatory variables | |
#explanatory variable names | |
explanatoryNames <- setdiff(names(yourData),responseName) | |
#explanatory variable formula (for the lm function) | |
explanatoryFormula <- explanatoryNames[1] | |
for(i in 2:length(explanatoryNames)){ | |
explanatoryFormula <- paste(explanatoryFormula, "+", explanatoryNames[i]) | |
} | |
explanatoryFormula <- paste(as.character(responseName), "~", explanatoryFormula) | |
#store coefficient estimates for each bootstrap sample in this matrix | |
bootstrapCoefficients <- matrix(0, nrow=numberBoot, ncol=numberExplanatory+1) | |
######IMPLEMENT THE RESAMPLING PROCEDURE | |
for(i in 1:numberBoot){ | |
#Create a vector of indices, which will make up our new sample | |
samp <- sample(1:originalSampleSize, size=originalSampleSize, replace=TRUE) | |
#save the new sample into a data frame called newSamp | |
newSamp <- yourData[samp,] | |
#run a linear regression to obtain bootstrap coef. estimates | |
newReg <- lm(as.formula(explanatoryFormula), data=newSamp) | |
bootstrapCoefficients[i, ] <- coef(newReg) | |
} | |
return(bootstrapCoefficients) | |
} | |
library(Stat2Data) | |
data(Perch) | |
head(Perch) | |
pairs(~Weight+Length+Width,Perch) | |
m=lm(Weight~Length+Width+Length:Width,Perch) | |
par(mfrow=c(2,2)) | |
plot(m) | |
dataPerch=Perch | |
dataPerch=dataPerch[,-1] | |
dataPerch$LW=dataPerch$Length*dataPerch$Width | |
head(dataPerch) | |
Boot= pairedBootstrap(dataPerch,"Weight",2000) | |
hist(Boot[,2],breaks=20) | |
quantile(Boot[,2],probs=c(0.025,0.975)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment