Today

  • Use lm() with two-group and three-group categorical predictors.
  • Interpret the reference group and dummy-coded coefficients.
  • Count dummy coefficients, beta coefficients, and total model parameters.
  • Calculate group means from coefficients, then use predict() as a check.
  • Read a coefficient t-test and an overall model F-test.

Complete the online lecture before the workshop and the Week 7 problem set afterwards.

From Continuous To Categorical Predictors

In Weeks 5–6, each continuous predictor entered as a single linear term added one slope coefficient. A categorical predictor can add more than one coefficient:

Predictor Coefficients added, excluding the intercept
Continuous age 1 slope
Medication group: 2 levels 1 group difference
Smoking group: 3 levels 2 group differences

One categorical predictor with G levels adds G − 1 coefficients. We still fit one intercept as well.

These are general linear models, whether predictors are continuous, categorical, or both. Generalised linear models extend this framework; that is a different term.

Population Means In A Group Model

The lecture represents two population means using one regression equation:

\[ \mu_i = \beta_0 + \beta_1 X_i. \]

  • For the reference group, \(X=0\): its mean is \(\beta_0\).
  • For the other group, \(X=1\): its mean is \(\beta_0+\beta_1\).
  • Therefore, \(\beta_1\) is the other group’s mean minus the reference mean.

lm() estimates these population parameters from our sample. The model assumes a common residual SD, sigma, around the group means.

Start In RStudio

Open your module’s RStudio Project and check that workshop_data is inside it.

  1. Download week_07_workshop.R from NOW.
  2. Move it into your project folder using your file explorer.
  3. Open it in RStudio.

Wait for each exercise checkpoint. Run one line at a time and keep your coefficient output visible for the worked examples.

Demonstration Data

blomkvist <- read_csv("workshop_data/analysis_blomkvist.csv")
blomkvist <- drop_na(blomkvist, rt, meds_group)
count(blomkvist, meds_group)

meds_group already contains “medicated” and “not medicated”. We do not need to create a new grouping variable.

Do the medication groups differ in mean hand reaction time?

The prepared data are adapted from Blomkvist et al. (2017).

Fit The Two-Group Model

m_lm <- lm(rt ~ meds_group, data = blomkvist)
m_lm
m_lm_summary <- summary(m_lm)
m_lm_summary
coef(m_lm)

The formula has the same structure as Week 5. The predictor now contains group labels rather than numeric values.

Exercise Checkpoint 1

Complete the data-loading steps and Part A1 with me.

Fit and print the model, save its summary, and print its coefficients. Stop before Part A2 and keep the coefficient output visible.

Identify which coefficient is the reference mean and which is a difference.

Read The Dummy Code

With these text labels, R orders the levels alphabetically. Under default treatment coding, the first level is the reference and the remaining level gets a binary indicator.

Medication group Not-medicated dummy Predicted mean
medicated 0 intercept
not medicated 1 intercept + group coefficient

“medicated” comes before “not medicated”, so it is coded 0; “not medicated” is coded 1.

Two levels give 2 - 1 = 1 dummy variable. In general, G levels give G - 1 dummy variables. R creates these codes inside lm(); we will read them, not change them.

Two Groups: Read The Coefficients

coef(m_lm) prints the values we need:

            (Intercept) meds_groupnot medicated 
               738.8143               -157.4875 
Coefficient Meaning Value (ms)
(Intercept) medicated mean 738.8143
meds_groupnot medicated not-medicated minus medicated mean -157.4875

Read the coefficient names to identify the reference mean and the other group’s difference.

Calculate The Two Group Means

738.814286
# [1] 738.8143
738.814286 + (-157.487488)
# [1] 581.3268
  • Medicated mean: the intercept alone.
  • Not-medicated mean: intercept + not-medicated difference.

Numerically: 738.8143 + (-157.4875) ≈ 581.3268 ms.

Check The Manual Calculation With predict()

new_meds <- tibble(
  meds_group = c("medicated", "not medicated")
)
new_meds
predict(m_lm, newdata = new_meds)

Now that we have calculated the means by hand, use predict() to check them. tibble() supplies one row per group.

predict() uses the fitted model and returns one predicted mean per row. The column name and group labels must match the model.

predict() uses the fitted model directly. For the manual calculation, read and add the printed coefficient values.

Read The Group Test

Use the coefficient row for the group difference, not the intercept row.

m_lm_summary <- summary(m_lm)
m_lm_summary

Find the meds_groupnot medicated row. Read its t value and Pr(>|t|) columns, as in Week 6.

The null hypothesis is that the population group difference is 0.

Compare With The Equal-Variance t-Test

This is a short connection to the lecture; lm() remains our main workflow.

m_ttest <- t.test(rt ~ meds_group, data = blomkvist,
                  var.equal = TRUE, mu = 0)
m_ttest$statistic
m_ttest$p.value

With the same rows and group ordering, t.test() calculates the reference mean minus the other mean; the model coefficient calculates the other mean minus the reference.

The t-values have opposite signs but the same magnitude. Their two-sided p-values agree.

What Does mu Mean In The Test?

In the regression equation, \(\mu_i\) is a population mean described by the intercept and group coefficient.

In t.test(), the argument mu specifies the hypothesised value:

  • One-sample test: a hypothesised population mean.
  • Two-sample test: a hypothesised difference between population means.

For our two-sample comparison, mu = 0 tests equal group means. It does not say that either group mean is 0.

Equal group means correspond to a group coefficient of 0, not an intercept of 0.

Exercise Checkpoint 2

Complete Part A2 with me.

Read the coefficient values for each named group, calculate its mean, check with predict(), then read the group t-statistic and p-value. Briefly compare with the provided equal-variance t-test.

Stop after Part A2.

Short Break

After the break, we will extend the same logic to three groups.

Three Groups Need Two Dummy Variables

Alphabetical order of the text levels: former smoker → non-smoker → smoker.

Group Non-smoker dummy Smoker dummy Population mean
former smoker 0 0 \(\beta_0\)
non-smoker 1 0 \(\beta_0+\beta_1\)
smoker 0 1 \(\beta_0+\beta_2\)

The first level, “former smoker”, is the reference: both its codes are 0. Three levels give 3 - 1 = 2 dummy variables. Each other level has a 1 in its own column and a 0 in the other.

R creates these codes automatically. Keep the text labels; one numeric 0, 1, 2 column would impose equally spaced predicted means.

Fit The Three-Group Model

blomkvist_smoker <- read_csv(
  "workshop_data/analysis_blomkvist.csv")
blomkvist_smoker <- drop_na(blomkvist_smoker, rt, smoker)
m_smoker <- lm(rt ~ smoker, data = blomkvist_smoker)
coef(m_smoker)

Start Part A3 with me. Use the intercept and the two differences to calculate the group means by hand.

Three Groups: Read The Coefficients

coef(m_smoker) prints the values we need:

     (Intercept) smokernon-smoker     smokersmoker 
       652.51316        -17.95079        -19.06444 
Coefficient Meaning Value (ms)
(Intercept) former-smoker mean 652.5132
smokernon-smoker non-smoker minus former-smoker mean -17.9508
smokersmoker smoker minus former-smoker mean -19.0644

Choose the difference for the named group. Do not add both differences to the intercept.

Calculate The Three Group Means

652.513158
# [1] 652.5132
652.513158 + (-17.950786)
# [1] 634.5624
652.513158 + (-19.064440)
# [1] 633.4487

These give the former-smoker, non-smoker, and smoker means, respectively.

  • Non-smoker: 652.5132 + (-17.9508) ≈ 634.5624 ms.
  • Smoker: 652.5132 + (-19.0644) ≈ 633.4487 ms.

In Part A3, choose the printed coefficient values for each named group, then continue to the overall test at Checkpoint 3.

Predictors, Coefficients, And Parameters

For rt ~ smoker, rt is the outcome and smoker is one categorical predictor with three levels.

Quantity What does it count? In this model
Predictor variables The observed input variables, here smoker 1
Beta coefficients The intercept and the two group differences 3
Model parameters All beta coefficients plus the shared residual SD, sigma 4

Here K = 2 counts the predictor coefficients, excluding the intercept. It does not count the original predictor variables. Sigma is a parameter, but not a beta coefficient or a predictor.

Count What The Model Estimates

For a group-only model with G groups, distinguish:

What are we counting? Number
Original predictor variables: one group variable 1
Dummy coefficients, K (excluding the intercept) G − 1
All beta coefficients: intercept + group differences G
All model parameters: beta coefficients + one shared sigma G + 1

The residual df is n − K − 1 = n − G. Count beta coefficients when working out residual df; do not subtract sigma as well.

Part A3: count predictors, coefficients, and parameters for numeric, categorical, and mixed examples. All examples have an intercept and one common sigma.

The Overall F-Test

m_smoker_summary <- summary(m_smoker)
m_smoker_summary$fstatistic
smoker_anova <- anova(m_smoker)
smoker_anova

With one fitted model, anova() shows its analysis-of-variance table. F value and Pr(>F) are the overall group-test statistic and p-value.

Read those values from the smoker row. Its Sum Sq is ESS; the Residuals Sum Sq is RSS. Together they make TSS.

The null hypothesis is that all three population means are equal. A coefficient t-test instead compares one group with the reference.

Choose The Test That Answers The Question

Output Null hypothesis
Intercept t-test The reference-group population mean is 0.
One group coefficient’s t-test That group’s population mean equals the reference mean.
Overall group F-test All group population means are equal.

The two group coefficients are differences from the reference; they are not a test of every possible pair of groups.

Locate the right row or overall test before reading a number. An overall F-test does not tell you which particular groups differ.

Exercise Checkpoint 3

Finish Part A3 with me.

Use your three manually calculated means, then read the overall F-statistic, its degrees of freedom, and p-value. Explain how that hypothesis differs from a coefficient test.

The two coefficient tests do not replace the overall F-test.

Independent Practice

Complete Part B with the Chinese lexical-decision data.

  • Fit rt ~ accuracy_group and calculate both means from its coefficients.
  • Read the group coefficient’s t-statistic and p-value.
  • Then fit rt ~ list_group using all four prepared list groups.
  • Calculate all four means from the intercept and group differences, then read the overall F-test.

Begin Part C if you finish Part B; otherwise complete it after the workshop. It consolidates core skills and is separate from the optional stretch script.

Discuss Your Output

  • How do coefficient names reveal the reference group?
  • Why are there three group coefficients for four list groups?
  • How does an overall F-test differ from one coefficient’s t-test?

The Week 7 problem set asks for two-group predictions and a three-group overall test. Use the same logic even when the data set and labels change.

Optional Stretch Script

Only open week_07_stretch.R if the core exercise felt comfortable. It checks the overall F-test against an intercept-only model. It is not required preparation for the exam.

After The Workshop

Recommended reading: Andrews (2021), Chapter 9, Normal Linear Models.

Complete the Week 7 problem set after this workshop. Calculate group means from the intercept and group differences; use predict() when a check is helpful. No downloaded dummy-coding function or extra prediction package is needed.

The first three questions use a two-group model. The remaining questions use three groups and ask for an overall test. Apply the same workflow to the new data and group labels.

Core R Functions

Function / syntax What it does
lm() / coef() fit a group model and extract coefficients
summary() print coefficient tests and overall model fit
tibble() / predict() specify groups and predict their means
t.test(..., var.equal = TRUE) run the matching equal-variance test
anova(model) show the overall group F-test
count() / summarise() check group sizes and means

Leave With

  • The intercept is the reference-group mean in a group-only model.
  • Each dummy coefficient is a difference from that mean.
  • A predictor with G groups needs G - 1 dummy coefficients.
  • A group-only normal model has G beta coefficients and one shared sigma.
  • Group predictions, coefficient tests, and overall tests answer different questions.

References

Andrews, M. (2021). Doing data science in R: An Introduction for Social Scientists. SAGE Publications Ltd.

Blomkvist, A. W., Eika, F., Rahbek, M. T., Eikhof, K. D., Hansen, M. D., Søndergaard, M., Ryg, J., Andersen, S., & Jørgensen, M. G. (2017). Reference data on reaction time and aging using the Nintendo Wii Balance Board: A cross-sectional study of 354 subjects from 20 to 99 years of age. PLoS One, 12(12), e0189598.