class: center, middle, inverse, title-slide .title[ # Data Science for Linguists ] .subtitle[ ## Getting the most out of your model ] .author[ ### Joseph V. Casillas, PhD ] .date[ ### Rutgers UniversitySpring 2023Last update: 2023-04-02 ] --- class: inverse, middle background-image: url(https://www.jvcasillas.com/media/rstats/memes/lm_interactions.png) background-size: 700px --- class: title-slide-section-grey, middle # Fine-tuning and understanding your models --- layout: true # Fine-tuning and understanding your models --- ### Overview ### 1. Interactions ### 2. Coding - categorical variables - Dummy coding - Sum coding - ~~Deviation coding~~ ### 3. Transformations - continuous variables - Centering - Standardizing - ~~Log transformation~~ --- layout: false class: title-slide-section-red, middle # Interactions --- layout: true # Interactions --- ### What is an interaction? .Large[ .content-box-blue[ When the effect of one of the predictor on the criterion changes at different levels/values of one of the other predictors. ] - Interactions are possible between continuous variables, continuous *and* discontinuous variables, as well as *only* discontinuous variables. - It is often the case that a significant interaction will make main effects uninterpretable. ] --- background-color: black ### Continuous variables -- background-image: url(./assets/img/continuous_interaction1.png) background-position: 70% 50% background-size: 900px -- background-image: url(./assets/img/continuous_interaction2.png) background-position: 70% 50% background-size: 900px --- <img src="index_files/figure-html/lex-decision-rt-1.png" width="75%" style="float:right;" /> ### Continuous and</br>categorical variables --- <img src="index_files/figure-html/int1-1.png" width="75%" style="float:right;" /> ### Categorical variables #### Interaction or no? -- <img = src="./assets/img/no_int.png" style="position: absolute; top: 280px; left: 550px;"></img> --- <img src="index_files/figure-html/int2-1.png" width="75%" style="float:right;" /> ### Categorical variables #### Interaction or no? -- <img = src="./assets/img/no_int.png" style="position: absolute; top: 280px; left: 550px;"></img> --- <img src="index_files/figure-html/int3-1.png" width="75%" style="float:right;" /> ### Categorical variables #### Interaction or no? -- <img = src="./assets/img/no_int.png" style="position: absolute; top: 280px; left: 550px;"></img> --- <img src="index_files/figure-html/int4-1.png" width="75%" style="float:right;" /> ### Categorical variables #### Interaction or no? -- <img = src="./assets/img/no_int.png" style="position: absolute; top: 280px; left: 550px;"></img> --- <img src="index_files/figure-html/int5-1.png" width="75%" style="float:right;" /> ### Categorical variables #### Interaction or no? -- <img = src="./assets/img/int.png" style="position: absolute; top: 350px; left: 550px;"></img> --- <img src="index_files/figure-html/int6-1.png" width="75%" style="float:right;" /> ### Categorical variables #### Interaction or no? -- <img = src="./assets/img/int.png" style="position: absolute; top: 350px; left: 550px;"></img> --- <img src="index_files/figure-html/int7-1.png" width="75%" style="float:right;" /> ### Categorical variables #### Interaction or no? -- <img = src="./assets/img/int.png" style="position: absolute; top: 350px; left: 550px;"></img> --- <img src="index_files/figure-html/int8-1.png" width="75%" style="float:right;" /> ### Categorical variables #### Interaction or no? -- <img = src="./assets/img/int.png" style="position: absolute; top: 350px; left: 550px;"></img> --- <img src="index_files/figure-html/int-main-effects1-1.png" width="50%" style="float:right;" /> ### Interpreting main effects .large[ .pull-left[ - Imagine you fit a model looking at `val ~ condition * group` - The model yields a main effect of *condition*, *group*, and a *condition* `\(\times\)` *group* interaction. - Can you interpret the main effects? What does a main effect of *condition* mean? What about a main effect of *group*? ] ] --- <img src="index_files/figure-html/int-main-effects2-1.png" width="50%" style="float:right;" /> ### Main effect of condition .large[ .pull-left[ - Collapsing over *group*, there is a difference between the *control* and the *treatment* conditions, i.e., the difference in means is not 0. - Thus, one might interpret the main effect of *condition* as something like "Overall, the control group had higher scores than the treatment group". - Technically this is "true", but careless, as it does not take into account the fact that the treatment had no effect on the `Late` group. ] ] --- <img src="index_files/figure-html/int-main-effects3-1.png" width="50%" style="float:right;" /> ### Main effect of group .large[ .pull-left[ - Collapsing over *condition*, there is a difference between `Early` and `Late`, i.e., the difference in means is not 0. - Thus, one might interpret the main effect as something like "Overall, the late group had higher average scores than the early group". - Technically this is "true", but careless, as it does not take into account the fact that the treatment is only effective on the `Early` group. ] ] --- ### Three-way interactions 😅 <img src="index_files/figure-html/three-way-int1-1.png" width="100%" /> --- layout: false background-image: url(https://media.giphy.com/media/3ofSB8NFxyfY6jw2Jy/giphy.gif) background-size: contain --- # Interactions ### Three-way interactions 😑 <img src="index_files/figure-html/three-way-int2-1.png" width="100%" /> --- background-image: url(https://i.imgflip.com/26x5rs.jpg) background-size: contain --- layout: false class: title-slide-section-red, middle # Coding categorical variables --- layout: true # Coding categorical variables --- ### Overview .pull-left[ - One of the benefits of 'doing ANOVA' with MRC is that you can include different types of predictors in your model, i.e., **categorical** and .blue[continuous]. - Neither classical ANOVA nor classical MRC can handle combinations of these two predictors - This is possible because of dummy coding - But there are other methods of coding categorical variables ] -- .pull-right[ - Treatment (dummy) coding - Sum coding - Deviation coding - ...and others ] --- class: title-slide-section-grey, middle ## .RUred[Treatment coding] --- ### Treatment (dummy) coding #### You get j-1 dummies .Large[ | Groups | d<sub>1</sub> | d<sub>2</sub> | d<sub>3</sub> | | :----- | :-----------: | :-----------: | :-----------: | | A | 0 | 0 | 0 | | B | 1 | 0 | 0 | | C | 0 | 1 | 0 | | D | 0 | 0 | 1 | ] --- ### Treatment (dummy) coding #### One level is taken as the *reference* or *baseline*. This is the intercept. .Large[ | Groups | d<sub>1</sub> | d<sub>2</sub> | d<sub>3</sub> | | | :----- | :-----------: | :-----------: | :-----------: | :--------------- | | **A** | **0** | **0** | **0** | ⬅︎ **Intercept** | | B | 1 | 0 | 0 | | | C | 0 | 1 | 0 | | | D | 0 | 0 | 1 | | ] --- ### Treatment (dummy) coding #### j-1 dummies = j-1 comparisons .Large[ | Groups | d<sub>1</sub> | d<sub>2</sub> | d<sub>3</sub> | | | :------- | :-----------: | :-----------: | :-----------: | :--------------- | | **A** | **0** | **0** | **0** | ⬅︎ **Intercept** | | .blue[B] | .blue[1] | 0 | 0 | | | C | 0 | 1 | 0 | | | D | 0 | 0 | 1 | | | | ⬆︎</br>**A**.blue[B]| | | | ] --- ### Treatment (dummy) coding #### j-1 dummies = j-1 comparisons .Large[ | Groups | d<sub>1</sub> | d<sub>2</sub> | d<sub>3</sub> | | | :------- | :-----------: | :-----------: | :-----------: | :--------------- | | **A** | **0** | **0** | **0** | ⬅︎ **Intercept** | | B | 1 | 0 | 0 | | | .blue[C] | 0 | .blue[1] | 0 | | | D | 0 | 0 | 1 | | | | | ⬆︎</br>**A**.blue[C]| | | ] --- ### Treatment (dummy) coding #### j-1 dummies = j-1 comparisons .Large[ | Groups | d<sub>1</sub> | d<sub>2</sub> | d<sub>3</sub> | | | :------- | :-----------: | :-----------: | :-----------: | :--------------- | | **A** | **0** | **0** | **0** | ⬅︎ **Intercept** | | B | 1 | 0 | 0 | | | C | 0 | 1 | 0 | | | .blue[D] | 0 | 0 | .blue[1] | | | | | | ⬆︎</br>**A**.blue[D] | | ] --- ### Review - Spanish spirantization - We are going to look at L2 acquisition of Spanish voiced stops - Spanish voiced stops: /b, d, g/ - Stop/approximant alternation: /b, d, g/ → [β, ð, ɣ] - Believed to be underlyingly stops (but this is contested) - Surface as stops in utterance initial position, after nasals, and after /l/ in the case of /d/ - Approximants in all other contexts - We have data from 30 participants (15 natives, 15 L2 learners) who produced /b/ in utterance initial and intervocalic position - We are interested in measuring degree of spirantization to see if learners can acquire a new phonetic category, [β] - Spirantization can be quantified by measuring the relative difference in the intensity (dB) of the target segment and that of the following vowel --- <img src="index_files/figure-html/spirantization-p1-1.png" width="48%" style="float:right;" /> ### Treatment (dummy) coding - Spanish spirantization .pull-left[ - We will fit a model that analyzes the `intensity difference` for the target segment ([β]) as a function group (native, learner) <table class="table" style="font-size: 17px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> group </th> <th style="text-align:right;"> mean_int_diff </th> <th style="text-align:right;"> sd_int_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> 17.003 </td> <td style="text-align:right;"> 3.279 </td> </tr> <tr> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 11.001 </td> <td style="text-align:right;"> 2.488 </td> </tr> </tbody> </table> ```r lm(intensity_diff ~ group, data = spir_tidy) ``` <table class="table" style="font-size: 17px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 17.003 </td> <td style="text-align:right;"> 0.556 </td> <td style="text-align:right;"> 30.593 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> groupnative </td> <td style="text-align:right;"> -6.002 </td> <td style="text-align:right;"> 0.963 </td> <td style="text-align:right;"> -6.235 </td> <td style="text-align:right;"> 0 </td> </tr> </tbody> </table> ] --- <img src="index_files/figure-html/spirantization-p2-1.png" width="48%" style="float:right;" /> ### Treatment (dummy) coding - Spanish spirantization .pull-left[ - If the categorical predictor includes more than one level <table class="table" style="font-size: 17px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> prof </th> <th style="text-align:right;"> mean_int_diff </th> <th style="text-align:right;"> sd_int_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> nat </td> <td style="text-align:right;"> 11.001 </td> <td style="text-align:right;"> 2.488 </td> </tr> <tr> <td style="text-align:left;"> adv </td> <td style="text-align:right;"> 15.506 </td> <td style="text-align:right;"> 2.715 </td> </tr> <tr> <td style="text-align:left;"> beg </td> <td style="text-align:right;"> 18.499 </td> <td style="text-align:right;"> 3.179 </td> </tr> </tbody> </table> ```r lm(intensity_diff ~ prof, data = spir_tidy) ``` <table class="table" style="font-size: 17px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 11.001 </td> <td style="text-align:right;"> 0.725 </td> <td style="text-align:right;"> 15.170 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> profadv </td> <td style="text-align:right;"> 4.506 </td> <td style="text-align:right;"> 1.026 </td> <td style="text-align:right;"> 4.393 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> profbeg </td> <td style="text-align:right;"> 7.498 </td> <td style="text-align:right;"> 1.026 </td> <td style="text-align:right;"> 7.311 </td> <td style="text-align:right;"> 0 </td> </tr> </tbody> </table> ] --- ### Treatment (dummy) coding - Spanish spirantization - If we want to test other comparisons we have to `relevel()` the factor. ```r mod_spir3 <- spir_tidy %>% * mutate(prof = fct_relevel(prof, "beg")) %>% lm(intensity_diff ~ prof, data = .) mod_spir3 %>% tidy() %>% kable(format = 'html', digits = 3) %>% kable_styling(., full_width = FALSE, font_size = 22) ``` <table class="table" style="font-size: 22px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 18.499 </td> <td style="text-align:right;"> 0.725 </td> <td style="text-align:right;"> 25.510 </td> <td style="text-align:right;"> 0.000 </td> </tr> <tr> <td style="text-align:left;"> profadv </td> <td style="text-align:right;"> -2.993 </td> <td style="text-align:right;"> 1.026 </td> <td style="text-align:right;"> -2.918 </td> <td style="text-align:right;"> 0.006 </td> </tr> <tr> <td style="text-align:left;"> profnat </td> <td style="text-align:right;"> -7.498 </td> <td style="text-align:right;"> 1.026 </td> <td style="text-align:right;"> -7.311 </td> <td style="text-align:right;"> 0.000 </td> </tr> </tbody> </table> --- ### Treatment (dummy) coding - Summary #### Advantages - Easy to do (default in R and most stats programs) - Easy to interpret if you know reference level and model is not too complicated #### Disadvantages - May give meaningless comparisons without releveling factor - May give meaningless comparisons if model includes continuous predictor --- class: title-slide-section-grey, middle ## .RUred[Sum coding] --- ### Sum coding - As we have seen, treatment coded dummy variables are discontinuous predictors coded as 0/1 - We can also use other values to get different model outputs - **Sum coding** (or "effect" coding) is often used when one wants the default model output to use an overall average value as the reference (rather than a possibly arbitrarily chosen group) - The values of the coded vectors must *sum to 0* - We still get j-1 estimates --- ### Sum coding #### How do we do it? - If our discontinuous variable only has two levels, we can add a new column to the dataframe that includes -1/1 - We can do this easily using the `mutate()` function ```r spir_tidy %>% * mutate(groupSum = if_else(group == "native", true = 1, false = -1)) ``` <table class="table" style="font-size: 18px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> prof </th> <th style="text-align:left;"> id </th> <th style="text-align:left;"> position </th> <th style="text-align:right;"> cIntensity </th> <th style="text-align:right;"> vIntensity </th> <th style="text-align:left;"> group </th> <th style="text-align:right;"> groupSum </th> <th style="text-align:right;"> intensity_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> beg </td> <td style="text-align:left;"> 01 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 58.58501 </td> <td style="text-align:right;"> 76.92710 </td> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> -1 </td> <td style="text-align:right;"> 18.34209 </td> </tr> <tr> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> beg </td> <td style="text-align:left;"> 02 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 60.72916 </td> <td style="text-align:right;"> 79.39805 </td> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> -1 </td> <td style="text-align:right;"> 18.66888 </td> </tr> <tr> <td style="text-align:left;"> 3 </td> <td style="text-align:left;"> beg </td> <td style="text-align:left;"> 03 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 61.53707 </td> <td style="text-align:right;"> 78.94344 </td> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> -1 </td> <td style="text-align:right;"> 17.40637 </td> </tr> </tbody> </table> <table class="table" style="font-size: 18px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> prof </th> <th style="text-align:left;"> id </th> <th style="text-align:left;"> position </th> <th style="text-align:right;"> cIntensity </th> <th style="text-align:right;"> vIntensity </th> <th style="text-align:left;"> group </th> <th style="text-align:right;"> groupSum </th> <th style="text-align:right;"> intensity_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 43 </td> <td style="text-align:left;"> nat </td> <td style="text-align:left;"> 13 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 69.39163 </td> <td style="text-align:right;"> 84.15049 </td> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 14.75886 </td> </tr> <tr> <td style="text-align:left;"> 44 </td> <td style="text-align:left;"> nat </td> <td style="text-align:left;"> 14 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 70.74004 </td> <td style="text-align:right;"> 82.05478 </td> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 11.31475 </td> </tr> <tr> <td style="text-align:left;"> 45 </td> <td style="text-align:left;"> nat </td> <td style="text-align:left;"> 15 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 70.53420 </td> <td style="text-align:right;"> 82.41582 </td> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 11.88162 </td> </tr> </tbody> </table> --- background-image: url(./index_files/figure-html/spirantization-p1-1.png) background-position: 98% 55% background-size: 550px ### Sum coding - Spanish spirantization .pull-left[ ```r lm(intensity_diff ~ groupSum, data = spir_tidy) ``` <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 14.002 </td> <td style="text-align:right;"> 0.481 </td> <td style="text-align:right;"> 29.091 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> groupSum </td> <td style="text-align:right;"> -3.001 </td> <td style="text-align:right;"> 0.481 </td> <td style="text-align:right;"> -6.235 </td> <td style="text-align:right;"> 0 </td> </tr> </tbody> </table> ] -- .footnote[ <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> group </th> <th style="text-align:right;"> mean_int_diff </th> <th style="text-align:right;"> sd_int_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> 17.003 </td> <td style="text-align:right;"> 3.279 </td> </tr> <tr> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 11.001 </td> <td style="text-align:right;"> 2.488 </td> </tr> </tbody> </table> - What is the intercept? The mean of group means. - What is `groupSum`? The effect of group on the intercept. - Calculate the intercept +/- the `groupSum` effect = group means. ] --- background-image: url(./index_files/figure-html/spirantization-p2-1.png) background-position: 90% 35% background-size: 500px ### Sum coding - Spanish spirantization .pull-left[ - What about with a 3-level factor? - This requires more tweeking. ```r spir_tidy$prof <- as.factor(spir_tidy$prof) *spir_tidy$profSum <- C(spir_tidy$prof, sum) contrasts(spir_tidy$profSum) ``` ``` ## [,1] [,2] ## adv 1 0 ## beg 0 1 ## nat -1 -1 ``` ```r lm(intensity_diff ~ profSum, data = spir_tidy) ``` <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 15.002 </td> <td style="text-align:right;"> 0.419 </td> <td style="text-align:right;"> 35.832 </td> <td style="text-align:right;"> 0.000 </td> </tr> <tr> <td style="text-align:left;"> profSum1 </td> <td style="text-align:right;"> 0.504 </td> <td style="text-align:right;"> 0.592 </td> <td style="text-align:right;"> 0.852 </td> <td style="text-align:right;"> 0.399 </td> </tr> <tr> <td style="text-align:left;"> profSum2 </td> <td style="text-align:right;"> 3.497 </td> <td style="text-align:right;"> 0.592 </td> <td style="text-align:right;"> 5.906 </td> <td style="text-align:right;"> 0.000 </td> </tr> </tbody> </table> ] -- .pull-right[ .footnote[ - Is this useful? Why would we do this? ] ] --- ### Sum coding - Summary #### Advantages - With two level factor, sum (effect) coding gives omnibus assessment of main effects - With more than two levels, sum (effect) coding assess the "uniqueness" of a given level compared to the overall average - Avoids (possibly) arbitrary selection of reference group - Makes MRC even more like ANOVA (advantage for researchers accustomed to ANOVA) #### Disadvantages - Involves releveling, adding columns to dataframe or resetting factor - May not always provide useful information? --- class: title-slide-section-grey, middle ## .RUred[Deviation coding] --- layout: true # Coding categorical variables --- ### Deviation coding - Dummy variables of discontinuous predictors can be coded as 0/1 (treatment coding) or -1/1 (sum coding) - There is another *sum to 0* possibility that is often used with 2-level factors: deviation coding - **Deviation coding** is often used when one wants the default model output to provide an overall assessment of effect size. - The values of the coded vectors still must *sum to 0* - We still get j-1 estimates --- ### Deviation coding #### How do we do it? - We can add a new column to the dataframe that includes -0.5/0.5 - We can do this easily using the `mutate()` function <table class="table" style="font-size: 18px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> prof </th> <th style="text-align:left;"> id </th> <th style="text-align:left;"> position </th> <th style="text-align:right;"> cIntensity </th> <th style="text-align:right;"> vIntensity </th> <th style="text-align:left;"> group </th> <th style="text-align:right;"> groupDev </th> <th style="text-align:right;"> intensity_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> beg </td> <td style="text-align:left;"> 01 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 58.58501 </td> <td style="text-align:right;"> 76.92710 </td> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> -0.5 </td> <td style="text-align:right;"> 18.34209 </td> </tr> <tr> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> beg </td> <td style="text-align:left;"> 02 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 60.72916 </td> <td style="text-align:right;"> 79.39805 </td> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> -0.5 </td> <td style="text-align:right;"> 18.66888 </td> </tr> <tr> <td style="text-align:left;"> 3 </td> <td style="text-align:left;"> beg </td> <td style="text-align:left;"> 03 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 61.53707 </td> <td style="text-align:right;"> 78.94344 </td> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> -0.5 </td> <td style="text-align:right;"> 17.40637 </td> </tr> </tbody> </table> <table class="table" style="font-size: 18px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> prof </th> <th style="text-align:left;"> id </th> <th style="text-align:left;"> position </th> <th style="text-align:right;"> cIntensity </th> <th style="text-align:right;"> vIntensity </th> <th style="text-align:left;"> group </th> <th style="text-align:right;"> groupDev </th> <th style="text-align:right;"> intensity_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 43 </td> <td style="text-align:left;"> nat </td> <td style="text-align:left;"> 13 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 69.39163 </td> <td style="text-align:right;"> 84.15049 </td> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 0.5 </td> <td style="text-align:right;"> 14.75886 </td> </tr> <tr> <td style="text-align:left;"> 44 </td> <td style="text-align:left;"> nat </td> <td style="text-align:left;"> 14 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 70.74004 </td> <td style="text-align:right;"> 82.05478 </td> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 0.5 </td> <td style="text-align:right;"> 11.31475 </td> </tr> <tr> <td style="text-align:left;"> 45 </td> <td style="text-align:left;"> nat </td> <td style="text-align:left;"> 15 </td> <td style="text-align:left;"> vcv </td> <td style="text-align:right;"> 70.53420 </td> <td style="text-align:right;"> 82.41582 </td> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 0.5 </td> <td style="text-align:right;"> 11.88162 </td> </tr> </tbody> </table> --- background-image: url(./index_files/figure-html/spirantization-p1-1.png) background-position: 98% 55% background-size: 550px ### Deviation coding - Spanish spirantization .pull-left[ ```r lm(intensity_diff ~ groupDev, data = spir_tidy) ``` <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 14.002 </td> <td style="text-align:right;"> 0.481 </td> <td style="text-align:right;"> 29.091 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> groupDev </td> <td style="text-align:right;"> -6.002 </td> <td style="text-align:right;"> 0.963 </td> <td style="text-align:right;"> -6.235 </td> <td style="text-align:right;"> 0 </td> </tr> </tbody> </table> ] -- .footnote[ <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> group </th> <th style="text-align:right;"> mean_int_diff </th> <th style="text-align:right;"> sd_int_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> 17.003 </td> <td style="text-align:right;"> 3.279 </td> </tr> <tr> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 11.001 </td> <td style="text-align:right;"> 2.488 </td> </tr> </tbody> </table> - What is the intercept? The mean of group means. - What is `groupDev`? The overall effect of group. - Calculates the difference between group means ] --- ### Deviation coding - Summary #### Advantages - Deviation coding gives omnibus assessment of main effects - Avoids (possibly) arbitrary selection of reference group - Makes MRC even more like ANOVA (advantage for researchers accustomed to ANOVA) #### Disadvantages - Involves releveling, adding columns to dataframe or resetting factor - May not always provide useful information? - Doesn't take advantage of the intercept --- layout: false background-color: lightblue # A note about ANOVA .pull-left[ #### MRC ``` Call: lm(formula = intensity_diff ~ groupSum, data = spir_tidy) Residuals: Min 1Q Median 3Q Max -5.1762 -2.1004 -0.5187 1.6664 7.9063 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 14.0016 0.4813 29.091 < 2e-16 *** groupSum -3.0009 0.4813 -6.235 1.66e-07 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 3.044 on 43 degrees of freedom Multiple R-squared: 0.4748, Adjusted R-squared: 0.4626 F-statistic: 38.87 on 1 and 43 DF, p-value: 1.665e-07 ``` ] -- .pull-right[ #### ANOVA </br> ``` aov(intensity_diff ~ groupSum, data = spir_tidy) ``` ``` Df Sum Sq Mean Sq F value Pr(>F) groupSum 1 360.2 360.2 38.87 1.66e-07 *** Residuals 43 398.5 9.3 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` .footnote[ .content-box-red[ Notice: the F-ratio and DFs ] ] ] --- background-color: lightblue # A note about ANOVA ### We can make them exactly the same... .pull-left[ #### MRC ``` Analysis of Variance Table Response: intensity_diff Df Sum Sq Mean Sq F value Pr(>F) groupSum 1 360.22 360.22 38.873 1.665e-07 *** Residuals 43 398.46 9.27 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` ] .pull-right[ #### ANOVA ``` Df Sum Sq Mean Sq F value Pr(>F) groupSum 1 360.2 360.2 38.87 1.66e-07 *** Residuals 43 398.5 9.3 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` ] --- background-color: lightblue # A note about ANOVA .pull-left[ #### T-test ``` Welch Two Sample t-test data: intensity_diff by group t = 6.8355, df = 35.836, p-value = 5.528e-08 alternative hypothesis: true difference in means between group learner and group native is not equal to 0 95 percent confidence interval: 4.220795 7.782855 sample estimates: mean in group learner mean in group native 17.00251 11.00068 ``` ] .pull-right[ #### MRC ``` Estimate Std. Error t value Pr(>|t|) (Intercept) 14.001595 0.4813115 29.090506 6.433300e-30 groupSum -3.000913 0.4813115 -6.234865 1.664552e-07 ``` </br> #### ANOVA ``` Df Sum Sq Mean Sq F value Pr(>F) groupSum 1 360.2 360.2 38.87 1.66e-07 *** Residuals 43 398.5 9.3 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` ] --- background-color: lightblue # A note about ANOVA ### What it means .Large[ - With a 1-way between-subjects design, MRC, ANOVA and the t-test essentially do the exact same thing - This is because they are all part of the general linear model (remember when I said 'everything is a linear model'?) - ANOVA and t-tests **require** categorical factors - T-test can only compare two groups (1 factor, 2 levels) - ANOVA can include many categorical factors with numerous levels - Neither the t-test nor ANOVA can model continuous predictors - MRC includes continuous and discontinuous predictors and gives us more information... - Parameter estimates - Comparisons - R<sup>2</sup> ] --- class: title-slide-section-grey, middle # Coding categorical predictors ## .RUred[Contrast coding] --- layout: true # Coding categorical predictors --- ### Contrast coding - Ideally we want to avoid tests that are not theoretically motivated 1. Because they are not interesting 2. To avoid capitalizing on chance - One way to avoid atheoretical comparisons is to use orthogonal contrast coding - This mean the researcher specifies the contrasts to test by hand-coding the dummy vector - For example, imagine we want to test the `adv` group with the native controls, as well as the `beg` and `adv` learner groups together with the natives. -- .pull-left[ .center[**Adv. vs. Natives**] <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> prof </th> <th style="text-align:right;"> mean_int_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> adv </td> <td style="text-align:right;"> 15.506 </td> </tr> <tr> <td style="text-align:left;"> nat </td> <td style="text-align:right;"> 11.001 </td> </tr> </tbody> </table> ] .pull-right[ .center[**Learners vs. Natives**] <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> group </th> <th style="text-align:right;"> mean_int_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> 17.003 </td> </tr> <tr> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 11.001 </td> </tr> </tbody> </table> ] --- ### Contrast coding .left-column[ 1. Manually code contrasts 1. Put them in a matrix 1. Calculate general inverse of matrix 1. Fit model using `contrasts` argument <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> prof </th> <th style="text-align:right;"> mean_int_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> adv </td> <td style="text-align:right;"> 15.506 </td> </tr> <tr> <td style="text-align:left;"> nat </td> <td style="text-align:right;"> 11.001 </td> </tr> </tbody> </table> <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> group </th> <th style="text-align:right;"> mean_int_diff </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> learner </td> <td style="text-align:right;"> 17.003 </td> </tr> <tr> <td style="text-align:left;"> native </td> <td style="text-align:right;"> 11.001 </td> </tr> </tbody> </table> ] .right-column[ ```r contrast1 <- c(1, 0, -1) # adv vs. native contrast2 <- c(1/2, 1/2, -1) # adv + beg vs. native prof_contrasts <- matrix(c(contrast1, contrast2), ncol = 2) prof_contrasts <- t(MASS::ginv(prof_contrasts)) show(prof_contrasts) ``` ``` ## [,1] [,2] ## [1,] 1.000000e+00 -0.6666667 ## [2,] -1.000000e+00 1.3333333 ## [3,] 1.110223e-16 -0.6666667 ``` ``` lm(intensity_diff ~ prof, data = spir_tidy, contrasts = list(prof = prof_contrasts)) ``` <table class="table" style="font-size: 17px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 15.0019 </td> <td style="text-align:right;"> 0.4187 </td> <td style="text-align:right;"> 35.8322 </td> <td style="text-align:right;"> 0e+00 </td> </tr> <tr> <td style="text-align:left;"> prof1 </td> <td style="text-align:right;"> 4.5055 </td> <td style="text-align:right;"> 1.0255 </td> <td style="text-align:right;"> 4.3934 </td> <td style="text-align:right;"> 1e-04 </td> </tr> <tr> <td style="text-align:left;"> prof2 </td> <td style="text-align:right;"> 6.0018 </td> <td style="text-align:right;"> 0.8881 </td> <td style="text-align:right;"> 6.7578 </td> <td style="text-align:right;"> 0e+00 </td> </tr> </tbody> </table> ] --- ### Contrast coding - Summary #### Advantages - Allows you to test only the comparisons you want - Test only theoretically motivated comparisons #### Disadvantages - Requires thought, coding by hand - What is the benefit of estimating the intercept? --- layout: false class: title-slide-section-red, middle # Transforming continuous predictors --- layout: true # Transforming continuous predictors --- ### Overview .large[ - What is a transformation? The process of converting data from one format to another - We can use linear and non-linear transformations to make our models more interpretable (especially in conjunction with coding discontinuous predictors) - Transformations do not affect the model fit, predictions, R<sup>2</sup>, etc. - The goal is to make our model easier to understand ] <p></p> .large[ - Examples - Log transformation - Centering - Standardizing ] --- class: title-slide-section-grey, middle ## .RUred[Log transformations] --- ### Log transformations #### What? - A log transformation converts the predictor to a non-linear logarithmic scale - A log scale is one that is based on orders of magnitude - The value of each 'tick' on the scale is the value of the previous 'tick' multiplied by a base value <p></p> - Some naturally occurring phenomena are better understood in a logarithmic scale - Seismic activity (Richter scale) - Sound (loudness, dB) - Odds (logits) #### Why? - A log transformation can make a predictor variable more normal (i.e., correct for skewness) - Log scales show percent change in an easily interpretable manner --- layout: false class: middle .pull-left[ <br><br> <table class="table" style="font-size: 18px; "> <thead> <tr> <th style="text-align:left;"> Typical experience </th> <th style="text-align:right;"> Pressure (μPa) </th> <th style="text-align:right;"> Decibel level </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Absolute threshold </td> <td style="text-align:right;"> 20 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> Faint whisper </td> <td style="text-align:right;"> 200 </td> <td style="text-align:right;"> 20 </td> </tr> <tr> <td style="text-align:left;"> Quiet office </td> <td style="text-align:right;"> 2000 </td> <td style="text-align:right;"> 40 </td> </tr> <tr> <td style="text-align:left;"> Conversation </td> <td style="text-align:right;"> 20000 </td> <td style="text-align:right;"> 60 </td> </tr> <tr> <td style="text-align:left;"> City bus </td> <td style="text-align:right;"> 200000 </td> <td style="text-align:right;"> 80 </td> </tr> <tr> <td style="text-align:left;"> Subway train </td> <td style="text-align:right;"> 2000000 </td> <td style="text-align:right;"> 100 </td> </tr> <tr> <td style="text-align:left;"> Loud thunder </td> <td style="text-align:right;"> 20000000 </td> <td style="text-align:right;"> 120 </td> </tr> <tr> <td style="text-align:left;"> Pain and damage </td> <td style="text-align:right;"> 200000000 </td> <td style="text-align:right;"> 140 </td> </tr> </tbody> </table> ] .pull-right[ <img src="index_files/figure-html/sound-ex-plot-1.png" width="100%" /> ] --- layout: true # Transforming continuous predictors --- .pull-left[ ### Log transformations - It is most common to use the natural log (log<sub>e</sub>)<sup>1</sup> - The values must be positive in order to be log-transformed - We can do it in R with the `log()` function - Consider the `language_diversity` data ```r tidy_lang_div <- language_diversity %>% dplyr::filter(Continent != 'Americas') %>% pivot_wider(names_from = "Measurement", values_from = "Value") %>% select(Country, MGS, Langs) %>% * mutate(log_langs = log(Langs)) ``` ] .pull-right[ <img src="index_files/figure-html/lang-div-hists-1.png" width="100%" /> ] .footnote[.scriptsize[<sup>1</sup> A mathematical constant, irrational and transcendental number equal to ~ 2.718281828459]] --- #### Log transformations .pull-left[ <img src="index_files/figure-html/lang-div-plots1-1.png" width="100%" /> <table class="table" style="font-size: 14px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> -16.67491 </td> <td style="text-align:right;"> 45.328814 </td> <td style="text-align:right;"> -0.3678655 </td> <td style="text-align:right;"> 0.7143855 </td> </tr> <tr> <td style="text-align:left;"> MGS </td> <td style="text-align:right;"> 18.08001 </td> <td style="text-align:right;"> 6.264528 </td> <td style="text-align:right;"> 2.8860932 </td> <td style="text-align:right;"> 0.0055630 </td> </tr> </tbody> </table> ] -- .pull-right[ <img src="index_files/figure-html/lang-div-plots2-1.png" width="100%" /> <table class="table" style="font-size: 14px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 2.4704391 </td> <td style="text-align:right;"> 0.3094346 </td> <td style="text-align:right;"> 7.983719 </td> <td style="text-align:right;"> 0.0e+00 </td> </tr> <tr> <td style="text-align:left;"> MGS </td> <td style="text-align:right;"> 0.2151393 </td> <td style="text-align:right;"> 0.0427644 </td> <td style="text-align:right;"> 5.030798 </td> <td style="text-align:right;"> 5.5e-06 </td> </tr> </tbody> </table> .tiny[ - A 1 month change in MGS is associated with a 22% increase in the number of languages. ] ] --- ### Log transformations - Summary #### Advantages - Can correct for skewness (note: this is not a guaranteed fix for non-normal data) <p></p> - Fairly easy to interpret - Coefficients with low values can be interpreted directly as percents - Coefficients with higher values should be exponentiated (`exp()`) <p></p> - If the criterion *and* the predictors are log transformed, parameter estimates interpreted as "for each 1% change in x, the predicted difference in y is \_\_%" --- class: title-slide-section-grey, middle ## .RUred[Centering] --- exclude: true --- ### Centering .pull-left[ - As mentioned previously, the intercepts in many of our models are not meaningful - This is particularly true when the average of a continuous predictor is far from 0 - Or when having a value of 0 makes no sense, i.e., IQ, vocab size, etc. <p></p> - An easy way to fix this is to center the predictor - A centered predictor has a mean of 0 - The standard deviation remains the same - To center a predictor you subtract the mean from each value ] -- .pull-right[ ```r mtcars %>% * mutate(disp_c = disp - mean(disp)) %>% summarize( mean_disp = mean(disp) %>% round(2), sd_disp = sd(disp) %>% round(2), mean_disp_c = mean(disp_c) %>% round(2), sd_disp_c = sd(disp_c) %>% round(2)) %>% t(.) ``` ``` [,1] mean_disp 230.72 sd_disp 123.94 mean_disp_c 0.00 sd_disp_c 123.94 ``` ] --- ### Centering - Ex. A <div style="float:left"> <img width="500" src="./index_files/figure-html/uncentered-plot-a-1.png"> </div> -- <div style="float:right"> <img width="500" src="./index_files/figure-html/uncentered-plot-a-extend-1.png"> </div> .center[ <table class="table" style="font-size: 18px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 15.971 </td> <td style="text-align:right;"> 3.905 </td> <td style="text-align:right;"> 4.090 </td> <td style="text-align:right;"> 0.000 </td> </tr> <tr> <td style="text-align:left;"> groupb </td> <td style="text-align:right;"> -12.700 </td> <td style="text-align:right;"> 5.523 </td> <td style="text-align:right;"> -2.300 </td> <td style="text-align:right;"> 0.024 </td> </tr> <tr> <td style="text-align:left;"> x </td> <td style="text-align:right;"> 0.246 </td> <td style="text-align:right;"> 0.322 </td> <td style="text-align:right;"> 0.765 </td> <td style="text-align:right;"> 0.446 </td> </tr> <tr> <td style="text-align:left;"> groupb:x </td> <td style="text-align:right;"> 1.068 </td> <td style="text-align:right;"> 0.455 </td> <td style="text-align:right;"> 2.347 </td> <td style="text-align:right;"> 0.022 </td> </tr> </tbody> </table> ] --- layout: false background-image: url(./index_files/figure-html/centered-plot-a-1.png) background-position: 50% 8% background-size: 1025px -- <br><br><br><br><br><br><br> <br><br><br><br><br><br><br> <br><br><br><br><br><br> .pull-left[ <table class="table" style="font-size: 15px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 18.949 </td> <td style="text-align:right;"> 0.324 </td> <td style="text-align:right;"> 58.505 </td> <td style="text-align:right;"> 0.000 </td> </tr> <tr> <td style="text-align:left;"> groupb </td> <td style="text-align:right;"> 0.218 </td> <td style="text-align:right;"> 0.458 </td> <td style="text-align:right;"> 0.476 </td> <td style="text-align:right;"> 0.635 </td> </tr> <tr> <td style="text-align:left;"> x_c </td> <td style="text-align:right;"> 0.246 </td> <td style="text-align:right;"> 0.322 </td> <td style="text-align:right;"> 0.765 </td> <td style="text-align:right;"> 0.446 </td> </tr> <tr> <td style="text-align:left;"> groupb:x_c </td> <td style="text-align:right;"> 1.068 </td> <td style="text-align:right;"> 0.455 </td> <td style="text-align:right;"> 2.347 </td> <td style="text-align:right;"> 0.022 </td> </tr> </tbody> </table> ] -- .pull-right[ <table class="table" style="font-size: 15px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 19.058 </td> <td style="text-align:right;"> 0.229 </td> <td style="text-align:right;"> 83.215 </td> <td style="text-align:right;"> 0.000 </td> </tr> <tr> <td style="text-align:left;"> groupSum </td> <td style="text-align:right;"> -0.109 </td> <td style="text-align:right;"> 0.229 </td> <td style="text-align:right;"> -0.476 </td> <td style="text-align:right;"> 0.635 </td> </tr> <tr> <td style="text-align:left;"> x_c </td> <td style="text-align:right;"> 0.780 </td> <td style="text-align:right;"> 0.228 </td> <td style="text-align:right;"> 3.429 </td> <td style="text-align:right;"> 0.001 </td> </tr> <tr> <td style="text-align:left;"> groupSum:x_c </td> <td style="text-align:right;"> -0.534 </td> <td style="text-align:right;"> 0.228 </td> <td style="text-align:right;"> -2.347 </td> <td style="text-align:right;"> 0.022 </td> </tr> </tbody> </table> ] --- layout: true # Transforming continuous predictors --- ### Centering - Ex. B <div style="float:left"> <img width="500" src="./index_files/figure-html/uncentered-plot-b-1.png"> </div> -- <div style="float:right"> <img width="500" src="./index_files/figure-html/uncentered-plot-b-extend-1.png"> </div> .center[ <table class="table" style="font-size: 18px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 5.592 </td> <td style="text-align:right;"> 3.481 </td> <td style="text-align:right;"> 1.606 </td> <td style="text-align:right;"> 0.112 </td> </tr> <tr> <td style="text-align:left;"> groupd </td> <td style="text-align:right;"> -3.090 </td> <td style="text-align:right;"> 4.924 </td> <td style="text-align:right;"> -0.628 </td> <td style="text-align:right;"> 0.532 </td> </tr> <tr> <td style="text-align:left;"> x </td> <td style="text-align:right;"> 0.261 </td> <td style="text-align:right;"> 0.287 </td> <td style="text-align:right;"> 0.910 </td> <td style="text-align:right;"> 0.366 </td> </tr> <tr> <td style="text-align:left;"> groupd:x </td> <td style="text-align:right;"> 1.699 </td> <td style="text-align:right;"> 0.406 </td> <td style="text-align:right;"> 4.189 </td> <td style="text-align:right;"> 0.000 </td> </tr> </tbody> </table> ] --- layout: false background-image: url(./index_files/figure-html/centered-plot-b-1.png) background-position: 50% 8% background-size: 1025px <br><br><br><br><br><br><br> <br><br><br><br><br><br><br> <br><br><br><br><br><br> -- .pull-left[ <table class="table" style="font-size: 15px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 8.750 </td> <td style="text-align:right;"> 0.289 </td> <td style="text-align:right;"> 30.303 </td> <td style="text-align:right;"> 0.000 </td> </tr> <tr> <td style="text-align:left;"> groupd </td> <td style="text-align:right;"> 17.465 </td> <td style="text-align:right;"> 0.408 </td> <td style="text-align:right;"> 42.770 </td> <td style="text-align:right;"> 0.000 </td> </tr> <tr> <td style="text-align:left;"> x_c </td> <td style="text-align:right;"> 0.261 </td> <td style="text-align:right;"> 0.287 </td> <td style="text-align:right;"> 0.910 </td> <td style="text-align:right;"> 0.366 </td> </tr> <tr> <td style="text-align:left;"> groupd:x_c </td> <td style="text-align:right;"> 1.699 </td> <td style="text-align:right;"> 0.406 </td> <td style="text-align:right;"> 4.189 </td> <td style="text-align:right;"> 0.000 </td> </tr> </tbody> </table> ] -- .pull-right[ <table class="table" style="font-size: 15px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 17.482 </td> <td style="text-align:right;"> 0.204 </td> <td style="text-align:right;"> 85.625 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> groupSum </td> <td style="text-align:right;"> -8.732 </td> <td style="text-align:right;"> 0.204 </td> <td style="text-align:right;"> -42.770 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> x_c </td> <td style="text-align:right;"> 1.111 </td> <td style="text-align:right;"> 0.203 </td> <td style="text-align:right;"> 5.476 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> groupSum:x_c </td> <td style="text-align:right;"> -0.850 </td> <td style="text-align:right;"> 0.203 </td> <td style="text-align:right;"> -4.189 </td> <td style="text-align:right;"> 0 </td> </tr> </tbody> </table> ] --- layout: true # Transforming continuous predictors --- background-color: black class: middle ## .white[Spanish spirantization (revisited)] --- <img src="index_files/figure-html/spirantization-p3-1.png" width="48%" style='float:right;' /> ### Sum coding - Spanish spirantization (revisited) .pull-left[ - We get j-1 contrasts - `lm()` conducts tests of independence: H<sub>0</sub> `\(\neq\)` 0 - If we center the criterion, the mean = 0 - We can remove the intercept and test whether each level is = 0 ```r lm(int_diff_c ~ -1 + profSum, data = spir_tidy) ``` <table class="table" style="font-size: 18px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> profSumadv </td> <td style="text-align:right;"> 0.504 </td> <td style="text-align:right;"> 0.725 </td> <td style="text-align:right;"> 0.695 </td> <td style="text-align:right;"> 0.491 </td> </tr> <tr> <td style="text-align:left;"> profSumbeg </td> <td style="text-align:right;"> 3.497 </td> <td style="text-align:right;"> 0.725 </td> <td style="text-align:right;"> 4.822 </td> <td style="text-align:right;"> 0.000 </td> </tr> <tr> <td style="text-align:left;"> profSumnat </td> <td style="text-align:right;"> -4.001 </td> <td style="text-align:right;"> 0.725 </td> <td style="text-align:right;"> -5.518 </td> <td style="text-align:right;"> 0.000 </td> </tr> </tbody> </table> ] --- ### Centering - Summary #### Advantages - We can make the intercept relevant - Combined with sum/deviation coding, interactions are much easier to interpret - Centering uncorrelates interaction terms #### Disadvantages - ??? <p></p> - In some cases it might not help (but it doesn't hurt) - When 0 is meaningful - When there are no interaction terms --- class: title-slide-section-grey, middle ## .RUred[Standardizing] --- ### Standardizing .pull-left[ - Sometimes we are interested in comparing effect sizes within/between models - Typically this is not possible because the parameter estimates are on different scales - We know we can calculate z-scores (standardize) variables when we want to "put them on the same scale" - When the criterion and the predictors are standardized the model coefficients are beta weights (assess effect size and are comparable between models) - A standardized variable has a mean of 0 and a standard deviation of 1 ] .pull-right[ ```r mtcars %>% * mutate(disp_std = (disp - mean(disp)) / sd(disp)) %>% summarize( mean_disp = mean(disp), sd_disp = sd(disp), mean_disp_std = mean(disp_std), sd_disp_std = sd(disp_std) ) %>% pivot_longer(cols = everything(), names_to = "metric", values_to = "val") %>% mutate(val = round(val)) %>% knitr::kable() ``` <table> <thead> <tr> <th style="text-align:left;"> metric </th> <th style="text-align:right;"> val </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> mean_disp </td> <td style="text-align:right;"> 231 </td> </tr> <tr> <td style="text-align:left;"> sd_disp </td> <td style="text-align:right;"> 124 </td> </tr> <tr> <td style="text-align:left;"> mean_disp_std </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> sd_disp_std </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> ] --- layout: false background-image: url(./index_files/figure-html/std-plot-a-1.png) background-position: 50% 8% background-size: 1025px <br><br><br><br><br><br><br> <br><br><br><br><br><br><br> <br><br><br><br><br><br> -- .pull-left[ <table class="table" style="font-size: 15px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 19.058 </td> <td style="text-align:right;"> 0.229 </td> <td style="text-align:right;"> 83.215 </td> <td style="text-align:right;"> 0.000 </td> </tr> <tr> <td style="text-align:left;"> groupSum </td> <td style="text-align:right;"> -0.109 </td> <td style="text-align:right;"> 0.229 </td> <td style="text-align:right;"> -0.476 </td> <td style="text-align:right;"> 0.635 </td> </tr> <tr> <td style="text-align:left;"> x_c </td> <td style="text-align:right;"> 0.780 </td> <td style="text-align:right;"> 0.228 </td> <td style="text-align:right;"> 3.429 </td> <td style="text-align:right;"> 0.001 </td> </tr> <tr> <td style="text-align:left;"> groupSum:x_c </td> <td style="text-align:right;"> -0.534 </td> <td style="text-align:right;"> 0.228 </td> <td style="text-align:right;"> -2.347 </td> <td style="text-align:right;"> 0.022 </td> </tr> </tbody> </table> ] -- .pull-right[ <table class="table" style="font-size: 15px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 0.000 </td> <td style="text-align:right;"> 0.103 </td> <td style="text-align:right;"> 0.000 </td> <td style="text-align:right;"> 1.000 </td> </tr> <tr> <td style="text-align:left;"> groupSum </td> <td style="text-align:right;"> -0.049 </td> <td style="text-align:right;"> 0.103 </td> <td style="text-align:right;"> -0.476 </td> <td style="text-align:right;"> 0.635 </td> </tr> <tr> <td style="text-align:left;"> x_std </td> <td style="text-align:right;"> 0.355 </td> <td style="text-align:right;"> 0.103 </td> <td style="text-align:right;"> 3.429 </td> <td style="text-align:right;"> 0.001 </td> </tr> <tr> <td style="text-align:left;"> groupSum:x_std </td> <td style="text-align:right;"> -0.243 </td> <td style="text-align:right;"> 0.103 </td> <td style="text-align:right;"> -2.347 </td> <td style="text-align:right;"> 0.022 </td> </tr> </tbody> </table> ] --- background-image: url(./index_files/figure-html/std-plot-b-1.png) background-position: 50% 8% background-size: 1025px <br><br><br><br><br><br><br> <br><br><br><br><br><br><br> <br><br><br><br><br><br> -- .pull-left[ <table class="table" style="font-size: 15px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 17.482 </td> <td style="text-align:right;"> 0.204 </td> <td style="text-align:right;"> 85.625 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> groupSum </td> <td style="text-align:right;"> -8.732 </td> <td style="text-align:right;"> 0.204 </td> <td style="text-align:right;"> -42.770 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> x_c </td> <td style="text-align:right;"> 1.111 </td> <td style="text-align:right;"> 0.203 </td> <td style="text-align:right;"> 5.476 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> groupSum:x_c </td> <td style="text-align:right;"> -0.850 </td> <td style="text-align:right;"> 0.203 </td> <td style="text-align:right;"> -4.189 </td> <td style="text-align:right;"> 0 </td> </tr> </tbody> </table> ] -- .pull-right[ <table class="table" style="font-size: 15px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 0.000 </td> <td style="text-align:right;"> 0.022 </td> <td style="text-align:right;"> 0.000 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> groupSum </td> <td style="text-align:right;"> -0.962 </td> <td style="text-align:right;"> 0.022 </td> <td style="text-align:right;"> -42.770 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> x_std </td> <td style="text-align:right;"> 0.124 </td> <td style="text-align:right;"> 0.023 </td> <td style="text-align:right;"> 5.476 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> groupSum:x_std </td> <td style="text-align:right;"> -0.095 </td> <td style="text-align:right;"> 0.023 </td> <td style="text-align:right;"> -4.189 </td> <td style="text-align:right;"> 0 </td> </tr> </tbody> </table> ] --- ### Standardizing - Summary #### Advantages - Makes intercept meaningful - Model output provides beta weights (effect size) - Beta weights can be compared with other variables (even in other models) #### Disadvantages - Interpreting predictions is more difficult (but we don't usually do that) --- ### Practice [model tuning practice](./assets/tuning_walkthrough/tuning.zip) --- exclude: true (Wickham and Grolemund, 2016; Hardy, 1993b; Hardy, 1993c; Hardy, 1993d) --- layout: false class: title-slide-final, left # References [1] M. Hardy. "Assessing Group Differences in Effects". In: _Regression with Dummy Variables_. Ed. by M. Hardy. Sage University Paper Series on Quantitative Applications in the Social Sciences - 93. Newbury Park, CA: Sage, 1993, pp. 29-63. ISBN: 9780803951280. [2] M. Hardy. "Creating Dummy Variables". In: _Regression with Dummy Variables_. Ed. by M. Hardy. Sage University Paper Series on Quantitative Applications in the Social Sciences - 93. Newbury Park, CA: Sage, 1993, pp. 7-17. ISBN: 9780803951280. [3] M. Hardy. "Using Dummy Variables as Regressors". In: _Regression with Dummy Variables_. Ed. by M. Hardy. Sage University Paper Series on Quantitative Applications in the Social Sciences - 93. Newbury Park, CA: Sage, 1993, pp. 18-28. ISBN: 9780803951280. [4] H. Wickham and G. Grolemund. _R for Data Science: Import, Tidy, Transform, Visualize, and Model Data_. O'Reilly Media, 2016. Figueredo, A. J. (2013). Continuous and Categorical Predictors. *Statistical Methods in Psychological Research*. Figueredo, A. J. (2013). The General Linear Model: ANOVA. *Statistical Methods in Psychological Research*.