1
$\begingroup$

I'm working on a logistic regression analysis using R and aiming to visualize the effects of the predictor "age" on the binary dependent variable "domestic violence." My dataset includes multiple predictors, and I want to plot the change in odds ratio for experiencing domestic violence as the age of women increases, while keeping all other predictors constant.

Here's a brief overview of my approach:

I've fitted a logistic regression model using the glm function:

model <- glm(violence ~ ., data = df, family = "binomial")

Now, I would like to create a plot that shows the effect of age on the odds ratio of experiencing domestic violence that is how a unit increase in the age changes the odds of a woman experiencing violence while keeping other variables constant. I've read about the margins package and how it can be used to calculate marginal effects and plot them. However, I'm struggling with the exact steps to achieve this. Could someone guide me through the process of using the margins package or any other package which I can use here to calculate and plot the desired effects?

Any advice on customizing the plot appearance and incorporating confidence intervals would also be greatly appreciated.

$\endgroup$
5
  • 3
    $\begingroup$ Hi cd21, you'll get more help if you post an example dataset. Otherwise, they have to make something up - which is a lot of work for them, and might not look like what you want. You can google how to create datasets if you can't post enough of your own for an example. $\endgroup$
    – Nova
    Aug 18, 2023 at 17:13
  • 1
    $\begingroup$ did you look thru the vignette of that package? cran.r-project.org/web/packages/margins/vignettes/… $\endgroup$
    – rawr
    Aug 18, 2023 at 17:15
  • 1
    $\begingroup$ The ggEffects package is great for plotting model output. Here's an example for logistic regression: strengejacke.github.io/ggeffects/articles/… . I can't say I would find it useful to plot odds ratios, though - it's much easier to interpret predicted probabilities. $\endgroup$
    – mkt
    Aug 18, 2023 at 19:39
  • $\begingroup$ This is a place for partial effects plots and nomograms. See this. Be sure not to assume linearity in covariate effects needlessly. $\endgroup$ Aug 18, 2023 at 21:58
  • $\begingroup$ The name of the famous software is R not r. I did an edit! $\endgroup$ Aug 18, 2023 at 22:35

1 Answer 1

1
$\begingroup$

You're using a lot of related terms in this question (odds, log odds, odds ratio) that would all require slightly different answers, I'll try and answer the ones I can think of a decent answer for.

Since you didn't specify a link function in the glm statement, you're using the standard logit link, or equivalently the expit response function. This means that the probability of experiencing domestic violence is modeled with this function (using 2 predictors for simplicity).

$$\pi(x) = \frac{1}{e^{-( \beta_0 + \beta_1x_1 + \beta_2x_2)}} $$

Say $x_1$ is age, then all you have to do is plug in your estimated betas and plot this as a function of $x_1$ to get a partial plot of age vs. probability of domestic violence. For example with the curve() command in R you can directly plot functions. Or with geom_function() in ggplot2.

You may notice that there is another unkown, namely $x_2$. You will have to choose at which level to keep the other predictors constant. Typically the mean is a good choice. As long as your model doesn't contain interactions with $x_1$ it won't change the effect.

Then to plot the log-odds, we can take the logit of both sides of the equation to get (because the logit is the inverse of the expit)

$$\ln\left(\frac{\pi(x)}{1 - \pi(x)}\right) = \beta_0 + \beta_1x_1 + \beta_2x_2$$

Now the left-hand side is the log odds, so you can just plot the expression on the right in terms of $x_1$ to get a partial plot of the log odds vs. age, again setting the other variables at their mean. As is customary in logistic regression, the log odds are actually a linear function.

Lastly, for the odds we can take the exponential of both sides to get

$$\frac{\pi(x)}{1 - \pi(x)} = e^{\beta_0 + \beta_1x_1 + \beta_2x_2} $$

Now the left side are the odds, so you can plot the right-hand side as a function of $x_1$ to get a partial plot of the odds vs. age.

The odds ratio and log odds ratio are actually constant, they are $\beta_1$ and $e^{\beta_1}$ respectively, they're just obtained by dividing the odds/log odds at x+1 by those evaluated at x.

In terms of confidence intervals I think you can just calculate it on the logit of $\pi(x)$ since there it is essentially like a linear model, and then backtransform the lower and upper bound with an expit to get a confidence interval on $\pi(x)$ itself.

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.