How to get only charts from gam.check

When applied gam.checkin a package, it mgcv Rdisplays some residual graphs and displays the main value. Is there a way to create graphics, not a printed result?

library(mgcv)
set.seed(0)
dat <- gamSim(1,n=200)
b   <- gam(y~s(x0)+s(x1)+s(x2)+s(x3), data=dat)
plot(b, pages=1)
gam.check(b, pch=19, cex=.3)
+4
source share
1 answer

There are four plots: top left, down and across:

  • QQ chart of balances
  • Bar Chart
  • Residual vs. linear predictor chart
  • Schedule of observations with set values.

In the code below, I assume it bcontains your installed model according to your example. First of all, we need

type <- "deviance"  ## "pearson" & "response" are other valid choices
resid <- residuals(b, type = type)
linpred <- napredict(b$na.action, b$linear.predictors)
observed.y <- napredict(b$na.action, b$y)

, NA, , linear.predictors y, .

10 gam.check(). ,

gam.check

R.

:

QQ plot

qq.gam():

qq.gam(b, rep = 0, level = 0.9, type = type, rl.col = 2, 
       rep.col = "gray80")

hist(resid, xlab = "Residuals", main = "Histogram of residuals")

plot(linpred, resid, main = "Resids vs. linear pred.", 
     xlab = "linear predictor", ylab = "residuals")

plot(fitted(b), observed.y, xlab = "Fitted Values", 
     ylab = "Response", main = "Response vs. Fitted Values")
+9

Source: https://habr.com/ru/post/1530751/


All Articles