Can I Create Gamma Distribution Models with Negative Value Predictor Variables?

78 Views Asked by At

I have a GLMM that I would like to create added variable plots for. Since it's a GLMM, the avPlots() function won't accept it as an input. I found this video that explains manually creating added variable plots in R. After creating the GLMM m1 from the centered and scaled data, I first created residuals of a model that excludes the predictor of interest rmpg. Then I calculated the residuals for a model using the predictor of interest (disp) as the dependent variable and all other predictors as independent variables rdisp. Then I created a linear model m2 using the second set of residuals as a predictor of the first set of residuals. Last, I plotted the two sets of residuals and used the linear model to add a trendline to the plot.

Example code

#load packages
library(lme4)
library(car)
library(MuMIn)
library(dplyr)

#set options for dredge function
options(na.action = "na.fail")

#reading in data
data(mtcars)

#duplicating dataframe for centering and scaling
mtcarsS<-mtcars

#scaling and centering variables
mtcarsS$mpg<-scale(mtcarsS$mpg, center=T, scale=T)
mtcarsS$cyl<-scale(mtcarsS$cyl, center=T, scale=T)
mtcarsS$disp<-scale(mtcarsS$disp, center=T, scale=T)
mtcarsS$hp<-scale(mtcarsS$hp, center=T, scale=T)
mtcarsS$drat<-scale(mtcarsS$drat, center=T, scale=T)
mtcarsS$wt<-scale(mtcarsS$wt, center=T, scale=T)
mtcarsS$qsec<-scale(mtcarsS$qsec, center=T, scale=T)
mtcarsS$vs<-scale(mtcarsS$vs, center=T, scale=T)
mtcarsS$am<-scale(mtcarsS$am, center=T, scale=T)
mtcarsS$gear<-scale(mtcarsS$gear, center=T, scale=T)
mtcarsS$carb<-scale(mtcarsS$carb, center=T, scale=T)

#removing negative values for ease of making an example glmm
mtcarsS<-mtcarsS %>% mutate(mpg=as.numeric(gsub("-", "", mtcarsS$mpg)),
                            cyl=as.numeric(gsub("-", "", mtcarsS$cyl)),
                            disp=as.numeric(gsub("-", "", mtcarsS$disp)),
                            hp=as.numeric(gsub("-", "", mtcarsS$hp)),
                            drat=as.numeric(gsub("-", "", mtcarsS$drat)),
                            wt=as.numeric(gsub("-", "", mtcarsS$wt)),
                            qsec=as.numeric(gsub("-", "", mtcarsS$qsec)),
                            vs=as.numeric(gsub("-", "", mtcarsS$vs)),
                            am=as.numeric(gsub("-", "", mtcarsS$am)),
                            gear=as.numeric(gsub("-", "", mtcarsS$gear)),
                            carb=as.numeric(gsub("-", "", mtcarsS$carb)))

#create the original model
m1<-glmer(mpg ~disp+hp+drat+wt+qsec + (1|cyl/vs),
          data = mtcarsS,family=Gamma(link = "log"))

#extract residuals using a model that leaves out the predictor variable of interest
rmpg<-residuals(glmer(mpg ~hp+drat+wt+qsec + (1|cyl/vs),
                      data = mtcarsS,family=Gamma(link = "log")))

#extract residuals using a model that contains the predictor variable of interest as the dependent variable
rdisp<-residuals(glmer(disp~hp+drat+wt+qsec + (1|cyl/vs),
                       data = mtcarsS,family=Gamma(link = "log")))

#create a linear model using the two sets of residuals
m2<-lm(rmpg~rdisp)

#plot the residuals and add a trendline
plot(rmpg,rdisp)
abline(m2,col="red",lwd=2)

This all works out well so far, however I've run into an issue. Earlier in my code I removed negative signs from my centered and scaled variables so I could create GLMMs with the gamma family argument, since I am unable to create models using the gamma family argument if my dependent variable has negative values. The issue is that my real data contains negative values in some of the predictor variables, meaning that I can't create the 2nd GLMM in my added variable plots procedure when the predictor variable I'm using as the dependent variable contains negative values. Is there any way I can get around this issue? In other words, is there any way I can create a GLMM with the Gamma family argument using a predictor variable with negative values? Thank you!

0

There are 0 best solutions below