Hi!
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.
Here is a reproducible example of my code:
someFunction <- function(data, weight_vector){
print(weight_vector)
lm <- lm(data, weights = weight_vector)
return(lm)
}
my_data <- as.data.frame(matrix(rnorm(500), 50, 10))
my_weight_vector <- matrix(1, 50, 1)
try <- someFunction(data = my_data, weight_vector = my_weight_vector)
#data <- my_data
#weight_vector <- my_weight_vector
Running the function gives an error. However when I uncomment the last two lines and run the lines within the function I get the desired result.
Does anyone have an idea what is going wrong?
Thanks in advance!