Skip to content

Commit

Permalink
Internal tooling fix (#12)
Browse files Browse the repository at this point in the history
* added more explicit HTTR retrieval

* Update internal_crcTools.R

* Added cmd required imports

* added explicit documentation for internal stepwise
  • Loading branch information
ryan-odea authored Aug 13, 2024
1 parent b03d912 commit e9633ab
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Syndemics/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ importFrom(httr,GET)
importFrom(httr,config)
importFrom(httr,content)
importFrom(httr,stop_for_status)
importFrom(stats,AIC)
importFrom(stats,as.formula)
importFrom(stats,coef)
importFrom(stats,confint)
importFrom(stats,formula)
importFrom(stats,glm)
importFrom(stats,poisson)
importFrom(stats,step)
importFrom(utils,capture.output)
importFrom(utils,combn)
importFrom(utils,read.csv)
4 changes: 1 addition & 3 deletions Syndemics/R/crc.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ crc <- function(data, freq.column, binary.variables, method = "poisson", formula
verbose = TRUE)){
dt <- data.table::as.data.table(data)
cor <- NULL
glm <- coef <- AIC <- dbcnt <- NULL
confint <- NULL
step_regression <- NULL
glm <- coef <- AIC <- dbcnt <- NULL

data_expansion <- data.table()
data_expansion <- dt[rep(1:.N, get(freq.column))][, (freq.column) := NULL]
Expand Down
65 changes: 65 additions & 0 deletions Syndemics/R/internal_crcTools.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,68 @@ formula_corr <- function(corr_matrix, threshold, freq.column){

return(formula_object)
}

#' Helper function for stepwise regression
#' @param data dataframe
#' @param y string: LHS of formula object
#' @param x string: RHS of formula object
#' @param method string: either 'poisson' or 'negbin'
#' @param direction string: stepwise direction
#' @param p.threshold numeric: threshold for stepwise selection
#' @param k integer: limit for k-way interaction terms
#' @param verbose
#'
#'
#' @keywords internal
#' @importFrom MASS glm.nb
#' @importFrom utils capture.output
#' @importFrom stats AIC coef confint formula glm poisson step

step_regression <- function(data, y, x, method = "poisson", direction = "both", p.threshold = 0.05, k = 2, verbose = TRUE) {
formula_init <- as.formula(paste(y, "~", paste(x, collapse = " + ")))
formula_max <- as.formula(paste(y, "~ (", paste(x, collapse = " + "), ")^", k))

if (!verbose) {
capture.output({
if (method == "poisson") {
init_mod <- glm(formula_init, family = poisson, data = data)
} else if (method == "negbin") {
init_mod <- glm.nb(formula_init, data = data)
}

final_mod <- suppressWarnings(step(init_mod,
scope = list(upper = formula_max, lower = formula_init),
direction = direction,
k = log(nrow(data))))
})
} else {
if (method == "poisson") {
init_mod <- glm(formula_init, family = poisson, data = data)
} else if (method == "negbin") {
init_mod <- glm.nb(formula_init, data = data)
}

final_mod <- step(init_mod,
scope = list(upper = formula_max, lower = formula_init),
direction = direction,
k = log(nrow(data)))
}

intercept <- coef(final_mod)[1]
estimate <- exp(intercept)
ci <- exp(confint(final_mod)[1, ])


results <- list(
model = method,
formula = formula(final_mod),
summary = summary(final_mod),
estimate = unname(estimate),
lower_ci = unname(ci[1]),
upper_ci = unname(ci[2]),
AIC = AIC(final_mod)
)

return(results)
}

38 changes: 38 additions & 0 deletions Syndemics/man/step_regression.Rd

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

0 comments on commit e9633ab

Please sign in to comment.