Generalized Linear Models

Generalized Linear Models

Generalized Linear Models based on Zuur et al. Kuhnert and Venables Normal Distribuon example: weights of 1280 sparrows Sparrows<-read.table("/Users/dbm/Documents/W2025/ZuurDataMixedModelling/ Sparrows.txt",header=TRUE) par(mfrow = c(2, 2)) hist(Sparrows$wt, nclass = 15, xlab = "Weight", main = "Observed data",xlim=c(10,30)) hist(Sparrows$wt, nclass = 15, xlab = "Weight", main = "Observed data", freq = FALSE,xlim=c(10,30)) Y <- rnorm(1281, mean = mean(Sparrows$wt), sd = sd(Sparrows$wt)) hist(Y, nclass = 15, main = "Simulated data",xlab = "Weight",xlim=c(10,30)) X <-seq(from = 0,to = 30,length = 200) Y <- dnorm(X, mean = mean(Sparrows$wt), sd = sd(Sparrows$wt)) plot(X, Y, type = "l", xlab = "Weight", ylab = "Probabli]es", ylim = c(0, 0.25), xlim = c(10, 30), main = "Normal density curve") par(mfrow = c(1, 1)) Observed data Observed data 0.20 250 150 Density 0.10 Frequency 50 0 0.00 10 15 20 25 30 10 15 20 25 30 Weight Weight Simulated data Normal density curve 250 0.20 200 150 0.10 Frequency 100 Probablities 50 0 0.00 10 15 20 25 30 10 15 20 25 30 Weight Weight Poisson Distribuon In prac]ce the variance is o`en bigger than the mean -> overdispersion Gamma Distribuon Binomial Distribuon Natural Exponen.al Family to get, e.g., Poisson: Generalized Linear Models Generalized Linear Models: Poisson regression x <- 0:100 y <- rpois(101,exp(0.01+0.03*x)) MyData <- data.frame(x,y) MyModel <- glm(y~x,data=MyData,family=poisson) summary(MyModel) coefs <- coef(MyModel) plot(y~x,MyData,ylim=c(0,26)) par(new=TRUE) plot(x,exp(0.01+0.03*x),ylim=c(0,26),type="l") par(new=TRUE) plot(x,exp(coefs[1]+coefs[2]*x),ylim=c(0,26),type="l",col="red") Es]mates parameters by maximizing the likelihood: −µi µi × e µ = exp(β + β x +…+ β x ) L = ∏ , i 0 1 1 d d i yi ! RoadKills <- read.table("/Users/dbm/Documents/W2025/ ZuurDataMixedModelling/RoadKills.txt",header=TRUE) RK <- RoadKills #Saves some space in the code plot(RK$D.PARK, RK$TOT.N, xlab = "Distance to park", ylab = "Road kills") M1 <- glm(TOT.N ~ D.PARK, family=poisson, data=RK) summary(M1) M2 <- glm(TOT.N ~ D.PARK, family=gaussian, data=RK) summary(M2) Call: glm(formula = TOT.N ~ D.PARK, family = poisson, data = RK) Deviance Residuals: Min 1Q Median 3Q Max -8.1100 -1.6950 -0.4708 1.4206 7.3337 Coefficients: Es]mate Std. Error z value Pr(>|z|) (Intercept) 4.316e+00 4.322e-02 99.87 <2e-16 *** D.PARK -1.059e-04 4.387e-06 -24.13 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for poisson family taken to be 1) Null deviance: 1071.4 on 51 degrees of freedom Residual deviance: 390.9 on 50 degrees of freedom AIC: 634.29 Number of Fisher Scoring iteraons: 4 Residual Deviance: Difference in G2 = -2 log L between a maximal model where every observaon has Poisson parameter equal to the observed value and your model Null Deviance: Difference in G2 = -2 log L between a maximal model where every observaon has Poisson parameter equal to the observed value and the model with just an intercept Some]mes useful to compare residual deviances for two models. Differences are chi-square distributed with degrees of freedom equal to the change in the number of parameters MyData <- data.frame(D.PARK = seq(from = 0, to = 25000, by = 1000)) G <- predict(M1, newdata = MyData, type = "link", se = TRUE) F <- exp(G$fit) FSEUP <- exp(G$fit + 1.96 * G$se.fit) FSELOW <- exp(G$fit - 1.96 * G$se.fit) lines(MyData$D.PARK, F, lty = 1) lines(MyData$D.PARK, FSEUP, lty = 2) lines(MyData$D.PARK, FSELOW, lty = 2) 100 80 60 Road kills 40 20 0 0 5000 10000 15000 20000 25000 Distance to park Model Selec.on in Generalized Linear Models 255000 250000 library(AED) library(lace) 245000 xyplot(Y ∼ X, aspect = "iso", col = 1, pch = 16, data = RoadKills) Y 240000 235000 258500 X Frequency RK$L.P.ROADHistogram of Frequency Histogram of RK$POLIC Histogram of 0 5 10 15 20 25 0 10 20 30 40 0.5 0 RK$L.P.ROAD RK$POLIC 1.5 4 8 2.5 Frequency Frequency Histogram of RK$WAT.RESHistogram of Histogram of RK$SHRUBHistogram of 0 5 10 15 0 10 20 30 0.0 0 RK$WAT.RES RK$SHRUB 0.5 2 1.0 4 1.5 6 Histogram of RK$D.WAT.COURHistogram of Frequency Frequency Histogram of RK$URBANHistogram of 0 2 4 6 8 10 0 5 10 15 20 25 30 35 0 0 RK$D.WAT.COUR RK$URBAN 400 10 800 20 30 Frequency Histogram of RK$OLIVE Histogram of 0 5 10 15 20 25 30 0 RK$OLIVE 20 40 60 How to select variables for inclusion in the model • Hypothesis tes]ng • Use the z-stas]c produced by summary • drop1(M2,test=“Chi”) • anova(M2,M3,test=“Chi”) • AIC, BIC, etc. - use step(M2) etc. Overdispersion Recall that in the Poisson model we assume the mean = variance With no overdispersion, residual mean deviance should be close to 1: 270.23 / 42 = 6.43…not close to 1 Or, consider mean squared residuals: sum(resid(M2, type=“pearson”)^2) / 42 = 5.93 (suggests standard errors should be increased by sqrt(5.93) = 2.44) Quasi-Poisson model accounts for overdispersion: Overdispersion AIC not defined for quasi-Poisson model Use drop1(M4, test=“F”) is approximately correct or, be~er, do cross-validaon 100 80 60 Road kills 40 20 0 0 5000 10000 15000 20000 25000 Distance to park Residuals for Poisson Model Because variance increases with the mean, standard residual doesn’t make much sense. Can use “Pearson residuals” For quasi-Poisson should also divide by the square root of the overdispersion parameter Nega.ve Binomial GLM M7 <- glm.nb(formula = TOT.N ~ OPEN.L + L.WAT.C + SQ.LPROAD + D.PARK, data = RK) plot(M7) Residuals vs Fitted Normal Q-Q 2 2 1 1 0 0 -1 -1 Residuals 39 37 -2 -2 1 devianceStd. resid. 1 -3 2 -3 2 1.5 2.0 2.5 3.0 3.5 4.0 4.5 -2 -1 0 1 2 Predicted values Theoretical Quantiles Scale-Location Residuals vs Leverage 2 0.5 1 2 19 . d 1.5 i 37 s e r 1 e c n 1.0 a i 0 v e d . d -1 t 0.5 S Std. PearsonStd. resid. 1 -2 Cook's distance2 0.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 0.00 0.05 0.10 0.15 0.20 0.25 Predicted values Leverage Contrast with quasi-Poisson model M4 Residuals vs Fitted Normal Q-Q 3 8 4 2 2 1 0 0 -2 -1 Residuals -4 37 1 -2 Std. devianceStd. resid. 1 -6 2 -3 2 -8 1.5 2.0 2.5 3.0 3.5 4.0 4.5 -2 -1 0 1 2 Predicted values Theoretical Quantiles Scale-Location Residuals vs Leverage 2 3 8 81 . 1 2 d 1.5 i s 0.5 e r 1 e c n 1.0 0 a i v e d -1 . d t 0.5 0.5 S Std. PearsonStd. resid. -2 1 1 Cook's distance2 -3 0.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 0.0 0.1 0.2 0.3 0.4 0.5 0.6 Predicted values Leverage What if zero is impossible? Library(VGAM) data(Snakes) M3A <- vglm(N_days~PDayRain + Tot_Rain + Road_Loc + PDayRain:Tot_Rain, family = posnegbinomial, data = Snakes) M4A <- vglm(N_days~PDayRain + Tot_Rain + Road_Loc + PDayRain:Tot_Rain, family = pospoisson, data = Snakes) Too many zeroes much more common try plot(table(rpois(10000,lambda))) Zero-altered Models Zero-altered Models Get the esmates of γ and β via maximum likelihood es.ma.on count part logis]c part library(pscl) f1 <- formula(Intensity~fArea*fYear + Length | fArea * fYear + Length) H1A <- hurdle(f1, dist = "poisson", link = "logit", data = ParasiteCod2) H1B <- hurdle(f1, dist = "negbin", link = "logit", data = ParasiteCod2) AIC(H1A,H1B) Can s]ll have overdispersion in ZIP/ZAP models so check ZINB/ZANB Zero-inflated Models .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    57 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us