Argh!! I use those all the time! I can't believe I overlooked something so obvious! Unfortunately, the pastebin was an abstraction of another problem I was having that WAS, in fact, creating a formula in another environment and passing it into the function the where data object was created and modeled. Thanks for your help understanding this!
Dave On Sun, Jun 23, 2013 at 2:31 PM, William Dunlap <wdun...@tibco.com> wrote: > By the way, you could use > lm(x ~ y + z, data=someData, weights=weight) > instead of > lm(x ~ y + z, data=someData, weights=someData$weight) > as 'weight' will be looked for in someData before any environment. > > (You still don't want to get into the habit of passing formula strings to > lm.) > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > > -----Original Message----- > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > On Behalf > > Of William Dunlap > > Sent: Sunday, June 23, 2013 1:26 PM > > To: Dave Mitchell; r-help@r-project.org > > Subject: Re: [R] environment of lm > > > > Your web site had > > WeightsBreaks <- function(){ > > someData <- data.frame(x = rnorm(20, 5, 1),y = 1:20 + runif(20),z = > 70:51, weight = > > 0.05) > > print( ls()) > > ModObject <- lm('x~y + z', data = someData, weights = > someData$weight) > > } > > and I got the result > > > print(WeightsBreaks()) > > [1] "someData" > > Error in eval(expr, envir, enclos) : object 'someData' not found > > When I remove the quotes around the formula string, from > > lm('x~y + z',...) > > to > > lm(x~y + z, ...) > > then I get > > > print(WeightsBreaks()) > > [1] "someData" > > > > Call: > > lm(formula = x ~ y + z, data = someData, weights = someData$weight) > > > > Coefficients: > > (Intercept) y z > > 47.7740 -0.5689 -0.6028 > > > > The object 'x ~ y + z' is a character string, not a formula. It gets > converted into a formula > > by as.formula by lm() or perhaps by something that lm() calls. The > 'environment of a > > formula' is the environment in which the formula is created (unless you > explicitly change > > it) so this explains why lm() could not find your stuff. > > > > If you are are constructing your formula string using something like > paste(), convert it > > yourself to a formula by calling as.formula explicitly from the > environment where the > > objects it uses are. > > > > Bill Dunlap > > Spotfire, TIBCO Software > > wdunlap tibco.com > > > > > > > -----Original Message----- > > > From: r-help-boun...@r-project.org [mailto: > r-help-boun...@r-project.org] On Behalf > > > Of Dave Mitchell > > > Sent: Sunday, June 23, 2013 9:32 AM > > > To: r-help@r-project.org > > > Subject: [R] environment of lm > > > > > > Hello, > > > I'm running into what I believe is a scoping issue having to do with > the > > > environment in which the weights argument is evaluated in lm. From > the lm > > > documentation "All of weights, subset and offset are evaluated in the > same > > > way as variables in formula, that is first in data and then in the > > > environment of formula.". In the code at http://pastebin.com/kqzxxicpI > > > have 3 functions identical to each other except for > > > > > > WeightsBreaks adds "weights = someData$weight" > > > and > > > Works uses the <<- assignment operator for someData. > > > > > > My interpretation of the quoted documentation above is that lm looks > at the > > > data argument to find weights, subset and offset and then to the > > > environment in which the formula argument lives. Am I missing > something? > > > Why is it that lm only sees weights (here someData$weight) when it's > in the > > > global environment, yet lm sees data (someData in this case) > regardless of > > > whether it's in the global environment or just local to the function? > > > Thanks for your time. > > > > > > Dave > > > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > > R-help@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@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.