Skip to content

Commit

Permalink
more informative information
Browse files Browse the repository at this point in the history
  • Loading branch information
BERENZ committed Jan 27, 2025
1 parent 0674ade commit 5d16f4f
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 231 deletions.
9 changes: 0 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export(cloglog_model_nonprobsvy)
export(control_inf)
export(control_out)
export(control_sel)
export(genSimData)
export(logit_model_nonprobsvy)
export(nonprob)
export(pop_size)
Expand Down Expand Up @@ -53,15 +52,13 @@ importFrom(stats,coef)
importFrom(stats,confint)
importFrom(stats,contrasts)
importFrom(stats,cooks.distance)
importFrom(stats,cor)
importFrom(stats,cov)
importFrom(stats,delete.response)
importFrom(stats,deviance)
importFrom(stats,dnorm)
importFrom(stats,get_all_vars)
importFrom(stats,glm.fit)
importFrom(stats,hatvalues)
importFrom(stats,lm.fit)
importFrom(stats,loess)
importFrom(stats,loess.control)
importFrom(stats,logLik)
Expand All @@ -77,18 +74,12 @@ importFrom(stats,printCoefmat)
importFrom(stats,pt)
importFrom(stats,qlogis)
importFrom(stats,qnorm)
importFrom(stats,rbinom)
importFrom(stats,rchisq)
importFrom(stats,reformulate)
importFrom(stats,residuals)
importFrom(stats,rexp)
importFrom(stats,rnorm)
importFrom(stats,runif)
importFrom(stats,sd)
importFrom(stats,setNames)
importFrom(stats,summary.glm)
importFrom(stats,terms)
importFrom(stats,uniroot)
importFrom(stats,update)
importFrom(stats,var)
importFrom(stats,vcov)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Breaking changes

- functions `pop.size`, `controlSel`, `controlOut` and `controlInf` were renamed to `pop_size`, `control_sel`, `control_out` and `control_inf` respectively.
- function `genSimData` removed completely as it is not used anywhere in the package.
- argument `maxLik_method` renamed to `maxlik_method` in the `control_sel` function.

### Features

Expand All @@ -15,6 +17,9 @@
### Bugfixes
- basic methods and functions related to variance estimation, weights and probability linking methods have been rewritten in a more optimal and readable way.

### Other
- more informative error messages added.

### Documentation

- annotation has been added that arguments such as `strata`, `subset` and `na_action` are not supported for the time being.
Expand Down
6 changes: 3 additions & 3 deletions R/boot_mi.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bootMI <- function(X_rand,
if (class(svydesign)[1] != "pps") {
rep_weights <- survey::as.svrepdesign(svydesign, type = rep_type, replicates = num_boot)$repweights$weights
} else {
stop("pps bootstrap variance in development")
stop("The pps bootstrap variance estimator is under development")
}
if (method == "glm") {
while (k <= num_boot) {
Expand Down Expand Up @@ -444,7 +444,7 @@ bootMI_multicore <- function(X_rand,
family_nonprobsvy <- family_nonprobsvy()
}

if (verbose) message("Multicores bootstrap in progress..")
if (verbose) message("Multicores bootstrap in progress...")

cl <- parallel::makeCluster(cores)
doParallel::registerDoParallel(cl)
Expand All @@ -460,7 +460,7 @@ bootMI_multicore <- function(X_rand,
if (class(svydesign)[1] != "pps") {
rep_weights <- survey::as.svrepdesign(svydesign, type = rep_type, replicates = num_boot)$repweights$weights
} else {
stop("pps bootstrap variance in development")
stop("The pps bootstrap variance estimator is under development.")
}
if (method == "glm") {
k <- 1:num_boot
Expand Down
22 changes: 11 additions & 11 deletions R/cloglogModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ cloglog_model_nonprobsvy <- function(...) {

if (maxLik_an$code %in% c(3:7, 100)) {
switch(as.character(maxLik_an$code),
"3" = warning("Warning in fitting selection model with maxLik: probably not converged."),
"4" = warning("Maxiteration limit reached in fitting selection model by maxLik."),
"5" = stop("Inifinite value of log_like in fitting selection model by maxLik, error code 5"),
"6" = stop("Inifinite value of gradient in fitting selection model by maxLik, error code 6"),
"7" = stop("Inifinite value of hessian in fitting selection model by maxLik, error code 7"),
"100" = stop("Error in fitting selection model with maxLik, error code 100:: Bad start."),
"3" = warning("Warning in fitting selection model with the `maxLik` package: probably not converged."),
"4" = warning("Maxiteration limit reached in fitting selection model by the `maxLik` package."),
"5" = stop("Infinite value of log_like in fitting selection model by the `maxLik` package, error code 5."),
"6" = stop("Infinite value of gradient in fitting selection model by the `maxLik` package, error code 6."),
"7" = stop("Infinite value of hessian in fitting selection model by the `maxLik` package, error code 7."),
"100" = stop("Error in fitting selection model with the `maxLik` package, error code 100: Bad start."),
)
}

Expand All @@ -153,18 +153,18 @@ cloglog_model_nonprobsvy <- function(...) {
)
if (maxLik_an$convergence %in% c(1, 10, 51, 52)) {
switch(as.character(maxLik_an$convergence),
"1" = warning("Warning in fitting selection model with optim: the iteration limit maxit had been reached."),
"10" = warning("Degeneracy of the Nelder Mead simplex in fitting selection model by optim."), # TODO -
"51" = warning("Warning from the L BFGS B when fitting by optim."), # TODO -
"52" = stop("Indicates an error from the L-BFGS-B method when fitting by optim.")
"1" = warning("Warning in fitting selection model with the `optim` function: the iteration limit maxit had been reached."),
"10" = warning("Degeneracy of the Nelder Mead simplex in fitting selection model by the `optim` function."), # TODO -
"51" = warning("Warning from the L-BFGS-B when fitting by the `optim` function."), # TODO -
"52" = stop("Indicates an error from the L-BFGS-B method when fitting by the `optim` function.")
)
}
theta <- maxLik_an$par
log_likelihood <- log_like(theta)
grad <- gradient(theta)
hess <- hessian(theta)
} else {
stop("Provided invalid optimizer.")
stop("Provide valid optimizer (`optim` or `maxLik`).")
}

list(
Expand Down
4 changes: 2 additions & 2 deletions R/control_inference.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ control_inf <- function(vars_selection = FALSE,
TRUE
} else {
if (!is.logical(keep_boot)) {
stop("keep_boot argument for controlInf must be logical")
stop("The `keep_boot` argument for `control_inf` function must be logical.")
} else {
keep_boot
}
},
nn_exact_se = if (!is.logical(nn_exact_se) & length(nn_exact_se) == 1) {
stop("Argument nn_exact_se must be a logical scalar")
stop("The `nn_exact_se` argument must be a logical scalar.")
} else {
nn_exact_se
},
Expand Down
6 changes: 3 additions & 3 deletions R/control_selection.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' @param trace logical value. If `TRUE` trace steps of the fitting algorithms. Default is `FALSE`
#' @param optimizer - optimization function for maximum likelihood estimation.
#' @param optim_method maximisation method that will be passed to [stats::optim()] function. Default is `BFGS`.
#' @param maxLik_method maximisation method that will be passed to [maxLik::maxLik()] function. Default is `NR`.
#' @param maxlik_method maximisation method that will be passed to [maxLik::maxLik()] function. Default is `NR`.
#' @param dependence logical value - `TRUE` if samples are dependent.
#' @param key binary key variable
#' @param est_method_sel Method of estimation for propensity score model.
Expand Down Expand Up @@ -51,7 +51,7 @@ control_sel <- function(method = "glm.fit", # perhaps another control function f
maxit = 500,
trace = FALSE,
optimizer = c("maxLik", "optim"),
maxLik_method = "NR",
maxlik_method = "NR",
optim_method = "BFGS",
dependence = FALSE,
key = NULL,
Expand All @@ -77,7 +77,7 @@ control_sel <- function(method = "glm.fit", # perhaps another control function f
maxit = maxit,
trace = trace,
optimizer = if (missing(optimizer)) "optim" else optimizer,
maxLik_method = maxLik_method,
maxlik_method = maxlik_method,
optim_method = optim_method,
dependence = dependence,
key = key,
Expand Down
2 changes: 1 addition & 1 deletion R/data_manip.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ model_frame <- function(formula, data, weights = NULL, svydesign = NULL, pop_tot
# X_rand <- model.matrix(formula, svydesign$variables[, nons_names])# matrix of probability sample with intercept
# }
} else {
stop("Variable names in data and svydesign do not match")
stop("The names of the variables in the data and in svydesign do not match.")
}

list(
Expand Down
78 changes: 0 additions & 78 deletions R/generateData.r

This file was deleted.

6 changes: 3 additions & 3 deletions R/internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ ff <- function(formula) {
formula_string <- paste(deparse(formula), collapse = " ")
formula_parts <- strsplit(formula_string, "~")[[1]]
if (length(formula_parts) != 2) {
stop("The formula must contain exactly one '~' operator.")
stop("The `formula` must contain exactly one '~' operator.")
}

lhs <- trimws(formula_parts[1])
Expand All @@ -165,7 +165,7 @@ ff <- function(formula) {
independent_vars <- strsplit(rhs, "\\s*\\+\\s*")[[1]]

if (any(duplicated(dependent_vars))) {
warning("Duplicate dependent variable names detected. They have been made unique.")
warning("Duplicate dependent variable names have been detected. They have been made unique.")
dependent_vars <- unique(dependent_vars)
}
outcome_formulas <- vector("list", length(dependent_vars))
Expand Down Expand Up @@ -261,7 +261,7 @@ process_family <- function(family_spec) {
} else if (inherits(family_spec, "family")) {
family <- family_spec
} else {
stop("Invalid family specification")
stop("Invalid family specification.")
}
return(family)
}
24 changes: 12 additions & 12 deletions R/logitModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ logit_model_nonprobsvy <- function(...) {
logLik = log_like,
grad = gradient,
hess = hessian,
method = control$maxLik_method,
method = control$maxlik_method,
start = start,
printLevel = control$print_level
)
Expand All @@ -108,12 +108,12 @@ logit_model_nonprobsvy <- function(...) {
log_likelihood <- log_like(theta)
if (maxLik_an$code %in% c(3:7, 100)) {
switch(as.character(maxLik_an$code),
"3" = warning("Warning in fitting selection model with maxLik: probably not converged."),
"4" = warning("Max iteration limit reached in fitting selection model by maxLik."),
"5" = stop("Inifinite value of log_like in fitting selection model by maxLik, error code 5"),
"6" = stop("Inifinite value of gradient in fitting selection model by maxLik, error code 6"),
"7" = stop("Inifinite value of hessian in fitting selection model by maxLik, error code 7"),
"100" = stop("Error in fitting selection model with maxLik, error code 100:: Bad start."),
"3" = warning("Warning in fitting selection model with the `maxLik` package: probably not converged."),
"4" = warning("Max iteration limit reached in fitting selection model by the `maxLik` package."),
"5" = stop("Infinite value of log_like in fitting selection model by the `maxLik` package, error code 5."),
"6" = stop("Infinite value of gradient in fitting selection model by the `maxLik` package, error code 6."),
"7" = stop("Infinite value of hessian in fitting selection model by the `maxLik` package, error code 7."),
"100" = stop("Error in fitting selection model with the `maxLik` package, error code 100: Bad start."),
)
}
} else if (control$optimizer == "optim") { # TODO add optimParallel for high-dimensional data
Expand All @@ -132,10 +132,10 @@ logit_model_nonprobsvy <- function(...) {
)
if (maxLik_an$convergence %in% c(1, 10, 51, 52)) {
switch(as.character(maxLik_an$convergence),
"1" = warning("Warning in fitting selection model with optim: the iteration limit maxit had been reached."),
"10" = warning("degeneracy of the Nelder Mead simplex in fitting selection model by optim."), # TODO -
"51" = warning("Warning from the L BFGS B when fitting by optim."), # TODO -
"52" = stop("Indicates an error from the L BFGS B method when fitting by optim.")
"1" = warning("Warning in fitting selection model with the `optim` function: the iteration limit maxit had been reached."),
"10" = warning("degeneracy of the Nelder Mead simplex in fitting selection model by the `optim` function."), # TODO -
"51" = warning("Warning from the L-BFGS-B when fitting by the `optim` function."), # TODO -
"52" = stop("Indicates an error from the L-BFGS-B method when fitting by the `optim` function.")
)
}

Expand All @@ -144,7 +144,7 @@ logit_model_nonprobsvy <- function(...) {
grad <- gradient(theta)
hess <- hessian(theta)
} else {
stop("Provided invalid optimizer.")
stop("Provide valid optimizer (`optim` or `maxLik`).")
}

list(
Expand Down
Loading

0 comments on commit 5d16f4f

Please sign in to comment.