I'm trying to build a function based on lm
in R-4.1.2, but I keep getting the error:
Error in eval(extras, data, env) : object not found.
Here is a reproducible example of the code:
my_fun <- function(formula, data, v) {
lm(formula, data, weights = 1/v)
}
my_fun(dist ~ speed, cars, rep(1, 50))
Error in eval(extras, data, env) : object 'v' not found
8.eval(extras, data, env)
7.eval(extras, data, env)
6.model.frame.default(formula = formula, data = data, weights = 1/v,
drop.unused.levels = TRUE)
5.stats::model.frame(formula = formula, data = data, weights = 1/v,
drop.unused.levels = TRUE)
4.eval(mf, parent.frame())
3.eval(mf, parent.frame())
2.lm(formula, data, weights = 1/v)
1.my_fun(dist ~ speed, cars, rep(1, 50))
It seems something wrong when passing the weight vector. Does anyone have a solution?
Thanks in advance!