glm: formula vs character

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

glm: formula vs character

KT_rfan
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: glm: formula vs character

Greg Snow-2
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
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: glm: formula vs character

Thomas Lumley
On Fri, 17 Sep 2010, Greg Snow wrote:

> 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.
>

Yes.

model.frame(), which is called from most modelling functions, uses the data= argument as the first place to look up variables in the formula and the environment of the formula as the fallback location.

In the most common case, the environment of the formula is the environment from which the modelling function was called, so this is the same as using parent.frame(), but in more complex cases it is different.

     -thomas

Thomas Lumley
Professor of Biostatistics
University of Washington, Seattle

______________________________________________
[hidden email] 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.