class: center, middle, inverse, title-slide .title[ # Regression ] .subtitle[ ## Part 2a ] .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> # Learning Goals 1. Skew, logs, and coefficients 2. Evaluating a regression: Univariate and multivariate visualization of errors 3. Next time: Root Mean Squared Error (RMSE) and cross Validation --- # Regression Recap -- - Regression very similar to **conditional means** <center><img src="figs/condmean.png" height=440px width=380px></center> --- # Regression Recap - Regression very similar to **conditional means** <center><img src="figs/condmean_reg.png" height=440px width=380px></center> --- # Regression Recap - For this class, don't need to know how it happens -- - But the intuition is obvious -- - Given `\(Y = \alpha + \beta X\)`, just tweak `\(\alpha\)` and `\(\beta\)` to reduce **errors** -- - Once you've minimized all the errors, you have the **line of best fit** --- # Visual Intuition <center><img src="./figs/regression-line.gif" width = 80%></center> --- # Evaluating Regression Results - Understanding the **errors** helps us evaluate the model -- - Define the errors `\(\varepsilon = Y - \hat{Y}\)` -- - True outcome values `\(Y\)` - Predicted outcome values `\(\hat{Y}\)` -- - Useful to assess model performance -- 1. **Look** with univariate and multivariate visualization of the errors 2. Calculate the **RMSE** --- # Introducing the .red[Data] -- - New dataset on **movies** -- - Download `mv.Rds` to your `data` folder and load to object `mv` - `require` `tidyverse`, and `plotly` packages ``` r require(tidyverse) mv <- read_rds('../data/mv.Rds') ``` --- # RQ: Hollywood Finances - .blue[Research Question]: What is the relationship between a movie's budget (how much it costs to make a movie) and a movie's earnings (how much money people pay to see the movie in theaters)? -- - .blue[Theory]: More money spent means more famous actors, better special effects, stronger marketing -- - .blue[Hypothesis]: earnings (`gross`) and costs (`budget`) should be **positively** correlated -- - `\(X\)`: ? - `\(Y\)`: ? --- ### Follow the process: Look -- - TONS of missingness! ``` r summary(mv %>% select(gross,budget)) ``` ``` ## gross budget ## Min. :7.140e+02 Min. : 5172 ## 1st Qu.:1.121e+07 1st Qu.: 16865322 ## Median :5.178e+07 Median : 37212044 ## Mean :1.402e+08 Mean : 57420173 ## 3rd Qu.:1.562e+08 3rd Qu.: 77844746 ## Max. :3.553e+09 Max. :387367903 ## NA's :3668 NA's :4482 ``` --- # Missingness - What does this mean for "generalizability" -- - "Generalizability": Do our conclusions from this data extend ("generalize") to the population at large? ``` r p <- mv %>% mutate(missing = ifelse(is.na(gross) | is.na(budget),1,0)) %>% group_by(year) %>% summarise(propMissing = mean(missing)) %>% # Calculate the proportion of observations missing either gross or budget ggplot(aes(x = year,y = propMissing)) + geom_bar(stat = 'identity') + labs(x = 'Year',y = '% Missing') + scale_y_continuous(labels = scales::percent) # Format the y-axis labels ``` --- # Missingness - We can only speak to post-2000s Hollywood! ``` r p ``` <img src="10a_RegressionPart2_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> --- # Follow the process: Look - What **type** of variables are earnings (`gross`) and costs (`budget`)? ``` r mv %>% drop_na(gross,budget) %>% select(gross,budget) %>% glimpse() ``` ``` ## Rows: 3,179 ## Columns: 2 ## $ gross <dbl> 73677478, 53278578, 723586629, 11490339, 62… ## $ budget <dbl> 93289619, 10883789, 160147179, 6996721, 139… ``` -- - Looks like continuous measures to me! --- # 2. Univariate Visualization ``` r mv %>% select(title,gross,budget) %>% gather(metric,dollars,-title) %>% ggplot(aes(x = dollars,color = metric)) + geom_density() ``` <img src="10a_RegressionPart2_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- # Log and Skew -- - Univariate visualization highlights significant **skew** in both measures -- - Most movies don't cost a lot and don't make a lot, but there are a few blockbusters that pull the density way out -- - Let's **wrangle** two new variables that take the log of these skewed measures -- - Logging transforms skewed measures to more "normal" measures - This is helpful for regression! ``` r mv <- mv %>% mutate(gross_log = log(gross), budget_log = log(budget)) ``` --- # 2. Univariate Visualization ``` r mv %>% select(title,gross_log,budget_log) %>% gather(metric,log_dollars,-title) %>% ggplot(aes(x = log_dollars,color = metric)) + geom_density() ``` <img src="10a_RegressionPart2_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> --- # 3. Conditional Analysis -- - Continuous X continuous variables? Scatter with `geom_point()`! ``` r mv %>% ggplot(aes(x = budget_log,y = gross_log)) + geom_point() ``` <img src="10a_RegressionPart2_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> --- # 3. Conditional Analysis - (BTW, I know I've been violating the tenets of data viz for several slides now. Let's fix that.) ``` r pSimple <- mv %>% drop_na(budget,gross) %>% mutate(profitable = ifelse(gross > budget,'Profitable','Unprofitable')) %>% ggplot(aes(x = budget,y = gross,text = paste0(title,' (',genre,', ',year,')'))) + geom_point() + scale_x_log10(labels = scales::dollar) + scale_y_log10(labels = scales::dollar) + labs(title = "Movie Costs and Returns", x = "Costs (logged budget)", y = "Returns (logged gross)") pFancy <- pSimple + geom_point(aes(color = profitable)) + scale_color_manual(guide = 'none',values = rev(c('red','black'))) + geom_abline(intercept = 0,slope = 1) ``` --- # 3. Conditional Analysis ``` r pFancy ``` <img src="10a_RegressionPart2_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> --- # Look with `plotly` to see outliers ``` r require(plotly) ggplotly(pFancy,tooltip = 'text') ```
--- # 4. Regression! ``` r pSimple + geom_smooth(aes(group = 1),method = 'lm',se = F) ``` <img src="10a_RegressionPart2_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> --- # 4. Regression! ``` r m <- lm(gross_log ~ budget_log,data = mv) summary(m) ``` ``` ## ## Call: ## lm(formula = gross_log ~ budget_log, data = mv) ## ## Residuals: ## Min 1Q Median 3Q Max ## -8.2672 -0.6354 0.1648 0.7899 8.5599 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 1.26107 0.30953 4.074 4.73e-05 *** ## budget_log 0.96386 0.01786 53.971 < 2e-16 *** ## --- ## Signif. codes: ## 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 1.281 on 3177 degrees of freedom ## (4494 observations deleted due to missingness) ## Multiple R-squared: 0.4783, Adjusted R-squared: 0.4782 ## F-statistic: 2913 on 1 and 3177 DF, p-value: < 2.2e-16 ``` --- # Interpretation - Remember the equation: `\(Y = \alpha + \beta * X\)` - Our `\(Y\)` is logged gross - Our `\(X\)` is logged budget - Thus we can rewrite as `\(gross\_log = \alpha + \beta * budget\_log\)` - What is `\(\alpha\)`? What is `\(\beta\)`? -- `\(gross\_log = 1.26 + 0.96 * budget\_log\)` --- # Interpreting with Logs -- - Previously, we said: - `\(\alpha\)` is the value of `\(Y\)` when `\(X\)` is zero - We need to convert back out of logged values using the `exp()` function - When `budget_log` is zero, the budget is `exp(0)` or $1 -- - Thus, we say: when the budget is $1, the movie makes 1.26 logged dollars, or... ``` r exp(1.26107) ``` ``` ## [1] 3.529196 ``` --- # Interpreting with Logs - For the `\(\beta\)` coefficient, it depends on where the logged variable appears: -- 1. `log(Y) ~ X`: 1 unit change in `\(X\)` → `(exp(b)-1)*100`% change in `\(Y\)` 2. `Y ~ log(X)`: 1% increase in `\(X\)` → `b/100` unit change in `\(Y\)` 3. `log(Y) ~ log(X)`: 1% increase in `\(X\)` → `b`% change in `\(Y\)` -- - In our example, a 1% increase in the budget corresponds to a 0.96% increase in gross - You will either need to memorize these rules, or (like me) just look them up every time