4
$\begingroup$

How to plot logistic models with many categorical variables?

Specifically,

I'm creating the following kind of model (more variables are still to be added):

glm(cancer ~ trt + factor(exposure) + skin + 
gender + factor(age), family = binomial, data = dta)

which is about modelling how different variables affect the risk of getting skin cancer.

One can see that it would be interesting to plot along both age (range: integers [28, 84]) and exposure (range: integers [1, 21]), however plotting against two variables doesn't seem to be possible in a typical y-x setting so plotting against two variables would either be a 3D plot or is there perhaps some other way?

$\endgroup$
5
  • $\begingroup$ Does (28:84) mean the amount of data points in two groups? Also you can plot more dimensions/groups by putting age on the x-axis and scaling or colouring the datapoints based on exposure. $\endgroup$
    – JAD
    Commented Oct 20, 2016 at 14:24
  • $\begingroup$ @JarkoDubbeldam Yeah I was thinking of using different colors for different factor levels of the second, non x-axis variable, but I haven't figured out how to do it in R. $\endgroup$
    – mavavilj
    Commented Oct 20, 2016 at 14:28
  • $\begingroup$ plot(y ~ x, col = "that third factor", type = 'p') should work if the third factor is a factor. Alternatively you could look at the ggplot2 package. $\endgroup$
    – JAD
    Commented Oct 20, 2016 at 14:30
  • $\begingroup$ @JarkoDubbeldam How can I get the colors as legends? $\endgroup$
    – mavavilj
    Commented Oct 20, 2016 at 15:25
  • $\begingroup$ Look at the effects package in R. It was made to do just this. $\endgroup$
    – Noah
    Commented Oct 6, 2017 at 14:05

2 Answers 2

2
$\begingroup$

Visualizing a logistic model with multiple continuous variables is considerably more complicated, but it becomes much simpler if all variables are categorical. When the X-variables are categorical, logistic regression is just fitting the proportion of 'successes' within each combination of categories. The standard way to plot proportions within a series of categories is to use a spineplot or a mosaicplot (cf., here).

If you have a larger number of variables, you could form a plot matrix of spineplots. The issue with plot matrices is that each is a marginal projection (cf., here). Another possibility is to form conditioning plots. I don't have access to your cancer dta dataset; below is a quick illustration with the Titanic dataset. If you wanted these for publication, you would want to do some extra work to make them 'pretty' (clean up the axes, etc.), but this should give you the idea.

data(Titanic)
d = as.data.frame(Titanic)
d = d[rep.int(row.names(d), times=d$Freq), 1:4]
d$Survived = factor(d$Survived, levels=c("Yes","No"))

pan.fun = function(x, y, ...){
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(usr[1:2], 0, 1.5), new=T )
    spineplot(as.factor(x), as.factor(y), main="", xlab="", ylab="", axes=F)
}
windows()
  pairs(d[,c(4,1:3)], panel=pan.fun)

spineplot matrix

windows()
  coplot(Survived~Sex|Class*Age, d, panel=pan.fun)

conditioning spineplot

$\endgroup$
0
$\begingroup$

Like I mentioned in the comments: you can plot more dimensions/groups by putting age on the x-axis and scaling or colouring the datapoints based on exposure.

An option to make fancy plots in R is using the ggplot2 package:

library(ggplot2)

data = data.frame(y = rnorm(100)+(1:100), x = 1:100, z = 100:1, w = runif(100))

p <- ggplot(data = data, aes(x = x, y = y, color = z, size = w))
p + geom_point()

As you can see, in the function aes you can specify the variable to be used as measure for the size or color. Surely there are many many more options. ggplot2 is a huge graphical package that has almost endless possiblities.

It works using the ggplot() function specifying the data to be plotted, and you can then add features to the plot using functions like geom_point().

Edit Since your response variable is binary, it might not really be interesting to plot it on one of your axes. Alternatively you could plot two continuous variables on the axes and plot the colour of the points as the response variable.


A bit off-topic, but is it a concious choice to specify age as a factor instead of just a continuous variable? In the current setting you are fitting 55 parameters just for age, which seems a bit over the top.

$\endgroup$
3
  • $\begingroup$ I might change it to continuous variable, but I took factor because I was unsure whether the age variable could display some special age groups or whether it behaves "linearly" (more age, more risk). $\endgroup$
    – mavavilj
    Commented Oct 20, 2016 at 15:36
  • $\begingroup$ That it a fair point, but if that is the case, you could also consider wider bins: 20-40, 40-60, 60+ for example. $\endgroup$
    – JAD
    Commented Oct 20, 2016 at 15:37
  • $\begingroup$ Or, maybe better, use a spline for age $\endgroup$ Commented Oct 6, 2017 at 15:23

Your Answer

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

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