[R] glm: formula vs character
Greg Snow
Greg.Snow at imail.org
Fri Sep 17 22:23:26 CEST 2010
I think what is going on (and someone is likely to correct me otherwise) is that formulas have an associated environment that gets passed along with them while character strings do not.
This means that when you pass the object to another function which passes it to another function, etc. that eventually one of those nested functions is going to do something with the formula, if it is an actual formula then it can look in that environment that has been passed along and find the variables that the formula refers to and everything is happy.
When you just use the text string, the deeply nested function that is using it does not know where to look for the variables referred to in the formula and you get your error.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Krishna Tateneni
> Sent: Thursday, September 16, 2010 4:56 PM
> To: r-help
> Subject: [R] glm: formula vs character
>
> Hello,
>
> This is a question motivated by curiosity, not a pressing problem. Any
> responses are much appreciated! In the following code, function reg1
> calls
> glm with a formula object while reg2 uses a string. In both cases, glm
> works; however, in the second case, the add1 function fails with the
> following message: "Error in eval(predvars, data, env) : invalid
> 'envir'
> argument." Any insight into what is going on "under the hood"?
>
> Many thanks,
> --Krishna
>
> x1 = rnorm(100)
> x2 = rnorm(100)
> x3 = rnorm(100)
> y = x1 + 2*x2 + 3*x3 + 0.05*rnorm(100)
>
> d = data.frame(y,x1,x2,x3)
> xset = paste("x",1:3,sep="")
>
> reg1<-function(dep,cand,data){
> f = paste(dep,"~1",sep="")
> reg = glm(as.formula(f),family="gaussian",data)
> add = add1(reg,cand,test="F")
> }
>
> reg2<-function(dep,cand,data){
> f = paste(dep,"~1",sep="")
> reg = glm(f,family="gaussian",data)
> add = add1(reg,cand,test="F")
> }
>
> r1 = reg1("y",xset,d)
> r2 = reg2("y",xset,d)
>
> [[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.
More information about the R-help
mailing list