Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glm.cluster reliant on/generates NULL wgt__ value #18

Open
tsrobinson opened this issue Aug 2, 2019 · 3 comments
Open

glm.cluster reliant on/generates NULL wgt__ value #18

tsrobinson opened this issue Aug 2, 2019 · 3 comments
Labels

Comments

@tsrobinson
Copy link

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

@alexanderrobitzsch
Copy link
Owner

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.

@b-johns
Copy link

b-johns commented Nov 17, 2019

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?

@ahoundetoungan
Copy link

ahoundetoungan commented Feb 11, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants