First step: let’s load in the data and required packages. This is the standard McMurray & Jongman dataset of fricative and vowel measures that we’ve been using this term.
library(tidyverse)
library(yarrr)
mcm <- read_csv("/Users/Thomas/Library/CloudStorage/OneDrive-YorkUniversity/LING 3300/Datasets/McMurrayJongmanFricativeAcoustics.csv")
Let’s say we’re interested in knowing whether the various fricative places of articulation differ significantly in M1 (Centre of Gravity). In this case, we have four places of articulation, not just two!
The key R code here when dealing with a categorical variable with more than two levels is as follows:
Here let’s get our fricatives into four place of articulation levels, look at what the differences in mean COG are, and then see what the result of the model looks like with treatment coding assigning alveolar as the baseline condition (listed as the first factor).
# Assigning 4 place of articulation levels
mcm$poa <- ifelse(mcm$Fricative %in% c("f", "v"), "labiodental", ifelse(mcm$Fricative %in% c("th", "dh"), "interdental", ifelse(mcm$Fricative%in% c("s", "z"), "alveolar", "postalveolar")))
# Looking at a boxplot of the overall data by Fricative
ggplot(mcm) +
geom_boxplot(aes(x = poa, y = M1, color = Fricative))
# Looking at a boxplot of the overall data by poa
ggplot(mcm) +
geom_boxplot(aes(x = poa, y = M1))
# Getting speaker COG means for each place of articulation
poa_means <- mcm %>%
group_by(Talker, poa) %>%
summarise(meanCOG = mean(M1, na.rm = T))
# Looking at a boxplot of the speaker mean data by poa
ggplot(poa_means) +
geom_boxplot(aes(x = poa, y = meanCOG))
# Looking at a pirate plot of the speaker mean data by poa
pirateplot(meanCOG ~ poa, poa_means,
inf.method = "ci")
# Putting alveolar as the first factor
poa_means$poa <- factor(poa_means$poa, levels = c('alveolar', 'postalveolar', 'labiodental', 'interdental'))
# No need to tell R to do treatment coding, it will do it by default. We can double check, though:
contrasts(poa_means$poa)
## postalveolar labiodental interdental
## alveolar 0 0 0
## postalveolar 1 0 0
## labiodental 0 1 0
## interdental 0 0 1
# Making model
fit.dummy4 <- lm(meanCOG ~ poa, poa_means)
summary(fit.dummy4)
##
## Call:
## lm(formula = meanCOG ~ poa, data = poa_means)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3250.8 -518.9 -50.0 590.7 1681.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6816.6 179.4 38.005 < 2e-16 ***
## poapostalveolar -2185.4 253.7 -8.616 7.28e-13 ***
## poalabiodental -746.5 253.7 -2.943 0.00431 **
## poainterdental -841.7 253.7 -3.318 0.00139 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 802.1 on 76 degrees of freedom
## Multiple R-squared: 0.5037, Adjusted R-squared: 0.4841
## F-statistic: 25.71 on 3 and 76 DF, p-value: 1.383e-11
This output gives us all of the pairwise comparisons between alveolar and the other places of articulation. But what about finding out if, for instance, the mean COG difference between labiodentals and interdentals is significant? In order to find that out, we’ll need to re-level our factors so that labiodental or interdental is the baseline.
poa_means$poa <- factor(poa_means$poa, levels = c('labiodental', 'interdental','alveolar', 'postalveolar'))
contrasts(poa_means$poa)
## interdental alveolar postalveolar
## labiodental 0 0 0
## interdental 1 0 0
## alveolar 0 1 0
## postalveolar 0 0 1
fit.dummy4a <- lm(meanCOG ~ poa, poa_means)
summary(fit.dummy4a)
##
## Call:
## lm(formula = meanCOG ~ poa, data = poa_means)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3250.8 -518.9 -50.0 590.7 1681.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6070.10 179.36 33.843 < 2e-16 ***
## poainterdental -95.22 253.66 -0.375 0.70842
## poaalveolar 746.50 253.66 2.943 0.00431 **
## poapostalveolar -1438.87 253.66 -5.673 2.43e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 802.1 on 76 degrees of freedom
## Multiple R-squared: 0.5037, Adjusted R-squared: 0.4841
## F-statistic: 25.71 on 3 and 76 DF, p-value: 1.383e-11
The key R code here when dealing with categorical variables is as follows:
poa_means$poa <- factor(poa_means$poa, levels = c('interdental','alveolar', 'postalveolar','labiodental'))
contrasts(poa_means$poa) <- contr.sum(4)
contrasts(poa_means$poa)
## [,1] [,2] [,3]
## interdental 1 0 0
## alveolar 0 1 0
## postalveolar 0 0 1
## labiodental -1 -1 -1
fit.sum4 <- lm(meanCOG ~ poa, poa_means)
summary(fit.sum4)
##
## Call:
## lm(formula = meanCOG ~ poa, data = poa_means)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3250.8 -518.9 -50.0 590.7 1681.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5873.20 89.68 65.490 < 2e-16 ***
## poa1 101.68 155.33 0.655 0.515
## poa2 943.40 155.33 6.073 4.60e-08 ***
## poa3 -1241.97 155.33 -7.996 1.12e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 802.1 on 76 degrees of freedom
## Multiple R-squared: 0.5037, Adjusted R-squared: 0.4841
## F-statistic: 25.71 on 3 and 76 DF, p-value: 1.383e-11
The interpretation of a multi-level sum-coded factor is complicated, though. What we’re seeing here on each poa1, poa2, poa3 is whether each of those poa mean COGs (except for the held-out level) is significantly different from the mean of the interdental, postalveolar, labiodental, and alveolar means. In this case, if you went back and re-leveled poa such that a different poa was held out, you’d see that while postalveolar COG is significantly lower than the mean of all four mean poa COGs and alveolar COG is significantly higher than that mean of four means, that is not true of the interdental or labiodental COGs. For the purposes of this data, this doesn’t seem like a very useful way of considering the data, though; we’re probably more interested in the actual pairwise differences that treatment coding gives us.
However, as it may only become clear later on, sum coding is sometimes more useful than in this particular case. For instance, when you deal with interactions between multiple predictors (not just the single predictor we’re working with here), sum coding is often a good idea.