Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. library(gridBase)
  2. library(grid)
  3. library(mgcv)
  4. library(statmod)
  5. ## nicer version of gam.check/rqgam.chack
  6. # - deviance residuals for the Q-Q and histogram
  7. # - RQR for resids vs linear pred
  8. # - response vs fitted
  9. # hist.p gives the quantiles of the residuals to show
  10. better_check <- function(b, k.sample = 5000, k.rep = 200, rep = 0, level = 0.9,
  11. rl.col = 2, rep.col = "gray80", hist.p=c(0.01, 0.099), ...){
  12.  
  13. # layout stuff
  14. opar <- par(mfrow=c(2,2))
  15.  
  16. # grab the randomised quantile residuals
  17. # requires statmod package
  18.  
  19. # need to do the right thing for mgcv's Tweedie
  20. if(grepl("^Tweedie",b$family$family)){
  21. if(is.null(environment(b$family$variance)$p)){
  22. p.val <- b$family$getTheta(TRUE)
  23. environment(b$family$variance)$p <- p.val
  24. }
  25. qres <- qres.tweedie(b)
  26. # and for negbin
  27. }else if(grepl("^Negative Binomial",b$family$family)){
  28. # need to set $theta
  29. if("extended.family" %in% class(b$family)){
  30. # for SNW's extended family, need to set TRUE in getTheta as theta
  31. # is on the wrong scale
  32. b$theta <- b$family$getTheta(TRUE)
  33. }else{
  34. b$theta <- b$family$getTheta()
  35. }
  36. qres <- qres.nbinom(b)
  37. }else{
  38. stop("Only negative binomial and Tweedie response distributions are supported.")
  39. }
  40.  
  41. # values of the linear predictor
  42. linpred <- napredict(b$na.action, b$linear.predictors)
  43.  
  44. ## get the deviance residuals
  45. resid <- residuals(b, type = "deviance")
  46.  
  47. ## Q-Q plot of deviance resids
  48. qq.gam(b, rep = rep, level = level, type = "deviance", rl.col = rl.col,
  49. rep.col = rep.col, main="Quantile-quatile plot", ...)
  50.  
  51. ## Q resids vs. linear pred
  52. plot(linpred, qres, main="Resids vs. linear pred.",
  53. xlab="linear predictor", ylab="Randomised quantile residuals",
  54. pch=19, cex=0.5, col=rgb(0, 0, 0, 0.6), ...)
  55. lines(lowess(linpred, qres), col = 2)
  56.  
  57. ## histogram of deviance resids
  58.  
  59. vps <- baseViewports()
  60. pushViewport(vps$figure)
  61.  
  62. # partial histogram
  63. hist(resid[resid >= quantile(resid, hist.p[1]) &
  64. resid <= quantile(resid, hist.p)[2]],
  65. xlab = "deviance residuals", main = "Histogram of residuals",...)
  66. box()
  67.  
  68. # setup the smaller plot
  69. pushViewport(viewport(x=-.4, y=.45 ,width=.3, height=.3))
  70. oopar <- par(plt = gridFIG(), new=TRUE)
  71.  
  72. # histogram of full data
  73. hist(resid, xlab = "", main="", axes=FALSE, ylab="", ...)
  74. axis(1, cex.axis=0.5, mgp=c(3, 0.1, 0), tck=-0.02)
  75.  
  76. # reset grid options for next plot
  77. popViewport(2)
  78. par(oopar)
  79.  
  80. ## Response vs. Fitted Values
  81. plot(fitted(b), b$y,
  82. main="Response vs. Fitted Values",
  83. xlab="Fitted Values", ylab="Response",
  84. pch=19, cex=0.5, col=rgb(0, 0, 0, 0.6), ...)
  85. lines(lowess(b$fitted.values, b$y), col = 2)
  86.  
  87. par(opar)
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement