I am trying to create a function that runs a linear model for different subsets of the data. When I run lines of code inside the function they work perfectly. However, when I run function itself I keep getting this error:
"Error in eval(extras, data, env) : object 'weight_vector' not found"
R cannot find the variable 'weight_vector' even though it could be printed the line before.
lm(my_data,my_weight_vector)
Error in model.frame.default(formula = my_data, data = my_weight_vector, :
'data' must be a data.frame, not a matrix or an array
After fixing that, look at the function signature for lm for the required form of the arguments beyond that.
The 'data' is actually a data.frame. The reason is you are getting that error is because you do not specify that 'my_weight_vector' is the input for the 'weights' argument of lm.
Try: lm(my_data, weights = my_weight_vector)
This will work, which is what I mean with the fact that the code works when run inside the function but not when run as a function.
Thanks again. This code does indeed work. But it only works because the function is using the 'y' that is the global variable, not the 'y' that is the function input.
Try:
some_Other <- function(x, y) fit <- lm(data = x, weights = y)
x <- as.data.frame(matrix(rnorm(500), 50, 10))
y_new <- matrix(1, 50, 1)
some_Other(x,y_new) -> fit
fit
The error will come back:
"Error in eval(extras, data, env) : object 'y' not found"
explanation,
when you pass 'my_data' as the first param to lm, this is interpreted as corresponding to the formula param. this magically works to know both the data, and assuming the intended formula, but it sets lm to only being aware of objects in the 'data' environment, rather than the calling function. by explicitly passing the required formula to formula, and the data to data we dont confuse the lm model about its calling environment