3
$\begingroup$

Does the predict function in R for gamma glm with log link predict the actual values or the mean value?

There is a gamma glm model in R with log link. Using predict(model,data,type = 'response') to get predictedval val in the scale of response variable, If I use use my predicted value inside qgamma function as qgamma(0.9,shape = 1/dispersion, scale = predictedval*dispersion) what will the output of qgamma signify?

$\endgroup$
8
  • $\begingroup$ Glm ''like'' regression predicts the mean value given the covariates. Selecting response in predict fuction back transforms the prediction out of the link scale (inverses the link),so the prediction is on same scale as the dependent variable. $\endgroup$ Feb 14, 2019 at 18:04
  • $\begingroup$ Once the link is inversed will the predicted value still not be the arithmetic mean and not the original y variable? $\endgroup$ Feb 14, 2019 at 18:15
  • $\begingroup$ If the transformed value is indeed an arithmetic mean then how can I know how close the distribution represents the overall population? Is out of time validation possible with gamma glm? If yes how? $\endgroup$ Feb 14, 2019 at 18:17
  • $\begingroup$ If you "overfit" the model you can return back the exact value of y, but when modeling your trying generalize and approximate. It may not be the same as the arithmatic mean because the mean is estimated using maximum likelihood and is conditional on the included dependent variables. The gamma distribution is very flexible and is in fact a series of distributions that changes shape depending on the response (variance changes with mean). The gamma distribution requires the data to be positive definite and continuous, if you fit those conditions the gamma distribution will quite often fit . $\endgroup$ Feb 14, 2019 at 18:43
  • $\begingroup$ The Pearson residuals should be normally distributed and show no signs of heteroscedacity. For more on model evaluation see: crcpress.com/… $\endgroup$ Feb 14, 2019 at 19:06

1 Answer 1

4
$\begingroup$

I was hoping someone else would expand upon my comments in a full answer, but here is mine, incase anyone needs help with a similar question.

1. Response to what does it predict

Glm ''like'' regression predicts the mean value given the independent variables. Selecting response in predict function back transforms the prediction out of the link scale (inverses the link), so the prediction is on the same scale as the dependent variable. If you "overfit" the model you can return back the exact value of y, but when modeling you're trying to generalize and approximate. It may not be the same as the arithmetic mean because the mean is estimated using maximum likelihood and is conditional on the included independent variables.

2. Response to how to "know" if the gamma distribution is right for my data

The gamma distribution is very flexible and is, in fact, a series of distributions that changes shape depending on the response (variance changes with mean). The gamma distribution requires the data to be positive definite and continuous if you fit those conditions the gamma distribution will quite often fit. Additionally, the Pearson residuals should be normally distributed and show no signs of heteroscedasticity. For more on model evaluation see: https://www.crcpress.com/Extending-the-Linear-Model-with-R-Generalized-Linear-Mixed-Effects-and/Faraway/p/book/9781498720960

3. Response to what happens when I use the qgamma function?

I am not 100% sure why the person you borrowed your sample code from is using the qgamma function. However, it looks to me like they are trying to return the 90th percent confidence limit. This is the code I use to return back 95% confidence limits on predictions.

 preds_link = predict(model, newdata = test_data,
                     type = "link",
                     se.fit = TRUE)# use the glm to make predictions, also provides  std. error
 critval <- 1.96 # critical value for approx 95% CI
 upper_ci_link <- preds_link$fit + (critval * preds_link$se.fit)# estimate upper CI for prediction on link scale
 lwr_ci_link <- preds_link$fit - (critval * preds_link$se.fit)# estimate lower CI for prediction on link scale
 fit_link <- preds_link$fit# returns fited value
 upper_ci <- model$family$linkinv(upper_ci_link)
 lwr_ci <- model$family$linkinv(lwr_ci_link)
 fit <- model$family$linkinv(fit)
 preds_link = data.frame(fit,lwr_ci,upper_ci,
                    fit_link,lwr_ci_link,
                    upper_ci_link)# puts predictions, CI in a single dataframe
 colnames(preds_link) = c("Prediction","LCL", "UCL",
                     "Link_Prediction","Link_LCL", "Link_UCL")# give variables logical names

Prediction, LCL, and UCL: Prediction and Confidence Limits on response scale (i.e., same scale as data)

Link_Prediction, Link_LCL, and Link_UCL: Prediction and Confidence Limits on link scale (i.e., log transformed)

If you wish to return back the 90th percent confidence value just change the critical value used to one corresponding to $\alpha$ = 0.10 instead of $\alpha$ = 0.05

$\endgroup$
5
  • $\begingroup$ @AnandSubramaniam Feel free to accept my answer if I helped you. $\endgroup$ Feb 22, 2019 at 21:23
  • $\begingroup$ If I use statsmodels in Python to implement the same thing how do I ensure that my prediction is on the response scale. Statsmodels.api.glm doesn't have a predict function to predict on the response scale! :-( $\endgroup$ Apr 11, 2019 at 19:42
  • $\begingroup$ Use the inverse link function on the prediction $\endgroup$ Apr 12, 2019 at 0:33
  • $\begingroup$ My data is gamma distributed (clearly skewed to the right) but when I use the log link function the distribution that my model is producing is Gaussian. Not sure why. Any thoughts appreciated! $\endgroup$
    – thecity2
    Mar 16, 2023 at 20:00
  • $\begingroup$ @thecity2 The gamma can take on the shape of a normal Gaussian. Gamma is a series of distributions that changes shape depending on the data. Beyond the requirement of positive data only, it is very flexible. My guess is the log link is normalizing or nearly normalizing your data, same as a log transformation. $\endgroup$ Mar 17, 2023 at 19:16

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.