0

I want to make 3D plots for linear Regression Models in R: I wish to display surface of the regression plane of a linear model. I have 2 continuous variables (say AGE, HEIGHT) and 2 factors (SEX, ALLERGIC). I want to display the predicted values of the LM w.r.t. the 2 continuous variables conditioned on the specified levels of each factor, e.g.

 ILLNESS = AGE|{SEX==MALE + ALLERGIC==YES} + HEIGHT|{SEX==MALE + ALLERGIC==YES} +
           AGE|{SEX==MALE + ALLERGIC==YES}*HEIGHT|{SEX==MALE + ALLERGIC==YES}

This is the outcome I have in mind:

enter image description here

First Question: Are there any cool function, where you can do this very easy?
Second Question: If not, how can I write formulas, where I can condition on >1 factor level?

Novice
  • 307
  • 2
  • 11
  • It would be helpful to post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so it's easier for us to provide you with a tested solution. – MrFlick Apr 16 '15 at 17:42

1 Answers1

1

First, let's make some sample input data to have something to test with.

set.seed(15)
dd <- data.frame(
    sex = sample(c("M","F"), 200, replace=T),
    allergic = sample(c("YES","NO"), 200, replace=T),
    age = runif(200, 18,65),
    height = rnorm(200, 6, 2)
)
expit <- function(x) exp(x)/(exp(x)+1)
dd <- transform(dd, 
    illness=expit(-1+(sex=="M")*.8-0.025*age*ifelse(sex=="M",-1,1)+.16*height*ifelse(allergic=="YES",-1,1)+rnorm(200))>.5
)

Now we define the set of values we want to predict over

gg<-expand.grid(sex=c("M","F"), allergic=c("YES","NO"))
vv<-expand.grid(age=18:65, height=3:9)

and then we fit a model, and use the predict function to calculate the response for each point on the surface we wish to plot.

mm <- glm(illness~sex+allergic+age+height, dd, family=binomial)
pd<-do.call(rbind, Map(function(sex, allergic) {
   nd <- cbind(vv, sex=sex, allergic=allergic)
   cbind(nd, pred=predict(mm, nd, type="response"))
}, sex=gg$sex, allergic=gg$allergic))

Finally, we can use lattice to plot the data

library(lattice)
wireframe(pred~age+height|sex+allergic, pd, drape=TRUE)

which give us

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This is absolutely awesome! I never thought, this would be so easy! Thank you! I have 3 questions to that: a) What is the object *mm* in your prediction function? b) I want to have the plane described by Y~X1_cont+X2_cont+X3_factor+ X1_cont*X2_cont+X1_cont*X3_factor+....+X2_factor*X3_factor but condition on all the factors in the way you have done. Do I get the same plane with your method? c) What if I have a big number of levels in each factor, is there a way to display only the most important factors? – Novice Apr 16 '15 at 21:22
  • `mm` was was the model which i accidentally left out. I've added it in. `predict()` will model all the interactions in your model so if that's what you're asking you should be fine. If you only want to plot a subset of factors, you'll have figure out your own want to subset to the "most important." You'd basically just adjust what you include in the `gg` data.frame of factors levels you want to condition on. – MrFlick Apr 16 '15 at 21:29