Question - 1

Consider the following data with x as the predictor and y as as the outcome. Give a P-value for the two sided hypothesis test of whether Ξ²1 from a linear regression model is 0 or not.

x <- c(0.61, 0.93, 0.83, 0.35, 0.54, 0.16, 0.91, 0.62, 0.62)
y <- c(0.67, 0.84, 0.6, 0.18, 0.85, 0.47, 1.1, 0.65, 0.36)

Answer:

fit<-lm(y~x)
summary(fit)$coefficients[2,4]
## [1] 0.05296439

Question - 2

Consider the previous problem, give the estimate of the residual standard deviation.

Answer:

summary(fit)$sigma
## [1] 0.2229981

Question - 3

In the πš–πšπšŒπšŠπš›πšœ data set, fit a linear regression model of weight (predictor) on mpg (outcome). Get a 95% confidence interval for the expected mpg at the average weight. What is the lower endpoint?

Answer:

data("mtcars")
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
fit<-lm(mpg~wt,data = mtcars)
newx<-data.frame(wt=mean(mtcars$wt))
p1<-data.frame(predict(fit,newdata=newx,interval = ("confidence")))
p1[2]
##        lwr
## 1 18.99098

Question - 4

Refer to the previous question. Read the help file for πš–πšπšŒπšŠπš›πšœ. What is the weight coefficient interpreted as?

Answer:

The estimated expected change in mpg per 1,000 lb increase in weight.

Question - 5

Consider again the πš–πšπšŒπšŠπš›πšœ data set and a linear regression model with mpg as predicted by weight (1,000 lbs). A new car is coming weighing 3000 pounds. Construct a 95% prediction interval for its mpg. What is the upper endpoint?

Answer:

fit<-lm(mpg~wt,data = mtcars)
newx<-data.frame(wt=3.0)
p1<-data.frame(predict(fit,newdata=newx,interval = ("prediction")))
p1[3]
##        upr
## 1 27.57355

Question - 6

Consider again the πš–πšπšŒπšŠπš›πšœ data set and a linear regression model with mpg as predicted by weight (in 1,000 lbs). A β€œshort” ton is defined as 2,000 lbs. Construct a 95% confidence interval for the expected change in mpg per 1 short ton increase in weight. Give the lower endpoint.

Answer:

mtcars$wt_st<-mtcars$wt/2
fit<-lm(mpg~wt_st,data = mtcars)
sumCoef<-summary(fit)$coefficients
c_int<-sumCoef[2,1]+c(-1,1)*qt(0.975,df=fit$df)*sumCoef[2,2]
c_int[1]
## [1] -12.97262

Question - 7

If my X from a linear regression is measured in centimeters and I convert it to meters what would happen to the slope coefficient?

Answer:

Let’s see it by doing an example

# X in centimeters
x <- c(0.61, 0.93, 0.83, 0.35, 0.54, 0.16, 0.91, 0.62, 0.62)
y <- c(0.67, 0.84, 0.6, 0.18, 0.85, 0.47, 1.1, 0.65, 0.36)
coef(lm(y~x))
## (Intercept)           x 
##   0.1884572   0.7224211
# X in meters
x <- x/100
y <- c(0.67, 0.84, 0.6, 0.18, 0.85, 0.47, 1.1, 0.65, 0.36)
coef(lm(y~x))
## (Intercept)           x 
##   0.1884572  72.2421080

Above example clearly shows that the slope would get multiplied by 100.

Question - 8

I have an outcome, Y, and a predictor, X and fit a linear regression model with Y=Ξ²0+Ξ²1X+Ο΅ to obtain Ξ²^0 and Ξ²^1. What would be the consequence to the subsequent slope and intercept if I were to refit the model with a new regressor, X+c for some constant, c?

Answer:

The new intercept would be Ξ²0βˆ’cΞ²1

Question - 9

Refer back to the mtcars data set with mpg as an outcome and weight (wt) as the predictor. About what is the ratio of the the sum of the squared errors, βˆ‘ni=1(Yiβˆ’Y^i)2 when comparing a model with just an intercept (denominator) to the model with the intercept and slope (numerator)?

Answer:

# This is a model with intercept and slope
fit1<-lm(mpg~wt,data = mtcars)
e1<-sum(fit1$residuals^2)
# This is a model with just an intercept
fit2<-lm(mpg~1,data=mtcars)
e2<-sum(fit2$residuals^2)
# Ratio
e1/e2
## [1] 0.2471672

Question - 10

Do the residuals always have to sum to 0 in linear regression?

Answer:

If an intercept is included, then they will sum to 0.