class: center, middle, inverse, title-slide .title[ # Regression ] .subtitle[ ## Part 1b ] .author[ ### Prof. Weldzius ] .institute[ ### Villanova University ] --- <style type="text/css"> .small .remark-code { /*Change made here*/ font-size: 85% !important; } .tiny .remark-code { /*Change made here*/ font-size: 50% !important; } </style> # Agenda 1. Introducing the Data 2. Demonstrating Regressions --- # Regression -- - Let's demonstrate what we learned last time with the `sc_debt` data ``` r require(tidyverse) debt <- read_rds('../data/sc_debt.Rds') glimpse(debt) ``` ``` ## Rows: 2,546 ## Columns: 16 ## $ unitid <int> 100654, 100663, 100690, 100706, 100… ## $ instnm <chr> "Alabama A & M University", "Univer… ## $ stabbr <chr> "AL", "AL", "AL", "AL", "AL", "AL",… ## $ grad_debt_mdn <int> 33375, 22500, 27334, 21607, 32000, … ## $ control <chr> "Public", "Public", "Private", "Pub… ## $ region <chr> "Southeast", "Southeast", "Southeas… ## $ preddeg <chr> "Bachelor's", "Bachelor's", "Associ… ## $ openadmp <int> 2, 2, 1, 2, 2, 2, 1, NA, 2, 2, 2, 1… ## $ adm_rate <dbl> 0.9175, 0.7366, NA, 0.8257, 0.9690,… ## $ ccbasic <int> 18, 15, 20, 16, 19, 15, 2, 22, 18, … ## $ sat_avg <int> 939, 1234, NA, 1319, 946, 1261, NA,… ## $ md_earn_wne_p6 <int> 25200, 35100, 30700, 36200, 22600, … ## $ ugds <int> 5271, 13328, 365, 7785, 3750, 31900… ## $ costt4_a <int> 23053, 24495, 14800, 23917, 21866, … ## $ selective <dbl> 0, 0, NA, 0, 0, 0, NA, NA, 0, 0, 0,… ## $ research_u <dbl> 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,… ``` --- # Research Camp - .blue[Research Question]: What is the relationship between SAT scores and median future earnings? -- - .blue[Theory]: Students with higher SAT scores work harder and have learned more. Employers reward these attributes with higher wages in the private market. -- - .blue[Hypothesis]: The relationship between SAT scores and future earnings should be positive. -- - **NB**: Important caveats to this simplistic theory! -- - Socioeconomic status: predicts both higher SAT scores and higher wages -- - **Correlation `\(\neq\)` Causation** --- # Set Up - Linking .blue[Theory] to .red[Data] -- - Our SAT scores are theorized to explain future earnings -- - Thus the SAT scores are the independent / explanatory / predictor variable `\(X\)` - And earnings are the dependent / outcome variable `\(Y\)` --- # Regression - There is a simple recipe to follow -- - And it is exactly how the syllabus for the class is designed! -- 1. Look at your data to identify missingness (**Wrangling**) 2. **Univariate** visualization of your variables 3. **Multivariate** visualization of your variables 4. **Regression** (today) 5. Evaluation of **errors** (next lecture) --- # Step 1: Look - Why worry about **missingness**? -- 1. **Substantive:** external validity 2. **Technical:** cross validation won't work! -- ``` r summary(debt %>% select(sat_avg,md_earn_wne_p6)) ``` ``` ## sat_avg md_earn_wne_p6 ## Min. : 737 Min. : 10600 ## 1st Qu.:1053 1st Qu.: 26100 ## Median :1119 Median : 31500 ## Mean :1141 Mean : 33028 ## 3rd Qu.:1205 3rd Qu.: 37400 ## Max. :1557 Max. :120400 ## NA's :1317 NA's :240 ``` --- # Step 2: Univariate Viz - Why visualize both `\(Y\)` and `\(X\)`? -- 1. **Substantive:** *See* which units you are talking about 2. **Technical:** Adjust for *skew* --- # Step 2: Univariate Viz - Why visualize both `\(Y\)` and `\(X\)`? ``` r debt %>% ggplot(aes(x = sat_avg)) + geom_histogram() ``` <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> --- # Step 2: Univariate Viz - Why visualize both `\(Y\)` and `\(X\)`? ``` r debt %>% ggplot(aes(x = md_earn_wne_p6)) + geom_histogram() ``` <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> --- # Step 3: Multivariate - Eyeball the relationship first! -- ``` r debt %>% ggplot(aes(x = sat_avg,y = md_earn_wne_p6)) + geom_point() ``` <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> --- # Step 3: Multivariate Viz - Adding regression line ``` r debt %>% ggplot(aes(x = sat_avg,y = md_earn_wne_p6)) + geom_point() + geom_smooth(method = 'lm',se = F) ``` <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- # Step 3: Multivariate Viz - Let's focus on two schools ``` r toplot <- debt %>% mutate(hl = ifelse(unitid %in% c(100654,179265),'hl','none')) # Choosing two examples p2 <- toplot %>% ggplot(aes(x = sat_avg, y = md_earn_wne_p6,color = hl,group = 1,alpha = hl)) + geom_point(data = toplot %>% filter(hl == 'none')) + geom_point(data = toplot %>% filter(hl == 'hl'),size =3) + scale_alpha_manual(values = c(1,.3)) + scale_color_manual(values = c('red','black')) + geom_smooth(method = 'lm',se = F) + theme(legend.position = 'none') + labs(title = "Graduate Earnings and SAT Scores", subtitle = "By School", x = "Average SAT Score", y = "Median Earnings of Grads") ``` --- # Step 3: Multivariate Viz - Adding regression line ``` r p2 ``` <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> --- # Step 3: Multivariate Viz - Defining `\(\varepsilon\)` ``` r p3 <- toplot %>% ggplot(aes(x = sat_avg, y = md_earn_wne_p6,color = hl,group = 1,alpha = hl)) + geom_point(data = toplot %>% filter(hl == 'none')) + geom_point(data = toplot %>% filter(hl == 'hl'),size =3) + scale_alpha_manual(values = c(1,.3)) + scale_color_manual(values = c('red','black')) + geom_smooth(method = 'lm',se = F) + annotate(geom = 'segment', x = toplot %>% filter(hl == 'hl') %>% .$sat_avg, y = toplot %>% filter(hl == 'hl') %>% .$md_earn_wne_p6, xend = toplot %>% filter(hl == 'hl') %>% .$sat_avg, yend = c(27500,41000),color = 'red',lwd = 1.2) + theme(legend.position = 'none') + labs(title = "Graduate Earnings and SAT Scores", subtitle = "By School", x = "Average SAT Score", y = "Median Earnings of Grads") ``` --- # Step 3: Multivariate Viz - Measuring errors ``` r p3 ``` <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- # The Data Scientist's Trade-off -- - Those mistakes seem pretty big! -- - Why not use a curvier line? --- # The Data Scientist's Trade-off - Those mistakes seem pretty big! - Why not use a curvier line? <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> --- # The Data Scientist's Trade-off - Those mistakes seem pretty big! - Why not use a curvier line? <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> --- # The Data Scientist's Trade-off - Those mistakes seem pretty big! - Why not use a curvier line? <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" /> --- # The Data Scientist's Trade-off - Those mistakes seem pretty big! - Why not use a curvier line? <img src="9b_RegressionPart1_files/figure-html/unnamed-chunk-17-1.png" style="display: block; margin: auto;" /> --- # The Data Scientist's Trade-off -- - Want to **reduce complexity** -- - But also want to be **accurate** -- - What is the right answer? -- - It depends on your .blue[theory] and the .red[data] -- - It is context-dependent -- - And this is still only using *linear regression models*! -- - This is a deep area of study, for those interested --- # Step 4: Regression - Introducing the `lm(formula,data)` function -- - Two inputs to care about: -- - `formula`: Code for `\(Y = \alpha + \beta X\)` - `data`: What is the data we are using? - `formula` is written as `Y ~ X` -- - `R` will calculate `\(\alpha\)` and `\(\beta\)` for us - Just need to tell it what is `\(Y\)` (`md_earn_wne_p6`) and `\(X\)` (`sat_avg`) - The tilde (`~`) is `R`'s version of the equals sign -- - Save the model to an object ``` r model_earn_sat <- lm(formula = md_earn_wne_p6 ~ sat_avg,data = debt) ``` --- # Step 4: Interpretation - What is in this object? -- - The regression results! Look at them with `summary()` ``` r summary(model_earn_sat) ``` ``` ## ## Call: ## lm(formula = md_earn_wne_p6 ~ sat_avg, data = debt) ## ## Residuals: ## Min 1Q Median 3Q Max ## -23239 -4311 -852 2893 78695 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -12053.87 1939.80 -6.214 7.12e-10 *** ## sat_avg 42.60 1.69 25.203 < 2e-16 *** ## --- ## Signif. codes: ## 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 7594 on 1196 degrees of freedom ## (1348 observations deleted due to missingness) ## Multiple R-squared: 0.3469, Adjusted R-squared: 0.3463 ## F-statistic: 635.2 on 1 and 1196 DF, p-value: < 2.2e-16 ``` --- # Step 4: Interpretation - Starting with the first column called `Estimate` -- - 1st Row `(Intercept)` is `\(\alpha\)`: the predicted value of `\(Y\)` when `\(X\)` is zero -- - Schools with average SAT scores of 0 produce graduates who earn -$12,053.87 - Sensible? -- - 2nd Row `sat_avg` is the `\(\beta\)`: the increase in `\(Y\)` when `\(X\)` increases by one -- - For each unit increase in the average SAT score, recent graduates earn $42.60 more - Sensible? --- # Step 4: Interpretation - Other 3 columns? -- - `Std. Error` is the "standard error" - `t value` is the "t-statistic" - `Pr(>|t|)` is the "p-value" -- - t-statistic = Estimate / standard error - p-value = function(t-statistic) -- - Only really need to remember the p-value for this course - This is 1 minus confidence - The lower the p-value, the more confident we are that the `Estimate` is not zero --- # Step 4: Interpretation ``` r summary(model_earn_sat) ``` ``` ## ## Call: ## lm(formula = md_earn_wne_p6 ~ sat_avg, data = debt) ## ## Residuals: ## Min 1Q Median 3Q Max ## -23239 -4311 -852 2893 78695 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -12053.87 1939.80 -6.214 7.12e-10 *** ## sat_avg 42.60 1.69 25.203 < 2e-16 *** ## --- ## Signif. codes: ## 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 7594 on 1196 degrees of freedom ## (1348 observations deleted due to missingness) ## Multiple R-squared: 0.3469, Adjusted R-squared: 0.3463 ## F-statistic: 635.2 on 1 and 1196 DF, p-value: < 2.2e-16 ``` --- # Another Example - We will come back to the RMSE next class -- - For now, let's try with a different research question! -- - .blue[What is the relationship between admissions and future earnings?] -- - .blue[Theory]: More selective schools are more prestigious - .blue[Hypothesis]: There should be a negative relationship between the admissions rate and future earnings <!-- --- --> <!-- # Do It Together! --> <!-- 1. Look at the data and acknowledge missingness --> <!-- 2. Univariate visualization of `\(X\)` and `\(Y\)` --> <!-- 3. Multivariate visualization of `\(X\)` and `\(Y\)` --> <!-- 4. Regression -->