Re: [R] Using $ accessor in GAM formula

From: P Ehlers <ehlers_at_ucalgary.ca>
Date: Thu, 05 May 2011 13:05:42 -0600

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.



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. Received on Thu 05 May 2011 - 19:10:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 05 May 2011 - 22:40:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive