miceadds
miceadds copied to clipboard
glm.cluster reliant on/generates NULL wgt__ value
I have glm.cluster embedded in a larger function. When I run the function (see below) in a new R session, I get the following error:
Error in eval(extras, data, env) : object 'wgt_' not found
When I run glm.cluster(...) with the parameters saved as variables but without assigning the output, it automatically saves wgt__ as a value in the session environment. This does not return an error and I can then run the overarching function without errors.
It seems to hinge on whether or not wgt__ is saved in the environment - as soon as I remove this from the environment, the error message returns. I can also run the function in a new session by assigning
wgt__ <- NULL before the function call.
I've included example code and some fake data which generates the error.
R Version: 3.6.0 Mac OSX 10.14.5
Minimal working example
`library(tidyverse) library(miceadds)
example_data <- read_csv("example_data.csv")
cluster_glm <- function(data, formula, cluster, type) {
mod <- glm.cluster(formula = formula, data = data, cluster = cluster, weights = NULL, family = if(type == "logit") { binomial(link="logit") } else { gaussian } ) %>% summary(.) %>% as.data.frame(.)
return(mod) }
mod_basic_vote <- cluster_glm(formula = "Y ~ X1 + X2 + X3", data = example_data, type = "logit", cluster = "pid")
wgt__ <- NULL
mod_basic_vote <- cluster_glm(formula = "Y ~ X1 + X2 + X3", data = example_data, type = "logit", cluster = "pid")` mwe.zip
I agree that overwriting wgt__ produces an error. I just did not see an alternative simple method to get the lm formula evaluation running inside the function.
I have been having the same issue as the original poster when calling lm.cluster and glm.cluster within a larger function. Is the best strategy for now to set wgt__ <- NULL just before calling the larger function, as he suggested? Or will this cause other problems that I'm not foreseeing?
@b-johns , adding wgt__ <- NULL before calling the function is a good solution if you do not use weight in glm.cluster. In this case, weight argument in glm.cluster is NULL and corresponds to what has been created before calling the function. If you use weight in glm.cluster, the best way is to add wgt__ <<- weight before glm.cluster, where weight is the vector of weights plugged into the function glm.cluster. Here is an example.
f <- function(...){ .... wgt__ <<- wghts mTP <- glm.cluster(formula = y ~ X, cluster = 'IDClass', weights = wghts, data = data) .... }
The line wgt__ <<- wghtsP creates wgt__ in the global environment because of the assignment approach <<-. This makes sure that the weight wgt__ created is the one used in the function glm.cluster. If there are no weights, wgt__ <<- wghtsP can be replaced with wgt__ <<- NULL.