Week two Quiz

Question 1.

Consider the following data with x as the predictor and y as as the outcome.

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)
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)
summary(lm(y~x))
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.27636 -0.18807  0.01364  0.16595  0.27143 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)   0.1885     0.2061   0.914    0.391  
## x             0.7224     0.3107   2.325    0.053 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.223 on 7 degrees of freedom
## Multiple R-squared:  0.4358, Adjusted R-squared:  0.3552 
## F-statistic: 5.408 on 1 and 7 DF,  p-value: 0.05296

Question 2.

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

    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)
    
    f<-lm(y~x)
    summary(f)$sigma
## [1] 0.2229981

Question 3.

In the mtcars 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?

 data("mtcars")
y<-mtcars$mpg
x<-mtcars$wt
#fit<-lm(y ~ x)
predict(lm(y ~ x),data.frame(x=mean(x)),interval = "confidence")
##        fit      lwr      upr
## 1 20.09062 18.99098 21.19027

Question 4.

Refer to the previous question. Read the help file for mtcars. What is the weight coefficient interpreted as?

?mtcars
## starting httpd help server ... done

The weight coefficient interpreted as the estimated expected change in mpg per 1,000 lb increase in weight.

Question 5.

Consider again the mtcars 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?

Since the distributions are normalized, the slope coefficient is equal to the correlation.

data("mtcars")
y<-mtcars$mpg
x<-mtcars$wt
#fit<-lm(y ~ x)
predict(lm(y ~ x),newdata = data.frame(x=3),interval = "prediction")
##        fit      lwr      upr
## 1 21.25171 14.92987 27.57355

Question 6.

Consider again the mtcars 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.

one short ton increase in weight is 2000lbs increase, to evaluate weight interms of a short ton we divide by 2

fit <- lm(mpg ~ wt, data = mtcars)
confint(fit)[2, ] * 2
##     2.5 %    97.5 % 
## -12.97262  -8.40527
## Or equivalently change the units
fit <- lm(mpg ~ I(wt * 0.5), data = mtcars)
confint(fit)[2, ]
##     2.5 %    97.5 % 
## -12.97262  -8.40527
##or
coeff<-summary(fit)$coefficients

Using \[Est + c(-1,1)\times qt(0.975,df) \times std error(Est)\] where

\[ \begin{aligned} Est&= coeff[2,1]&= -10.689\\ df&= fit\$df=30\\ std error(Est)&=coeff[2,2]&=1.118 \end{aligned} \]

 coeff[2,1] + c(-1,1)*qt(0.975, fit$df) *coeff[2,2]
## [1] -12.97262  -8.40527

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?

\[ X cm \times \frac{m}{100cm} = \frac{Xm}{100} ~~\mbox{and}~~ \beta_1 \frac{kg}{cm} \times\frac{100cm}{m} = \left(100\beta_1\right)\frac{kg}{m} \]

It would get multiplied by 100.

Question 8.

I have an outcome, \(Y\), and a predictor, \(X\) and fit a linear regression model with \(Y = \beta_0 + \beta_1 X + \epsilon\) to obtain \(\hat \beta_0\) and \(\hat \beta_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\)?

if \(Y = \beta_0 + \beta_1 X + \epsilon\) then \(Y = \beta_0 - c\beta_1 + \beta_1 (X + c) + \epsilon\) so that the answer is that the intercept gets subtracted by \(c\beta_1\)

The new intercept would be \(\hat \beta_0 - c \hat \beta_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, \(\sum_{i=1}^n (Y_i - \hat Y_i)^2\) when comparing a model with just an intercept (denominator) to the model with the intercept and slope (numerator)?

#method 1
fit1 <- lm(mpg ~ wt, data = mtcars)
fit2 <- lm(mpg ~ 1, data = mtcars)
1 - summary(fit1)$r.squared
## [1] 0.2471672
#method2
sse1 <- sum((predict(fit1) - mtcars$mpg)^2)
sse2 <- sum((predict(fit2) - mtcars$mpg)^2)
sse1/sse2
## [1] 0.2471672

Question 10.

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

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