Re: [R] passing formula arg to mgcv::gam
I think that this should work in gam(), but in any case a fix is to put the subset variable into the data frame. The formula interface works best this way. In your example you could add idx=1:100 to the data frame and then use subset=idx %in% 1:10 -thomas On Mon, 27 Sep 2004, Vadim Ogranovich wrote: Hi, I have a function, callGam, that fits a gam model to a subset of a dataframe. The argument to callGam is a formula, the subset is determined inside the function itself. My naïve approach generates and error, see below. I guess this is because 'idx' is loocked up in the environment of 'formula', but I am too ignorant about environments to be able to tell for sure. Could someone please suggest a way around? Thanks, Vadim library(mgcv) callGam - function(formula) { + idx - seq(10) + gam(formula, data=data.frame(x=rnorm(100), y=rnorm(100)), subset=idx) + } gam.fit - callGam(y ~ x) Error in eval(expr, envir, enclos) : Object idx not found __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Thomas Lumley Assoc. Professor, Biostatistics [EMAIL PROTECTED] University of Washington, Seattle__ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] passing formula arg to mgcv::gam
This is a self-response :-). It was indeed a problem with environments. One way to get around is to reset the environment, e.g. inside callGam do formula - as.formula(unclass(formula)) Not too aesthetic, but works. Is there a less kludgy way to do this? BTW, forgot to mention. This is R-1.9.1 on RH-7.3. Thanks, Vadim -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Vadim Ogranovich Sent: Monday, September 27, 2004 3:24 PM To: [EMAIL PROTECTED] Subject: [R] passing formula arg to mgcv::gam Hi, I have a function, callGam, that fits a gam model to a subset of a dataframe. The argument to callGam is a formula, the subset is determined inside the function itself. My naïve approach generates and error, see below. I guess this is because 'idx' is loocked up in the environment of 'formula', but I am too ignorant about environments to be able to tell for sure. Could someone please suggest a way around? Thanks, Vadim library(mgcv) callGam - function(formula) { + idx - seq(10) + gam(formula, data=data.frame(x=rnorm(100), y=rnorm(100)), + subset=idx) } gam.fit - callGam(y ~ x) Error in eval(expr, envir, enclos) : Object idx not found __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] passing formula arg to mgcv::gam
How about environment(formula) - environment() Vadim Ogranovich vograno at evafunds.com writes: : : This is a self-response . : : It was indeed a problem with environments. One way to get around is to reset the environment, e.g. inside : callGam do : formula - as.formula(unclass(formula)) : : Not too aesthetic, but works. Is there a less kludgy way to do this? : : BTW, forgot to mention. This is R-1.9.1 on RH-7.3. : : Thanks, : Vadim : : -Original Message- : From: r-help-bounces at stat.math.ethz.ch : [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Vadim : Ogranovich : Sent: Monday, September 27, 2004 3:24 PM : To: r-help at stat.math.ethz.ch : Subject: [R] passing formula arg to mgcv::gam : : Hi, : : I have a function, callGam, that fits a gam model to a subset : of a dataframe. The argument to callGam is a formula, the : subset is determined inside the function itself. My nave : approach generates and error, see below. I guess this is : because 'idx' is loocked up in the environment of 'formula', : but I am too ignorant about environments to be able to tell : for sure. Could someone please suggest a way around? : : Thanks, : Vadim : : library(mgcv) : : callGam - function(formula) { : + idx - seq(10) : + gam(formula, data=data.frame(x=rnorm(100), y=rnorm(100)), : + subset=idx) } : : gam.fit - callGam(y ~ x) : Error in eval(expr, envir, enclos) : Object idx not found __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html