[R] retrieving partial residuals of gam fit (mgcv)

Simon Wood s.wood at bath.ac.uk
Wed Feb 16 18:12:47 CET 2011


The partial residuals for a smooth term are just the whole model residuals + 
the corresponding estimate of the smooth term. You can use predict to access 
the latter and compute them quite easily. For example...

     set.seed(0) ## simulate some data...
     dat <- gamSim(1,n=400,dist="normal",scale=2)
     ## fit a model...
     b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
     fv <- predict(b,type="terms") ## get term estimates
     ## compute partial residuals for first smooth...          
     prsd1 <- residuals(b,type="working") + fv[,1]
     plot(b,select=1) ## plot first smooth
     ## add random selection of partial residuals
     ind <- sample(1:length(prsd1),100)
     points(dat$x0[ind],prsd1[ind],pch=19,col="pink") 


On Wednesday 16 February 2011 12:35, Jannis wrote:
> Dear list,
>
>
> does anybody know whether there is a way to easily retrieve the so called
> "partial residuals" of a gam fit with package mgcv? The partial residuals
> are the residuals you would get if you would "leave out" a particular
> predictor and are the dots in the plots created by
>
> plot(gam.object,residuals=TRUE)
>
> residuals.gam() gives me whole model residuals and
> predict.gam(...,type='terms') only gives me the response function and its
> standard deviation.
>
> I would like to retrieve the values of these residuals as I would like to
> only plot a random sample of the residuals to avoid overloading the plot
> (>100000 data points).
>
>
> Thanks a lot in advance
> Jannis
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented, minimal,
> self-contained, reproducible code.

-- 
> Simon Wood, Mathematical Sciences, University of Bath, Bath, BA2 7AY UK
> +44 1225 386603  www.maths.bath.ac.uk/~sw283



More information about the R-help mailing list