Visualizing marginal effects using ggeffects in R | by Matthieu Renard | Towards Data Science

It’s a known dilemma: You know that your variable X1 impacts your variable Y, and you can show it in a regression analysis, but it is hard to show it graphically. The reason is that there is another variable impacting Y, namely X2, and only after the effect of X2 is taken into account does the effect of X1 on Y appear. So you would need a way of showing how X1 impacts Y while taking the effect of X2 into account.
This is where marginal effects come in handy. They enable you to show how a change of the independent variable of interest impacts your dependent variable while taking all other independent variables into account. More specifically, you could use the package ggeffects to visualize the marginal effects of key variables.
The following tutorial will use the salaries dataset available in base R. It contains data on professors' salaries and several other variables that would intuitively influence their salary.
You can load it by simply entering this command.
df=Salaries
We want to answer the key question in this dataset: Does a professor’s sex influence their salary?
Naively, one could try to answer this question by simply running the below regression model:
fit1=lm(salary~sex,df)summary(fit1)
Just looking at the output, it seems that sex influences a professor’s salary. Also, when plotting the impact of sex on salary, the impact seems to be significant.
#install and load ggplotinstall.packages("ggplot2")library(ggplot2)#plot the relationshipggplot(df,aes(y=salary, x=sex))+  stat_summary(fun.y="mean", geom="bar",position="dodge")+  stat_summary(fun.data = mean_se, geom = "errorbar", position="dodge",width=.8)+  labs(    x="Sex",    y="Salary",    title="The Effect of Sex on Salary"  )
However, the question is: Is it sex that is driving this difference, or is sex correlated to a more immediate predictor of salary? This question is easy to answer in a regression. One adds more covariates to check if it really is sex driving the difference or something else.
fit2=lm(salary~sex+yrs.since.phd+yrs.service+rank+discipline,df)summary(fit2)
As one can see, the marginal effect of sex now is insignificant, implying that sex isn’t an actual predictor of salary in this dataset, but rather that it is correlated with one of the other variables in the dataset, which can be summarized as indicators of seniority and the academic discipline.
So we know that sex is not a significant predictor of salary. But if we were to plot the relationship between sex and salary — as in the above plot — it would look as if sex were an actual salary predictor. What if we wanted to plot the actual effect of sex on a salary after taking all other covariates into account — i.e., plot the marginal effect? This is where ggeffects comes into play.
So, we install ggeffects and load it, if we haven’t done so already.
install.packages("ggeffects")library(ggeffects)
Then we use the ggpredict function from the ggeffects package and predict the marginal effect for each sex in the dataset. We save the output, a tidy data frame, under the name dummy.
dummy=ggpredict(fit2, terms = "sex")
Then, we use ggplot to plot these marginal effects.
ggplot(dummy, aes(x=x, y=predicted)) +  geom_bar(stat = "identity", position = position_dodge()) +  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), alpha = .9,position = position_dodge())+  labs(    x="Sex",    y="Salary",    title="The Marginal Effect of Sex on Salary"  )+scale_x_continuous(breaks=c(1,2) ,labels = c("Female", "Male"))
We can now plot the actual marginal effect of sex on a salary after controlling for all other predictors.
Found this story interesting? Follow me on Medium to see my other stories!