library(boot)
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
head(mydata)
mydata$rank <- factor(mydata$rank)
my.mod <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")
summary(my.mod)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.989979 1.139951 -3.500 0.000465 ***
gre 0.002264 0.001094 2.070 0.038465 *
gpa 0.804038 0.331819 2.423 0.015388 *
rank2 -0.675443 0.316490 -2.134 0.032829 *
rank3 -1.340204 0.345306 -3.881 0.000104 ***
rank4 -1.551464 0.417832 -3.713 0.000205 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# Set up the non-parametric bootstrap
logit.bootstrap <- function(data, indices) {
d <- data[indices, ]
fit <- glm(admit ~ gre + gpa + rank, data = d, family = "binomial")
return(coef(fit))
}
set.seed(12345) # seed for the RNG to ensure that you get exactly the same results as here
logit.boot <- boot(data=mydata, statistic=logit.bootstrap, R=10000) # 10'000 samples
logit.boot
Bootstrap Statistics :
original bias std. error
t1* -3.989979073 -7.217244e-02 1.165573039
t2* 0.002264426 4.054579e-05 0.001146039
t3* 0.804037549 1.440693e-02 0.354361032
t4* -0.675442928 -8.845389e-03 0.329099277
t5* -1.340203916 -1.977054e-02 0.359502576
t6* -1.551463677 -4.720579e-02 0.444998099
# Calculate confidence intervals (Bias corrected ="bca") for each coefficient
boot.ci(logit.boot, type="bca", index=1) # intercept
95% (-6.292, -1.738 )
boot.ci(logit.boot, type="bca", index=2) # gre
95% ( 0.0000, 0.0045 )
boot.ci(logit.boot, type="bca", index=3) # gpa
95% ( 0.1017, 1.4932 )
boot.ci(logit.boot, type="bca", index=4) # rank2
95% (-1.3170, -0.0369 )
boot.ci(logit.boot, type="bca", index=5) # rank3
95% (-2.040, -0.629 )
boot.ci(logit.boot, type="bca", index=6) # rank4
95% (-2.425, -0.698 )
library(boot)
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
head(mydata)
mydata$rank <- factor(mydata$rank)
my.mod <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")
summary(my.mod)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.989979 1.139951 -3.500 0.000465 ***
gre 0.002264 0.001094 2.070 0.038465 *
gpa 0.804038 0.331819 2.423 0.015388 *
rank2 -0.675443 0.316490 -2.134 0.032829 *
rank3 -1.340204 0.345306 -3.881 0.000104 ***
rank4 -1.551464 0.417832 -3.713 0.000205 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# Set up the non-parametric bootstrap
logit.bootstrap <- function(data, indices) {
d <- data[indices, ]
fit <- glm(admit ~ gre + gpa + rank, data = d, family = "binomial")
return(coef(fit))
}
set.seed(12345) # seed for the RNG to ensure that you get exactly the same results as here
logit.boot <- boot(data=mydata, statistic=logit.bootstrap, R=10000) # 10'000 samples
logit.boot
Bootstrap Statistics :
original bias std. error
t1* -3.989979073 -7.217244e-02 1.165573039
t2* 0.002264426 4.054579e-05 0.001146039
t3* 0.804037549 1.440693e-02 0.354361032
t4* -0.675442928 -8.845389e-03 0.329099277
t5* -1.340203916 -1.977054e-02 0.359502576
t6* -1.551463677 -4.720579e-02 0.444998099
# Calculate confidence intervals (Bias corrected ="bca") for each coefficient
boot.ci(logit.boot, type="bca", index=1) # intercept
95% (-6.292, -1.738 )
boot.ci(logit.boot, type="bca", index=2) # gre
95% ( 0.0000, 0.0045 )
boot.ci(logit.boot, type="bca", index=3) # gpa
95% ( 0.1017, 1.4932 )
boot.ci(logit.boot, type="bca", index=4) # rank2
95% (-1.3170, -0.0369 )
boot.ci(logit.boot, type="bca", index=5) # rank3
95% (-2.040, -0.629 )
boot.ci(logit.boot, type="bca", index=6) # rank4
95% (-2.425, -0.698 )