## Regression skewed data ## Set working directory setwd("C:/Users/Documents") ## Install (if not already) and load following libraries library(readstata13) ## to read STATA data library(ggplot2) ## for plots (ggplot graphs) library("MASS") ## To fit negative binomial model library(AER) ## For dispersion parameter test library(pscl) ## Testing between Poisson and negative binomial regression models, using Vuong test. library(ggpubr) ## For plotting multiple ggplots using `ggarrange' function rm(list=ls()) ## reading data from STATA data file and saving in an object `regdata'. regdata = read.dta13("filename.dta") ## Converting numerical variable, gender (0,1) to factor variable, sex (Female, Male) regdata$sex = factor(regdata$gender) levels(regdata$sex) = c("Female","Male") ## Histograms of weekly minutes of habitual PA (Figure 1) ggplot(regdata, aes(x=TotPA)) + geom_histogram(color="black", fill="white",bins=15) + labs(x="",y="")+ #theme_minimal() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) ## Descriptive measures summary(regdata$age) ## summary measures of age var(regdata$age) ## variance of age summary(regdata$medCond) ## summary measures of NCMC var(regdata$medCond) ## variance of NCMC summary(regdata$TotPA) ## summary measures of PA var(regdata$TotPA) ## variance of PA summary(regdata$ave_mvpa) ## summary measures of mvpa var(regdata$ave_mvpa) ## variance of mvpa ## Proportion of zeros in PA length(which(regdata$TotPA==0))/dim(regdata)[1] ######### fitting models to COUNT DATA ######## ## outcome variable (TotPA) to fit on age and NCMC (medCond) ## Standard linear regression model fitlm=lm(TotPA ~ age + sex + medCond , data=regdata) ## Poisson regression fitp=glm(TotPA ~ age + sex + medCond ,family=poisson, data=regdata) ## quasi-Poisson regression fitqp=glm(TotPA ~ age + sex + medCond ,family=quasipoisson, data=regdata) ## Negative binoial regression fitnb=glm.nb(TotPA ~ age + sex + medCond , data=regdata) ## calculating 95% confidence interval of various regession models confint(fitlm); confint(fitp); confint(fitqp); confint(fitnb) ## Test of dispersion, only for poisson glm dispersiontest(fitp) ## Corresponding negative binomial model, testing significance of dispersion parameter ## of negative binomial regression. Quadratic formulation (trafo = function(x) x^2) dispersiontest(fitp, trafo = 2) ## Testing between Poisson and negative binomial regression models, using Vuong test. ## Vuong, Q.H. (1989). Likelihood ratio tests for model selection and non-nested hypotheses. ## Econometrica. 57(2). 307–333. vuong(fitp, fitnb) ## AIC's ## Calculating AIC of quasipoisson distribution dof = length(fitqp$coeff)+1 ## model parameters, plus phi phi.fit = summary(fitqp)$dispersion ## fitted phi value mu.fit = fitqp$fitted.values ## fitted values aicqp = 2*dof - 2*sum(dnbinom(regdata$TotPA, mu=mu.fit, size = mu.fit/(phi.fit - 1), log=T)) ## AIC of other models aic=t(AIC(fitlm,fitp,fitqp,fitnb)) ## Replacing AIC of quasiPoisson aic[2,"fitqp"]=aicqp aic ############################################################################## ############################################################################### ########## CONTINUOUS OUTCOME ######### ### Plotting original (untransformed) and transformed MVPA ### cdata1=ggplot(regdata, aes(x=ave_mvpa)) + geom_histogram(color="black", fill="white",bins=15) + labs(x="minutes of mvpa",y="Frequency") + ylim(0,90) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) cdata2=ggplot(regdata, aes(x=log(ave_mvpa))) + geom_histogram(color="black", fill="white",bins=15) + labs(x="log of minutes of mvpa",y="Frequency") + ylim(0,90) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) cdata3=ggplot(regdata, aes(x=sqrt(ave_mvpa))) + geom_histogram(color="black", fill="white",bins=15) + labs(x="square root of minutes of mvpa",y="Frequency") + ylim(0,90) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) ## plotting in one window ggarrange(cdata1, cdata2, cdata3 , ncol = 1, nrow = 3) ######################################################### ########## MODEL FITTINGS TO CONTINUOUS OUTCOME ######### ## outcome variable (MVPA) to fit on age and sex # Standard Linear regression model mod1 <- lm(ave_mvpa ~ age + sex + medCond, data=regdata) # Linear regression on Square roort transformed MVPA mod2 <- lm(sqrt(ave_mvpa) ~ age + sex + medCond, data=regdata) # Linear regression on Log transformed MVPA mod3 <- lm(log(ave_mvpa) ~ age + sex + medCond, data=regdata) # GLM: Gamma regression with Log link mod4 <- glm(ave_mvpa ~ age + sex + medCond, family=Gamma(link = "log"),data=regdata) # GLM: Inverse guassian regression with log link mod5 <- glm(ave_mvpa ~ age + sex + medCond, family=inverse.gaussian(link = "log"),data=regdata) ## 95% confidence interval of various models confint(mod1); confint(mod2); confint(mod3); confint(mod4); confint(mod5) ## Calculating Predicted values from various models on original scale predlm = predict(mod1) predlmsqrt = predict(mod2)^2 predlmlog = exp(predict(mod3)) predglmg = predict(mod4,type="response") predglmig = predict(mod5,type="response") ## Plotting predicted and actual values from various models on original scale against age ggplot(regdata,aes(y=ave_mvpa,x=age,shape=sex))+ geom_point() + geom_point(aes(y = predlm,x=age,shape=sex,color="LM")) + geom_point(aes(y = predlmsqrt,x=age,shape=sex,color="LM sqrt")) + geom_point(aes(y = predlmlog,x=age,shape=sex,color="LM log")) + geom_point(aes(y = predglmg,x=age,shape=sex,color="GLM gamma")) + geom_point(aes(y = predglmig,x=age,shape=sex,color="GLM IG")) + geom_line(aes(y = predlm,x=age,group=sex,color="LM")) + geom_line(aes(y = predlmsqrt,x=age,group=sex,color="LM sqrt")) + geom_line(aes(y = predlmlog,x=age,group=sex,color="LM log")) + geom_line(aes(y = predglmg,x=age,group=sex,color="GLM gamma")) + geom_line(aes(y = predglmig,x=age,group=sex,color="GLM IG")) + labs(shape="Original", colour="Predicted", x="Age",y="MVPA") + scale_color_discrete(breaks=c("LM","LM sqrt","LM log","GLM gamma","GLM IG"), guide = guide_legend(override.aes = list(shape = c(rep(NA,5))))) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))+ geom_hline(yintercept=0, linetype="dashed") ## Comparing GLM-gamma with GLM IG using residuals vs fitted values plot rvfglmg=ggplot(regdata, aes(y=residuals(mod4),x=predict(mod4))) + geom_point(color="black", fill="white",shape=21) + geom_hline(yintercept=0, linetype="dashed") + ylim(-2.5,2.5) + labs(x="Linear predictor",y="Deviance residuals",title="GLM-gamma") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) rvfglmig=ggplot(regdata, aes(y=residuals(mod5),x=predict(mod5))) + geom_point(color="black", fill="white",shape=21) + geom_hline(yintercept=0, linetype="dashed") + ylim(-2.5,2.5)+ labs(x="Linear predictor",y="Deviance residuals",title="GLM-IG") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) ##residuals vs fitted values plot all together in one plot window ggarrange(rvfglmg, rvfglmig , ncol = 1, nrow = 2) ###### AIC values of various models ###### ## Corrected/adjusted AIC values from transformed models N=length(regdata$ave_mvpa) ## AIC of Square roort transformed MVPA aic.sqrt=sum(log(regdata$ave_mvpa))+2*N*log(2) + AIC(mod2) ## AIC of Log transformed MVPA aic.log=2*sum(log(regdata$ave_mvpa))+AIC(mod3) ## AIC values of all models aic=c(AIC(mod1),aic.sqrt,aic.log,AIC(mod4),AIC(mod5))