Skip to content

Commit

Permalink
Get all warnings out of R CMD check
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Koeneman committed Sep 12, 2023
1 parent ce7cb4d commit 01441e3
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 58 deletions.
25 changes: 13 additions & 12 deletions R/BootGOFTestLM.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#' @description
#' Performs a bootstrap goodness-of-fit procedure to assess the fit of a normal linear regression model
#'
#' @param model A fitted `lm` object.
#' @param data A dataframe used to fit `model`.
#' @param x A fitted \code{lm} object.
#' @param data A dataframe used to fit the model given by \code{x}.
#' @param boot_iter An integer indicating number of bootstrap iterations to perform.
#' @param level Confidence level of the bootstrap interval used in the test.
#' @param return_dist A logical specifying whether to optionally return the bootstrap distribution. Defaults to FALSE.
#' @param ... Additional arguments.
#' @return A list containing the specification and results of the test.
#'
#' @examples
Expand All @@ -21,19 +22,19 @@
#' BootGOFTestLM(model_list[[length(model_list)]], data = data)
#'
#' @export
BootGOFTestLM <- function(model, data, boot_iter = 1000, level = 0.95, return_dist = FALSE){
if(!inherits(model,"lm")){
BootGOFTestLM <- function(x, data, boot_iter = 1000, level = 0.95, return_dist = FALSE, ...){
if(!inherits(x,"lm")){
stop("Model supplied is not a linear model. A model of class lm must be supplied.")
}

boot_dist <- sapply(1:boot_iter, FUN = function(x){
boot_dist <- sapply(1:boot_iter, FUN = function(t){
boot_data <- data[sample(1:nrow(data), nrow(data), TRUE),]
return(SandwichEstGOF(lm(formula(model), data = boot_data)))
return(SandwichEstGOF(lm(formula(x), data = boot_data)))
})
boot_int <- unname(quantile(boot_dist, c((1-level)/2,(1-(1-level)/2))))

out <- list(
null_val = 2*nobs(model),
null_val = 2*nobs(x),
level = level,
boot_iter = boot_iter,
boot_int = boot_int
Expand All @@ -58,12 +59,12 @@ print.BootGOFTestLM <- function(x, ...) {
}


SandwichEstGOF <- function(model){
n <- nobs(model)
y <- matrix(model.response(model.frame(model)), nrow = n, ncol = 1)
X <- model.matrix(model)
SandwichEstGOF <- function(x){
n <- nobs(x)
y <- matrix(model.response(model.frame(x)), nrow = n, ncol = 1)
X <- model.matrix(x)
p <- ncol(X)
B <- matrix(coef(model), nrow = p, ncol = 1)
B <- matrix(coef(x), nrow = p, ncol = 1)


sighat2 <- as.numeric(t(y-(X%*%B)) %*% (y-(X%*%B))*(1/n))
Expand Down
6 changes: 3 additions & 3 deletions R/FitGLMSubsets.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#' Perform all subsets regression for generalized linear models
#'
#' @description
#' Fit a specified generalized linear model on all subsets of covariates supplied. Produces an output suitable for use with the `StandICModelSelect` function.
#' Fit a specified generalized linear model on all subsets of covariates supplied. Produces an output suitable for use with the \code{StandICModelSelect} function.
#'
#' @param response A character string specifying the name of the response variable.
#' @param data A dataframe containing a column corresponding to the response variable in addition to columns for each covariate of interest.
#' @param family A family suitable for supplying to the `glm` function specifying the error distribution and link function.
#' @param family A family suitable for supplying to the \code{glm} function specifying the error distribution and link function.
#' @param intercept A logical indicating whether an intercept term should be considered in models. Defaults to TRUE.
#' @param force_intercept A logical indicating whether to force an intercept term into all models if an intercept is desired. Defaults to TRUE.
#' @param ... Additional arguments that may be supplied when calling `glm` to fit the models of interest.
#' @param ... Additional arguments that may be supplied when calling \code{glm} to fit the models of interest.
#'
#' @return A list of fitted linear models suitable for use with the `StandICModelSelect` function.
#'
Expand Down
35 changes: 18 additions & 17 deletions R/StandICModelSelect.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#' @description
#' Perform model selection on a list of models using standardized information criteria.
#'
#' @param model_list A list containing the fitted model objects on which to perform model selection. Model objects must have a `logLik` method defined for them.
#' @param IC A character string containing the base information criteria to use. Options are `AIC`, `BIC`, and `AICc` for linear models. Default option is `AIC`.
#' @param ref_model_index An integer with the index of the largest candidate model to use as the reference. If not supplied, defaults to the model with largest number of estimated coefficients in `model_list`.
#' @param x A list containing the fitted model objects on which to perform model selection. Model objects must have a \code{logLik} method defined for them.
#' @param IC A character string containing the base information criteria to use. Options are "AIC", "BIC", and "AICc" for linear models. Default option is `AIC`.
#' @param ref_model_index An integer with the index of the largest candidate model to use as the reference. If not supplied, defaults to the model with largest number of estimated coefficients in \code{x}.
#' @param sd_cutoff A numeric describing how many standard deviations to use when formulating a cutoff for model viability.
#' @param user_df An optional vector the same length as `model_list` where one can specify the degrees of freedom of each fitted model. If not supplied, the degrees of freedom for each model is calculated to be the number of estimated regression coefficients.
#' @param user_df An optional vector the same length as \code{x} where one can specify the degrees of freedom of each fitted model. If not supplied, the degrees of freedom for each model is calculated to be the number of estimated regression coefficients.
#' @param ... Additional arguments.
#'
#' @return A list containing the final model selected in addition to standardized information criteria and difference in degrees of freedom for all candidate models.
#'
Expand All @@ -24,30 +25,30 @@
#' # display best model
#' model_select$best_model
#' @export
StandICModelSelect <- function(model_list, IC = "AIC", ref_model_index = NULL, sd_cutoff = 2, user_df = NULL){
StandICModelSelect <- function(x, IC = "AIC", ref_model_index = NULL, sd_cutoff = 2, user_df = NULL, ...){
if(!(IC[1] %in% c("AIC", "BIC", "AICc"))){
stop("Selected IC is not a valid option. Please select a valid IC.")
}
if(!inherits(model_list,"list") | length(model_list) == 0){
if(!inherits(x,"list") | length(x) == 0){
stop("Supplied model list is empty or not a list. Please supply a valid model list.")
}

if(!is.null(user_df)){
df_vec <- user_df
} else{
df_vec <- sapply(model_list, FUN = function(x){return(length(coef(x)))})
df_vec <- sapply(x, FUN = function(t){return(length(coef(t)))})
}
IC_vec <- sapply(model_list, FUN = function(x){return(eval(parse(text = paste0(IC,"(x)"))))})
IC_vec <- sapply(x, FUN = function(t){return(eval(parse(text = paste0(IC,"(t)"))))})

ref_model_index <- ifelse(is.null(ref_model_index),which.max(df_vec),ref_model_index)
tryCatch({invisible(logLik(model_list[[ref_model_index]]))},
tryCatch({invisible(logLik(x[[ref_model_index]]))},
error = function(cond){message("Reference model object does not have logLik method.
Please submit a valid model object.")}
)

ref_IC <- eval(parse(text = paste0(IC,"(model_list[[",ref_model_index,"]])")))
ref_df <- length(coef(model_list[[ref_model_index]]))
n <- nobs(model_list[[ref_model_index]])
ref_IC <- eval(parse(text = paste0(IC,"(x[[",ref_model_index,"]])")))
ref_df <- length(coef(x[[ref_model_index]]))
n <- nobs(x[[ref_model_index]])

if(sum(df_vec >= ref_df) > 1){
stop("Reference model is not the largest candidate model. Please specify the sole largest candidate model.")
Expand All @@ -59,9 +60,9 @@ StandICModelSelect <- function(model_list, IC = "AIC", ref_model_index = NULL, s
2*ref_df*(n/(n-ref_df-1)))/sqrt(2*(ref_df-df_vec)) )
}

stand_IC <- sapply(1:length(model_list), FUN = function(x){
model <- model_list[[x]]
df <- df_vec[x]
stand_IC <- sapply(1:length(x), FUN = function(t){
model <- x[[t]]
df <- df_vec[t]
IC_val <- eval(parse(text = paste0(IC,"(model)")))
if(df == ref_df){
return(
Expand All @@ -79,12 +80,12 @@ StandICModelSelect <- function(model_list, IC = "AIC", ref_model_index = NULL, s
BIC = sqrt(1/2)*(1-log(n)) + sd_cutoff,
AICc = max_expect + sd_cutoff)
meets_cutoff <- (stand_IC < cutoff)
best_model_index <- ((1:length(model_list))[meets_cutoff])[which(df_vec[meets_cutoff] == min(df_vec[meets_cutoff]))]
best_model_index <- ((1:length(x))[meets_cutoff])[which(df_vec[meets_cutoff] == min(df_vec[meets_cutoff]))]
best_model_index <- best_model_index[which.min(stand_IC[best_model_index])]

out <- list(
best_model_index = best_model_index,
best_model = model_list[[best_model_index]],
best_model = x[[best_model_index]],
meets_cutoff = meets_cutoff,
IC = IC,
sd_cutoff = sd_cutoff,
Expand Down
15 changes: 12 additions & 3 deletions man/BootGOFTestLM.Rd

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

27 changes: 13 additions & 14 deletions man/DBModelSelect-package.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@
The DESCRIPTION file:
\packageDESCRIPTION{DBModelSelect}
\packageIndices{DBModelSelect}
~~ An overview of how to use the package, including the most important functions ~~
The \code{DBModelSelect package} provides several methods of
model selection based in distributional theory. This includes
an implementation of selection using standardized information
criteria in the \code{StandICModelSelect} function, and
the implementation of an omnibus goodness-of-fit test for
linear models in the \code{BootGOFTestLM} function.
}
\author{
\packageAuthor{DBModelSelect}

Maintainer: \packageMaintainer{DBModelSelect}
}
\references{
~~ Literature or other references for background information ~~
}
\keyword{ package }
\seealso{
~~ Optional links to other man pages, e.g. ~~
~~ \code{\link[<pkg>:<pkg>-package]{<pkg>}} ~~
Useful links:
\itemize{
\item \url{https://github.com/shkoeneman/DBModelSelect}
}
\examples{

}
\author{
\strong{Maintainer}: Scott H. Koeneman \email{Scott.Koeneman@jefferson.edu}

}
6 changes: 3 additions & 3 deletions man/FitGLMSubsets.Rd

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

15 changes: 9 additions & 6 deletions man/StandICModelSelect.Rd

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

0 comments on commit 01441e3

Please sign in to comment.