######################
### conference #10 ###
######################
library("arm")
############
### 10.2 ###
############
Bypass <- read.table("bypass.data.txt", header=T)
str(Bypass)
### b
logit1 <- glm(new ~ I(age<80)
, family=binomial(link="logit"), data=Bypass)
display(logit1)
logit2 <- glm(new ~ I(age<80) + severity
, family=binomial(link="logit"), data=Bypass)
display(logit2, digits=3)
logit3 <- glm(new ~ I(age<80) + severity
, family=binomial(link="logit"), data=Bypass, subset=(age>=70 & age <=90))
display(logit3, digits=3)
logit4 <- glm(new ~ I(age<80) + severity
, family=binomial(link="logit"), data=Bypass, subset=(age>=75 & age <=85))
display(logit4, digits=3)
invlogit(coef(logit4)[1] + coef(logit4)[2] + coef(logit4)[3]*mean(Bypass$severity[Bypass$age>=75 & Bypass$age<80]))
invlogit(coef(logit4)[1] + coef(logit4)[3]*mean(Bypass$severity[Bypass$age>=80 & Bypass$age<=85]))
invlogit(coef(logit4)[1] + coef(logit4)[2] + coef(logit4)[3]*mean(Bypass$severity[Bypass$age>=75 & Bypass$age<80]))*(1-invlogit(coef(logit4)[1]>=75 & Bypass$age<80])))
invlogit(coef(logit4)[1] + coef(logit4)[3]*mean(Bypass$severity[Bypass$age>=80 & Bypass$age<=85]))*(1-invlogit(coef(logit4)[1] + coef(logit4)[3]*mean(Bypass$severity[Bypass$age>=80 & Bypass$age<=85])))
lm1 <- lm(stay ~ severity, data=Bypass)
display(lm1)
lm2 <- lm(stay ~ severity + age, data=Bypass)
display(lm2)
lm3 <- lm(stay ~ severity + age + I(age^2), data=Bypass)
display(lm3)
plot(stay ~ severity, data=Bypass, subset=new==0, ylim=c(15,45))
points(stay ~ severity, pch=3, data=Bypass, subset=new==1)
plot(stay ~ age, data=Bypass, subset=new==0, ylim=c(15,45), xlim=c(50,90))
points(stay ~ age, pch=3, data=Bypass, subset=new==1)
plot(severity ~ age, data=Bypass, subset=(new==1), xlim=range(Bypass$age), ylim=range(Bypass$severity))
points(severity ~ age, data=Bypass, subset=(new==0), pch=3)
### BONUS c ###
# without severity
Bypass$age80 <- Bypass$age - 80
TE1 <- lm(stay ~ new + age80 + age80:new, data=Bypass)
display(TE1)
# using the age threshold as a local IV
library("sem")
RDD.tsls <- tsls(stay ~ new + age80 , ~ I(age<80) + age80:I(age<80) + age80 , data=Bypass)
summary(RDD.tsls)
RDD.tsls2 <- tsls(stay ~ new + age80 + age80:new, ~ I(age<80) + age80:I(age<80) + age80 , data=Bypass)
summary(RDD.tsls2)
RDD.tsls3 <- tsls(stay ~ new + age80 , ~ I(age<80) + age80 , data=Bypass)
summary(RDD.tsls3a)
# example of a corresponding Wald estimate
RDD.itt <- lm(stay ~ I(age<80) + age80 + age80:I(age<80), data=Bypass)
display(RDD.itt)
RDD.comply <- lm(new ~ I(age<80) + age80 + age80:I(age<80), data=Bypass)
display(RDD.comply)
RDD.Wald <- coef(RDD.itt)[2]/coef(RDD.comply)[2]
RDD.Wald
# using severity
TE <- lm(stay ~ new + severity + age, data=Bypass)
display(TE)
############
### 10.3 ###
############
### a
n <- 1000
ni <- 500
h0 <- rnorm(n, mean=69.1, sd=2.9)
h1 <- h0 + rnorm(n, mean=2, sd=.5)
milk0 <- rbinom(n, size=1, prob=.3)
milk1 <- ifelse(milk0==1, 1, rbinom(n, size=1, prob=.5))
never <- milk0==0 & milk1==0
always <- milk0==1 & milk1==1
complier <- milk0==0 & milk1==1
sum(never)
sum(always)
sum(complier)
voucher <- rep(c(1,0), c(ni, n-ni))
milk <- ifelse(voucher, milk1, milk0)
h <- ifelse(milk, h1, h0)
ITT <- lm(h ~ voucher)
display(ITT)
comply <- lm(milk ~ voucher)
display(comply)
LATE.Wald <- coef(ITT)[2]/coef(comply)[2]
LATE.Wald
library("sem")
LATE.tsls <- tsls(h ~ milk, ~ voucher )
summary(LATE.tsls)
### b
n.b <- 1000
never.b <- rep(c(1, 0, 0), c(100, 100, 800))
always.b <- rep(c(0, 1, 0), c(100, 100, 800))
complier.b <- rep(c(0, 0, 1), c(100, 100, 800))
h0.b <- rnorm(n.b, mean=69.1, sd=2.9)
h1.b <- ifelse(complier.b, h0.b + rnorm(n.b, mean=2, sd=.5), h0.b + rnorm(n.b, mean=1, sd=.5))
voucher.b <- rbinom(n.b, 1, .5)
milk.b <- ifelse(never.b, 0, ifelse(always.b, 1, ifelse(voucher.b, 1, 0)))
h.b <- ifelse(milk.b, h1.b, h0.b)
ITT.b <- lm(h.b ~ voucher.b)
display(ITT.b)
comply.b <- lm(milk.b ~ voucher.b)
display(comply.b)
LATE.Wald.b <- coef(ITT.b)[2]/coef(comply.b)[2]
LATE.Wald.b
LATE.tsls.b <- tsls(h.b ~ milk.b, ~ voucher.b )
summary(LATE.tsls.b)
### c
n.c <- 1000
never.c <- rep(c(1, 0, 0), c(400, 400, 200))
always.c <- rep(c(0, 1, 0), c(400, 400, 200))
complier.c <- rep(c(0, 0, 1), c(400, 400, 200))
h0.c <- rnorm(n.c, mean=69.1, sd=2.9)
h1.c <- ifelse(complier.c, h0.c + rnorm(n.c, mean=2, sd=.5), h0.c + rnorm(n.c, mean=1, sd=.5))
voucher.c <- rbinom(n.c, 1, .5)
milk.c <- ifelse(never.c, 0, ifelse(always.c, 1, ifelse(voucher.c, 1, 0)))
h.c <- ifelse(milk.c, h1.c, h0.c)
ITT.c <- lm(h.c ~ voucher.c)
display(ITT.c)
comply.c <- lm(milk.c ~ voucher.c)
display(comply.c)
LATE.Wald.c <- coef(ITT.c)[2]/coef(comply.c)[2]
LATE.Wald.c
LATE.tsls.c <- tsls(h.c ~ milk.c, ~ voucher.c )
summary(LATE.tsls.c)
Friday, December 18, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment