I would like to able to call lm
within a function and specify the weights
variable as an argument passed to the outside function that is then passed to lm
. Below is a reproducible example where the call works if it is made to lm
outside of a function, but produces the error message Error in eval(expr, envir, enclos) : object 'weightvar' not found
when called from within a wrapper function.
olswrapper <- function(form, weightvar, df){
ols <- lm(formula(form), weights = weightvar, data = df)
}
df <- mtcars
ols <- lm(mpg ~ cyl + qsec, weights = gear, data = df)
summary(ols)
ols2 <- olswrapper(mpg ~ cyl + qsec, weightvar = gear, df = df)
#Produces error: "Error in eval(expr, envir, enclos) : object 'weightvar' not found"