please show the R-code thanks. 2. For question 2, you will use a subset of the m
ID: 3305633 • Letter: P
Question
please show the R-code
thanks.
2. For question 2, you will use a subset of the mtcars data. (Run "?mtcars" for detailed information about the data.) Run the following R codes and use the mtcars2 dataset to answer the questions (a)-(e). Please include your R codes and output for all the following questions R codes: set.seed (100) sub-index sample (nrow (mtcars) ,20, replace-FALSE) mtcars2-mtcars [sub index,] Now, the mtcars2 dataset contains 11 attributes for 20 automobiles. Our interest is to construct a multiple linear regression model that predicts the fuel consumption (Y = mpg) from number of cylinders (X) = cyl) and horsepower (X2-hp) (a) Fit the regression model: Yi = 0 + ylxil + hpXi2 + ei-What percent of the variation in fuel consumption is explained by your regression model? (1 pt) (b) Construct a 90% confidence interval for Ayl. (1 pt) 0 at = 0.05. (Make a conclusion (c) Test Ho : hp-0 vs Ho : hp based on the p-value.) (1 pt) (d) Fit another regression model: Y Bohp+i using the hp (horse- power) predictor only. Test H0 : hp-0 vs Ho : 1p 0 at = 0.05. (Make a conclusion based on the p-value.) (1 pt) (e) Were your conclusions from (c) and (d) consistent? If not, how can the contradictory results be explained? (1 pt)Explanation / Answer
The r snippet is as follows
data(mtcars)
mtcars
set.seed(100)
sub_index = sample(nrow(mtcars),20,replace = FALSE)
mtcars2 = mtcars[sub_index,]
## fit the regression model
fit <- lm(mpg~cyl+hp,data=mtcars2)
summary(fit)
b) confidence interval
# upper limit
-2.845+ 1.64*0.6522
# lower limit
-2.845- 1.64*0.6522
c) from the regression output we see that the p value for hp is 0.5681 , which is not less than
alpha = 0.05 , hence we fail to reject the null hypothesis and conclude that Beta hp = 0
## hp predictor only
fit1 <- lm(mpg~hp,data=mtcars2)
summary(fit1)
this time we see that the p value for hp is less than 0.05 , hence we reject the null hypothesis in favor of alternate hypothesis
and conclude that Beta HP is not equal to zero
The results of the models are
summary(fit)
Call:
lm(formula = mpg ~ cyl + hp, data = mtcars2)
Residuals:
Min 1Q Median 3Q Max
-5.1775 -1.8221 0.3529 0.9954 6.8980
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 39.009142 2.541303 15.350 2.15e-11 ***
cyl -2.845021 0.652236 -4.362 0.000424 ***
hp -0.009647 0.016575 -0.582 0.568181
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.048 on 17 degrees of freedom
Multiple R-squared: 0.8019, Adjusted R-squared: 0.7786
F-statistic: 34.41 on 2 and 17 DF, p-value: 1.056e-06
with hp only
summary(fit1)
Call:
lm(formula = mpg ~ hp, data = mtcars2)
Residuals:
Min 1Q Median 3Q Max
-5.677 -2.506 -1.202 1.828 8.258
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.08342 2.13199 14.110 3.57e-11 ***
hp -0.06832 0.01370 -4.988 9.54e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 4.313 on 18 degrees of freedom
Multiple R-squared: 0.5802, Adjusted R-squared: 0.5569
F-statistic: 24.88 on 1 and 18 DF, p-value: 9.539e-05
The results are different for both the regression models , when we fit the model using both hp and cycl then the variation in the cyl variable takes precedence to calculate the variation in y , the r square in the first case is 80% ,which means that the model is able to explain 80% variation of the data
on the contrary , hp though signifcant explains only 58% variation in the data
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.