My question is the following of this question I asked some hours ago. Looking at this post will help a lot understanding the question that follows.

I make a model with 1 response variable and 2 explanatory variables, one of them being a factor.

In my model, the response variable is transformed. I'd like to display on a graph my variables but I want the explanatory variable not to be transformed. Moreover I'd like to add the predicted line given by my model which for this purpose should be back-transformed! And to add one more slight difficulty I'd like to do it on a ggplot.

My question is how can I extend @Roland's solution to ggplot and to several explanatory variables?

Here is an example:

set.seed(12)

resp = (rnorm(120)+20)^3.79
expl1 = rep(c(1,2,3,4),30)
expl2 = rep(1:3,40)
df = data.frame(resp=resp,expl1=expl1,expl2=expl2)

m=lm(resp~expl1*factor(expl2), data=df)
ggplot(data=df,aes(y=resp,x=expl1,shape=factor(expl2)))+geom_point() + geom_smooth(se=F)

Instead of the lines that are displayed I'd like to have the predicted values of my model after back-transformation. I could add method='lm', formula =resp~expl1*factor(expl2) in geom_smooth but whether I transform or not respI will the same problem. Either the line does not fit because transformed, or it doesn't correspond to my model.

Hope my question makes sense! Thanks for your help!

有帮助吗?

解决方案

You can use predict with more than one explanatory variable much like how it was used in the answer to your previous question. Then you simply need to back-transform your predictions to the original scale. The predictions are in a different data.frame, so if I use ggplot I usually map the plot with the original data.frame and add the points, then draw the lines using the data.frame of predictions.

# A transformed response
df$resp2 = df$resp^(1/3.79)

m2 = lm(resp2 ~ expl1 * expl2, data=df)

plotexpl = seq(1, 4, by = .1)
# Use expand grid to make dataset for predictions
newdat = expand.grid(expl1 = plotexpl, expl2 = c("1", "2", "3"))
newdat$pred = predict(m2, newdata = newdat)

# Back-transform predictions
newdat$back = newdat$pred^3.79

require(ggplot2)
ggplot(df, aes(x = expl1, y = resp, color = expl2)) +
    geom_point() + geom_line(data = newdat, aes(y = back) )

其他提示

If you are willing to live with a base graphics solution instead of ggplot2 then the Predict.Plot function in the TeachingDemos package will plot predicted relationships between one of the predictors and the response at values of the other predictors and combine these plots to show multiple conditionings. To get the proper transformation/back transformation of the response it may be better to use glm with a link function rather than lm.

If you need a ggplot2 solution then you could still use Predict.Plot as a starting point and adapt it to ggplot2.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top