Вы находитесь на странице: 1из 3

# Pset

4
Mohamed
11/1/2017
Problem 1b.
X1 = runif(200,min=0,max=200)
X2 = (10 * X1) + rnorm(200,mean = 0, sd = 1)
Y1 = (X1 * 7) + 19

# In order to get the X2 Added Variable Plot, we must regress X2 on X1 and
take those residuals.
X22 = lm(X2~X1)\$resid
Y2 = lm(Y1~X1)\$resid

AVP1 = lm(Y2 ~ X22)

# Partial Residual Plot
PP = lm(Y1 ~ X1 + X2)
resprp = PP\$resid
#summary(PP)
rj = resprp + PP\$coefficients[3] * X2
regprp = lm(rj ~ X2)

# As we defined X1, Y1, and Y2, their variance do not change. We must carry
out a loop.
Avpval=NULL
Prpval=NULL
for (i in 1:10000){
X2 = (10 * X1) + rnorm(200,mean = 0, sd = 1)
X22 = lm(X2~X1)\$resid
AVP2 = lm(Y2 ~ X22)
PP = lm(Y1 ~ X1 + X2)
resprp = PP\$resid
rj = resprp + PP\$coefficients[3] * X2
regprp = lm(rj ~ X2)
Avpval[i] = summary(AVP2)\$coefficients[2,4]
Prpval[i] = summary(regprp)\$coefficients[2,4]
}

par(mfrow=c(1,2))
hist(Prpval);hist(Avpval)

par(mfrow=c(1,1))

Problem 4a
## 95% Bonferroni confidence intervals

Tr1 <- c(3,5,7)
Tr2 <- c(6,8)
Tr3 <- c(8,10)
Tr4 <- c(8,10,12)
xbar <- numeric(4)
xbar[1] <- mean(Tr1)
xbar[2] <- mean(Tr2)
xbar[3] <- mean(Tr3)
xbar[4] <- mean(Tr4)

X <- matrix(0,10,4)
X[,1] <- c(1,1,1,1,1,1,1,1,1,1)
X[,2] <- c(0,0,0,1,1,0,0,0,0,0)
X[,3] <- c(0,0,0,0,0,1,1,0,0,0)
X[,4] <- c(0,0,0,0,0,0,0,1,1,1)
Y <- c(3,5,7,6,8,8,10,8,10,12)
fit <- X%*%solve(t(X)%*%X)%*%t(X)%*%Y
Sum <- sum((Y-fit)^2)/9

n <- 10
p <- 4
M1M2LB <- (xbar[1]-xbar[2])-qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M1M2UB <- (xbar[1]-xbar[2])+qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M1M3LB <- (xbar[1]-xbar[3])-qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M1M3UB <- (xbar[1]-xbar[3])+qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M1M4LB <- (xbar[1]-xbar[4])-qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M1M4UB <- (xbar[1]-xbar[4])+qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M2M3LB <- (xbar[2]-xbar[3])-qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M2M3UB <- (xbar[2]-xbar[3])+qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M2M4LB <- (xbar[2]-xbar[4])-qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M2M4UB <- (xbar[2]-xbar[4])+qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M3M4LB <- (xbar[3]-xbar[4])-qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)
M3M4UB <- (xbar[3]-xbar[4])+qt(0.05/(2*6),n-p,lower.tail=F)*sqrt(Sum/n)

c(M1M2LB,M1M2UB)

c(M1M3LB,M1M3UB)

c(M1M4LB,M1M4UB)

c(M2M3LB,M2M3UB)

c(M2M4LB,M2M4UB)

c(M3M4LB,M3M4UB)