yonicd / lmmen

R package that solves the linear mixed model elastic net
2 stars 4 forks source link

multiple random effects (crosspost from R-sig-mixed) #5

Open mebrooks opened 1 year ago

mebrooks commented 1 year ago

Sorry but I'm crossposting from R-sig-mixed. At first I didn't see this was the same as the CRAN package. I'll update here if I get anything useful there.

Both of the models below can be fit in glmmLasso, but only the one with a single RE (lm1) can be cross-validated with lmmen. I was getting a similar error with cv.glmmLasso:: cv.glmmLasso until I made the change described in this commit . For that package, it was due to assuming that q_start was a scalar (as in the case of a single RE).

library(glmmLasso)
data("soccer")
library(lmmen)

soccer[,c(4,5,9:16)]<-scale(soccer[,c(4,5,9:16)],center=TRUE,scale=TRUE)
soccer<-data.frame(soccer)

lm1 <- glmmLasso(points ~ transfer.spendings + ave.unfair.score 
                 + ball.possession + tackles 
                 + ave.attend + sold.out, rnd = list(team=~1), 
                 lambda=50, data = soccer)

summary(lm1)

lm2 <- glmmLasso(points ~ transfer.spendings + ave.unfair.score 
                 + ball.possession + tackles 
                 + ave.attend + sold.out, rnd = list(team=~1, pos=~1), 
                 lambda=50, data = soccer)

summary(lm2)

cv.lm1 <- cv.glmmLasso(form.fixed= points ~ transfer.spendings + ave.unfair.score 
                    + ball.possession + tackles 
                    + ave.attend + sold.out, 
                    form.rnd = list(team=~1), 
                    lambda=seq(5,250, by=5), dat = soccer)

cv.lm2 <- cv.glmmLasso(form.fixed= points ~ transfer.spendings + ave.unfair.score 
                    + ball.possession + tackles 
                    + ave.attend + sold.out, 
                    form.rnd = list(team=~1, pos=~1), 
                    lambda=seq(5,250, by=5), dat = soccer)

This is the error (below).

> cv.lm2=cv.glmmLasso(form.fixed= points ~ transfer.spendings + ave.unfair.score 
+                     + ball.possession + tackles 
+                     + ave.attend + sold.out, 
+                     form.rnd = list(team=~1, pos=~1), 
+                     lambda=seq(5,250, by=5), dat = soccer)
Error in diag(diag(q_start), sum(s)) : 
  'nrow' or 'ncol' cannot be specified when 'x' is a matrix
In addition: There were 50 or more warnings (use warnings() to see the first 50)
> traceback()
8: stop("'nrow' or 'ncol' cannot be specified when 'x' is a matrix")
7: diag(diag(q_start), sum(s))
6: est.glmmLasso.RE(fix = fix, rnd = rnd, data = data, lambda = lambda, 
       family = family, final.re = final.re, switch.NR = switch.NR, 
       control = control)
5: est.glmmLasso(fix, rnd, data = data, lambda = lambda, family = family, 
       switch.NR = switch.NR, final.re = final.re, control = control)
4: glmmLasso::glmmLasso(fix = as.formula(form.fixed), rnd = form.rnd, 
       data = dat, lambda = lambda[opt], switch.NR = FALSE, final.re = TRUE, 
       control = list(start = Delta.start[opt, ], q_start = Q.start.base))
3: withCallingHandlers(expr, warning = function(w) if (inherits(w, 
       classes)) tryInvokeRestart("muffleWarning"))
2: suppressWarnings({
       final <- glmmLasso::glmmLasso(fix = as.formula(form.fixed), 
           rnd = form.rnd, data = dat, lambda = lambda[opt], switch.NR = FALSE, 
           final.re = TRUE, control = list(start = Delta.start[opt, 
               ], q_start = Q.start.base))
       final
   })
1: cv.glmmLasso(form.fixed = points ~ transfer.spendings + ave.unfair.score + 
       ball.possession + tackles + ave.attend + sold.out, form.rnd = list(team = ~1, 
       pos = ~1), lambda = seq(5, 250, by = 5), dat = soccer)
mebrooks commented 1 year ago

This error seems to be fixed in @bbolker's fork.

yonicd commented 1 year ago

happy to merge a PR is @bbolker wants to create one.