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

Dev #192

Merged
merged 3 commits into from
Jun 5, 2024
Merged

Dev #192

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
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.

Loading