Customizing Marginal Effects Plots in R using the `margins` Package

Introduction to Margins Plotting in R

=====================================================

In this article, we will delve into the world of marginal effects plotting using the margins package in R. Specifically, we will explore how to customize the plot by choosing which explanatory variables to include. We’ll start with a general overview of marginal effects and then move on to the specifics of creating plots.

What are Marginal Effects?


Marginal effects refer to the change in the dependent variable (response) resulting from a one-unit change in an independent variable (predictor). In other words, they represent the effect size of each predictor on the response. Marginal effects can be used to estimate the causal impact of a specific predictor on the outcome.

The margins Package


The margins package is designed specifically for plotting marginal effects in R. It provides an easy-to-use interface for estimating and visualizing these effects. However, like any package, it has its limitations. In this article, we’ll focus on one of those limitations: customizing the plot.

The Challenge


When creating a margins plot, you can choose to include all explanatory variables or select specific ones using the variables option. However, when trying to use the which argument, which takes a character vector, the documentation doesn’t provide clear guidance on how to specify it.

The Solution


To answer the question posed in the Stack Overflow post, we need to understand that the which argument is used to specify the variables of interest. However, instead of using a character vector, you should use the variables option within the margins() function.

Using the variables Option


To create a plot with only specific explanatory variables, you can pass them as an argument to the variables option:

library(margins)
mod <- glm(am ~ cyl + hp + wt + mpg, family = binomial, data = mtcars)
marg <- margins(mod, variables = c("cyl", "hp"))
plot(marg)

This will create a plot that only includes the marginal effects of cyl and hp.

Using the which Argument


Unfortunately, there is no built-in way to use the which argument directly with the margins() function. However, as we’ll see in the next section, this doesn’t mean you can’t customize your plot.

A Workaround: Creating a Custom Plot


If you still want to include the which argument in your code, you can use the subset() function to create a character vector of variables:

library(margins)
mod <- glm(am ~ cyl + hp + wt + mpg, family = binomial, data = mtcars)
varnames <- c("cyl", "hp")
marg <- margins(mod, which = varnames)
plot(marg)

However, as you can see, this requires a bit more effort and might not be the most elegant solution.

Conclusion


In conclusion, while the margins package provides an easy-to-use interface for plotting marginal effects, it also has its limitations. By using the variables option within the margins() function, you can customize your plot to include only specific explanatory variables. This approach is more straightforward and efficient than trying to use the which argument directly.

Additional Tips and Tricks


  • Make sure to check the documentation for any package you’re using to ensure you’re using it correctly.
  • If you’re having trouble with a particular function or command, try searching online for examples or tutorials specific to that package.
  • Don’t be afraid to ask for help when you’re stuck. The R community is known for being helpful and supportive.

References


Note: This article assumes some basic knowledge of R programming and statistical concepts. If you’re new to R, we recommend starting with the official tutorial or a beginner’s book.


Last modified on 2023-11-13