class: center, middle, inverse, title-slide # Lab 07: ## Model Selection & Diagnostics ### 03.05.20 --- ### Agenda 1. Mid-semester survey 2. Lab Introduction --- ### Mid-semester Survey Please take a few minutes to fill out a short mid-semester survey: https://duke.qualtrics.com/jfe/form/SV_bQlQ0Z1VhOhZOD3 Your responses are anonymous. --- ## Lab introduction - <font class="vocab">Goal: </font> Better understand model selection and diagnostics and how to do these procedures in R - **Part I**: This lab will focus on model selection in the `regsubsets` and `step` functions using Adj. `\(R^2\)`, BIC, and AIC as selection criteria - **Part II**: Model diagnostics (leverage, standardized residuals and VIF) - <font class="vocab">Tips: </font> - Use the [Model Selection & Diagnostics notes](https://www2.stat.duke.edu/courses/Spring20/sta210.001/slides/lec-slides/12-model-selection.html) to help you complete the lab. Some of the primary functions you'll use are on the next two slides. --- ### `step` function (AIC) ```r null_model <- lm(Y ~ 1, data = my_data) full_model <- lm(Y ~ ., data = my_data) ``` - Forward selection ```r regfit_forward <- step(null_model, scope = formula(full_model), direction = "forward") ``` - Backward selection ```r regfit_backward <- step(full_model, direction = "backward") ``` --- ### `regsubsets` function (BIC, Adj. `\(R^2\)`) ```r library(leaps) ``` - Forward selection ```r regfit_forward <- regsubsets(Y ~ ., data = my_data, method="forward") ``` - Backward selection ```r regfit_backward <- regsubsets(Y ~ ., data = my_data, method="backward") ``` - Select a model ```r sel_summary <- summary(regfit_forward) coef(regfit_backward, which.max(sel_summary$adjr2)) # Adj R-sq coef(regfit_backward, which.min(sel_summary$bic)) # BIC ``` --- ### Project - If you finish the lab early, you should use the remaining time to work on the project proposal - Details about the proposal may be found in the [project instructions](https://www2.stat.duke.edu/courses/Spring20/sta210.001/project/project.html) - Please let your TA know if you have any questions about the proposal or finding data