# # Sýnidæmi í bootstrap, tekið úr quick-boot og endurbætt. # # Bootstrap 95% CI for R-Squared library(boot) # function to obtain R-Squared from the data rsq <- function(formula, data, indices) { d <- data[indices,] # allows boot to select sample fit <- lm(formula, data=d) return(summary(fit)$r.square) } # bootstrapping with 1000 replications results <- boot(data=mtcars, statistic=rsq, R=1000, formula=mpg~wt+disp) # view results results plot(results) # get 95% confidence interval boot.ci(results,type="bca") # # Ber saman við CI með Fisher-transformation # nn=nrow(mtcars) r0=(summary(lm(mpg~wt+disp,data=mtcars))$r.square)^0.5 tanh(atanh(r0)-1.96*1/sqrt(nn-3))^2 tanh(atanh(r0)+1.96*1/sqrt(nn-3))^2 # # Regression-dæmi # bs <- function(formula, data, indices) { d <- data[indices,] # allows boot to select sample fit <- lm(formula, data=d) return(coef(fit)) } # bootstrapping with 1000 replications results <- boot(data=mtcars, statistic=bs, R=1000, formula=mpg~wt+disp) # view results results plot(results, index=1) # intercept plot(results, index=2) # wt plot(results, index=3) # disp # get 95% confidence intervals boot.ci(results, type="bca", index=1) # intercept boot.ci(results, type="bca", index=2) # wt boot.ci(results, type="bca", index=3) # disp # # Ber saman við venjulega regression # summary(lm(mpg~wt+disp,data=mtcars))