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

address numerical instability for small and sparse samples #26

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions R/multinom_get_augmentations.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# adapted from github.com/statdivlab/radEmu, included in raoBust to avoid dependence on radEmu
# helper function for multinomial logistic regression with Firth penalty
multinom_get_augmentations <- function(G, Y, B, z) {
multinom_get_augmentations <- function(G, Y, B, z, use_pseudo = FALSE) {

n <- length(z)
p <- nrow(B)
Expand All @@ -23,8 +23,14 @@ multinom_get_augmentations <- function(G, Y, B, z) {
info <- Matrix::forceSymmetric(info)

# get symmetric square root of inverse of info matrix
info_chol <- Matrix::chol(info, pivot = FALSE)
info_half_inv <- Matrix::solve(info_chol)
if (!use_pseudo) {
info_chol <- Matrix::chol(info, pivot = FALSE)
info_half_inv <- Matrix::solve(info_chol)
} else {
info_inv <- MASS::ginv(as.matrix(info))
info_half_inv <- Matrix::chol(info_inv, pivot = TRUE)
}


# get augmentations
augs <- matrix(0, nrow = n, ncol = J)
Expand Down
2 changes: 1 addition & 1 deletion R/multinom_penalized_estimation.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ multinom_penalized_estimation <- function(beta, X, Y, null = TRUE, strong = FALS
z <- log(rowSums(Y)) - log(rowSums(exp(Xaug %*% beta_curr)))

# calculate augmentations
augs <- multinom_get_augmentations(G, Y, beta_curr, z)
augs <- multinom_get_augmentations(G, Y, beta_curr, z, pseudo_inv)
Y_curr <- Y + augs

# get new beta values
Expand Down
14 changes: 12 additions & 2 deletions R/multinom_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,19 @@ covariates in formula must be provided.")
#compute mle under alternative
beta_alt <- matrix(-0.02, nrow = p + 1, ncol = J-1)
if (!penalty) {
mle_alt <- multinom_fisher_scoring(beta_alt, X, Y, null = FALSE, pseudo_inv = pseudo_inv)
mle_alt <- tryCatch({multinom_fisher_scoring(beta_alt, X, Y, null = FALSE, pseudo_inv = pseudo_inv)},
error = function(cond) {
warning("MLE under alternative hypothesis cannot be calculated.")
print(cond)
return(NA)
})
} else {
mle_alt <- multinom_penalized_estimation(beta_alt, X, Y, null = FALSE, pseudo_inv = pseudo_inv)
mle_alt <- tryCatch({multinom_penalized_estimation(beta_alt, X, Y, null = FALSE, pseudo_inv = pseudo_inv)},
error = function(cond) {
warning("MLE under alternative hypothesis cannot be calculated.")
print(cond)
return(NA)
})
}

return(list("test_stat" = T_GS,
Expand Down
Loading