Hi all,
I need to loop over "lm" within a function using "weights". For example:
mydata = data.frame(y=rnorm(100, 500, 100), x= rnorm(100),
group=rep(c(0,1), 50), myweight=1/runif(100))
reg.by.wt <- function(formula, wt, by, data) {
if(missing(by)) {
summary(lm(formula=formula, data=data, weights=data[[wt]]))
} else {
lapply(split(data, data[by]), function(i)
summary(lm(formula=formula, data=i, weights=data[[wt]])))
}
}
reg.by.wt(formula = y ~ x, by="group", data=mydata, wt="myweight")
Error in summary(lm(formula = formula, data = i, weights = data[[wt]])) :
error in evaluating the argument 'object' in selecting a method for
function 'summary': Error in eval(expr, envir, enclos) : object 'wt'
not found
The functions works if I change "weights=data[[wt]]" for
"weights=myweight", but I need wt to be an argument in quotes, not an
object. It seems the issue is related to ?lm = "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., but I can't
figure it out. Can you please provide any advice? Thank you.
Daniel
lm weights argument within function
2 messages · Daniel Caro, William Dunlap
One way to accomplish this is to assign a new environment to the
formula, an environment which inherits from the formula's original
environment but one that you can add things to without affecting the
original environment. Also, since you do this in a function only the
copy of the formula in the function is affected; when the function is
done your formula is not changed. E.g.,
mydata <- data.frame( # a dataset that one can recreate
y = log2(1:100),
x = 1:100,
group = rep(c(0,1), each = 50),
myweight = 1/(1:100),
yourweight = 1:100)
reg.by.wt <- function(formula, wt, by, data) {
newEnv <- new.env(parent = environment(formula))
newEnv$wt <- wt
newEnv$data <- data
environment(formula) <- newEnv
if(missing(by)) {
summary(lm(formula = formula, data = data, weights = data[[wt]]))
} else {
lapply(split(data, data[by]),
function(i) {
newEnv$i <- i
summary(lm(formula = formula, data = i, weights = i[[wt]]))
})
}
}
try(reg.by.wt(formula = y ~ x, by="group", data=mydata, wt="myweight"))
try(reg.by.wt(formula = y ~ x, data=mydata, wt="myweight"))
try(reg.by.wt(formula = y ~ x, by="group", data=mydata, wt="yourweight"))
There may be less sneaky ways of doing this sort of thing.
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, Aug 1, 2014 at 8:36 AM, Daniel Caro <dcarov at gmail.com> wrote:
Hi all,
I need to loop over "lm" within a function using "weights". For example:
mydata = data.frame(y=rnorm(100, 500, 100), x= rnorm(100),
group=rep(c(0,1), 50), myweight=1/runif(100))
reg.by.wt <- function(formula, wt, by, data) {
if(missing(by)) {
summary(lm(formula=formula, data=data, weights=data[[wt]]))
} else {
lapply(split(data, data[by]), function(i)
summary(lm(formula=formula, data=i, weights=data[[wt]])))
}
}
reg.by.wt(formula = y ~ x, by="group", data=mydata, wt="myweight")
Error in summary(lm(formula = formula, data = i, weights = data[[wt]])) :
error in evaluating the argument 'object' in selecting a method for
function 'summary': Error in eval(expr, envir, enclos) : object 'wt'
not found
The functions works if I change "weights=data[[wt]]" for
"weights=myweight", but I need wt to be an argument in quotes, not an
object. It seems the issue is related to ?lm = "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., but I can't
figure it out. Can you please provide any advice? Thank you.
Daniel
______________________________________________ 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.