In a small-scale regression study, we collected data on the number of children in a family Xi and the number of hours per week spent shopping Yi. The following data were obtained:
i | 1 | 2 | 3 | 4 | 5 | 6 |
Xi | 2 | 6 | 3 | 1 | 1 | 9 |
Yi | 13 | 17 | 12 | 12 | 9 | 22 |
Assume we performed a simple linear regression of Yi on Xi, i.e. E(Yi) = ?0 + ?1Xi
(a) By hand compute X?X, X?Y, (X?X)-1, b, Y^(means Y-hat), and e. What are the dimensions of each matrix?
(b) Create a simple linear regression in R(R-program) and write down the fitted equation and the value of MSE. Interpret the coefficient b1.
(c) Use the MSE value to compute (Var(b))^(means Var(b) with a hat over it). From this matrix find estimates for Var(b0), Var(b1), and Cov(b0, b1).
(d) Construct a 95% confidence interval for ?1. Use the matrix formula from Chapter 6. Interpret this interval.
(e) Construct a 95% confidence interval for E(Yh) and a 95% prediction interval for Yh(new) at Xh = 4.
(f) In R reproduce parts (a), (c), (d), and (e). When recreating the results from parts (d) and (e), compute the confidence intervals and prediction intervals two ways:
•Using matrix notation
•Using either the confint or predict functions
Also compute the hat matrix H. What is the dimension of H? Also use H to compute Y and compare to the result in part (a).
Template for R parts of problem below.
# Type in children (x) and hours (y) as vectors. Set n to be the length
children <-
hours <-
n <-
##############################
########## Part (b) ##########
##############################
# Create a simple linear regression Y ~ X
fit <-
# Circle the value used to find MSE
summary(fit)
MSE <- # Type a number or use sigma() function
##############################
########## Part (f) ##########
##############################
# Create X and Y matrices.
ones <-
X <-
X # Displays matrix X
Y <-
Y # Displays matrix Y
# Display the dimensions of each of the X and Y matrices. See Chapter 5 Notes
# Calculate X'X, X'Y, (X'X)^(-1), and b.
XX <-
XY <-
XXinv <-
b <-
Yhat <-
e <-
# Display matrices (don't change)
XX
XY
XXinv
b
Yhat
e
# Calculate the variance-covariance matrix Var(b) two different ways
# 1: using MSE and XXinv
# 2: using the vcov function
Var.b1 <- # Method 1
Var.b1 # Displays matrix
Var.b2 <- # Method 2
Var.b2 # Displays matrix
# Construct a 95% confidence interval for beta1 two different ways
# 1: using matrix notation
# 2: using the confint function
beta1.lower <- # Method 1 lower
beta1.upper <- # Method 1 upper
c(beta1.lower, beta1.upper) # Displays interval (don't change)
beta1.int <- # Method 2
beta1.int # Displays interval
# Construct a 95% confidence interval for E(Y) when children=4 two different ways
# 1: using matrix notation
# 2: using the predict function
Xh <- # Enter vector Xh
Yh <- # Compute vector Yh using matrices
EY.lower <- # Method 1 lower
EY.upper <- # Method 1 upper
c(EY.lower, EY.upper) # Displays interval (don't change)
EY.int <- # Method 2
EY.int # Displays interval
# Construct a 95% prediction interval for Y when children=4 two different ways
# 1: using matrix notation
# 2: using the predict function
Y.lower <- # Method 1 lower
Y.upper <- # Method 1 upper
c(Y.lower, Y.upper) # Displays interval (don't change)
Y.int <- # Method 2
EY.int # Displays interval
# Compute the Hat matrix, H and Yhat. Find the dimension of H
H <-
H # Displays matrix
Yhat <-
Yhat
x'x is (2x2), x'y is (2x1) and (x'x)-1 is 2x2 matrix
y | b | yhat | e | sse | sigma2 | varb | |
---|---|---|---|---|---|---|---|
13 | 9.0714286 | 11.850649 | 1.1493506 | 7.7077922 | 2.5692641 | 1.1011132 | -0.183519 |
17 | 1.3896104 | 17.409091 | -0.409091 | -0.183519 | 0.0500506 | ||
12 | 13.24026 | -1.24026 | |||||
12 | 10.461039 | 1.538961 | |||||
9 | 10.461039 | -1.461039 | |||||
22 | 21.577922 | 0.4220779 |
regression equation y=b0+b1*x=9.0714+1.3896x
SE(b1)=sqrt(0.05)= 0.2236
95% confidence interval for b1=1.3896t(0.05/2,errror df=4)*SE(b1)=1.38962.7764*0.2236=1.38960.6208=
(0.7688, 2.0104)
Get Answers For Free
Most questions answered within 1 hours.