2

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!

Deku
  • 81
  • 1
  • 3
  • `lm` expects the weights to be coming from your `data` object or from the environment where your formula is defined. See the dup for more details. – MrFlick May 18 '23 at 15:23

0 Answers0