class: center, middle, inverse, title-slide # Modelling nonlinear relationships ### Dr. Çetinkaya-Rundel ### 2018-02-26 --- ## Exam debrief - Mean: 86.23, SD: 17.7 - Median: 91, IQR: 14 - Solutions will be posted tonight (I'll email), without the explanation of methods - Visit office hours with any questions - For regrade requests, fill out the regrade request form, do not directly reach out to the TA who graded your work - Pay attention to organization and styling of your code - Use tidyverse syntax --- ## Announcements - Office hours this week: + Tuesday 1-3pm + Thursday 10-11:30 (as usual) - HW 03 posted tonight, due next Monday --- ## Follow along RStudio Cloud -> More Modelling Paris Paintings (make your own copy) --- class: center, middle # Tidy regression output --- Let's revisit the model predicting heights of paintings from their widths: ```r pp <- read_csv("data/paris_paintings.csv", na = c("n/a", "", "NA")) ``` ```r m_ht_wt <- lm(Height_in ~ Width_in, data = pp) ``` --- ## Not-so-tidy regression output - You might come accross these in your googling adventures, but we'll try to stay away from them - Not because they are wrong, but because they don't result in tidy data frames as results. --- ## Not-so-tidy regression output (1) Option 1: ```r m_ht_wt ``` ``` ## ## Call: ## lm(formula = Height_in ~ Width_in, data = pp) ## ## Coefficients: ## (Intercept) Width_in ## 3.6214 0.7808 ``` --- ## Not-so-tidy regression output (2) Option 2: ```r summary(m_ht_wt) ``` ``` ## ## Call: ## lm(formula = Height_in ~ Width_in, data = pp) ## ## Residuals: ## Min 1Q Median 3Q Max ## -86.714 -4.384 -2.422 3.169 85.084 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 3.621406 0.253860 14.27 <2e-16 ## Width_in 0.780796 0.009505 82.15 <2e-16 ## ## Residual standard error: 8.304 on 3133 degrees of freedom ## (258 observations deleted due to missingness) ## Multiple R-squared: 0.6829, Adjusted R-squared: 0.6828 ## F-statistic: 6749 on 1 and 3133 DF, p-value: < 2.2e-16 ``` --- ## Review .question[ What makes a data frame tidy? ] -- 1. Each variable forms a column. 2. Each observation forms a row. 3. Each type of observational unit forms a table. --- ## Tidy regression output Achieved with functions from the broom package: - `tidy`: Constructs a data frame that summarizes the model's statistical findings: coefficient estimates, *standard errors, test statistics, p-values*. - `augment`: Adds columns to the original data that was modeled. This includes predictions and residuals. - `glance`: Constructs a concise one-row summary of the model. This typically contains values such as `\(R^2\)`, adjusted `\(R^2\)`, *and residual standard error that are computed once for the entire model*. --- ## Tidy your model's statistical findings ```r tidy(m_ht_wt) ``` ``` ## term estimate std.error statistic p.value ## 1 (Intercept) 3.6214055 0.253860346 14.26535 8.822046e-45 ## 2 Width_in 0.7807964 0.009504522 82.14999 0.000000e+00 ``` ```r tidy(m_ht_wt) %>% select(term, estimate) %>% mutate(estimate = round(estimate, 3)) ``` ``` ## term estimate ## 1 (Intercept) 3.621 ## 2 Width_in 0.781 ``` --- ## Augment data with model results New variables of note (for now): - `.fitted`: Predicted value of the response variable - `.resid`: Residuals .small[ ```r augment(m_ht_wt) %>% slice(1:5) ``` ``` ## # A tibble: 5 x 10 ## .rownames Height_in Width_in .fitted .se.fit .resid .hat .sigma ## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 1 37.0 29.5 26.7 0.166 10.3 0.000399 8.30 ## 2 2 18.0 14.0 14.6 0.165 3.45 0.000396 8.31 ## 3 3 13.0 16.0 16.1 0.158 - 3.11 0.000361 8.31 ## 4 4 14.0 18.0 17.7 0.152 - 3.68 0.000337 8.31 ## 5 5 14.0 18.0 17.7 0.152 - 3.68 0.000337 8.31 ## # ... with 2 more variables: .cooksd <dbl>, .std.resid <dbl> ``` ] -- .question[ Why might we be interested in these new variables? ] --- ## Residuals plot .small[ ```r m_ht_wt_aug <- augment(m_ht_wt) ggplot(m_ht_wt_aug, mapping = aes(x = .fitted, y = .resid)) + geom_point(alpha = 0.5) + geom_hline(yintercept = 0, color = "gray", lty = 2) + labs(x = "Predicted height", y = "Residuals") ``` <!-- --> ] -- .question[ What does this plot tell us about the fit of the linear model? ] --- ## Glance to assess model fit ```r glance(m_ht_wt) ``` ``` ## r.squared adj.r.squared sigma statistic p.value df logLik AIC ## 1 0.6829468 0.6828456 8.30427 6748.621 0 2 -11083.45 22172.89 ## BIC deviance df.residual ## 1 22191.04 216054.5 3133 ``` -- ```r glance(m_ht_wt)$r.squared ``` ``` ## [1] 0.6829468 ``` -- ``` The $R^2$ is 68.29%. ``` The `\(R^2\)` is 68.29%. -- ```r glance(m_ht_wt)$adj.r.squared ``` ``` ## [1] 0.6828456 ``` --- class: center, middle # Exploring linearity --- ## Data: Paris Paintings <!-- --> --- ## Price vs. width .question[ Describe the relationship between price and width of painting. ] ``` ## Warning: Removed 256 rows containing missing values (geom_point). ``` <!-- --> --- ## Let's focus on paintings with `Width_in < 100` ```r pp_wt_lt_100 <- pp %>% filter(Width_in < 100) ``` --- ## Price vs. width .question[ Which plot shows a more linear relationship? ] .small[ .pull-left[ <!-- --> ] .pull-right[ <!-- --> ] ] --- ## Price vs. width, residuals .question[ Which plot shows a residuals that are uncorrelated with predicted values from the model? ] .small[ .pull-left[ <!-- --> ] .pull-right[ <!-- --> ] ] -- .question[ What's the unit of residuals? ] --- ## Transforming the data - We saw that `price` has a right-skewed distribution, and the relationship between price and width of painting is non-linear. -- - In these situations a transformation applied to the response variable may be useful. -- - In order to decide which transformation to use, we should examine the distribution of the response variable. -- - The extremely right skewed distribution suggests that a log transformation may be useful. - log = natural log, `\(ln\)` - Default base of the `log` function in R is the natural log: <br> `log(x, base = exp(1))` --- ## Logged price vs. width .question[ How do we interpret the slope of this model? ] <!-- --> --- ## Interpreting models with log transformation ```r m_lprice_wt <- lm(log(price) ~ Width_in, data = pp_wt_lt_100) m_lprice_wt %>% tidy() %>% select(term, estimate) %>% mutate(estimate = round(estimate, 3)) ``` ``` ## term estimate ## 1 (Intercept) 4.669 ## 2 Width_in 0.019 ``` --- ## Linear model with log transformation $$ \widehat{log(price)} = 4.67 + 0.02 Width $$ -- - For each additional inch the painting is wider, the log price of the painting is expected to be higher, on average, by 0.02 livres. -- - which is not a very useful statement... --- ## Working with logs - Subtraction and logs: `\(log(a) − log(b) = log(a / b)\)` -- - Natural logarithm: `\(e^{log(x)} = x\)` -- - We can use these identities to "undo" the log transformation --- ## Interpreting models with log transformation The slope coefficient for the log transformed model is 0.02, meaning the log price difference between paintings whose widths are one inch apart is predicted to be 0.02 log livres. -- $$ log(\text{price for width x+1}) - log(\text{price for width x}) = 0.02 $$ -- $$ log\left(\frac{\text{price for width x+1}}{\text{price for width x}}\right) = 0.02 $$ -- $$ e^{log\left(\frac{\text{price for width x+1}}{\text{price for width x}}\right)} = e^{0.02} $$ -- $$ \frac{\text{price for width x+1}}{\text{price for width x}} \approx 1.02 $$ -- For each additional inch the painting is wider, the price of the painting is expected to be higher, on average, by a factor of 1.02. --- ## Shortcuts in R ```r m_lprice_wt %>% tidy() %>% select(term, estimate) %>% mutate(estimate = round(estimate, 3)) ``` ``` ## term estimate ## 1 (Intercept) 4.669 ## 2 Width_in 0.019 ``` ```r m_lprice_wt %>% tidy() %>% select(term, estimate) %>% mutate(estimate = round(exp(estimate), 3)) ``` ``` ## term estimate ## 1 (Intercept) 106.540 ## 2 Width_in 1.019 ``` --- ## Recap - Non-constant variance is one of the most common model violations, however it is usually fixable by transforming the response (y) variable. -- - The most common transformation when the response variable is right skewed is the log transform: `\(log(y)\)`, especially useful when the response variable is (extremely) right skewed. -- - This transformation is also useful for variance stabilization. -- - When using a log transformation on the response variable the interpretation of the slope changes: *"For each unit increase in x, y is expected on average to be higher/lower <br> by a factor of `\(e^{b_1}\)`."* -- - Another useful transformation is the square root: `\(\sqrt{y}\)`, especially useful when the response variable is counts. --- ## Transform, or learn more? - Data transformations may also be useful when the relationship is non-linear - However in those cases a polynomial regression may be more appropriate + This is beyond the scope of this course, but you’re welcomed to try it for your final project, and I’d be happy to provide further guidance --- ## Aside: when `\(y = 0\)` In some cases the value of the response variable might be 0, and ```r log(0) ``` ``` ## [1] -Inf ``` -- The trick is to add a very small number to the value of the response variable for these cases so that the `log` function can still be applied: ```r log(0 + 0.00001) ``` ``` ## [1] -11.51293 ``` --- class: center, middle # The linear model with multiple predictors --- ## Price, surface area, and living artist .question[ What is the typical surface area for paintings? ] <!-- --> -- Less than 1000 square inches (which is roughly a painting that is 31in x 31in). There are very few paintings that have surface area above 5000 square inches. --- ## Price, surface area, and living artist For simplicity let's focus on the paintings with `Surface < 5000`: <!-- --> --- ## Price vs. surface and living artist .question[ Does the relationship between surface and logged price vary by whether or not the artist is living? ] .small[ ```r ggplot(data = pp_Surf_lt_5000, mapping = aes(y = log(price), x = Surface, color = factor(artistliving))) + geom_point(alpha = 0.3) + geom_smooth(method = "lm", se = FALSE) + labs(color = "Living artist") ``` <!-- --> ] --- ## Modelling with main effects ```r m_main <- lm(log(price) ~ Surface + factor(artistliving), data = pp_Surf_lt_5000) m_main %>% tidy() %>% select(term, estimate) %>% mutate(estimate = round(estimate, 5)) ``` ``` ## term estimate ## 1 (Intercept) 4.88008 ## 2 Surface 0.00027 ## 3 factor(artistliving)1 0.13721 ``` -- Linear model: $$ \widehat{log(price)} = 4.88 + 0.00027~surface + 0.14~artistliving $$ -- - Plug in 0 for `artistliving` to get the linear model for paintings by non-living artists. - Plug in 1 for `artistliving` to get the linear model for paintings by living artists. --- ## Interpretation of main effects .pull-left[ <!-- --> ] .pull-right[ - Non-living artist: `\(\widehat{log(price)} = 4.88 + 0.00027~surface + 0.14 \times 0\)` `\(= 4.88 + 0.00027~surface\)` - Living artist: `\(\widehat{log(price)} = 4.88 + 0.00027~surface + 0.14 \times 1\)` `\(= 5.02 + 0.00027~surface\)` ] - Rate of change in price as the surface area of the painting increases does not vary between paintings by living and non-living artists (same slope), - Paintings by living artists are consistently more expensive than paintings by non-living artists (different intercept).