patperry / r-mbest

Moment-Based Estimation for Hierarchical Models (R Package)
Apache License 2.0
24 stars 6 forks source link

Error in evaluating the function #10

Open fanne-stat opened 4 years ago

fanne-stat commented 4 years ago

I found there might be a bug in the function mhglm_ml: when the formula is defined in a function environment, the eval(mf$formula) part will fail.

A minimal example:

test_mhglm <- function(data){
  formula1 <- as.formula("y ~ 1 + x + (1 + x | g1/g2/g3)")
  mhglm_ml(formula = formula1, data = data)
}

test_mhglm(data = g_data)

Here is the error:

 Error in eval(mf$formula) : object 'formula1' not found 

I think this has to be related to the default setting parent.frame() in the eval function.

fanne-stat commented 4 years ago

Sorry one line missing in the minimal example:

g_data <- mhglm_sim(n = 30, m_per_level = c(10, 5, 2), sd_intercept = c(1, 1, 1),
                    sd_slope = c(1, 1, 1), family = "gaussian", seed = 12345)

formula1 <- as.formula("y ~ 1 + x + (1 + x | g1/g2/g3)")

test_mhglm <- function(data){
  formula1 <- as.formula("y ~ 1 + x + (1 + x | g1/g2/g3)")
  mhglm_ml(formula = formula1, data = data)
}

test_mhglm(data = g_data)