# R predictive plot with cplot and GLM

I am using the cplot() command from the margins package to analyze predictive outcomes across different model specifications while coming across two issues. Below is my code:

library("margins")
library("ggplot2")
library("data.table")

for (fam in c("gauss", "gamma")) {
# fit models
if (fam == "gauss") {
glm_nc <- glm('y~x',
data=df,
glm <- glm('y~x+c1+c2',
data=df,
}
if (fam == "gamma") {
glm_nc <- glm('y~x',
data=df,
glm <- glm('y~x+c1+c2',
data=df,
}
# 2. plot predicted values
p1 <- cplot(glm, x="x", data=df, what="prediction")
p2 <- cplot(glm_nc, "x", data=df, what="prediction")
# 3. combine graphs
plotdata <- p1[,c('xvals', 'yvals')]
plotdata$$p2y <- p2$$yvals
ggplot(plotdata ,aes(x=xvals)) +
geom_line(aes(y=yvals), color='darkred')+
geom_line(aes(y=p2y), color="steelblue")+
theme_bw()

ggsave(paste0(fam, '.png'))
}


I compare the outcome of a bivariate regression $$y = \alpha + \beta x + \varepsilon$$ with a specification two control variables: $$y =\alpha + \beta x + \gamma_1 c_1 + \gamma_2 c_2 + \epsilon$$. In addition, I try two GLM specifications, with guassian and Gamma function, respectively (So my equations above are not correct regarding the functional form).

Note that $$y$$, $$x$$, $$c_1$$ and $$c_2$$ are continuous.

I have two questions on the resulting graphs.

1. For the Gaussian Model, the two linesintersect at x = 400, roughly. Why don't they intersect at the mean of x, which is around 170? As a sidenote, this is what happens if I estimate lm('y~x', data=df).
2. Why do the lines of predicted values for the Gamma model never intersect?
• It is curious that you characterize these fitted functions as "lines" because they are not: they are exponential curves, as you have explicitly requested in your software. – whuber Aug 27 '19 at 14:32
• I called them lines because they are line graphs, not because they are linear. – E. Sommer Aug 27 '19 at 14:33
• Understood--except that your question appears to be grounded in a result about OLS that asserts the true lines that it fits must pass through the point of averages. That's why the meaning of "line" in your post is worth exploring. – whuber Aug 27 '19 at 14:53
• I guess you are right that part of my problem is I extrapolate something from OLS which does not hold for GLM. – E. Sommer Aug 27 '19 at 14:57
• That's part of it, but it's an interesting question whose resolution goes deeper than that: the covariates play a role, too. – whuber Aug 27 '19 at 15:03