Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot the results of a multivariate logistic regression model in R

Tags:

plot

r

regression

I would like to plot the results of a multivariate logistic regression analysis (GLM) for a specific independent variables adjusted (i.e. independent of the confounders included in the model) relationship with the outcome (binary).

I have seen posts that recommend the following method using the predict command followed by curve, here's an example;

x     <- data.frame(binary.outcome, cont.exposure)
model <- glm(binary.outcome ~ cont.exposure, family=binomial, data=x)
plot(cont.exposure, binary.outcome, xlab="Temperature",ylab="Probability of Response") 
curve(predict(model, data.frame(cont.exposure=x), type="resp"), add=TRUE, col="red")

However this does not seem to work for multivariate regression models. I get the following error when I add 'age' (arbitrary - could be any variable of same length) as a confounding variable;

> x     <- data.frame(binary.outcome, cont.exposure, age)
> model <- glm(binary.outcome ~ cont.exposure + age, family=binomial, data=x)
> plot(cont.exposure, binary.outcome, xlab="Temperature",ylab="Probability of Response") 
> curve(predict(model, data.frame(cont.exposure=x), type="resp"), add=TRUE, col="red")
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  variable lengths differ (found for 'age')
In addition: Warning message:
  'newdata' had 101 rows but variable(s) found have 698 rows 

The above model is a simplified version of the models I'd like to run, but the principle is the same; I would like to plot the relationship between a binary outcome variable and a continuous exposure, independent of confounding factors..

It would be great to get either a workaround for the above, or an alternative way to view the relationship I am interested in. Many thanks.

like image 829
Luke Avatar asked Jul 02 '12 10:07

Luke


People also ask

How do you plot a logistic regression graph in R?

To plot the logistic regression curve in base R, we first fit the variables in a logistic regression model by using the glm() function. The glm() function is used to fit generalized linear models, specified by giving a symbolic description of the linear predictor.

How do you visualize a multivariable regression?

9.3. The best way to visualize multiple linear regression is to create a visualization for each independent variable while holding the other independent variables constant. Doing this allows us to see how each relationship between the DV and IV looks.

How do you visualize logistic regression results?

To visualize the logistic regression fit, we first use the predict function to generate the model predictions about probability of survival as a function of age. Having generated the predicted probabilities of survival we can then add these prediction lines to our previous plot using geom_line .

How do you display multiple regression results?

Still, in presenting the results for any multiple regression equation, it should always be clear from the table: (1) what the dependent variable is; (2) what the independent variables are; (3) the values of the partial slope coefficients (either unstandardized, standardized, or both); and (4) the details of any test of ...


1 Answers

set.seed(12345)
dataset <- expand.grid(Temp = rnorm(30), Age = runif(10))
dataset$Truth <- with(dataset, plogis(2 * Temp - 3 * Age))
dataset$Sample <- rbinom(nrow(dataset), size = 1, prob = dataset$Truth)
model <- glm(Sample ~ Temp + Age, data = dataset, family = binomial)
newdata <- expand.grid(
  Temp = pretty(dataset$Temp, 20), 
  Age = pretty(dataset$Age, 5))
newdata$Sample <- predict(model, newdata = newdata, type = "response")
library(ggplot2)
ggplot(newdata, aes(x = Temp, y = Sample)) + geom_line() + facet_wrap(~Age)

enter image description here

ggplot(newdata, aes(x = Temp, y = Sample, colour = Age, group = Age)) + 
  geom_line()

enter image description here

like image 58
Thierry Avatar answered Sep 23 '22 12:09

Thierry