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

Incorrect result by predict.mmblogit() in case of multiple random formulas #24

Closed
fweber144 opened this issue Apr 12, 2022 · 3 comments
Closed

Comments

@fweber144
Copy link

fweber144 commented Apr 12, 2022

I'm not 100% sure, but I think line

ZD <- blockMatrix(ZD)
needs to be removed. Consider the following reprex:

data(VA, package = "MASS")
ngrp <- 8L
VA$grp <- gl(n = ngrp,
             k = floor(nrow(VA) / ngrp),
             length = nrow(VA),
             labels = paste0("gr", seq_len(ngrp)))
set.seed(2643)
VA$grp <- sample(VA$grp)

nsbj <- 13L
VA$sbj <- gl(n = nsbj,
             k = floor(nrow(VA) / nsbj),
             length = nrow(VA),
             labels = paste0("subj", seq_len(nsbj)))
VA$sbj <- sample(VA$sbj)

fit_multi <- mclogit::mblogit(cell ~ treat + age + Karn,
                              random = list(~ 1 | sbj,
                                            ~ 1 | grp),
                              data = VA)

VA_new <- head(VA, 1)
VA_new$sbj <- "subj1"
VA_new$grp <- "gr1"
lpreds_multi <- predict(fit_multi, newdata = VA_new)

# Manual check:
VA_new$treat2 <- as.numeric(VA_new$treat == "2")
fixnms_b <- c("treat2", "age", "Karn")
nlats <- nlevels(VA$cell) - 1L
mfixef <- fit_multi$coefmat
mranef <- setNames(fit_multi$random.effects, names(fit_multi$groups))
# Actual result:
lpreds_multi_man <- matrix(mfixef[, "(Intercept)"],
                           nrow = nrow(VA_new),
                           ncol = nlats, byrow = TRUE) +
  as.matrix(VA_new[, fixnms_b, drop = FALSE]) %*% t(mfixef[, fixnms_b]) +
  t(mranef$sbj[1:3, ])
all.equal(unname(lpreds_multi), unname(lpreds_multi_man), tolerance = 1e-15)
## --> Gives TRUE.
# Expected result:
lpreds_multi_man_expect <- lpreds_multi_man +
  t(mranef$grp[1:3, ])
all.equal(unname(lpreds_multi), unname(lpreds_multi_man_expect), tolerance = 1e-15)
## --> Gives "Mean relative difference: 0.2396385".

# After commenting line `ZD <- blockMatrix(ZD)` in mclogit:::predict.mmblogit() and
# running
lpreds_multi <- predict(fit_multi, newdata = VA_new)
# I get
all.equal(unname(lpreds_multi), unname(lpreds_multi_man), tolerance = 1e-15)
## --> Gives "Mean relative difference: 0.2759492".
# and
all.equal(unname(lpreds_multi), unname(lpreds_multi_man_expect), tolerance = 1e-15)
## --> Gives TRUE.
# as expected.

EDIT: I was using mclogit version 0.9.4.1 (at commit dadb418) for this.

@melff
Copy link
Owner

melff commented Apr 12, 2022

Thanks for the report and the vigorous testing. I'll see to it ASAP.

@melff
Copy link
Owner

melff commented Apr 14, 2022

This appears to be fixed in release 0.9.4.2

@melff melff closed this as completed Apr 19, 2022
fweber144 added a commit to fweber144/projpred that referenced this issue Apr 20, 2022
@fweber144
Copy link
Author

Yes, I can confirm this. Thank you!

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

No branches or pull requests

2 participants