I am currently learning how to do statistical modelling in R and realised that if the response variable is dichotomous, it is better to go for logistic modelling as compared to linear modelling.
I used the run17
dataset from the cherryblossom
package in R. The run17
dataset contains details for all 19,961 runners in the 2017 Cherry Blossom Run, which is an annual road race that takes place in Washington, DC. The dataset has an event
column which denotes whether the participant ran in the "10 Mile" event or the "5 Km" event. I was interested to see if participants' event choice selection was affected by their age
and sex
. Since the event
variable is dichotomous, I first converted it to boolean values (1 = 10 Mile event and 0 = 5 Km event) for it to work with the formula.
As the response variable is dichotomous, I used logistic modelling. But at the same, I also tried linear modelling to see what is difference between them. Given below is the code I used.
# Loading the required packages
if (!require(cherryblossom)) install.packages('cherryblossom')
library(cherryblossom)
library(dplyr)
# Converting event variable values into boolean
run17_boolean <- run17
run17_boolean$event <- recode(run17_boolean$event, "10 Mile" = 1, "5K" = 0)
# Building the linear model
model_lm <- lm(event ~ sex + age, data = run17_boolean)
# Building the logistic model
model_glm <- glm(event ~ sex + age, data = run17_boolean, family = "binomial")
# Summary of the model
summary(model_lm)
summary(model_glm)
I find that the summary of the models is almost the same. I am still learning how to interpret the model summaries so I may be wrong here. I then tried to plot the model using ggplot2
to see if they are visually similar.
library(ggplot2)
# Linear model
run17_boolean %>% ggplot(aes(age, event, col = sex)) +
stat_smooth(method = "lm") +
labs(x = "Age",
y = "Event",
title = "Event ~ Age + Sex (linear model)") +
theme_bw()
# Logistic model
run17_boolean %>% ggplot(aes(age, event, col = sex)) +
stat_smooth(method = "glm", method.args = list(family = "binomial")) +
labs(x = "Age",
y = "Event",
title = "Event ~ Age + Sex (logistic model)") +
theme_bw()
The graphs are also very similar as the fitted values are very similar between the plots.
My question is why when linear modelling, which works best for continuous data gave very close results to logistic modelling even when I had a dichotomous response variable?
Or is it just a chance that I got very similar results when
lm()
andglm()
was used for this particular data and would not work for other datasets?