I've run into a scoping problem in R.
I'm calling a function that
* creates a formula
* calculates a weight vector
* calls lm with that formula and weights
This fails.
Here's a simplified reproduce example:
# f works, g doesn't, h is a workaround
rm(w)
data <- data.frame(y=runif(20), x=runif(20), z=runif(20))
f <- function(k){
w <- data$z^k
coef(lm(y~x, data = data, weights = w))
}
g <- function(k){
w <- data$z^k
Formula <- parse(text="y~x")[[1]]
coef(lm(Formula, data = data, weights = w))
}
h <- function(k){
w <- data$z^k
Formula <- parse(text="y~x")[[1]]
# Following line fails due to scoping bug. Use workaround.
# coef(lm(Formula, data = data, weights = w))
Call <- paste("coef(lm(", deparse(Formula), ", data=data, weights=w))")
eval(parse(text=Call)[[1]])
}
f(2)
(Intercept) x
0.7452512 -0.3413998
g(2)
Error in eval(expr, envir, enclos) : object "w" not found
(Intercept) x
0.7452512 -0.3413998
--please do not edit the information below--
Version:
platform = i486-pc-linux-gnu
arch = i486
os = linux-gnu
system = i486, linux-gnu
status =
major = 2
minor = 7.0
year = 2008
month = 04
day = 22
svn rev = 45424
language = R
version.string = R version 2.7.0 (2008-04-22)
Locale:
LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C
Search Path:
.GlobalEnv, package:stats, package:graphics, package:grDevices, package:utils, package:datasets, package:showStructure, package:Rcode, package:splus2R, package:methods, Autoloads, package:base
Try replacing the last line in g with:
lmout <- do.call(lm, list(Formula, data = data, weights = w))
coef(lmout)
or replace w with environment()$w thereby explicitly telling it where to look.
On Fri, May 30, 2008 at 11:40 AM, <rocket at google.com> wrote:
I've run into a scoping problem in R.
I'm calling a function that
* creates a formula
* calculates a weight vector
* calls lm with that formula and weights
This fails.
Here's a simplified reproduce example:
# f works, g doesn't, h is a workaround
rm(w)
data <- data.frame(y=runif(20), x=runif(20), z=runif(20))
f <- function(k){
w <- data$z^k
coef(lm(y~x, data = data, weights = w))
}
g <- function(k){
w <- data$z^k
Formula <- parse(text="y~x")[[1]]
coef(lm(Formula, data = data, weights = w))
}
h <- function(k){
w <- data$z^k
Formula <- parse(text="y~x")[[1]]
# Following line fails due to scoping bug. Use workaround.
# coef(lm(Formula, data = data, weights = w))
Call <- paste("coef(lm(", deparse(Formula), ", data=data, weights=w))")
eval(parse(text=Call)[[1]])
}
f(2)
(Intercept) x
0.7452512 -0.3413998
g(2)
Error in eval(expr, envir, enclos) : object "w" not found
(Intercept) x
0.7452512 -0.3413998
--please do not edit the information below--
Version:
platform = i486-pc-linux-gnu
arch = i486
os = linux-gnu
system = i486, linux-gnu
status =
major = 2
minor = 7.0
year = 2008
month = 04
day = 22
svn rev = 45424
language = R
version.string = R version 2.7.0 (2008-04-22)
Locale:
LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C
Search Path:
.GlobalEnv, package:stats, package:graphics, package:grDevices, package:utils, package:datasets, package:showStructure, package:Rcode, package:splus2R, package:methods, Autoloads, package:base
On 5/30/2008 11:40 AM, rocket at google.com wrote:
I've run into a scoping problem in R.
No, in your use of it.
I'm calling a function that
* creates a formula
... incorrectly.
* calculates a weight vector
* calls lm with that formula and weights
This fails.
Here's a simplified reproduce example:
# f works, g doesn't, h is a workaround
rm(w)
data <- data.frame(y=runif(20), x=runif(20), z=runif(20))
f <- function(k){
w <- data$z^k
coef(lm(y~x, data = data, weights = w))
}
g <- function(k){
w <- data$z^k
Formula <- parse(text="y~x")[[1]]
If this were simply
Formula <- y ~ x
it would work, because then the formula would get the evaluation
environment attached. I assume your real case builds up a formula in
text and then parses it, in which case you'll have to evaluate the
formula to get the environment set:
> g
function (k)
{
w <- data$z^k
Formula <- eval(parse(text = "y ~ x"))
coef(lm(Formula, data = data, weights = w))
}
> g(2)
(Intercept) x
0.7777419 -0.7023185
In more complicated cases, you may need to explicitly set the
environment of the constructed formula, using e.g.
environment(Formula) <- environment()
Duncan Murdoch
coef(lm(Formula, data = data, weights = w))
}
h <- function(k){
w <- data$z^k
Formula <- parse(text="y~x")[[1]]
# Following line fails due to scoping bug. Use workaround.
# coef(lm(Formula, data = data, weights = w))
Call <- paste("coef(lm(", deparse(Formula), ", data=data, weights=w))")
eval(parse(text=Call)[[1]])
}
f(2)
(Intercept) x
0.7452512 -0.3413998
g(2)
Error in eval(expr, envir, enclos) : object "w" not found
(Intercept) x
0.7452512 -0.3413998
--please do not edit the information below--
Version:
platform = i486-pc-linux-gnu
arch = i486
os = linux-gnu
system = i486, linux-gnu
status =
major = 2
minor = 7.0
year = 2008
month = 04
day = 22
svn rev = 45424
language = R
version.string = R version 2.7.0 (2008-04-22)
Locale:
LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C
Search Path:
.GlobalEnv, package:stats, package:graphics, package:grDevices, package:utils, package:datasets, package:showStructure, package:Rcode, package:splus2R, package:methods, Autoloads, package:base