I want to pass a glm model to a self-defined function but run into the error message
Error in eval(expr, envir, enclos) : object ... not found
I want to pass the fitted glm model and use the same formula, family, data and weights as the input to fit a new model. I have tried with the code below (which works fine if I just set glmModel = testModel and run the code within the function manually). What goes wrong?
glmPassing <- function(glmModel){
weightsTest = glmModel$weights
glmTest = glm(formula = glmModel$formula, family = glmModel$family, data = glmModel$data, weights = weightsTest)
summary(glmTest)
}
test = iris
testModel = glm(Sepal.Length ~ Sepal.Width + I(Sepal.Width^2), family = gaussian, data = test, weights = Petal.Width)
glmPassing(testModel)
I have separated the weights to a new variable since I found out that's what causes the error message. With the code above the error message is
Error in eval(expr, envir, enclos) : object 'weightsTest' not found
Thank you!
glmTest = update(glmModel, weights = weightsTest). I think the problem is that variables are resolved where the formula is created, not where the function is called from. - MrFlick