Skip to content

Commit

Permalink
Merge pull request #192 from jr-leary7/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jr-leary7 authored Jun 5, 2024
2 parents a3746b3 + 4adb067 commit e0c0125
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Depends:
Imports:
geeM,
MASS,
mpath,
dplyr,
stats,
utils,
Expand Down
40 changes: 30 additions & 10 deletions R/fitGLMM.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#' @param M.glm The number of possible basis functions to use in the calls to \code{\link{marge2}} when choosing basis functions adaptively.
#' @param return.basis (Optional) Whether the basis model matrix (denoted \code{B_final}) should be returned as part of the \code{marge} model object. Defaults to FALSE.
#' @param return.GCV (Optional) Whether the final GCV value should be returned as part of the \code{marge} model object. Defaults to FALSE.
#' param vebose (Optional) Should intermediate output be printed to the console? Defaults to FALSE.
#' @return An object of class \code{marge} containing the fitted model & other optional quantities of interest (basis function matrix, GCV, etc.).
#' @seealso \code{\link[glmmTMB]{glmmTMB}}
#' @seealso \code{\link{testDynamic}}
Expand All @@ -38,7 +39,8 @@ fitGLMM <- function(X_pred = NULL,
approx.knot = TRUE,
M.glm = 3,
return.basis = FALSE,
return.GCV = FALSE) {
return.GCV = FALSE,
verbose = FALSE) {
# check inputs
if (is.null(X_pred) || is.null(Y) || is.null(id.vec)) { stop("You forgot some inputs to fitGLMM().") }
if (is.unsorted(id.vec)) { stop("Your data must be ordered by subject, please do so before running fitGLMM().") }
Expand Down Expand Up @@ -74,17 +76,35 @@ fitGLMM <- function(X_pred = NULL,
})
marge_style_names <- glm_marge_knots$old_coef
coef_names <- glm_marge_knots$coef
if (verbose) {
message(paste0("Generated a total of ",
length(coef_names),
" basis functions across ",
length(unique(id.vec)),
" subjects."))
}
# run NB LASSO with all possible basis functions
lasso_formula <- stats::as.formula(paste0("Y ~ ", paste0(colnames(glmm_basis_df), collapse = " + ")))
pruned_model <- mpath::glmregNB(lasso_formula,
data = glmm_basis_df,
offset = log(1 / Y.offset),
parallel = FALSE,
nlambda = 50,
alpha = 1,
standardize = TRUE,
trace = FALSE,
link = log)
if (is.null(Y.offset)) {
pruned_model <- mpath::glmregNB(lasso_formula,
data = glmm_basis_df,
parallel = FALSE,
nlambda = 50,
alpha = 1,
standardize = TRUE,
trace = FALSE,
link = log)
} else {
pruned_model <- mpath::glmregNB(lasso_formula,
data = glmm_basis_df,
offset = log(1 / Y.offset),
parallel = FALSE,
nlambda = 50,
alpha = 1,
standardize = TRUE,
trace = FALSE,
link = log)
}
# identify nonzero basis functions in minimum AIC model
nonzero_coefs <- which(as.numeric(pruned_model$beta[, which.min(pruned_model$aic)]) != 0)
# build formula automatically
Expand Down
6 changes: 4 additions & 2 deletions man/fitGLMM.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e0c0125

Please sign in to comment.