2
$\begingroup$

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")
df <- read.csv('data.csv')

for (fam in c("gauss", "gamma")) {
# fit models
if (fam == "gauss") {
    glm_nc <- glm('y~x',
                  data=df,
                  family=gaussian(link='log'))
    glm <- glm('y~x+c1+c2',
                 data=df,
                 family=gaussian(link='log'))
}
if (fam == "gamma") {
    glm_nc <- glm('y~x',
                      data=df,
                      family=Gamma(link='log'))
    glm <- glm('y~x+c1+c2',
                   data=df,
                   family=Gamma(link='log'))
}
# 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).

Gauss

Gamma

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?
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – whuber Aug 27 '19 at 14:32
  • $\begingroup$ I called them lines because they are line graphs, not because they are linear. $\endgroup$ – E. Sommer Aug 27 '19 at 14:33
  • $\begingroup$ 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. $\endgroup$ – whuber Aug 27 '19 at 14:53
  • $\begingroup$ I guess you are right that part of my problem is I extrapolate something from OLS which does not hold for GLM. $\endgroup$ – E. Sommer Aug 27 '19 at 14:57
  • $\begingroup$ That's part of it, but it's an interesting question whose resolution goes deeper than that: the covariates play a role, too. $\endgroup$ – whuber Aug 27 '19 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.