0

Goal: get R^2 marginal and conditional in dredge results using an optimizer in the origninal model

This branches off of this question: dredge doesnt work when specifying glmer optimizer and the two solutions provided.

Solution 1: change r.squaredLR.R package code

Solution 2: add a function into the dredge function to call r.squaredGLMM instead of r.squaredLR

I tried Solution 2 first, which works perfectly on the simulated data, but when I try it on my model I get the error :

Error in r.squaredGLMM(x, null = nullmodel)["delta", ] : subscript out of bounds

I then tried Solution 1 by altering the source code of r.squaredLR.R as descripbed and saving it as a R script and using source() to call the edited 'null.fit' function as to avoid editing r.squaredLR.R permenantly (I call MuMIn before sourcing the edited function). Yet this doesn't work.

Back to Solution 2...

I tried to simulate data similar to mine and was able to get the same error (the lmercontrol argument is disregarded in this global model, but I get the desired error so I didn't try to correct the data to need lmercontrol).

#Solution 2 attempt
set.seed(101)
dd <- data.frame(x1= rnorm(1920), x2=rnorm(1920), x3=rnorm(1920), x4=rnorm(1920),
                 treatment = factor(rep(1:2, each=3)),
                 replicate = factor(rep(1:3, each=1)),
                 stage = factor(rep(1:5, each=384)),
                 country = factor(rep(1:4, each=96)),
                 plot = factor(rep(1:10, each=24)),
                 chamber = factor(rep(1:6, each=1)),
                 n = 1920)

library(lme4)
dd$y <- simulate(~ x1 + x2 + x3 + (1|plot),
                 family = binomial,
                 weights = dd$n,
                 newdata = dd,
                 newparams = list(beta = c(1,1,1,1),
                                theta = 1))[[1]]

# my real response variable 'y' has a poisson distribution, but I had difficulty figuring 
# out how to simulate a poisson distribution so I left the bionomial. 

m0 <- lmer(y~ x1 + x2 + x3 + x4 + treatment*replicate*stage + (1|chamber) + (1|country/plot),
            data=dd,
            na.action = "na.fail",
           REML = F,
            lmercontrol = glmerControl(optimizer="bobyqa"))

nullmodel <- MuMIn:::.nullFitRE(m0)
dredge(m0, m.lim = c(0,5), rank = "AIC", extra =list(R2 = function(x)     {
  r.squaredGLMM(x, null = nullmodel)["delta", ]}))

A suggested reason for the error "subscript out of bounds" was that "the data being put into the algorithm are not in the format that the function expects."

Indeed, the function works when I remove ["delta", ] and I get the columns R21 and R22, but without taking into account the delta column these values are probably incorrect and I'm not sure which one is marginal and conditional R^2.

If you have any ideas, I'm all ears! Thanks in advance for all help.

  • Ben Bolker should probably have mentioned it: `dredge` is a pretty dubious practice for model building and R² is certainly not the objective measure you should focus on. – Roland Jul 24 '19 at 10:56
  • Regarding solution 1: https://stackoverflow.com/questions/12178830/change-internal-function-of-a-package – Roland Jul 24 '19 at 10:56
  • Solution 2 is the way to go. But, since you are using "lmer" (so, a gaussian family), R_GLMM^2 does not use the delta method, and the function result does not have the respective row named "delta". Hence the "out of bounds" error. It helps if you try your `extra` function on the global model first. – Kamil Bartoń Jul 24 '19 at 15:13
  • @ Roland, thank you for the link, I ended up using assignInNamespace() which did the trick for **Solution 1**, and thanks for your further details on dredge and R². I will be using the R² as a supplementary detail, but I have a large number of models so calculating it independantly would be very time consuming. – L.Gillespie Jul 26 '19 at 07:09
  • @ Kamil Bartoń, in a quick search I didn't find an 'extra' argument for lmer in the lme4 package to put the argument in the global model. Although Solution 2 is preferable, I was able to make it work with Solution 1 so I will be using that. Thanks for your help! – L.Gillespie Jul 26 '19 at 07:12
  • @L.Gillespie: By the `extra` function I meant your `R2` function which you pass as the `extra` argument to `dredge`. Replacing `r.squaredLR` may not give you what you want (even if it technically "works"), because `dredge` does a few tricks when you use the `extra="R^2"` argument to optimize the computation. So I'd advise against your "solution 1". – Kamil Bartoń Jul 26 '19 at 11:56

0 Answers0