Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
conroylau committed Sep 14, 2021
1 parent a216d51 commit 152fd43
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 49 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Imports:
progressr (>= 0.6.0),
plyr (>= 1.8.6),
furrr (>= 0.2.0),
Matrix (>= 1.2.18)
Matrix (>= 1.2.18),
methods (>= 3.6.1)
Suggests:
knitr (>= 1.28),
rmarkdown (>= 2.0),
Expand All @@ -44,6 +45,7 @@ Suggests:
Rcplex (>= 0.3.3),
limSolve (>= 1.5.6),
lpSolveAPI (>= 5.5.2.0.17.4),
spelling (>= 2.1)
spelling (>= 2.1),
kableExtra (>= 1.2.1)
VignetteBuilder: knitr
Language: en-US
6 changes: 4 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ S3method(print,lpmodel)
S3method(print,lpmodel.natural)
S3method(print,mincriterion)
S3method(print,subsample)
S3method(sigma,summation)
S3method(summary,bisection.print)
S3method(summary,chorussell)
S3method(summary,dkqs)
Expand Down Expand Up @@ -124,6 +123,7 @@ export(post.bs)
export(pval)
export(quan.stat)
export(rcplex.optim)
export(sigma.summation)
export(smatrixconvert)
export(standard.form)
export(standard.lpmodel)
Expand All @@ -136,7 +136,6 @@ export(tau.constraints)
import(boot)
import(expm)
import(furrr)
import(lpSolveAPI)
import(plyr)
import(progressr)
import(scales)
Expand All @@ -145,4 +144,7 @@ importFrom(Matrix,norm)
importFrom(Matrix,t)
importFrom(Matrix,which)
importFrom(expm,sqrtm)
importFrom(methods,as)
importFrom(methods,is)
importFrom(pracma,ceil)
importFrom(utils,tail)
10 changes: 7 additions & 3 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ check.solver <- function(x, name.var, norm = 2 , qc = FALSE) {
} else if (requireNamespace("cplexAPI", quietly = TRUE) == TRUE) {
solver <- cplexapi.optim
x <- "cplexAPI"
} else if (requireNamespace("lpsolveAPI", quietly = TRUE) == TRUE) {
} else if (requireNamespace("lpSolveAPI", quietly = TRUE) == TRUE) {
solver <- lpsolveapi.optim
x <- "lpSolveAPI"
}
Expand Down Expand Up @@ -944,6 +944,8 @@ check.lpobjects <- function(data, mat, mat.name, mat.cat, R) {

#' Check function: matrix
#'
#' @importFrom methods is
#'
#' @description This function checks if the matrix objects in the
#' \code{lpmodel} object are in the correct format.
#'
Expand Down Expand Up @@ -978,7 +980,7 @@ check.matrix <- function(mat, mat.name, mat.cat, inside.list) {
return(list(mat.update = mat.update,
err.ind = 0,
dim = dim(mat.update)))
} else if (isTRUE(is(mat, "sparseMatrix"))) {
} else if (isTRUE(methods::is(mat, "sparseMatrix"))) {
return(list(mat.update = mat,
err.ind = 0,
dim = dim(mat)))
Expand Down Expand Up @@ -1009,6 +1011,8 @@ check.matrix <- function(mat, mat.name, mat.cat, inside.list) {

#' Check function: vector
#'
#' @importFrom methods is
#'
#' @description This function checks if the matrix objects in \code{lpmodel}
#' are in the correct format. If not, an error message is displayed.
#'
Expand All @@ -1025,7 +1029,7 @@ check.vector <- function(vec, vec.name, inside.list) {
msg.vector <- paste0("The object '%s' in 'lpmodel' has to be a %s.")

# Turn it into a matrix if it is not a list
if (!is.list(vec) | !is(vec, "sparseMatrix")) {
if (!is.list(vec) | !methods::is(vec, "sparseMatrix")) {
vec <- as.matrix(vec)
}
if (nrow(vec) != 1 & ncol(vec) != 1) {
Expand Down
8 changes: 6 additions & 2 deletions R/estbounds.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ estbounds <- function(data = NULL, lpmodel, kappa = 0, norm = 2,

#' Computes the true bounds subjected to shape constraints
#'
#' @importFrom methods is
#'
#' @description This function computes the true bounds subjected to the shape
#' constraints without approximation.
#'
Expand Down Expand Up @@ -221,7 +223,7 @@ estbounds.original <- function(data, lpmodel, original.sense, solver) {
# Matrices
A.obs.hat <- lpmodel.eval(data, lpmodel$A.obs, 1)
A.original <- rbind(A.obs.hat, A.shp.matrix)
if (!is.matrix(A.original) & !is(A.original, "sparseMatrix")) {
if (!is.matrix(A.original) & !methods::is(A.original, "sparseMatrix")) {
A.original <- matrix(A.original, nrow = 1)
}

Expand Down Expand Up @@ -309,6 +311,8 @@ estbounds.original <- function(data, lpmodel, original.sense, solver) {

#' Estimates the bounds with shape constraints (stage 2 with 1-norm)
#'
#' @importFrom methods is
#'
#' @description This function evaluates the solution to stage 2 of the
#' two-step procedure to obtain the estimated bound with the 1-norm.
#'
Expand Down Expand Up @@ -356,7 +360,7 @@ estbounds2.L1 <- function(data, firststepsoln, lpmodel, modelsense, kappa,
# Update the linear constraint
c <- larg$bf
A.step2 <- rbind(larg$A, c)
if (!is.matrix(A.step2) & !is(A.step2, "sparseMatrix")) {
if (!is.matrix(A.step2) & !methods::is(A.step2, "sparseMatrix")) {
A.step2 <- matrix(A.step2, nrow = 1)
}
b.step2 <- Reduce(rbind, c(larg$rhs, Qhat * (1 + kappa)))
Expand Down
21 changes: 14 additions & 7 deletions R/fsst.R
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ full.beta.bs <- function(lpmodel, beta.tgt, beta.obs.bs, R) {
#' \eqn{\hat{\beta}_{{\rm obs}, n}}.
#'
#' @import furrr progressr
#' @importFrom utils tail
#'
#' @inheritParams dkqs.bs
#' @inheritParams dkqs.bs.fn
Expand Down Expand Up @@ -644,7 +645,7 @@ fsst.beta.bs <- function(n, data, beta.obs.hat, lpmodel, R, maxR, progress,
} else {
bs.list <- iseq
i0 <- bs.list[1]
i1 <- tail(bs.list, n = 1)
i1 <- utils::tail(bs.list, n = 1)
}

# Set the default for progress bar
Expand Down Expand Up @@ -838,7 +839,9 @@ fsst.weight.matrix <- function(weight.matrix, beta.obs.hat, beta.sigma) {
#' @return Returns the estimator of the asymptotic variance.
#' \item{sigma.mat}{The estimator of the asymptotic variance.}
#'
#' @export
#' @usage sigma.summation(n, beta.bs.list, progress, eval.count)
#'
#' @export sigma.summation
#'
sigma.summation <- function(n, beta.bs.list, progress, eval.count) {
beta.obs.hat <- beta.bs.list[[1]]
Expand Down Expand Up @@ -1001,6 +1004,8 @@ beta.star.qp <- function(data, lpmodel, beta.tgt, weight.mat, beta.obs.hat,

#' Computes the solution to the cone problem
#'
#' @importFrom methods as
#'
#' @description This function computes the solution to the cone problem.
#'
#' @importFrom Matrix t
Expand Down Expand Up @@ -1043,9 +1048,9 @@ fsst.cone.lp <- function(n, omega.i, beta.n, beta.star, lpmodel, indicator,

# Construct the constraints matrix
A <- rbind(lpmodel$A.obs, lpmodel$A.shp, lpmodel$A.tgt)
A.mat1 <- as(cbind(omega.i, -diag(p), diag(p)), "sparseMatrix")
A.mat1 <- methods::as(cbind(omega.i, -diag(p), diag(p)), "sparseMatrix")
A.mat2 <- cbind(zero.p, ones.p, ones.p)
A.mat3 <- as(cbind(Matrix::t(A), zero.dp, zero.dp), "sparseMatrix")
A.mat3 <- methods::as(cbind(Matrix::t(A), zero.dp, zero.dp), "sparseMatrix")
A.mat <- rbind(A.mat1, A.mat2, A.mat3)

# Construct RHS vector
Expand Down Expand Up @@ -1078,7 +1083,7 @@ fsst.cone.lp <- function(n, omega.i, beta.n, beta.star, lpmodel, indicator,
# Update constraints matrix
A.mat.ext1 <- asmat(cbind(A.mat, zero.Am))
A.mat.ext2 <- asmat(cbind(diag(p), zero.pp, zero.pp, -A))
A.mat.ext <- as(rbind(A.mat.ext1, A.mat.ext2), "sparseMatrix")
A.mat.ext <- methods::as(rbind(A.mat.ext1, A.mat.ext2), "sparseMatrix")

# Update RHS vector
rhs.ext <- Reduce(rbind, c(rhs.mat, zero.p1))
Expand Down Expand Up @@ -1499,7 +1504,7 @@ fsst.range <- function(n, beta.obs.hat, x.star, lpmodel, weight.mat.root) {
# ---------------- #
# Step 1: Compute the matrix inside the norm
# ---------------- #
A.obs.hat <- lpmodel.eval(data, lpmodel$A.obs, 1)
A.obs.hat <- lpmodel$A.obs
beta.obs.star <- A.obs.hat %*% x.star
range.arg <- sqrt(n) * weight.mat.root %*% (beta.obs.hat - beta.obs.star)

Expand Down Expand Up @@ -1904,6 +1909,8 @@ fsst.pval <- function(range.n, cone.n, range.n.list, cone.n.list, R,

#' Checks and updates the input in \code{fsst}
#'
#' @importFrom methods is
#'
#' @description This function checks and updates the input from the user in the
#' \code{\link[lpinfer]{fsst}} function. If there is any invalid input,
#' the function will be terminated and error messages will be printed.
Expand Down Expand Up @@ -2088,7 +2095,7 @@ fsst.check <- function(data, lpmodel, beta.tgt, R, Rmulti, lambda, rho, n,
# It can be a square 'data.frame', 'matrix' or a 'sparseMatrix'.
omega.i <- previous.output$omega.i
if (!(is.matrix(omega.i) | is.data.frame(omega.i) |
is(omega.i, "sparseMatrix"))) {
methods::is(omega.i, "sparseMatrix"))) {
omega.i <- NA
warning(paste0("The class of the 'omega.i' matrix in the list ",
"'previous.output' has to be one of the ",
Expand Down
4 changes: 3 additions & 1 deletion R/invertci.R
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ print.invertci_single <- function(x, ...) {
#' function \code{\link[lpinfer]{invertci}}.
#'
#' @inheritParams print.invertci
#' @inheritParams summary.invertci
#'
#' @return Nothing is returned
#'
Expand Down Expand Up @@ -870,13 +871,14 @@ summary.invertci_multiple <- function(x, alphas, msg.bound, ...) {
#' the results in each step of the bisection method.
#'
#' @inheritParams bisec.print
#' @inheritParams print.invertci
#' @param i The row number that we want to print for \code{df_bis}.
#'
#' @return Nothing is returned.
#'
#' @export
#'
summary.bisection.print <- function(df_bis, i) {
summary.bisection.print <- function(df_bis, i, ...) {
# ---------------- #
# Step 1: Data cleaning
# ---------------- #
Expand Down
9 changes: 7 additions & 2 deletions R/lpmodel.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#' Evaluates an object inside \code{lpmodel}
#'
#' @importFrom methods is
#'
#' @description This function returns the matrix or vector depending on the
#' class of the variable in the \code{lpmodel} object. In the design of
#' the \code{lpinfer} module, objects in \code{lpmodel} can have three
Expand Down Expand Up @@ -27,7 +29,8 @@ lpmodel.eval <- function(data, obj, i) {
obj.eval <- obj(data)
} else if (class(obj) == "list") {
obj.eval <- obj[[i]]
} else if (!is.matrix(obj) & !is.data.frame(obj) & !is(obj, "sparseMatrix")) {
} else if (!is.matrix(obj) & !is.data.frame(obj) &
!methods::is(obj, "sparseMatrix")) {
obj.eval <- matrix(obj, nrow = 1)
} else if (class(obj) == "data.frame") {
obj.eval <- as.matrix(obj)
Expand Down Expand Up @@ -222,6 +225,8 @@ lpmodel.natural <- function(A.obs = NULL, A.shp = NULL, A.tgt = NULL,

#' Print the \code{lpmodel} or \code{lpmodel.natural} object
#'
#' @importFrom methods is
#'
#' @description This function prints the details of the components that are
#' contained in the \code{lpmodel} or \code{lpmodel.natural} object.
#'
Expand Down Expand Up @@ -278,7 +283,7 @@ lpm.print <- function(x, lpm.string, data = NULL, ...) {
dimension.tmp <- paste0(dimension.str[1], "x", dimension.str[2])
}
} else if (class.tmp %in% c("data.frame", "matrix", "numeric") |
is(obj, "sparseMatrix")) {
methods::is(obj, "sparseMatrix")) {
dim.obj <- dim(obj)
if (is.null(dim.obj)) {
dimension.tmp <- paste0("1x", length(obj))
Expand Down
15 changes: 11 additions & 4 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ error.id.match <- function(error.list, df.error) {

#' Coerces a \code{sparseMatrix} as a \code{matrix}
#'
#' @importFrom methods is
#'
#' @description This function coerces a \code{sparseMatrix} as a
#' \code{matrix}. This function is used specifically in the
#' \code{\link[lpinfer]{gurobi.optim}} function to ensure that the
Expand All @@ -200,7 +202,7 @@ error.id.match <- function(error.list, df.error) {
#' @export
#'
smatrixconvert <- function(mat) {
if (is(mat, "sparseMatrix") | is(mat, "Matrix")) {
if (methods::is(mat, "sparseMatrix") | methods::is(mat, "Matrix")) {
return(as.matrix(mat))
} else {
return(mat)
Expand All @@ -209,6 +211,9 @@ smatrixconvert <- function(mat) {

#' Coerces a \code{dgeMatrix} as a \code{sparseMatrix}
#'
#' @importFrom methods is
#' @importFrom methods as
#'
#' @description This function coerces a \code{dgeMatrix} as a
#' \code{sparseMatrix}.This function is used specifically in the
#' \code{\link[lpinfer]{gurobi.optim}} function to ensure that the
Expand All @@ -221,15 +226,17 @@ smatrixconvert <- function(mat) {
#' @export
#'
dmatrixconvert <- function(mat) {
if (is(mat, "dgeMatrix")) {
return(as(mat, "sparseMatrix"))
if (methods::is(mat, "dgeMatrix")) {
return(methods::as(mat, "sparseMatrix"))
} else {
return(mat)
}
}

#' Coerces non-\code{sparseMatrix} objects as \code{matrix}
#'
#' @importFrom methods is
#'
#' @description This function coerse non-\code{sparseMatrix} objects as
#' \code{matrix} and keeps \code{sparseMatrix} unchanged.
#'
Expand All @@ -240,7 +247,7 @@ dmatrixconvert <- function(mat) {
#' @export
#'
asmat <- function(obj) {
if (is(obj, "sparseMatrix")) {
if (methods::is(obj, "sparseMatrix")) {
return(obj)
} else {
return(as.matrix(obj))
Expand Down
16 changes: 7 additions & 9 deletions R/optim.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ cplexapi.optim <- function(Af, bf, nf, A, rhs, sense, modelsense, lb,
# Step 2: Update the notations
# ---------------- #
# Model sense
modelsense[modelsense == "min"] <- CPX_MIN
modelsense[modelsense == "max"] <- CPX_MAX
modelsense[modelsense == "min"] <- cplexAPI::CPX_MIN
modelsense[modelsense == "max"] <- cplexAPI::CPX_MAX

# Inequality/equality signs
sense[sense == "<="] <- "L"
Expand All @@ -139,7 +139,7 @@ cplexapi.optim <- function(Af, bf, nf, A, rhs, sense, modelsense, lb,
# Bounds
lb[lb == Inf] <- cplexAPI::CPX_INFBOUND
lb[lb == -Inf] <- -cplexAPI::CPX_INFBOUND
ub <- rep(CPX_INFBOUND, length(lb))
ub <- rep(cplexAPI::CPX_INFBOUND, length(lb))

# ---------------- #
# Step 3: cplexAPI environment
Expand All @@ -162,7 +162,7 @@ cplexapi.optim <- function(Af, bf, nf, A, rhs, sense, modelsense, lb,
# Step 4: Solve the problem
# ---------------- #
# A linear program is identified if obj2 == NULL
if (is.null(obj2) == TRUE) {
if (is.null(objective_return$obj2) == TRUE) {
# Solving linear program
cplexAPI::copyLpwNamesCPLEX(env,
prob,
Expand Down Expand Up @@ -217,7 +217,7 @@ rcplex.optim <- function(Af, bf, nf, A, rhs, sense, modelsense, lb,
# ---------------- #
# Step 1: Obtain the coefficients of the objective function
# ---------------- #
objective_return = objective.function(Af, bf, nf, weight)
objective_return <- objective.function(Af, bf, nf, weight)

# ---------------- #
# Step 2: Update vectors and sense
Expand Down Expand Up @@ -468,9 +468,7 @@ objective.function <- function(A, b, n, weight = NULL) {
#' LP solver by \code{lpSolveAPI}
#'
#' @description This function computes the solution to the linear program
#' using the \code{lpsolveAPI} package.
#'
#' @import lpSolveAPI
#' using the \code{lpSolveAPI} package.
#'
#' @inheritParams gurobi.optim
#' @inheritParams dkqs
Expand All @@ -488,7 +486,7 @@ objective.function <- function(A, b, n, weight = NULL) {
#' @export
#'
lpsolveapi.optim <- function(Af, bf, nf, A, rhs, sense, modelsense, lb,
weight = diag(length(b))) {
weight = NULL, ...) {
# ---------------- #
# Step 1: Obtain the coefficients of the objective function
# ---------------- #
Expand Down

0 comments on commit 152fd43

Please sign in to comment.