Skip to content

Commit

Permalink
changed marge2() to properly use GEE mode in backward_sel_WIC()
Browse files Browse the repository at this point in the history
  • Loading branch information
jr-leary7 committed Oct 25, 2024
1 parent 621866e commit 5d29fc9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions R/backward_sel_WIC.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @importFrom gamlss gamlss
#' @importFrom geeM geem
#' @importFrom MASS negative.binomial
#' @importFrom stats vcov
#' @importFrom stats vcov coef
#' @param Y The response variable. Defaults to NULL.
#' @param B_new The model matrix. Defaults to NULL.
#' @param is.gee Is the model a GEE? Defaults to FALSE.
Expand Down Expand Up @@ -47,7 +47,7 @@ backward_sel_WIC <- function(Y = NULL,
vcov_mat <- try({ stats::vcov(fit, type = "all") }, silent = TRUE)
if (inherits(vcov_mat, "try-error")) {
covmat_unscaled <- chol2inv(fit$mu.qr$qr[1:(fit$mu.df - fit$mu.nl.df), 1:(fit$mu.df - fit$mu.nl.df), drop = FALSE])
wald_stat <- unname(coef(fit) / sqrt(diag(covmat_unscaled))^2)[-1]
wald_stat <- unname(stats::coef(fit) / sqrt(diag(covmat_unscaled))^2)[-1]
} else {
wald_stat <- unname((vcov_mat$coef / vcov_mat$se)[-c(1, length(vcov_mat$coef))]^2)
}
Expand Down
25 changes: 17 additions & 8 deletions R/marge2.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ marge2 <- function(X_pred = NULL,
}
q <- ncol(X_pred) # Number of predictor variables
B <- as.matrix(rep(1, NN)) # Start with the intercept model.
# estimate "known" theta for GEE models
if (is.gee || !is.glmm) {
theta_hat <- MASS::theta.mm(y = Y,
mu = mean(Y),
dfr = NN - 1)
}
# estimate "known" theta for NB distribution
theta_hat <- MASS::theta.mm(y = Y,
mu = mean(Y),
dfr = NN - 1)

pen <- 2 # penalty for GCV criterion -- could also switch to log(N) later
colnames(B) <- "Intercept"
Expand Down Expand Up @@ -777,7 +775,13 @@ marge2 <- function(X_pred = NULL,
colnames(wic_mat_2)[(ncol_B + 1)] <- "Forward pass model"

wic_mat_2[1, (ncol_B + 1)] <- full.wic
wic1_2 <- backward_sel_WIC(Y = Y, B_new = B_new)
wic1_2 <- backward_sel_WIC(Y = Y,
B_new = B_new,
is.gee = is.gee,
id.vec = id.vec,
cor.structure = cor.structure,
theta.hat = theta_hat,
sandwich.var = sandwich.var)
wic_mat_2[2, 2:(length(wic1_2) + 1)] <- wic1_2
WIC_2 <- sum(apply(wic_mat_2[seq(2), ], 1, min, na.rm = TRUE)) + 2 * ncol(B_new)
WIC_vec_2 <- c(WIC_vec_2, WIC_2)
Expand All @@ -788,7 +792,12 @@ marge2 <- function(X_pred = NULL,
cnames_2 <- c(cnames_2, list(colnames(B_new_2)))
for (i in 2:(ncol_B - 1)) {
wic1_2 <- backward_sel_WIC(Y = Y,
B_new = B_new_2)
B_new = B_new_2,
is.gee = is.gee,
id.vec = id.vec,
cor.structure = cor.structure,
theta.hat = theta_hat,
sandwich.var = sandwich.var)
if (i != (ncol_B - 1)) {
wic_mat_2[(i + 1), colnames(B_new_2)[-1]] <- wic1_2
WIC_2 <- sum(apply(wic_mat_2[seq(i + 1), ], 1, min, na.rm = TRUE)) + 2 * ncol(B_new_2)
Expand Down

0 comments on commit 5d29fc9

Please sign in to comment.