# Does including gender as a predictor variable mean I should use a glm function, not an lm function, in R?

I have been modelling a data set that contains several predictor variables, but after extensive research, I am even more confused as to whether I should be using a lm() or glm() function for the following:

Model4 <-lm(Height_cm ~ Sleep.hours + Gender + Age + Exercise, data=Data.dat.complete)


Where height is continuous, sleep.hours is continuous, gender is male/female, age is continuous and exercise is count (the number of times someone exercised in a week).

As I have a binary variable (gender), my diagnostic plots do not look too good, and I am tempted to use a glm() with family=binomial and link=logit. However, after researching for several hours, I am beginning to question this, as I have seen examples of gender being fitted using just lm() and my other predictors are clearly not binary.

If at all possible, I would like to try and work at least some of this out on my own, but if someone could please point me in the right direction (i.e. where is the flaw in my understanding), it would be very much appreciated.

Thank you.

Diagnostic plots:

• Can you please include links to some of your not-good-looking diagnostic plots? The short answer to your question is that the choice of lm vs glm depends on the response variable (Height_cm), not anything having to do with the predictor variables ... Nov 21, 2021 at 23:05
• 1. Having a binary variable is a nonissue for a linear model (and is not related in any way to the differences between lm and glm). 2. The plots look just fine to me (maybe a small amount of heteroskedasticity - perhaps - but if present it would be of little consequence). What do you think is wrong with them? Is it the gap on the x-axis in the first plot? That's just a big gender effect. Nov 21, 2021 at 23:22
• You're being badly misled by that unfortunate choice of phrasing. There can be all manner of pattern in the horizontal direction - gaps, skewness, etc. What you're looking for is whether the vertical distribution in thin vertical slices (with more than a few points in a slice) is more or less the same across each section of the x-axis. The average in each slice should stay close to 0, the spread should be more or less constant, and so on. Nov 21, 2021 at 23:28
• It would be a great idea to go back and clarify this with your statistics lecturer - ideally even have them present something about this in class, as I've seen this confusion happen many many many times ... "Reference line should be flat and horizontal", as in the plots below, is a better rubric. Nov 21, 2021 at 23:28
• Continuous variables can be negative too. DItto discrete. Nov 22, 2021 at 16:52

• The short (tl;dr) answer to your question is that the choice of linear (lm) vs generalized linear (glm) models depends on the response variable (Height_cm), not on anything having to do with the predictor variables. Since your response is continuous, you definitely should avoid the standard GLMs (Poisson, binomial/logistic) which are meant for count or proportion data.
• your diagnostic plots don't look that bad to me: no systematic variation in y as a function of x for residual-vs-fitted or scale-location plots; Q-Q plot is approximately a straight line; all residuals have Cook's distance < 0.5 (within innermost contour). . I suspect that the 'badness' you're referring to is the non-uniform distribution of the fitted values (x-axis in fitted-vs-residual and scale-location) plots. This is presumably happening because there is a big effect of gender (the only binary predictor I see in your data set); you can plot(Model4, col = as.numeric(Data.dat.complete\$Gender)) to check this.
You may find the annotated version of the diagnostics from the performance::diagnostics() function useful (I don't agree with all of the design decisions, e.g. the fifth plot showing "normality of residuals" is redundant and less revealing than the Q-Q plot, but overall it's helpful).