"Error in eval(extras, data, env) : object 'weight_vector' not found"

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!

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.

Hi,

Thanks for your answer!

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 and I hope you can help me further!

are you intending to fit the model V1 ~ V2 + V3 + V4 + V5 + V6 + V7 + V8 + V9 + V10 ?

some_Other <- function(x, y) fit <- lm(data = x, weights = y)

x <- as.data.frame(matrix(rnorm(500), 50, 10))
y <- matrix(1, 50, 1)

some_Other(x,y) -> fit
fit
#> 
#> Call:
#> lm(data = x, weights = y)
#> 
#> Coefficients:
#> (Intercept)           V2           V3           V4           V5           V6  
#>    0.174822     0.139770     0.062912    -0.201581    -0.117796    -0.021710  
#>          V7           V8           V9          V10  
#>    0.004388     0.083064     0.100941     0.007045

Created on 2020-10-01 by the reprex package (v0.3.0.9001)

Yes, exactly! Sorry for not explaining.

Hi,

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"

solution

someFunction <- function(data, weight_vector){
  lmresult <- lm(formula=V1 ~ .,
                 data=data, 
                 weights = weight_vector)
  return(lmresult)
}
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)

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

2 Likes

Yeah, that’s what I meant earlier about looking at the lm signature

1 Like

Thanks a lot! This fixed my problem!

Sorry, didn't realise that was what you meant at first. Thanks though!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.