[R] Using $ accessor in GAM formula

P Ehlers ehlers at ucalgary.ca
Thu May 5 21:05:42 CEST 2011


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.

Peter Ehlers

On 2011-05-05 12:06, David Winsemius wrote:
>
> On May 5, 2011, at 1:08 PM, Gene Leynes wrote:
>
>> This is not mission critical, but it's bothering me.  I'm getting
>> inconsistent results when I use the $ accessor in the gam formula
>>
>> *In window #1:*
>>> library(mgcv)
>>> dat=data.frame(x=1:100,y=sin(1:100/50)+rnorm(100,0,.05))
>>> str(dat)
>>> gam(dat$y~s(dat$x))
>> Error in eval(expr, envir, enclos) : object 'x' not found
>
> I get the same error, but using the standard R approach to passing
> data and formulae to regression functions I have not difficulty:
>
>   >  gam(y~s(x), data=dat)
>
> Family: gaussian
> Link function: identity
>
> Formula:
> y ~ s(x)
>
> Estimated degrees of freedom:
> 4.1552  total = 5.155229
>
> GCV score: 0.002242771
>
>>>
>>
>> *In window #2:*
>>> gm = gam(dat$cf~s(dat$s))
>>> gm
>>
>> Family: gaussian
>> Link function: identity
>>
>> Formula:
>> dat$cf ~ s(dat$s)
>>
>> Estimated degrees of freedom:
>> 8.7757  total = 9.77568980091
>>
>> GCV score: 302.551417213
>>
>>
>>
>>
>> Has anyone else seen the same thing?
>>
>> In both cases I'm using Windows 7 and R 2.13.0
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.



More information about the R-help mailing list