Directions: Complete the following exercises using the code discussed during computer lab. Save your work in an R script as well as a Word document containing the necessary output and comments. Be sure to use notes in the script to justify any computations. If you have any questions, do not hesitate to ask.
1 Simple Linear Regression
- Load the data set pressure from the datasets package in R. Perform a Simple Linear Regression on the two variables. Provide the regression equation, coefficients table, and anova table. Summarize your findings. What is the relationship between the t statistic for temperature and the F statistic in the ANOVA table?
- Refer to the previous exercise. Check the assumptions on the regression model and report your results. Be sure to include the scatterplot with regression equation, normal QQ plot, and residual plot. Explain what you see.
- Refer to exercise 1. Experiment with different transformations of the data to improve the model. What is the best transformation?
2 Multiple Linear Regression
- Load the swiss data set from the ‘datasets’ package in R. Find the correlation matrix and print the pairwise scatterplots. What variables seem to be related?
- Run a Multiple Regression on Fertility using all of the other variables as predictors. Print the model and coefficients table. Explain the meaning of the significant coefficients.
- Check the assumptions using the diagnostic tests mentioned in this section. Discuss your findings.
- Run a stepwise selection method to reduce the dimension of the model using the backward direction. Print the new model and new coefficients table. Check the assumptions and discuss any changes.
- Use Mallow’s Cp to determine the best model. Does your choice match the model in the previous exercise?
3 Principal Component Analysis
- Load the longley data set from the R datasets package. This data set was used to predict a countries GNP based on several variables. Find the correlation matrix of the explanatory variables.
- Refer to the previous exercise. Perform a principal component analysis on the explanatory variables using the correlation matrix. Use a scree plot to determine the optimal number of components and report them. Try to explain the meaning behind each component.
- Refer to the previous exercise. What proportion of variation does each component explain? What is the total cumulative variance explained by the optimal number of components?
Day 2 Lab Activities - Solutions解答
Simple Linear Regression
1. > pressure.lm <- lm(pressure ~ temperature, data = pressure)
The temperature coefficient is positive so if there is a significant relationship between temperature and pressure, it is a direct relationship. Since the p-value is less than 0.05, temperature is indeed significant in the model. The relationship between the t statistic and the F statistic is t^2 = F.
2. Linearity: The scatterplot shows a clear violation of the linearity assumption. The data appears to be exponentially increasing. The standardized residual plot reinforces this observation.
Equal Variance: The lack of a linear relationship makes it difficult to determine the equality of variance in observations.
Normality: The Normal Quantile plot shows a lack of linearity at the tails of the data set. A Shapiro-Wilk test verifies that the residuals do not follow a normal distribution.
3. Using a Box Cox transformation, the optimal transformation is either
or
where λ = 0.01
Multiple Linear Regression
1.
2.
All of the predictors are significant except examination.
3. Residual Plot: There is a random pattern in the residual plot which causes no concern with the model fit.
Normal Q-Q Plot: The data follows the diagonal line quite nicely, indicating that the residuals probably satisfy the normality assumption.
Scale - Location: The data is randomly scattered which indicates that the homoscedasticity assumption is probably met.
4.
The new model does not include the examination variable. Now all of the predictors are significant.
Residual Plot: There is a random pattern in the residual plot which causes no concern with the model fit.
Normal Q-Q Plot: The data no longer seems to follow a precise normal distribution. This assumption may now be violated.
Scale - Location: The data is randomly scattered which indicates that the homoscedasticity assumption is probably met.
5. The two models that best fit Mallow's Cp are the model with all 5 variables or the model with the 4 variables Agriculture, Education, Catholic, and Infant.Mortality. We prefer a simpler model in statistics, so the best model choice is the model with four explanatory variables. This is the exact same model that backward selection had identified.
Principal Component Analysis
The first component is a standardized measure of GNP and the second component is difficult to interpret.
3.