[R] Using $ accessor in GAM formula

Rolf Turner rolf.turner at xtra.co.nz
Thu May 5 23:58:50 CEST 2011


On 06/05/11 07:05, P Ehlers wrote:
> Gene,
>
> David has given you the preferred code. I just want to
> point out that the $-accessor is often not the best
> thing to use. Both dat[["y"]] and dat[, "y"] will work
> just fine.

Admittedly one should use the preferred code, i.e. gam(y ~ s(x),data=dat)
and avoid all the hoo-hah, but it's strange that the dodgey code
throws an error with gam(dat1$y ~ s(dat1$x))  but not with
gam(dat2$cf ~ s(dat2$s)) --- where dat1 and dat2 are identical except
for their column names.  Why are the column names having an impact
upon finding the variables/objects in question?

And it isn't just "x" and "y" that cause the error; I tried naming the 
columns
"u" and "v" and the error was thrown in that case also.  It seems that 
having
that name "s" involved keeps the error from happening.  It's probably not
coincidental that that is the name of a *function* that gam() uses.

Something a bit subtle is going on; it would be nice to be able to 
understand it.

Just out of pure academic interest. :-)

     cheers,

         Rolf Turner



More information about the R-help mailing list