I want to run the following model: Weight ~ Height*Sex, where * sign means interaction. I got the following result:

modell <- lm(df$weight ~ df$height*df$SEX)
# ...
# Coefficients:
#                        Estimate  Std. Error t value   Pr(>|t|)
# (Intercept)             29.5514    43.1282   0.685    0.495
# df$height               0.2996     0.2408    1.244    0.217
# df$SEXfemale            7.0516     61.6167   0.114    0.909
# df$height:df$SEXfemale -0.1176     0.3594   -0.327    0.744
# Residual standard error: 11.79 on 96 degrees of freedom
# Multiple R-squared:  0.3452,  Adjusted R-squared:  0.3248 
# F-statistic: 16.87 on 3 and 96 DF,  p-value: 7.015e-09

As you can see, I got only df_SEXfemale and df_height:df_SEXfemale. But coefficients with df_SEXmale are absent (I suppose because they are interpreted as number 0). And df$SEX is a factor variable with 2 levels (male and female).

So my questions are:

  1. How can I correct this situation?
  2. How can I plot regression lines for both groups separately (female and male) without using the ggplot2 package?
  • $\begingroup$ 1) use lm(weight ~ height*SEX, data = df) 2) read more about binary dependent variable regressions $\endgroup$ – MichaelChirico Jan 4 '17 at 19:14
  • 2
    $\begingroup$ Please review some posts on dummy variable coding for an answer to your first question. (They will show there is nothing to correct.) The second question is off topic. $\endgroup$ – whuber Jan 4 '17 at 19:21
  • 1
    $\begingroup$ Also, read about the contrasts argument in lm. $\endgroup$ – Firebug Jan 4 '17 at 19:29

You have this model:

$$\text{Weight}=\beta_0+\beta_1\text{Height}+\beta_2\text{Sex}+ \beta_3\text{Height}\cdot\text{Sex}$$

In your case, $\text{Sex(Male)} = 0$ and $\text{Sex(Female)} = 1$

Substituting we get two equations:



So there you have it, $\text{Male}$ is the baseline which the coefficient corresponds, for $\text{Female}$ you need to sum the other coefficients.

Look at this example and how the contrasts argument change the fit

data = iris[1:100,3:5]
data$Species = factor(data$Species)
fit1 = lm(Petal.Length ~ Petal.Width * Species, data = data, contrasts = list(Species = c(1,0)))
fit2 = update(fit1, contrasts = list(Species = c(0,1)))

Basically, you can take the intercept and slope of each fit as the regression line equation for each group in Species.

By default, one element of contrasts will be 0, i.e. its corresponding level will be the baseline as I explained. But that doesn't need to be the case, you could specify contrasts = list(someFactor = c(-1,1)), and the baseline would be an intermediate state. To get the regression lines of each level in someFactor you would need to respectively subtract and sum coefficients.


Since you have only two levels of a dummy variable, one level will be taken as the "default" (men in this case) and the other as the "effect". So basically what you got here is a regression line for men (intercept + df$height estimate) and another line for females (using all estimators). I know it seems a bit weird, but there is nothing to correct. As I understand it you got what you wanted.

Now, for plotting this without using ggpot2 (why? ggplot2 is great!), I haven't used the basic plot function for a while, so there might be a better way to do this, but you could use the abline() function. It takes two coordinates and plots a strait line between them. So you can use the models minimal and maximal predictions (using predict function) for males and females separately and ad two lines to your plot.


There is nothing to "correct" in this situation. You just need to understand how to interpret your output.

Your model is:

$$ W = \beta_0 + \beta_1 H + \beta_2 F + \beta_3 (H \times F) + \varepsilon \hspace{1em} \text{with} \hspace{1em} \varepsilon \sim \text{iid}\ N(0,\sigma^2) $$

where $H$ is a continuous variable for height and $F$ is a binary variable equal to 1 for female and 0 for male.

You have estimated the coefficients to find:

$$ \hat{W} = 29.55 + 0.30H + 7.05F - 0.12(H \times F) $$

Specifically, for males $F = 0$ and the fitted regression line is:

$$ \hat{W} = 29.55 + 0.30H $$

For females, $F = 1$ and the fitted regression line is:

$$ \hat{W} = (29.55 + 7.05) + (0.30 - 0.12)H $$

In general, whenever you include a binary or categorical variable in a regression model that has an intercept, one level of that variable must be omitted and treated as the baseline. Here, "male" is that baseline.



To the best of my knowledge because SEX variable is binary Df$height already incorporates that interaction.

If you want to build plot two regression lines then for male line you have (Intercept) as intercept, height as slope; and for female line(Intercept) + SEXfemale as intercept, and height + height:SEXfemale as slope.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.