1

I ran a GAMM model with a large dataset (over 20,000 cases) using mgcv. Because of the large number of data points, it is very difficult to see the smoothed lines among the residual points in the plot. Is it possible to specify different colors for the points and the smoothed fit lines?

Here is an example adopted from the mgcv documentation:

library(mgcv)

## simple examples using gamm as alternative to gam
set.seed(0) 
dat <- gamSim(1,n=200,scale=2)
b <- gamm(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
plot(b$gam, pages=1, residuals=T, col='#FF8000', shade=T, shade.col='gray90')

plot(absbmidiffLog.GAMM$gam, pages=1, residuals=T, pch=19, cex=0.01, scheme=1, 
     col='#FF8000', shade=T,shade.col='gray90')

I have looked into the visreg package, but it does not seem to work with gamma objects.

marbles
  • 121
  • 7
  • 1
    [See this](http://stackoverflow.com/questions/19735149/is-it-possible-to-plot-the-smooth-components-of-a-gam-fit-with-ggplot2), might be very helpful. The diagram shown there appears to be what you want to produce. – shekeine Aug 06 '15 at 21:13
  • I have looked into that, but I have been unable to make it work with `gamma`. It seems that `gamma.model$gam` is structured differently from the `gam.model`. – marbles Aug 06 '15 at 21:59

1 Answers1

0

I also found it surprisingly difficult/impossible to choose 2 different colors. A workaround for me is to add the points later, i.e first call plot.gam with residuals=FALSE and then add the points with the base R points() call. This only works properly though if you shift the gam plot to its proper mean. Here is the code for one of the terms. (Use a for loop to get all four on one page)

library(mgcv)

## simple examples using gamm as alternative to gam
set.seed(0) 
dat <- gamSim(1,n=200,scale=2)
b <- gamm(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
plot(b$gam, select=3, shift = coef(b$gam)[1], residuals=FALSE, col='#FF8000',  shade=T, shade.col='gray90')
points(y~x3, data=dat,pch=20,cex=0.75,col=rgb(1,0.65,0,0.25))

enter image description here

Markus Loecher
  • 367
  • 1
  • 16