0

I am trying to fit a one-inflated Beta regression mixed model using the gamlss package using the following code:

# Minimal reproducible example:
efficiency <- rbeta(n = 85, shape1 = 2, shape2 = 2, ncp = 2) # My actual variable contains several "ones", but it does not matter here
distance <- rpois(n = 85, lambda = 4)
manager_id <- c(rep(1, 10), rep(2, 2), 3:75)
manager_id <- as.factor(manager_id)
eff <- data.frame(efficiency, distance, manager_id)

mod1 <- gamlss::gamlss(efficiency~distance + gamlss::re(random = ~1|manager_id, method = "ML"),
                      data = eff, family = gamlss.dist::BEOI())

Yet I get the following error:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"function"’ to a data.frame

I believe the error comes from the nlme::lme() function that is called by gamlss::re() to fit the random effect because it works fine when I do not include this part of the code. I could perhaps use gamlss::random() instead of gamlss::re() to fit the random effect but the help page of these functions states that re() should be used when the normality assumption of the response variable cannot be satisfied.
Could someone please help me figure out how to solve this problem?

Fanfoué
  • 165
  • 11
  • FYI, stop using `data.frame(cbind(..))`, just use `data.frame`; the column names and data are *mostly* the same, but when using your current method, the factors are converted back to strings (see `str(eff)`, showing `num` in my console). I don't know if that would break the random effects, but it is the difference in *appearance* between a categorical and continuous variable `manager_id`. (I don't have `gamlss` installed, so cannot further test.) Good luck! – r2evans Apr 27 '21 at 12:36
  • 1
    @r2evans Thank you for your comment, I will change the code accordingly. However, in my actual dataset, `manager_id` truly is a `factor` and so the error is triggered by something else. – Fanfoué Apr 27 '21 at 14:24
  • If you use `re()` without `gamlss::` before it seems to be ok, actually I don't know why but I can't find the re() source code and maybe we can't call it from gamlss – Basti Apr 27 '21 at 14:32
  • @BastienDucreux you are right, thank you! This is strange indeed but now the code works (yet the model does not seem to fit my data well, but that's another problem). – Fanfoué Apr 27 '21 at 14:59

0 Answers0