diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..febe13e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,24 @@ +dist: xenial +matrix: + include: + - language: r + r: + - release + install: + - mkdir ~/.R + - R -e 'install.packages(c("kntir", "rmarkdown", "testthat"), repos = "http://cran.rstudio.com")' + - echo "CXX11=g++-7" > ~/.R/Makevars + - R CMD build dgemmR + - R CMD INSTALL *tar.gz + script: + - R CMD check *tar.gz + +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - libopenblas-base libopenblas-dev gcc-7 g++-7 gfortran-7 + +notifications: + email: false \ No newline at end of file diff --git a/README.md b/README.md index 6ecebde..b34856d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Build Status](https://travis-ci.com/rehbergT/zeroSum.svg?branch=master)](https://travis-ci.com/rehbergT/zeroSum) + ## News: Version 2.0 * Improved support for multinomial logistic and Cox proportional hazards regression. * Improved performance (runtime avx detection and multithreading support now also available for macOS). @@ -84,13 +86,13 @@ with or without the zero-sum constraint can be easily compared. A binary package (*.zip) is available, which can be installed in R using: - install.packages("https://github.com/rehbergT/zeroSum/raw/master/zeroSum_2.0.2.zip", repos = NULL) + install.packages("https://github.com/rehbergT/zeroSum/raw/master/zeroSum_2.0.3.zip", repos = NULL) ## Linux and macOS The source package (*.tar.gz) can be installed in R using: - install.packages("https://github.com/rehbergT/zeroSum/raw/master/zeroSum_2.0.2.tar.gz", repos = NULL) + install.packages("https://github.com/rehbergT/zeroSum/raw/master/zeroSum_2.0.3.tar.gz", repos = NULL) ## Devtools @@ -103,7 +105,7 @@ Git clone or download this repository and open a terminal within the zeroSum fol The source package can be build and installed with: R CMD build zeroSum - R CMD INSTALL zeroSum_2.0.2.tar.gz + R CMD INSTALL zeroSum_2.0.3.tar.gz # Quick start diff --git a/zeroSum/DESCRIPTION b/zeroSum/DESCRIPTION index e4f16dd..117c6c6 100644 --- a/zeroSum/DESCRIPTION +++ b/zeroSum/DESCRIPTION @@ -4,8 +4,8 @@ Description: Regressions functions for creating linear models with the elastic-net regularization and the zero-sum constraint (sum of coefficients = 0). The usage of this package is very similar to that of the "glmnet" package. -Version: 2.0.2 -Date: 2019-02-06 +Version: 2.0.3 +Date: 2019-04-29 Author: Thorsten Rehberg, Michael Altenbuchinger Maintainer: Thorsten Rehberg Imports: diff --git a/zeroSum/R/coef.R b/zeroSum/R/coef.R index ff3d932..cd2312c 100644 --- a/zeroSum/R/coef.R +++ b/zeroSum/R/coef.R @@ -14,11 +14,10 @@ #' #' @examples #' set.seed(1) -#' x <- log2(exampleData$x+1) +#' x <- log2(exampleData$x + 1) #' y <- exampleData$y -#' fit <- zeroSum( x, y, alpha=1) -#' coef(fit, s="lambda.min") -#' +#' fit <- zeroSum(x, y, alpha = 1) +#' coef(fit, s = "lambda.min") #' @importFrom stats coef #' #' @export diff --git a/zeroSum/R/costFunction.R b/zeroSum/R/costFunction.R index 91e4856..a2c3e40 100644 --- a/zeroSum/R/costFunction.R +++ b/zeroSum/R/costFunction.R @@ -1,11 +1,11 @@ #' Description of costFunction function #' #' @keywords internal -costFunction <- function(data, useC=FALSE) { +costFunction <- function(data, useC = FALSE) { out <- list() - x <- data$x - N <- nrow(x) - y <- data$y + x <- data$x + N <- nrow(x) + y <- data$y beta <- data$beta weights <- data$w penalty.factor <- data$v @@ -15,28 +15,26 @@ costFunction <- function(data, useC=FALSE) { xtb <- x %*% beta[-1, ] + beta[1, ] ## calculation of the residual sum of squares - res <- y - xtb - out$loglikelihood <- -as.numeric(weights %*% (res ^ 2)) / 2 - + res <- y - xtb + out$loglikelihood <- -as.numeric(weights %*% (res^2)) / 2 } else if (data$type == zeroSumTypes[2, 2]) { xtb <- x %*% beta[-1, ] + beta[1, ] ## calculation of the loglikelihood expXB <- log1p(exp(xtb)) out$loglikelihood <- as.numeric(weights %*% (y * xtb - expXB)) - } else if (data$type == zeroSumTypes[3, 2]) { xb <- x %*% beta[-1, ] for (i in 1:ncol(beta)) { - xb[, i] <- xb[, i] + rep(beta[1, i], N) + xb[, i] <- xb[, i] + rep(beta[1, i], N) } xby <- rowSums(xb * y) a <- max(xb) xb <- xb - a out$loglikelihood <- as.numeric(weights %*% - (xby - log(rowSums(exp(xb))) - a)) - } else if (data$type == zeroSumTypes[4, 2]) { + (xby - log(rowSums(exp(xb))) - a)) + } else if (data$type == zeroSumTypes[4, 2]) { y <- cbind(data$y, data$status) y <- cbind(y, duplicated(y[, 1] + y[, 2])) d <- rep(0, N) @@ -52,8 +50,9 @@ costFunction <- function(data, useC=FALSE) { k <- j + 1 ## search for duplicates (ties) and add the weights while (k <= N && y[k, 3] == 1) { - if (y[k, 2] == 1) + if (y[k, 2] == 1) { d[j] <- d[j] + weights[k] + } k <- k + 1 } j <- k @@ -74,22 +73,22 @@ costFunction <- function(data, useC=FALSE) { ## calculation of the ridge penalty (the intercept is not penalized ## therefore the first beta is excluded) - out$ridge <- sum(t((beta[-1, , drop = FALSE]) ^ 2) %*% penalty.factor) + out$ridge <- sum(t((beta[-1, , drop = FALSE])^2) %*% penalty.factor) ## calculation of the lasso penalty (the intercept is not penalized ## therefore the first beta is excluded) - out$lasso <- sum(t(abs(beta[-1, , drop = FALSE])) %*% penalty.factor) + out$lasso <- sum(t(abs(beta[-1, , drop = FALSE])) %*% penalty.factor) ## calculation of the cost function out$cost <- -out$loglikelihood + data$lambda * - ((1 - data$alpha) * out$ridge / 2 + data$alpha * out$lasso) + ((1 - data$alpha) * out$ridge / 2 + data$alpha * out$lasso) out$fusion <- 0 if (data$useFusion) { out$fusion <- 0.0 for (i in 1:ncol(data$beta)) { out$fusion <- out$fusion + sum(abs(as.numeric(data$fusion %*% - data$beta[-1, i]))) + data$beta[-1, i]))) } out$cost <- out$cost + data$gamma * out$fusion } diff --git a/zeroSum/R/exportToCSV.R b/zeroSum/R/exportToCSV.R index d5cb5e0..e09532f 100644 --- a/zeroSum/R/exportToCSV.R +++ b/zeroSum/R/exportToCSV.R @@ -6,172 +6,125 @@ #' @keywords internal #' #' @export -exportToCSV <- function( - x, - y, - family = "gaussian", - alpha = 1.0, - lambda = NULL, - lambdaSteps = 100, - weights = NULL, - penalty.factor = NULL, - zeroSum.weights = NULL, - nFold = NULL, - foldid = NULL, - epsilon = NULL, - standardize = FALSE, - intercept = TRUE, - zeroSum = TRUE, - threads = 1, - cvStop = 0.1, - path = "", - name = "", - ...) { - args <- list(...) - - if (methods::hasArg("beta")) { - beta <- args$beta - } else { - beta <- NULL - } - - if (methods::hasArg("center")) { - center <- args$center - } else { - center <- TRUE - } - - if (methods::hasArg("fusion")) { - fusion <- args$fusion - } else { - fusion <- NULL - } - - if (methods::hasArg("gamma")) { - gamma <- args$gamma - } else { - gamma <- 0.0 - } - - if (methods::hasArg("gammaSteps")) { - gammaSteps <- args$gammaSteps - } else { - gammaSteps <- 1 - } - - if (methods::hasArg("useApprox")) { - useApprox <- args$useApprox - } else { - useApprox <- TRUE - } - - if (methods::hasArg("downScaler")) { - downScaler <- args$downScaler - } else { - downScaler <- 1.0 - } - - if (methods::hasArg("algorithm")) { - algorithm <- args$algorithm - } else { - algorithm <- "CD" - } - - if (methods::hasArg("verbose")) { - verbose <- args$verbose - } else { - verbose <- FALSE - } - - if (methods::hasArg("polish")) { - usePolish <- args$polish - } else { - usePolish <- TRUE - } - - if (methods::hasArg("precision")) { - precision <- args$precision - } else { - precision <- 1e-8 - } - - if (methods::hasArg("rotatedUpdates")) { - rotatedUpdates <- args$rotatedUpdates - } else { - rotatedUpdates <- FALSE - } - - data <- regressionObject(x, y, beta, alpha, lambda, gamma, family, weights, - penalty.factor, zeroSum.weights, fusion, precision, intercept, - useApprox, downScaler, algorithm, rotatedUpdates, usePolish, - standardize, lambdaSteps, gammaSteps, nFold, foldid, epsilon, - cvStop, verbose, threads, center, zeroSum) - - utils::write.csv(format(data$x, digits = 18, scientific = TRUE, - trim = TRUE), quote = FALSE, file = paste0(path, "x", name, ".csv")) - utils::write.csv(format(data$y, digits = 18, scientific = TRUE, - trim = TRUE), quote = FALSE, file = paste0(path, "y", name, ".csv")) +exportToCSV <- function(x, + y, + family = "gaussian", + alpha = 1.0, + lambda = NULL, + lambdaSteps = 100, + weights = NULL, + penalty.factor = NULL, + zeroSum.weights = NULL, + nFold = NULL, + foldid = NULL, + epsilon = NULL, + standardize = FALSE, + intercept = TRUE, + zeroSum = TRUE, + threads = "auto", + cvStop = 0.1, + path = "", + name = "", + ...) { + data <- regressionObject( + x, y, family, alpha, lambda, lambdaSteps, weights, + penalty.factor, zeroSum.weights, nFold, foldid, epsilon, + standardize, intercept, zeroSum, threads, cvStop, ... + ) + + utils::write.csv(format(data$x, + digits = 18, scientific = TRUE, + trim = TRUE + ), quote = FALSE, file = paste0(path, "x", name, ".csv")) + utils::write.csv(format(data$y, + digits = 18, scientific = TRUE, + trim = TRUE + ), quote = FALSE, file = paste0(path, "y", name, ".csv")) if (data$type == zeroSumTypes[4, 2]) { utils::write.csv(data$status, - file = paste0(path, "status", name, ".csv")) + file = paste0(path, "status", name, ".csv") + ) } - if (!is.null(data$fusionC) && data$useFusion) + if (!is.null(data$fusionC) && data$useFusion) { utils::write.csv(data$fusionC, - file = paste0(path, "fusion", name, ".csv")) + file = paste0(path, "fusion", name, ".csv") + ) + } cols <- max(length(data$lambda), length(data$gamma), ncol(x), nrow(x)) settings <- matrix("", nrow = 29, ncol = cols) - rownames(settings) <- c("N", "P", "K", "nc", "type", "useZeroSum", - "useFusion", "useIntercept", "useApprox", "center", "standardize", - "usePolish", "rotatedUpdates", "precision", "algorithm", "nFold", - "cvStop", "verbose", "cSum", "alpha", "downScaler", "threads", - "seed", "w", "u", "v", "lambda", "gamma", "foldid") - - settings[1, 1] <- nrow(data$x) - settings[2, 1] <- ncol(data$x) - settings[3, 1] <- ncol(data$y) - settings[4, 1] <- data$nc - settings[5, 1] <- data$type - settings[6, 1] <- data$useZeroSum - settings[7, 1] <- data$useFusion - settings[8, 1] <- data$useIntercept - settings[9, 1] <- data$useApprox + rownames(settings) <- c( + "N", "P", "K", "nc", "type", "useZeroSum", + "useFusion", "useIntercept", "useApprox", "center", "standardize", + "usePolish", "rotatedUpdates", "precision", "algorithm", "nFold", + "cvStop", "verbose", "cSum", "alpha", "downScaler", "threads", + "seed", "w", "u", "v", "lambda", "gamma", "foldid" + ) + + settings[1, 1] <- nrow(data$x) + settings[2, 1] <- ncol(data$x) + settings[3, 1] <- ncol(data$y) + settings[4, 1] <- data$nc + settings[5, 1] <- data$type + settings[6, 1] <- data$useZeroSum + settings[7, 1] <- data$useFusion + settings[8, 1] <- data$useIntercept + settings[9, 1] <- data$useApprox settings[10, 1] <- data$center settings[11, 1] <- data$standardize settings[12, 1] <- data$usePolish settings[13, 1] <- data$rotatedUpdates - settings[14, 1] <- format(data$precision, digits = 18, - scientific = TRUE, trim = TRUE) + settings[14, 1] <- format(data$precision, + digits = 18, + scientific = TRUE, trim = TRUE + ) settings[15, 1] <- data$algorithm settings[16, 1] <- data$nFold settings[17, 1] <- data$cvStop settings[18, 1] <- data$verbose - settings[19, 1] <- format(data$cSum, digits = 18, - scientific = TRUE, trim = TRUE) - settings[20, 1] <- format(data$alpha, digits = 18, - scientific = TRUE, trim = TRUE) - settings[21, 1] <- format(data$downScaler, digits = 18, - scientific = TRUE, trim = TRUE) + settings[19, 1] <- format(data$cSum, + digits = 18, + scientific = TRUE, trim = TRUE + ) + settings[20, 1] <- format(data$alpha, + digits = 18, + scientific = TRUE, trim = TRUE + ) + settings[21, 1] <- format(data$downScaler, + digits = 18, + scientific = TRUE, trim = TRUE + ) settings[22, 1] <- data$threads settings[23, 1] <- data$seed - settings[24, 1:nrow(data$x)] <- format(data$w, digits = 18, - scientific = TRUE, trim = TRUE) - settings[25, 1:ncol(data$x)] <- format(data$u, digits = 18, - scientific = TRUE, trim = TRUE) - settings[26, 1:ncol(data$x)] <- format(data$v, digits = 18, - scientific = TRUE, trim = TRUE) - - settings[27, 1:length(data$lambda)] <- format(data$lambda, digits = 18, - scientific = TRUE, trim = TRUE) - settings[28, 1:length(data$gamma)] <- format(data$gamma, digits = 18, - scientific = TRUE, trim = TRUE) + settings[24, 1:nrow(data$x)] <- format(data$w, + digits = 18, + scientific = TRUE, trim = TRUE + ) + settings[25, 1:ncol(data$x)] <- format(data$u, + digits = 18, + scientific = TRUE, trim = TRUE + ) + settings[26, 1:ncol(data$x)] <- format(data$v, + digits = 18, + scientific = TRUE, trim = TRUE + ) + + settings[27, 1:length(data$lambda)] <- format(data$lambda, + digits = 18, + scientific = TRUE, trim = TRUE + ) + settings[28, 1:length(data$gamma)] <- format(data$gamma, + digits = 18, + scientific = TRUE, trim = TRUE + ) settings[29, 1:nrow(data$x)] <- data$foldid - utils::write.csv(settings, quote = FALSE, file = paste0(path, "settings", - name, ".csv"), na = "") + utils::write.csv(settings, quote = FALSE, file = paste0( + path, "settings", + name, ".csv" + ), na = "") saveRDS(data, file = paste0(path, "rDataObject", name, ".rds")) - } diff --git a/zeroSum/R/extCostFunction.R b/zeroSum/R/extCostFunction.R index 4951a5b..537494d 100644 --- a/zeroSum/R/extCostFunction.R +++ b/zeroSum/R/extCostFunction.R @@ -5,14 +5,31 @@ #' @keywords internal #' #' @export -extCostFunction <- function(x, y, beta, alpha = 1, lambda = 0, gamma = 0, - family = zeroSumTypes[1, 1], weights = NULL, - penalty.factor = NULL, fusion = NULL, - useC = FALSE) { - data <- regressionObject(x, y, beta, alpha, lambda, gamma, - type = family, weights = weights, penalty.factor = penalty.factor, - fusion = fusion, standardize = FALSE, useIntercept = FALSE, nFold = 0, - useZeroSum = FALSE, center = FALSE +extCostFunction <- function(x, + y, + beta, + alpha = 1.0, + lambda = NULL, + family = "gaussian", + lambdaSteps = 100, + weights = NULL, + penalty.factor = NULL, + zeroSum.weights = NULL, + nFold = NULL, + foldid = NULL, + epsilon = NULL, + standardize = FALSE, + intercept = TRUE, + zeroSum = TRUE, + threads = "auto", + cvStop = 0.1, + useC = FALSE, + ...) { + data <- regressionObject( + x, y, family, alpha, lambda, lambdaSteps, weights, + penalty.factor, zeroSum.weights, nFold, foldid, epsilon, + standardize, intercept, zeroSum, threads, cvStop, + beta = beta, ... ) return(costFunction(data, useC)) diff --git a/zeroSum/R/getNullModels.R b/zeroSum/R/getNullModels.R index 1f39dc4..24f92b4 100644 --- a/zeroSum/R/getNullModels.R +++ b/zeroSum/R/getNullModels.R @@ -19,14 +19,14 @@ getLogisticNullModel <- function(y, worg) { getMultinomialNullModel <- function(y, worg, iterations) { out <- list() - N <- nrow(y) - K <- ncol(y) + N <- nrow(y) + K <- ncol(y) out$beta0 <- rep(0, K) - for (ii in 1 : iterations) { - for (k in 1: K) { + for (ii in 1:iterations) { + for (k in 1:K) { out$beta0[k] <- -log((sum(worg) / as.numeric(worg %*% y[, k]) - 1) / - (sum(exp(out$beta0)[-k]))) + (sum(exp(out$beta0)[-k]))) } } out$beta0 <- as.numeric(scale(out$beta0, center = TRUE, scale = FALSE)) @@ -38,7 +38,7 @@ getMultinomialNullModel <- function(y, worg, iterations) { out$z <- matrix(0.0, nrow = N, ncol = K) out$w <- matrix(0.0, nrow = N, ncol = K) - for (k in 1: K) { + for (k in 1:K) { out$z[, k] <- out$beta0[k] + (y[, k] - out$p[k]) / w[k] out$w[, k] <- worg * w[k] } @@ -47,7 +47,7 @@ getMultinomialNullModel <- function(y, worg, iterations) { } getCoxNullModel <- function(y, status, worg) { - N <- nrow(y) + N <- nrow(y) ## find ties with events y <- cbind(y, status) @@ -67,8 +67,9 @@ getCoxNullModel <- function(y, status, worg) { k <- j + 1 ## search for duplicates (ties) and add the weights while (k <= N && y[k, 3] == 1) { - if (y[k, 2] == 1) + if (y[k, 2] == 1) { d[j] <- d[j] + worg[k] + } k <- k + 1 } j <- k diff --git a/zeroSum/R/inputChecks.R b/zeroSum/R/inputChecks.R index e6c327d..99b7a48 100644 --- a/zeroSum/R/inputChecks.R +++ b/zeroSum/R/inputChecks.R @@ -12,19 +12,21 @@ zeroSumTypes <- data.frame(c("gaussian", "binomial", "multinomial", "cox"), - 1:4, stringsAsFactors = FALSE) + 1:4, + stringsAsFactors = FALSE +) colnames(zeroSumTypes) <- c("Type", "Int") zeroSumAlgos <- data.frame(c("CD", "SA", "LS", "CDP"), c(1, 2, 3, 4), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE +) colnames(zeroSumAlgos) <- c("Algo", "Int") checkNumericMatrix <- function(x, varName) { - if (any(class(x) == "tbl") & typeof(x) == "list") { x <- as.matrix(x) } @@ -59,14 +61,15 @@ checkNumericMatrix <- function(x, varName) { checkSparseMatrix <- function(x, varName) { x <- methods::as(x, "sparseMatrix") if (class(x) != "dgCMatrix" | typeof(x) != "S4") { - message <- sprintf(paste0("Type of %s is not a sparse matrix or cannot", - "be casted to a sparse matrix\n"), varName) + message <- sprintf(paste0( + "Type of %s is not a sparse matrix or cannot", + "be casted to a sparse matrix\n" + ), varName) stop(message) } } checkNumericVector <- function(x, varName) { - if (any(class(x) == "tbl") & typeof(x) == "list") { x <- as.matrix(x) } @@ -133,8 +136,9 @@ checkSurvialDataVector <- function(x, varName) { checkData <- function(x, y, w, type) { x <- checkNumericMatrix(x, "x") - if (is.null(colnames(x))) - colnames(x) <- as.character(seq(1, ncol(x))) + if (is.null(colnames(x))) { + colnames(x) <- as.character(seq(1, ncol(x))) + } N <- nrow(x) if (is.null(w)) { @@ -145,15 +149,13 @@ checkData <- function(x, y, w, type) { } status <- NULL - ord <- NULL + ord <- NULL if (type == zeroSumTypes[1, 1]) { y <- checkNumericVector(y, "y") - } else if (type == zeroSumTypes[2, 1]) { checkBinominalVector(y, "y") y <- as.matrix(as.numeric(y)) - } else if (type == zeroSumTypes[3, 1]) { checkMultinominalVector(y, "y") N <- length(y) @@ -163,7 +165,6 @@ checkData <- function(x, y, w, type) { ymatrix[i, y[i]] <- 1.0 } y <- ymatrix - } else if (type == zeroSumTypes[4, 1]) { checkSurvialDataVector(y, "y") y <- as.matrix(y) @@ -182,8 +183,9 @@ checkData <- function(x, y, w, type) { w <- w[ord] } - if (nrow(x) != NROW(y)) - stop("nrow(x) != nrow(y) !") + if (nrow(x) != NROW(y)) { + stop("nrow(x) != nrow(y) !") + } return(list(x = x, y = y, w = w, status = status, ord = ord)) } @@ -191,15 +193,17 @@ checkData <- function(x, y, w, type) { checkType <- function(type) { if (class(type) != "character" & typeof(type) != "character" | !(type %in% zeroSumTypes[, 1])) { - message <- paste0("Selected type is not valid. Use gaussian, binomial", - ", multinomial or cox!") + message <- paste0( + "Selected type is not valid. Use gaussian, binomial", + ", multinomial or cox!" + ) stop(message) } } checkAlgo <- function(algo, name) { - if (class(algo) != "character" & typeof(algo) != "character" - | !(algo %in% zeroSumAlgos[, 1])) { + if (class(algo) != "character" & typeof(algo) != "character" + | !(algo %in% zeroSumAlgos[, 1])) { message <- sprintf("Selected %s is not valid") stop(message) } diff --git a/zeroSum/R/regressionObject.R b/zeroSum/R/regressionObject.R index da778fb..6510556 100644 --- a/zeroSum/R/regressionObject.R +++ b/zeroSum/R/regressionObject.R @@ -5,13 +5,95 @@ #' @useDynLib zeroSum, .registration = TRUE #' #' @keywords internal -regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, - type=zeroSumTypes[1, 1], weights=NULL, penalty.factor=NULL, - zeroSumWeights=NULL, fusion=NULL, precision=1e-8, useIntercept=TRUE, - useApprox=TRUE, downScaler=1, algorithm="CD", rotatedUpdates=TRUE, - usePolish=TRUE, standardize=TRUE, lambdaSteps=1, gammaSteps=1, nFold=1, - foldid=NULL, epsilon=NULL, cvStop = 0.1, verbose=FALSE, threads=1, - center=TRUE, useZeroSum, cSum = 0.0) { +regressionObject <- function(x, y, type, alpha, lambda, lambdaSteps, weights, + penalty.factor, zeroSum.weights, nFold, foldid, + epsilon, standardize, intercept, useZeroSum, threads, + cvStop, ...) { + args <- list(...) + if (methods::hasArg("beta")) { + beta <- args$beta + } else { + beta <- NULL + } + + if (methods::hasArg("center")) { + center <- args$center + } else { + center <- TRUE + } + + if (methods::hasArg("fusion")) { + fusion <- args$fusion + } else { + fusion <- NULL + } + + if (methods::hasArg("gamma")) { + gamma <- args$gamma + } else { + gamma <- 0.0 + } + + if (methods::hasArg("gammaSteps")) { + gammaSteps <- args$gammaSteps + } else { + gammaSteps <- 1 + } + + if (methods::hasArg("useApprox")) { + useApprox <- args$useApprox + } else { + useApprox <- TRUE + } + + if (methods::hasArg("downScaler")) { + downScaler <- args$downScaler + } else { + downScaler <- 1.0 + } + + if (methods::hasArg("algorithm")) { + algorithm <- args$algorithm + } else { + algorithm <- "CD" + } + + if (methods::hasArg("verbose")) { + verbose <- args$verbose + } else { + verbose <- FALSE + } + + if (methods::hasArg("polish")) { + usePolish <- args$polish + } else { + usePolish <- TRUE + } + + if (methods::hasArg("precision")) { + precision <- args$precision + } else { + precision <- 1e-8 + } + + if (methods::hasArg("rotatedUpdates")) { + rotatedUpdates <- args$rotatedUpdates + } else { + rotatedUpdates <- FALSE + } + + if (methods::hasArg("cSum")) { + cSum <- args$cSum + } else { + cSum <- 0.0 + } + + if (methods::hasArg("seed")) { + seed <- as.integer(args$seed) + } else { + seed <- sample(.Machine$integer.max, size = 1) + } + data <- list() checkType(type) id <- which(zeroSumTypes[, 1] == type) @@ -30,8 +112,8 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, data$P <- P data$N <- N - data$cSum <- checkDouble(cSum, "cSum") - data$downScaler <- checkDouble(downScaler, "downScaler") + data$cSum <- checkDouble(cSum, "cSum") + data$downScaler <- checkDouble(downScaler, "downScaler") data$alpha <- checkDouble(alpha, "alpha") if (threads == "auto") { @@ -42,7 +124,7 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, if (data$type == zeroSumTypes[4, 2]) { data$K <- 1 - useIntercept <- as.integer(FALSE) + intercept <- as.integer(FALSE) data$status <- tmp$status } @@ -53,14 +135,14 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, } data$v <- penalty.factor - if (is.null(zeroSumWeights)) { - zeroSumWeights <- rep(1, P) + if (is.null(zeroSum.weights)) { + zeroSum.weights <- rep(1, P) } else { - checkNonNegativeWeights(zeroSumWeights, P, "zeroSumWeights") + checkNonNegativeWeights(zeroSum.weights, P, "zeroSum.weights") } - data$u <- zeroSumWeights - data$gammaSteps <- checkInteger(gammaSteps, "gammaSteps") - data$gamma <- checkDouble(gamma, "gamma") + data$u <- zeroSum.weights + data$gammaSteps <- checkInteger(gammaSteps, "gammaSteps") + data$gamma <- checkDouble(gamma, "gamma") if (length(gamma) == 1 && gamma == 0.0) { data$useFusion <- as.integer(FALSE) @@ -92,8 +174,10 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, ## check if more cv folds than samples are selected if (data$nFold > N) { - print(paste0("more CV folds (default nFold=10) than sample size! ", - "Setting nFold to ", N)) + print(paste0( + "more CV folds (default nFold=10) than sample size! ", + "Setting nFold to ", N + )) data$nFold <- N } @@ -102,51 +186,58 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, nonCensored <- sum(data$status == 1) if (nonCensored < data$nFold) { - print(paste0("more CV folds (default nFold=10) than ", - "non-censored ! Setting nFold to ", nonCensored)) + print(paste0( + "more CV folds (default nFold=10) than ", + "non-censored ! Setting nFold to ", nonCensored + )) data$nFold <- nonCensored } data$foldid <- rep(0, N) data$foldid[data$status == 1] <- sample(rep(rep(1:data$nFold), - length.out = nonCensored)) + length.out = nonCensored + )) data$foldid[data$status == 0] <- sample(rep(rep(1:data$nFold), - length.out = data$N - nonCensored)) - }else{ + length.out = data$N - nonCensored + )) + } else { data$foldid <- sample(rep(1:data$nFold, length.out = N)) } - }else{ - if (!is.null(tmp$ord)) + } else { + if (!is.null(tmp$ord)) { foldid <- foldid[tmp$ord] + } - if (length(foldid) != N) + if (length(foldid) != N) { stop("invalid fold numbering (( length(foldid) != N ))\n") + } data$nFold <- max(foldid) data$foldid <- foldid } data$foldid <- as.integer(data$foldid) - data$nFold <- as.integer(data$nFold) + data$nFold <- as.integer(data$nFold) - if (data$nFold != 0) + if (data$nFold != 0) { checkFolds(data$status, data$foldid, data$nFold, data$type) + } if (is.null(fusion)) { data$fusion <- NULL - data$nc <- as.integer(0) - }else{ + data$nc <- as.integer(0) + } else { checkSparseMatrix(fusion, "fusion") data$fusion <- fusion - data$nc <- as.integer(nrow(fusion)) + data$nc <- as.integer(nrow(fusion)) } - data$useIntercept <- checkInteger(useIntercept) + data$useIntercept <- checkInteger(intercept) data$center <- as.integer(center) if (standardize == TRUE) { data$standardize <- as.integer(TRUE) - } else{ + } else { data$standardize <- as.integer(FALSE) } @@ -166,24 +257,21 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, ridge <- TRUE } lambdaMax <- NULL - nM <- NULL + nM <- NULL res <- NULL if (data$type == zeroSumTypes[1, 2]) { - nM <- sum(data$y * data$w) + nM <- sum(data$y * data$w) res <- as.matrix(data$y - nM, ncol = 1) * data$w - - }else if (data$type == zeroSumTypes[2, 2]) { - nM <- getLogisticNullModel(data$y, data$w) + } else if (data$type == zeroSumTypes[2, 2]) { + nM <- getLogisticNullModel(data$y, data$w) res <- as.matrix((nM$z - nM$beta0) * nM$w, ncol = 1) - } else if (data$type == zeroSumTypes[3, 2]) { nM <- getMultinomialNullModel(data$y, data$w, 10) res <- matrix(0, ncol = ncol(nM$z), nrow = nrow(nM$z)) for (i in 1:nrow(res)) res[i, ] <- (nM$z[i, ] - nM$beta0) * nM$w[i, ] - - } else if (data$type == zeroSumTypes[4, 2]) { + } else if (data$type == zeroSumTypes[4, 2]) { res <- getCoxNullModel(data$y, data$status, data$w) } @@ -194,15 +282,17 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, wm <- (data$w %*% data$x) * sw for (i in 1:P) vtmp[i] <- data$v[i] * sqrt(((data$x[, i] - wm[i])^2 %*% - data$w) * sw) + data$w) * sw) } if (data$useZeroSum) { lambdaMax <- .Call("lambdaMax", data$x, res, data$u, vtmp, - data$alpha, PACKAGE = "zeroSum") - }else{ + data$alpha, + PACKAGE = "zeroSum" + ) + } else { lambdaMax <- max((abs(t(data$x) %*% res) - / (vtmp * data$alpha))[vtmp != 0]) + / (vtmp * data$alpha))[vtmp != 0]) } if (!is.null(epsilon)) { @@ -213,7 +303,6 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, } else { epsilon <- 0.0001 } - } # lambdaMin is calculated with the epsilon paramter @@ -222,7 +311,8 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, # the lambda sequence is constructed by equally distributing lambdaSteps # value on the lineare log scale between lambdaMin and lambdaMax data$lambda <- exp(seq(log(lambdaMax), log(lambdaMin), - length.out = data$lambdaSteps)) + length.out = data$lambdaSteps + )) # revert alpha to zero in the ridge case if (ridge == TRUE) { @@ -232,23 +322,23 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, if (length(data$gamma) == 1 && data$gammaSteps > 1) { print("approx gamma todo") - - }else if (length(data$gamma) > 1) { + } else if (length(data$gamma) > 1) { data$gammaSteps <- length(data$gamma) } - - if (cvStop == FALSE) - cvStop <- 1.0; + if (cvStop == FALSE) { + cvStop <- 1.0 + } data$cvStop <- checkDouble(cvStop, "CV-Stop") - if (data$cvStop < 0 || data$cvStop > 1) + if (data$cvStop < 0 || data$cvStop > 1) { stop("cvStop is not within [0,1]\n") + } data$cvStop <- as.integer(round(data$lambdaSteps * data$cvStop)) if (is.null(data$fusion)) { data$fusionC <- NULL - }else{ + } else { data$fusionC <- as.matrix(Matrix::summary(data$fusion)) data$fusionC <- data$fusionC[order(data$fusionC[, 2]), ] data$fusionC[, 1:2] <- data$fusionC[, 1:2] - 1 ## C starts with 0 @@ -260,24 +350,25 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, cols <- cols * (data$nFold + 1) beta <- matrix(0, ncol = cols, nrow = data$P + 1) - if(cSum != 0.0){ + if (cSum != 0.0) { beta[2, ] <- rep(cSum, cols) } - }else{ + } else { beta <- as.matrix(beta) if (nrow(beta) != ncol(x) + 1) { stop("Length of betas doesn't match ncol(x)!") } if (ncol(beta) != (data$nFold + 1) * data$K) { - stop("Ncol of beta must be equal to the number of CV folds +1 ", - "(for creating the final model using all samples)!") + stop( + "Ncol of beta must be equal to the number of CV folds +1 ", + "(for creating the final model using all samples)!" + ) } if (data$useZeroSum) { - if (!any(as.numeric(beta[-1, ] %*% data$u) != data$cSum)) { + if (!any(as.numeric(t(beta[-1, ]) %*% data$u) != data$cSum)) { stop("Sum of betas doesn't match cSum!") } } - } data$beta <- beta @@ -291,20 +382,20 @@ regressionObject <- function(x, y, beta, alpha, lambda, gamma=0.0, if (data$useFusion) { - data$algorithm <- as.integer(3) + data$algorithm <- as.integer(3) } data$useApprox <- checkInteger(useApprox) if (data$type == zeroSumTypes[1, 2]) { data$useApprox <- as.integer(FALSE) - }else if (data$algorithm == 1) { + } else if (data$algorithm == 1) { data$useApprox <- as.integer(TRUE) } data$rotatedUpdates <- checkInteger(rotatedUpdates) - data$usePolish <- checkInteger(usePolish) - data$seed <- sample(.Machine$integer.max, size = 1) + data$usePolish <- checkInteger(usePolish) + data$seed <- sample(.Machine$integer.max, size = 1) return(data) } diff --git a/zeroSum/R/zeroSum.R b/zeroSum/R/zeroSum.R index 799b280..60f3a13 100644 --- a/zeroSum/R/zeroSum.R +++ b/zeroSum/R/zeroSum.R @@ -95,120 +95,36 @@ #' #' @examples #' set.seed(1) -#' x <- log2(exampleData$x+1) +#' x <- log2(exampleData$x + 1) #' y <- exampleData$y -#' fit <- zeroSum( x, y, alpha=1) -#' plot( fit, "test") -#' coef(fit, s="lambda.min") -#' -#' +#' fit <- zeroSum(x, y, alpha = 1) +#' plot(fit, "test") +#' coef(fit, s = "lambda.min") #' @useDynLib zeroSum, .registration = TRUE #' @export -zeroSum <- function( - x, - y, - family = "gaussian", - alpha = 1.0, - lambda = NULL, - lambdaSteps = 100, - weights = NULL, - penalty.factor = NULL, - zeroSum.weights = NULL, - nFold = NULL, - foldid = NULL, - epsilon = NULL, - standardize = FALSE, - intercept = TRUE, - zeroSum = TRUE, - threads = "auto", - cvStop = 0.1, - ...) { - - args <- list(...) - - if (methods::hasArg("beta")) { - beta <- args$beta - } else { - beta <- NULL - } - - if (methods::hasArg("center")) { - center <- args$center - } else { - center <- TRUE - } - - if (methods::hasArg("fusion")) { - fusion <- args$fusion - } else { - fusion <- NULL - } - - if (methods::hasArg("gamma")) { - gamma <- args$gamma - } else { - gamma <- 0.0 - } - - if (methods::hasArg("gammaSteps")) { - gammaSteps <- args$gammaSteps - } else { - gammaSteps <- 1 - } - - if (methods::hasArg("useApprox")) { - useApprox <- args$useApprox - } else { - useApprox <- TRUE - } - - if (methods::hasArg("downScaler")) { - downScaler <- args$downScaler - } else { - downScaler <- 1.0 - } - - if (methods::hasArg("algorithm")) { - algorithm <- args$algorithm - } else { - algorithm <- "CD" - } - - if (methods::hasArg("verbose")) { - verbose <- args$verbose - } else { - verbose <- FALSE - } - - if (methods::hasArg("polish")) { - usePolish <- args$polish - } else { - usePolish <- TRUE - } - - if (methods::hasArg("precision")) { - precision <- args$precision - } else { - precision <- 1e-8 - } - - if (methods::hasArg("rotatedUpdates")) { - rotatedUpdates <- args$rotatedUpdates - } else { - rotatedUpdates <- FALSE - } - - if (methods::hasArg("cSum")) { - cSum <- args$cSum - } else { - cSum <- 0.0 - } - - data <- regressionObject(x, y, beta, alpha, lambda, gamma, family, weights, - penalty.factor, zeroSum.weights, fusion, precision, intercept, - useApprox, downScaler, algorithm, rotatedUpdates, usePolish, - standardize, lambdaSteps, gammaSteps, nFold, foldid, epsilon, - cvStop, verbose, threads, center, zeroSum, cSum) +zeroSum <- function(x, + y, + family = "gaussian", + alpha = 1.0, + lambda = NULL, + lambdaSteps = 100, + weights = NULL, + penalty.factor = NULL, + zeroSum.weights = NULL, + nFold = NULL, + foldid = NULL, + epsilon = NULL, + standardize = FALSE, + intercept = TRUE, + zeroSum = TRUE, + threads = "auto", + cvStop = 0.1, + ...) { + data <- regressionObject( + x, y, family, alpha, lambda, lambdaSteps, weights, + penalty.factor, zeroSum.weights, nFold, foldid, epsilon, + standardize, intercept, zeroSum, threads, cvStop, ... + ) start <- Sys.time() @@ -217,7 +133,7 @@ zeroSum <- function( end <- Sys.time() runtime <- as.numeric(end - start, units = "secs") - if (verbose) { + if (data$verbose) { print(sprintf("runtime: %.3fs", runtime)) } diff --git a/zeroSum/man/coef.zeroSum.Rd b/zeroSum/man/coef.zeroSum.Rd index 3f274d5..a250b95 100644 --- a/zeroSum/man/coef.zeroSum.Rd +++ b/zeroSum/man/coef.zeroSum.Rd @@ -23,9 +23,8 @@ This function returns the coefficients of a zeroSum object object. } \examples{ set.seed(1) -x <- log2(exampleData$x+1) +x <- log2(exampleData$x + 1) y <- exampleData$y -fit <- zeroSum( x, y, alpha=1) -coef(fit, s="lambda.min") - +fit <- zeroSum(x, y, alpha = 1) +coef(fit, s = "lambda.min") } diff --git a/zeroSum/man/exportToCSV.Rd b/zeroSum/man/exportToCSV.Rd index afb0787..21ad84f 100644 --- a/zeroSum/man/exportToCSV.Rd +++ b/zeroSum/man/exportToCSV.Rd @@ -9,7 +9,7 @@ exportToCSV(x, y, family = "gaussian", alpha = 1, lambda = NULL, lambdaSteps = 100, weights = NULL, penalty.factor = NULL, zeroSum.weights = NULL, nFold = NULL, foldid = NULL, epsilon = NULL, standardize = FALSE, intercept = TRUE, - zeroSum = TRUE, threads = 1, cvStop = 0.1, path = "", + zeroSum = TRUE, threads = "auto", cvStop = 0.1, path = "", name = "", ...) } \description{ diff --git a/zeroSum/man/extCostFunction.Rd b/zeroSum/man/extCostFunction.Rd index 9708908..8f8320f 100644 --- a/zeroSum/man/extCostFunction.Rd +++ b/zeroSum/man/extCostFunction.Rd @@ -6,9 +6,12 @@ wraps input into a regression object and calls the costFunction and returns the result} \usage{ -extCostFunction(x, y, beta, alpha = 1, lambda = 0, gamma = 0, - family = zeroSumTypes[1, 1], weights = NULL, penalty.factor = NULL, - fusion = NULL, useC = FALSE) +extCostFunction(x, y, beta, alpha = 1, lambda = NULL, + family = "gaussian", lambdaSteps = 100, weights = NULL, + penalty.factor = NULL, zeroSum.weights = NULL, nFold = NULL, + foldid = NULL, epsilon = NULL, standardize = FALSE, + intercept = TRUE, zeroSum = TRUE, threads = "auto", cvStop = 0.1, + useC = FALSE, ...) } \description{ Description of costFunction function diff --git a/zeroSum/man/regressionObject.Rd b/zeroSum/man/regressionObject.Rd index b2b9444..2fdd303 100644 --- a/zeroSum/man/regressionObject.Rd +++ b/zeroSum/man/regressionObject.Rd @@ -4,14 +4,9 @@ \alias{regressionObject} \title{Description of regresion object} \usage{ -regressionObject(x, y, beta, alpha, lambda, gamma = 0, - type = zeroSumTypes[1, 1], weights = NULL, penalty.factor = NULL, - zeroSumWeights = NULL, fusion = NULL, precision = 1e-08, - useIntercept = TRUE, useApprox = TRUE, downScaler = 1, - algorithm = "CD", rotatedUpdates = TRUE, usePolish = TRUE, - standardize = TRUE, lambdaSteps = 1, gammaSteps = 1, nFold = 1, - foldid = NULL, epsilon = NULL, cvStop = 0.1, verbose = FALSE, - threads = 1, center = TRUE, useZeroSum, cSum = 0) +regressionObject(x, y, type, alpha, lambda, lambdaSteps, weights, + penalty.factor, zeroSum.weights, nFold, foldid, epsilon, standardize, + intercept, useZeroSum, threads, cvStop, ...) } \description{ Description of regresion object diff --git a/zeroSum/man/zeroSum.Rd b/zeroSum/man/zeroSum.Rd index d9c124d..26b3124 100644 --- a/zeroSum/man/zeroSum.Rd +++ b/zeroSum/man/zeroSum.Rd @@ -105,11 +105,9 @@ elastic-net the optimal regularization is determined by cross validation. } \examples{ set.seed(1) -x <- log2(exampleData$x+1) +x <- log2(exampleData$x + 1) y <- exampleData$y -fit <- zeroSum( x, y, alpha=1) -plot( fit, "test") -coef(fit, s="lambda.min") - - +fit <- zeroSum(x, y, alpha = 1) +plot(fit, "test") +coef(fit, s = "lambda.min") } diff --git a/zeroSum/src/Parallel.h b/zeroSum/src/Parallel.h index 400b83e..1e003fd 100644 --- a/zeroSum/src/Parallel.h +++ b/zeroSum/src/Parallel.h @@ -1,7 +1,8 @@ -#ifndef THREAD_POOL_H -#define THREAD_POOL_H +#ifndef PARALLEL_H +#define PARALLEL_H #include +#include #include #include diff --git a/zeroSum/src/zeroSum.h b/zeroSum/src/zeroSum.h index 08a0e0b..fff3bb6 100644 --- a/zeroSum/src/zeroSum.h +++ b/zeroSum/src/zeroSum.h @@ -21,8 +21,6 @@ #include #include #include -#include -#include #include #include #include diff --git a/zeroSum/tests/testthat/test_costfunction_cox.R b/zeroSum/tests/testthat/test_costfunction_cox.R index b84a5de..0f8e3de 100644 --- a/zeroSum/tests/testthat/test_costfunction_cox.R +++ b/zeroSum/tests/testthat/test_costfunction_cox.R @@ -18,8 +18,10 @@ test_that("cox cost function is correct", { for (i in 1:(P - 1)) fusion[i, (i + 1)] <- -1 gamma <- 0.30 - cost <- extCostFunction(x, y, rnorm(P + 1), alpha, lambda, family = "cox", - gamma = gamma, fusion = fusion, useC = TRUE) + cost <- extCostFunction(x, y, rnorm(P + 1), alpha, lambda, + family = "cox", + gamma = gamma, fusion = fusion, useC = TRUE + ) expect_equal(cost$loglikelihood, cost$C$loglikelihood, tolerance = 1e-10) expect_equal(cost$ridge, cost$C$ridge, tolerance = 1e-10) diff --git a/zeroSum/tests/testthat/test_costfunction_linear.R b/zeroSum/tests/testthat/test_costfunction_linear.R index 3691235..4bf3cf1 100644 --- a/zeroSum/tests/testthat/test_costfunction_linear.R +++ b/zeroSum/tests/testthat/test_costfunction_linear.R @@ -19,11 +19,12 @@ test_that("linear cost function is correct", { gamma <- 0.30 cost <- extCostFunction(x, y, rnorm(P + 1), alpha, lambda, - family = "gaussian", gamma = gamma, fusion = fusion, useC = TRUE) + family = "gaussian", gamma = gamma, fusion = fusion, useC = TRUE + ) expect_equal(cost$loglikelihood, cost$C$loglikelihood, tolerance = 1e-13) - expect_equal(cost$ridge, cost$C$ridge, tolerance = 1e-13) - expect_equal(cost$lasso, cost$C$lasso, tolerance = 1e-13) - expect_equal(cost$fusion, cost$C$fusion, tolerance = 1e-13) - expect_equal(cost$cost, cost$C$cost, tolerance = 1e-13) + expect_equal(cost$ridge, cost$C$ridge, tolerance = 1e-13) + expect_equal(cost$lasso, cost$C$lasso, tolerance = 1e-13) + expect_equal(cost$fusion, cost$C$fusion, tolerance = 1e-13) + expect_equal(cost$cost, cost$C$cost, tolerance = 1e-13) }) diff --git a/zeroSum/tests/testthat/test_costfunction_multinomial.R b/zeroSum/tests/testthat/test_costfunction_multinomial.R index a561568..0227976 100644 --- a/zeroSum/tests/testthat/test_costfunction_multinomial.R +++ b/zeroSum/tests/testthat/test_costfunction_multinomial.R @@ -1,6 +1,6 @@ context("Testing multinomial cost function") -test_that("multinomial cost function is correct", { +test_that("multinomial cost function is correct", { set.seed(10) x <- log2(exampleData$x) y <- exampleData$yMultinomial @@ -19,12 +19,14 @@ test_that("multinomial cost function is correct", { gamma <- 0.30 beta <- matrix(rnorm((P + 1) * 3), ncol = 3) - cost <- extCostFunction(x, y, beta, alpha, lambda, family = "multinomial", - gamma = gamma, fusion = fusion, useC = TRUE) + cost <- extCostFunction(x, y, beta, alpha, lambda, + family = "multinomial", + gamma = gamma, fusion = fusion, useC = TRUE + ) expect_equal(cost$loglikelihood, cost$C$loglikelihood, tolerance = 1e-15) - expect_equal(cost$ridge, cost$C$ridge, tolerance = 1e-15) - expect_equal(cost$lasso, cost$C$lasso, tolerance = 1e-15) - expect_equal(cost$fusion, cost$C$fusion, tolerance = 1e-15) - expect_equal(cost$cost, cost$C$cost, tolerance = 1e-15) + expect_equal(cost$ridge, cost$C$ridge, tolerance = 1e-15) + expect_equal(cost$lasso, cost$C$lasso, tolerance = 1e-15) + expect_equal(cost$fusion, cost$C$fusion, tolerance = 1e-15) + expect_equal(cost$cost, cost$C$cost, tolerance = 1e-15) }) diff --git a/zeroSum/tests/testthat/test_coxRegression.R b/zeroSum/tests/testthat/test_coxRegression.R index eb20b33..dbe3b4e 100644 --- a/zeroSum/tests/testthat/test_coxRegression.R +++ b/zeroSum/tests/testthat/test_coxRegression.R @@ -9,45 +9,62 @@ test_that("cox regression test", { alpha <- 0.5 lambda <- 0.2 - A <- zeroSum(x, y, alpha, lambda, family = "cox", standardize = FALSE, - weights = w, zeroSum = FALSE) + A <- zeroSum(x, y, alpha, lambda, + family = "cox", standardize = FALSE, + weights = w, zeroSum = FALSE + ) eA <- extCostFunction(x, y, coef(A), alpha, lambda, family = "cox") - A_LS <- zeroSum(x, y, alpha, lambda, family = "cox", standardize = FALSE, - algorithm = "LS", weights = w, zeroSum = FALSE) + A_LS <- zeroSum(x, y, alpha, lambda, + family = "cox", standardize = FALSE, + algorithm = "LS", weights = w, zeroSum = FALSE + ) eA_LS <- extCostFunction(x, y, coef(A_LS), alpha, lambda, family = "cox") - A_LS2 <- zeroSum(x, y, alpha, lambda, family = "cox", standardize = FALSE, - algorithm = "LS", useApprox = FALSE, weights = w, - zeroSum = FALSE) + A_LS2 <- zeroSum(x, y, alpha, lambda, + family = "cox", standardize = FALSE, + algorithm = "LS", useApprox = FALSE, weights = w, + zeroSum = FALSE + ) eA_LS2 <- extCostFunction(x, y, coef(A_LS2), alpha, lambda, family = "cox") eCompA <- extCostFunction(x, y, ref$test_cox$A, alpha, lambda, - family = "cox") + family = "cox" + ) expect_equal(eA$cost, eA_LS$cost, tolerance = 1e-4) expect_equal(cor(as.numeric(coef(A)), as.numeric(coef(A_LS))), - 1.0, tolerance = 1e-3) + 1.0, + tolerance = 1e-3 + ) expect_equal(eA$cost, eA_LS2$cost, tolerance = 1e-4) expect_equal(cor(as.numeric(coef(A)), as.numeric(coef(A_LS2))), 1.0, - tolerance = 1e-2) + tolerance = 1e-2 + ) expect_equal(eA$cost, eCompA$cost, tolerance = 1e-4) expect_equal(cor(as.numeric(coef(A)), as.numeric(ref$test_cox$A)), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) - B <- zeroSum(x, y, alpha, lambda, standardize = FALSE, - family = "cox") + B <- zeroSum(x, y, alpha, lambda, + standardize = FALSE, + family = "cox" + ) eB <- extCostFunction(x, y, coef(B), alpha, lambda, family = "cox") - B_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - family = "cox") + B_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + family = "cox" + ) eB_LS <- extCostFunction(x, y, coef(B_LS), alpha, lambda, family = "cox") - B_LS2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - useApprox = FALSE, family = "cox") + B_LS2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + useApprox = FALSE, family = "cox" + ) eB_LS2 <- extCostFunction(x, y, coef(B_LS2), alpha, lambda, family = "cox") @@ -55,64 +72,85 @@ test_that("cox regression test", { expect_equal(eB$cost, eB_LS2$cost, tolerance = 0.05) expect_equal(cor(as.numeric(coef(B)), as.numeric(coef(B_LS))), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) expect_equal(cor(as.numeric(coef(B_LS)), as.numeric(coef(B_LS2))), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) expect_equal(sum(coef(B)[-1]), 0.0, tolerance = 1e-14) expect_equal(sum(coef(B_LS)[-1]), 0.0, tolerance = 1e-14) - C <- zeroSum(x, y, alpha, lambda, standardize = TRUE, - family = "cox", zeroSum = FALSE) + C <- zeroSum(x, y, alpha, lambda, + standardize = TRUE, + family = "cox", zeroSum = FALSE + ) eC <- extCostFunction(x, y, coef(C), alpha, lambda, family = "cox") - C_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - family = "cox", zeroSum = FALSE) + C_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + family = "cox", zeroSum = FALSE + ) eC_LS <- extCostFunction(x, y, coef(C_LS), alpha, lambda, family = "cox") - C_LS2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - useApprox = FALSE, family = "cox", zeroSum = FALSE) + C_LS2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + useApprox = FALSE, family = "cox", zeroSum = FALSE + ) eC_LS2 <- extCostFunction(x, y, coef(C_LS2), alpha, lambda, family = "cox") eCompC <- extCostFunction(x, y, ref$test_cox$C, alpha, - lambda, family = "cox") + lambda, + family = "cox" + ) ## glmnet packages gives here slightly wore results -> higher tolerance expect_equal(eC$cost, eCompC$cost, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(C)), as.numeric(ref$test_cox$C)), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) ## ls seems to perform a little bit better -> higher tolerance expect_equal(eC$cost, eC_LS$cost, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(C)), as.numeric(coef(C_LS))), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) expect_equal(eC$cost, eC_LS2$cost, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(C)), as.numeric(coef(C_LS2))), 1.0, - tolerance = 1e-2) + tolerance = 1e-2 + ) - D <- zeroSum(x, y, alpha, lambda, standardize = TRUE, - family = "cox") + D <- zeroSum(x, y, alpha, lambda, + standardize = TRUE, + family = "cox" + ) eD <- extCostFunction(x, y, coef(D), alpha, lambda, family = "cox") - D_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - family = "cox") + D_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + family = "cox" + ) eD_LS <- extCostFunction(x, y, coef(D_LS), alpha, lambda, family = "cox") - D_LS2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - useApprox = FALSE, family = "cox") + D_LS2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + useApprox = FALSE, family = "cox" + ) eD_LS2 <- extCostFunction(x, y, coef(D_LS2), alpha, lambda, family = "cox") expect_equal(eD$cost / eD_LS$cost, 1.0, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(D)), as.numeric(coef(D_LS))), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) expect_equal(eD$cost / eD_LS2$cost, 1.0, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(D)), as.numeric(coef(D_LS2))), 1.0, - tolerance = 1e-4) + tolerance = 1e-4 + ) expect_equal(sum(coef(D)[-1]), 0.0, tolerance = 1e-14) expect_equal(sum(coef(D_LS)[-1]), 0.0, tolerance = 1e-14) @@ -122,64 +160,99 @@ test_that("cox regression test", { ## fusion kernel test P <- ncol(x) fusion <- Matrix(0, nrow = P - 1, ncol = P, sparse = TRUE) - for (i in 1:(P - 1)) fusion[i, i] <- 1 + for (i in 1:(P - 1)) fusion[i, i] <- 1 for (i in 1:(P - 1)) fusion[i, (i + 1)] <- -1 gamma <- 0.03 lambda <- 0.001 set.seed(1) - FA1 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", gamma = gamma, - fusion = fusion, family = "cox", zeroSum = FALSE) - eFA1 <- extCostFunction(x, y, coef(FA1), alpha, lambda, family = "cox", - gamma = gamma, fusion = fusion) - - FA2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", gamma = gamma, - fusion = fusion, useApprox = FALSE, - family = "cox", zeroSum = FALSE) - eFA2 <- extCostFunction(x, y, coef(FA2), alpha, lambda, family = "cox", - gamma = gamma, fusion = fusion) - - FA3 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", gamma = gamma, - fusion = fusion, family = "cox") - eFA3 <- extCostFunction(x, y, coef(FA3), alpha, lambda, family = "cox", - gamma = gamma, fusion = fusion) - - FA4 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", gamma = gamma, - fusion = fusion, useApprox = FALSE, family = "cox") - eFA4 <- extCostFunction(x, y, coef(FA4), alpha, lambda, family = "cox", - gamma = gamma, fusion = fusion) - - FA5 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, family = "cox", zeroSum = FALSE) - eFA5 <- extCostFunction(x, y, coef(FA5), alpha, lambda, family = "cox", - gamma = gamma, fusion = fusion) - - FA6 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, useApprox = FALSE, - family = "cox", zeroSum = FALSE) - eFA6 <- extCostFunction(x, y, coef(FA6), alpha, lambda, family = "cox", - gamma = gamma, fusion = fusion) - - FA7 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, family = "cox") - eFA7 <- extCostFunction(x, y, coef(FA7), alpha, lambda, family = "cox", - gamma = gamma, fusion = fusion) - - FA8 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, useApprox = FALSE, family = "cox") - eFA8 <- extCostFunction(x, y, coef(FA8), alpha, lambda, family = "cox", - gamma = gamma, fusion = fusion) - - - costs <- c(eFA1$cost, eFA2$cost, eFA3$cost, eFA4$cost, eFA5$cost, eFA6$cost, - eFA7$cost, eFA8$cost) + FA1 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", gamma = gamma, + fusion = fusion, family = "cox", zeroSum = FALSE + ) + eFA1 <- extCostFunction(x, y, coef(FA1), alpha, lambda, + family = "cox", + gamma = gamma, fusion = fusion + ) + + FA2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", gamma = gamma, + fusion = fusion, useApprox = FALSE, + family = "cox", zeroSum = FALSE + ) + eFA2 <- extCostFunction(x, y, coef(FA2), alpha, lambda, + family = "cox", + gamma = gamma, fusion = fusion + ) + + FA3 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", gamma = gamma, + fusion = fusion, family = "cox" + ) + eFA3 <- extCostFunction(x, y, coef(FA3), alpha, lambda, + family = "cox", + gamma = gamma, fusion = fusion + ) + + FA4 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", gamma = gamma, + fusion = fusion, useApprox = FALSE, family = "cox" + ) + eFA4 <- extCostFunction(x, y, coef(FA4), alpha, lambda, + family = "cox", + gamma = gamma, fusion = fusion + ) + + FA5 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, family = "cox", zeroSum = FALSE + ) + eFA5 <- extCostFunction(x, y, coef(FA5), alpha, lambda, + family = "cox", + gamma = gamma, fusion = fusion + ) + + FA6 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, useApprox = FALSE, + family = "cox", zeroSum = FALSE + ) + eFA6 <- extCostFunction(x, y, coef(FA6), alpha, lambda, + family = "cox", + gamma = gamma, fusion = fusion + ) + + FA7 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, family = "cox" + ) + eFA7 <- extCostFunction(x, y, coef(FA7), alpha, lambda, + family = "cox", + gamma = gamma, fusion = fusion + ) + + FA8 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, useApprox = FALSE, family = "cox" + ) + eFA8 <- extCostFunction(x, y, coef(FA8), alpha, lambda, + family = "cox", + gamma = gamma, fusion = fusion + ) + + + costs <- c( + eFA1$cost, eFA2$cost, eFA3$cost, eFA4$cost, eFA5$cost, eFA6$cost, + eFA7$cost, eFA8$cost + ) for (i in 1:length(costs)) expect_lte(costs[i], ref$test_cox$fusionCosts[i] + 5e-2) # calculate fusion terms and expect that adjacent features are equal - fused <- abs(as.numeric(fusion %*% cbind(coef(FA1), coef(FA2), coef(FA3), - coef(FA4), coef(FA5), coef(FA6), coef(FA7), coef(FA8))[-1, ])) + fused <- abs(as.numeric(fusion %*% cbind( + coef(FA1), coef(FA2), coef(FA3), + coef(FA4), coef(FA5), coef(FA6), coef(FA7), coef(FA8) + )[-1, ])) expect_gte(sum(fused < 1e-3) / length(fused), 0.3) - }) diff --git a/zeroSum/tests/testthat/test_cv.R b/zeroSum/tests/testthat/test_cv.R index aab3769..05112c1 100644 --- a/zeroSum/tests/testthat/test_cv.R +++ b/zeroSum/tests/testthat/test_cv.R @@ -1,28 +1,43 @@ context("Testing cross validation") test_that("cross validation okay", { - x <- log2(exampleData$x) y <- exampleData$y set.seed(1) - fi <- sample(rep(rep(1:10), length.out = nrow(x))) - A <- zeroSum(x, y, family = "gaussian", zeroSum = FALSE, foldid = fi, - standardize = FALSE, threads = 1) - B <- zeroSum(x, y, family = "gaussian", foldid = fi, - standardize = TRUE, threads = 4) + fi <- c( + 9L, 2L, 7L, 6L, 6L, 4L, 8L, 6L, 5L, 2L, 5L, 4L, 4L, 7L, 9L, + 8L, 1L, 9L, 1L, 10L, 3L, 3L, 7L, 2L, 5L, 10L, 1L, 8L, 1L, 3L, + 10L + ) + A <- zeroSum(x, y, + family = "gaussian", zeroSum = FALSE, foldid = fi, + standardize = FALSE, threads = 1 + ) + B <- zeroSum(x, y, + family = "gaussian", foldid = fi, + standardize = TRUE, threads = 4 + ) y <- exampleData$ylogistic - C <- zeroSum(x, y, family = "binomial", zeroSum = FALSE, - foldid = fi, standardize = FALSE) - D <- zeroSum(x, y, family = "binomial", zeroSum = FALSE, - foldid = fi, standardize = TRUE) + C <- zeroSum(x, y, + family = "binomial", zeroSum = FALSE, + foldid = fi, standardize = FALSE + ) + D <- zeroSum(x, y, + family = "binomial", zeroSum = FALSE, + foldid = fi, standardize = TRUE + ) y <- exampleData$yCox set.seed(1) - F <- zeroSum(x, y, family = "cox", foldid = fi, zeroSum = FALSE, - threads = 1) + F <- zeroSum(x, y, + family = "cox", foldid = fi, zeroSum = FALSE, + threads = 1 + ) set.seed(1) - G <- zeroSum(x, y, family = "cox", foldid = fi, zeroSum = FALSE, - threads = 4) + G <- zeroSum(x, y, + family = "cox", foldid = fi, zeroSum = FALSE, + threads = 4 + ) ref <- readRDS("references.rds") expect_equal(ref$test_cv$A$cv_stats, A$cv_stats, tolerance = 1e-1) @@ -33,8 +48,10 @@ test_that("cross validation okay", { expect_equal(F$cv_stats, G$cv_stats, tolerance = 1e-10) expect_equal(ref$test_cv$F$cv_stats[1:68, ], F$cv_stats[1:68, ], - tolerance = 1e-1) + tolerance = 1e-1 + ) expect_equal(ref$test_cv$G$cv_stats[1:68, ], G$cv_stats[1:68, ], - tolerance = 1e-1) + tolerance = 1e-1 + ) expect_equal(ref$test_cv$glmnetCoefs, as.numeric(coef(F)), tolerance = 1e-1) }) diff --git a/zeroSum/tests/testthat/test_lambdaApprox.R b/zeroSum/tests/testthat/test_lambdaApprox.R index 4dd5cc5..7296e37 100644 --- a/zeroSum/tests/testthat/test_lambdaApprox.R +++ b/zeroSum/tests/testthat/test_lambdaApprox.R @@ -5,8 +5,21 @@ test_that("lambda approximation equals glmnet", { y <- exampleData$y set.seed(1) - fi <- sample(rep(rep(1:10), length.out = nrow(x))) - w <- runif(nrow(x)) + fi <- c( + 9L, 2L, 7L, 6L, 6L, 4L, 8L, 6L, 5L, 2L, 5L, 4L, 4L, 7L, 9L, + 8L, 1L, 9L, 1L, 10L, 3L, 3L, 7L, 2L, 5L, 10L, 1L, 8L, 1L, 3L, + 10L + ) + w <- c( + 0.599565825425088, 0.493541307048872, 0.186217601411045, 0.827373318606988, + 0.668466738192365, 0.79423986072652, 0.107943625887856, 0.723710946040228, + 0.411274429643527, 0.820946294115856, 0.647060193819925, 0.78293276228942, + 0.553036311641335, 0.529719580197707, 0.789356231689453, 0.023331202333793, + 0.477230065036565, 0.7323137386702, 0.692731556482613, 0.477619622135535, + 0.8612094768323, 0.438097107224166, 0.244797277031466, 0.0706790471449494, + 0.0994661601725966, 0.31627170718275, 0.518634263193235, 0.662005076417699, + 0.406830187188461, 0.912875924259424, 0.293603372760117 + ) A_zs <- zeroSum(x, y, foldid = fi, weights = w) A_gl <- zeroSum(x, y, foldid = fi, weights = w, zeroSum = FALSE) diff --git a/zeroSum/tests/testthat/test_linearRegression.R b/zeroSum/tests/testthat/test_linearRegression.R index ebfc4e8..052e141 100644 --- a/zeroSum/tests/testthat/test_linearRegression.R +++ b/zeroSum/tests/testthat/test_linearRegression.R @@ -9,78 +9,105 @@ test_that("linear regression equals glmnet", { lambda <- 1.32 ## linear Regression - A <- zeroSum(x, y, alpha, lambda, algorithm = "CD", standardize = FALSE, - family = "gaussian", zeroSum = FALSE) + A <- zeroSum(x, y, alpha, lambda, + algorithm = "CD", standardize = FALSE, + family = "gaussian", zeroSum = FALSE + ) eA <- extCostFunction(x, y, coef(A), alpha, lambda, family = "gaussian") - A_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - family = "gaussian", zeroSum = FALSE) + A_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + family = "gaussian", zeroSum = FALSE + ) eA_LS <- extCostFunction(x, y, coef(A_LS), alpha, lambda, family = "gaussian") eCompA <- extCostFunction(x, y, ref$test_linear$A, alpha, lambda, - family = "gaussian") + family = "gaussian" + ) expect_equal(eA$cost / eCompA$cost, 1.0, tolerance = 1e-2) - expect_equal(eA$cost / eA_LS$cost, 1.0, tolerance = 1e-2) + expect_equal(eA$cost / eA_LS$cost, 1.0, tolerance = 1e-2) expect_equal(cor(as.numeric(coef(A)), as.numeric(coef(A_LS))), - 1.0, tolerance = 1e-5) + 1.0, + tolerance = 1e-5 + ) expect_equal(cor(as.numeric(coef(A)), ref$test_linear$A), - 1.0, tolerance = 1e-5) + 1.0, + tolerance = 1e-5 + ) ## linear Regression zerosum - B <- zeroSum(x, y, alpha, lambda, algorithm = "CD", standardize = FALSE, - family = "gaussian") + B <- zeroSum(x, y, alpha, lambda, + algorithm = "CD", standardize = FALSE, + family = "gaussian" + ) eB <- extCostFunction(x, y, coef(B), alpha, lambda, family = "gaussian") - B_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - family = "gaussian") + B_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + family = "gaussian" + ) eB_LS <- extCostFunction(x, y, coef(B_LS), alpha, lambda, family = "gaussian") expect_equal(eB$cost / eB_LS$cost, 1.0, 1e-3) expect_equal(cor(as.numeric(coef(B)), as.numeric(coef(B_LS))), 1.0, - tolerance = 1e-4) + tolerance = 1e-4 + ) expect_equal(sum(coef(B)[-1]), 0.0, tolerance = 1e-13) expect_equal(sum(coef(B_LS)[-1]), 0.0, tolerance = 1e-13) ## linear Regression standardized - C <- zeroSum(x, y, alpha, lambda, algorithm = "CD", standardize = TRUE, - family = "gaussian", zeroSum = FALSE) + C <- zeroSum(x, y, alpha, lambda, + algorithm = "CD", standardize = TRUE, + family = "gaussian", zeroSum = FALSE + ) eC <- extCostFunction(x, y, coef(C), alpha, lambda, family = "gaussian") - C_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - family = "gaussian", zeroSum = FALSE) + C_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + family = "gaussian", zeroSum = FALSE + ) eC_LS <- extCostFunction(x, y, coef(C_LS), alpha, lambda, family = "gaussian") eCompC <- extCostFunction(x, y, ref$test_linear$C, alpha, lambda, - family = "gaussian") + family = "gaussian" + ) ## glmnet packages gives here slightly wore results -> higher tolerance expect_equal(eC$cost, eCompC$cost, tolerance = 1e-2) expect_equal(cor(as.numeric(coef(C)), ref$test_linear$A), 1.0, - tolerance = 1e-2) + tolerance = 1e-2 + ) ## ls seems to perform a little bit better -> higher tolerance expect_equal(eC$cost, eC_LS$cost, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(C)), as.numeric(coef(C_LS))), 1.0, - tolerance = 1e-6) + tolerance = 1e-6 + ) ## linear Regression zerosum standardized - D <- zeroSum(x, y, alpha, lambda, algorithm = "CD", standardize = TRUE, - family = "gaussian") + D <- zeroSum(x, y, alpha, lambda, + algorithm = "CD", standardize = TRUE, + family = "gaussian" + ) eD <- extCostFunction(x, y, coef(D), alpha, lambda, family = "gaussian") - D_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - family = "gaussian") + D_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + family = "gaussian" + ) eD_LS <- extCostFunction(x, y, coef(D_LS), alpha, lambda, family = "gaussian") expect_equal(eD$cost / eD_LS$cost, 1.0, tolerance = 1e-2) expect_equal(cor(as.numeric(coef(D)), as.numeric(coef(D_LS))), - 1.0, tolerance = 1e-6) + 1.0, + tolerance = 1e-6 + ) expect_equal(sum(coef(D)[-1]), 0.0, tolerance = 1e-13) expect_equal(sum(coef(D_LS)[-1]), 0.0, tolerance = 1e-13) @@ -89,45 +116,62 @@ test_that("linear regression equals glmnet", { ## fusion kernel test P <- ncol(x) fusion <- Matrix(0, nrow = P - 1, ncol = P, sparse = TRUE) - for (i in 1:(P - 1)) fusion[i, i] <- 1 + for (i in 1:(P - 1)) fusion[i, i] <- 1 for (i in 1:(P - 1)) fusion[i, (i + 1)] <- -1 gamma <- 0.30 lambda <- 0.01 set.seed(1) - FA1 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - gamma = gamma, fusion = fusion, family = "gaussian", - zeroSum = FALSE) - eFA1 <- extCostFunction(x, y, coef(FA1), alpha, lambda, family = "gaussian", - gamma = gamma, fusion = fusion) - - FA2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - gamma = gamma, fusion = fusion, family = "gaussian") - eFA2 <- extCostFunction(x, y, coef(FA2), alpha, lambda, family = "gaussian", - gamma = gamma, fusion = fusion) + FA1 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + gamma = gamma, fusion = fusion, family = "gaussian", + zeroSum = FALSE + ) + eFA1 <- extCostFunction(x, y, coef(FA1), alpha, lambda, + family = "gaussian", + gamma = gamma, fusion = fusion + ) + + FA2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + gamma = gamma, fusion = fusion, family = "gaussian" + ) + eFA2 <- extCostFunction(x, y, coef(FA2), alpha, lambda, + family = "gaussian", + gamma = gamma, fusion = fusion + ) gamma <- 0.03 lambda <- 0.001 - FA3 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, family = "gaussian", - zeroSum = FALSE) - eFA3 <- extCostFunction(x, y, coef(FA3), alpha, lambda, family = "gaussian", - gamma = gamma, fusion = fusion) - - FA4 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, family = "gaussian") - eFA4 <- extCostFunction(x, y, coef(FA4), alpha, lambda, family = "gaussian", - gamma = gamma, fusion = fusion) + FA3 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, family = "gaussian", + zeroSum = FALSE + ) + eFA3 <- extCostFunction(x, y, coef(FA3), alpha, lambda, + family = "gaussian", + gamma = gamma, fusion = fusion + ) + + FA4 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, family = "gaussian" + ) + eFA4 <- extCostFunction(x, y, coef(FA4), alpha, lambda, + family = "gaussian", + gamma = gamma, fusion = fusion + ) costs <- c(eFA1$cost, eFA2$cost, eFA3$cost, eFA4$cost) for (i in 1:length(costs)) - expect_lte(costs[i], ref$test_linear$fusionCosts[i] + 5e-2) + expect_lte(costs[i], ref$test_linear$fusionCosts[i] * 1.05) # calculate fusion terms and expect that most adjacent features are equal - fused <- abs(as.numeric(fusion %*% cbind(coef(FA1), coef(FA2), coef(FA3), - coef(FA4))[-1, ])) + fused <- abs(as.numeric(fusion %*% cbind( + coef(FA1), coef(FA2), coef(FA3), + coef(FA4) + )[-1, ])) expect_gte(sum(fused < 1e-5) / length(fused), 0.3) - }) diff --git a/zeroSum/tests/testthat/test_logisticRegression.R b/zeroSum/tests/testthat/test_logisticRegression.R index a116052..c23b8a6 100644 --- a/zeroSum/tests/testthat/test_logisticRegression.R +++ b/zeroSum/tests/testthat/test_logisticRegression.R @@ -9,116 +9,158 @@ test_that("logistic regression equals glmnet", { lambda <- 0.01 ## logistic Regression - A <- zeroSum(x, y, alpha, lambda, algorithm = "CD", standardize = FALSE, - family = "binomial", zeroSum = FALSE) + A <- zeroSum(x, y, alpha, lambda, + algorithm = "CD", standardize = FALSE, + family = "binomial", zeroSum = FALSE + ) eA <- extCostFunction(x, y, coef(A), alpha, lambda, family = "binomial") - A_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - family = "binomial", zeroSum = FALSE) + A_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + family = "binomial", zeroSum = FALSE + ) eA_LS <- extCostFunction(x, y, coef(A_LS), alpha, lambda, family = "binomial") - A_LS2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - useApprox = FALSE, family = "binomial", zeroSum = FALSE) + A_LS2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + useApprox = FALSE, family = "binomial", zeroSum = FALSE + ) eA_LS2 <- extCostFunction(x, y, coef(A_LS2), alpha, lambda, - family = "binomial") + family = "binomial" + ) eCompA <- extCostFunction(x, y, ref$test_logistic$A, alpha, lambda, - family = "binomial") + family = "binomial" + ) expect_equal(eA$cost / eA_LS$cost, 1.0, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(A)), as.numeric(coef(A_LS))), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) expect_equal(eA$cost / eA_LS2$cost, 1.0, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(A)), as.numeric(coef(A_LS2))), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) expect_equal(eA$cost / eCompA$cost, 1.0, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(A)), ref$test_logistic$A), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) ## logistic Regression zerosum - B <- zeroSum(x, y, alpha, lambda, algorithm = "CD", standardize = FALSE, - family = "binomial") + B <- zeroSum(x, y, alpha, lambda, + algorithm = "CD", standardize = FALSE, + family = "binomial" + ) eB <- extCostFunction(x, y, coef(B), alpha, lambda, family = "binomial") - B_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - family = "binomial") + B_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + family = "binomial" + ) eB_LS <- extCostFunction(x, y, coef(B_LS), alpha, lambda, - family = "binomial") + family = "binomial" + ) - B_LS2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - useApprox = FALSE, family = "binomial") + B_LS2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + useApprox = FALSE, family = "binomial" + ) eB_LS2 <- extCostFunction(x, y, coef(B_LS2), alpha, lambda, - family = "binomial") + family = "binomial" + ) expect_equal(eB$cost, eB_LS$cost, tolerance = 1e-5) expect_equal(eB$cost, eB_LS2$cost, tolerance = 1e-5) expect_equal(cor(as.numeric(coef(B)), as.numeric(coef(B_LS))), 1.0, - tolerance = 1e-4) + tolerance = 1e-4 + ) expect_equal(cor(as.numeric(coef(B_LS)), as.numeric(coef(B_LS2))), 1.0, - tolerance = 1e-6) + tolerance = 1e-4 + ) expect_equal(sum(coef(B)[-1]), 0.0, tolerance = 1e-12) expect_equal(sum(coef(B_LS)[-1]), 0.0, tolerance = 1e-12) expect_equal(sum(coef(B_LS2)[-1]), 0.0, tolerance = 1e-12) ## logistic Regression standardized - C <- zeroSum(x, y, alpha, lambda, algorithm = "CD", standardize = TRUE, - family = "binomial", zeroSum = FALSE) + C <- zeroSum(x, y, alpha, lambda, + algorithm = "CD", standardize = TRUE, + family = "binomial", zeroSum = FALSE + ) eC <- extCostFunction(x, y, coef(C), alpha, lambda, family = "binomial") - C_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - family = "binomial", zeroSum = FALSE) + C_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + family = "binomial", zeroSum = FALSE + ) eC_LS <- extCostFunction(x, y, coef(C_LS), alpha, lambda, family = "binomial") - C_LS2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - useApprox = FALSE, family = "binomial", zeroSum = FALSE) + C_LS2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + useApprox = FALSE, family = "binomial", zeroSum = FALSE + ) eC_LS2 <- extCostFunction(x, y, coef(C_LS2), alpha, lambda, - family = "binomial") + family = "binomial" + ) eCompC <- extCostFunction(x, y, ref$test_logistic$C, alpha, lambda, - family = "binomial") + family = "binomial" + ) ## glmnet packages gives here slightly wore results -> higher tolerance expect_equal(eC$cost, eCompC$cost, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(C)), ref$test_logistic$C), 1.0, - tolerance = 1e-4) + tolerance = 1e-4 + ) ## ls seems to perform a little bit better -> higher tolerance expect_equal(eC$cost, eC_LS$cost, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(C)), as.numeric(coef(C_LS))), 1.0, - tolerance = 1e-4) + tolerance = 1e-4 + ) expect_equal(eC$cost, eC_LS2$cost, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(C)), as.numeric(coef(C_LS2))), 1.0, - tolerance = 1e-4) + tolerance = 1e-4 + ) ## logistic Regression zerosum standardized - D <- zeroSum(x, y, alpha, lambda, algorithm = "CD", standardize = TRUE, - family = "binomial") + D <- zeroSum(x, y, alpha, lambda, + algorithm = "CD", standardize = TRUE, + family = "binomial" + ) eD <- extCostFunction(x, y, coef(D), alpha, lambda, family = "binomial") - D_LS <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - family = "binomial") + D_LS <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + family = "binomial" + ) eD_LS <- extCostFunction(x, y, coef(D_LS), alpha, lambda, - family = "binomial") + family = "binomial" + ) - D_LS2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - useApprox = FALSE, family = "binomial") + D_LS2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + useApprox = FALSE, family = "binomial" + ) eD_LS2 <- extCostFunction(x, y, coef(D_LS2), alpha, lambda, - family = "binomial") + family = "binomial" + ) expect_equal(eD$cost / eD_LS$cost, 1.0, tolerance = 1e-2) expect_equal(cor(as.numeric(coef(D)), as.numeric(coef(D_LS))), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) expect_equal(eD$cost / eD_LS2$cost, 1.0, tolerance = 1e-2) expect_equal(cor(as.numeric(coef(D)), as.numeric(coef(D_LS2))), 1.0, - tolerance = 1e-3) + tolerance = 1e-3 + ) expect_equal(sum(coef(D)[-1]), 0.0, tolerance = 1e-14) expect_equal(sum(coef(D_LS)[-1]), 0.0, tolerance = 1e-14) @@ -128,60 +170,94 @@ test_that("logistic regression equals glmnet", { ## fusion kernel test P <- ncol(x) fusion <- Matrix(0, nrow = P - 1, ncol = P, sparse = TRUE) - for (i in 1:(P - 1)) fusion[i, i] <- 1 + for (i in 1:(P - 1)) fusion[i, i] <- 1 for (i in 1:(P - 1)) fusion[i, (i + 1)] <- -1 gamma <- 0.03 lambda <- 0.001 set.seed(1) - FA1 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - gamma = gamma, fusion = fusion, family = "binomial", - zeroSum = FALSE) - eFA1 <- extCostFunction(x, y, coef(FA1), alpha, lambda, family = "binomial", - gamma = gamma, fusion = fusion) - - FA2 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - gamma = gamma, fusion = fusion, useApprox = FALSE, - family = "binomial", zeroSum = FALSE) - eFA2 <- extCostFunction(x, y, coef(FA2), alpha, lambda, family = "binomial", - gamma = gamma, fusion = fusion) - - FA3 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - gamma = gamma, fusion = fusion, family = "binomial") - eFA3 <- extCostFunction(x, y, coef(FA3), alpha, lambda, family = "binomial", - gamma = gamma, fusion = fusion) - - FA4 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = FALSE, - gamma = gamma, fusion = fusion, useApprox = FALSE, - family = "binomial") - eFA4 <- extCostFunction(x, y, coef(FA4), alpha, lambda, family = "binomial", - gamma = gamma, fusion = fusion) - - FA5 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, family = "binomial", - zeroSum = FALSE) - eFA5 <- extCostFunction(x, y, coef(FA5), alpha, lambda, family = "binomial", - gamma = gamma, fusion = fusion) - - FA6 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, useApprox = FALSE, - family = "binomial", zeroSum = FALSE) - eFA6 <- extCostFunction(x, y, coef(FA6), alpha, lambda, family = "binomial", - gamma = gamma, fusion = fusion) - - FA7 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, family = "binomial") - eFA7 <- extCostFunction(x, y, coef(FA7), alpha, lambda, family = "binomial", - gamma = gamma, fusion = fusion) - - FA8 <- zeroSum(x, y, alpha, lambda, algorithm = "LS", standardize = TRUE, - gamma = gamma, fusion = fusion, useApprox = FALSE, - family = "binomial") - eFA8 <- extCostFunction(x, y, coef(FA8), alpha, lambda, family = "binomial", - gamma = gamma, fusion = fusion) - - costs <- c(eFA1$cost, eFA2$cost, eFA3$cost, eFA4$cost, eFA5$cost, eFA6$cost, - eFA7$cost, eFA8$cost) + FA1 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + gamma = gamma, fusion = fusion, family = "binomial", + zeroSum = FALSE + ) + eFA1 <- extCostFunction(x, y, coef(FA1), alpha, lambda, + family = "binomial", + gamma = gamma, fusion = fusion + ) + + FA2 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + gamma = gamma, fusion = fusion, useApprox = FALSE, + family = "binomial", zeroSum = FALSE + ) + eFA2 <- extCostFunction(x, y, coef(FA2), alpha, lambda, + family = "binomial", + gamma = gamma, fusion = fusion + ) + + FA3 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + gamma = gamma, fusion = fusion, family = "binomial" + ) + eFA3 <- extCostFunction(x, y, coef(FA3), alpha, lambda, + family = "binomial", + gamma = gamma, fusion = fusion + ) + + FA4 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = FALSE, + gamma = gamma, fusion = fusion, useApprox = FALSE, + family = "binomial" + ) + eFA4 <- extCostFunction(x, y, coef(FA4), alpha, lambda, + family = "binomial", + gamma = gamma, fusion = fusion + ) + + FA5 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, family = "binomial", + zeroSum = FALSE + ) + eFA5 <- extCostFunction(x, y, coef(FA5), alpha, lambda, + family = "binomial", + gamma = gamma, fusion = fusion + ) + + FA6 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, useApprox = FALSE, + family = "binomial", zeroSum = FALSE + ) + eFA6 <- extCostFunction(x, y, coef(FA6), alpha, lambda, + family = "binomial", + gamma = gamma, fusion = fusion + ) + + FA7 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, family = "binomial" + ) + eFA7 <- extCostFunction(x, y, coef(FA7), alpha, lambda, + family = "binomial", + gamma = gamma, fusion = fusion + ) + + FA8 <- zeroSum(x, y, alpha, lambda, + algorithm = "LS", standardize = TRUE, + gamma = gamma, fusion = fusion, useApprox = FALSE, + family = "binomial" + ) + eFA8 <- extCostFunction(x, y, coef(FA8), alpha, lambda, + family = "binomial", + gamma = gamma, fusion = fusion + ) + + costs <- c( + eFA1$cost, eFA2$cost, eFA3$cost, eFA4$cost, eFA5$cost, eFA6$cost, + eFA7$cost, eFA8$cost + ) for (i in 1:length(costs)) expect_lte(costs[i], ref$test_logistic$fusionCosts[i] + 5e-2) @@ -189,6 +265,4 @@ test_that("logistic regression equals glmnet", { # calculate fusion terms and expect that most adjacent features are equal fused <- abs(as.numeric(fusion %*% cbind(coef(FA1), coef(FA2), coef(FA3), coef(FA4), coef(FA5), coef(FA6), coef(FA7), coef(FA8))[-1, ])) expect_gte(sum(fused < 1e-5) / length(fused), 0.3) - - }) diff --git a/zeroSum/tests/testthat/test_multinomialRegression.R b/zeroSum/tests/testthat/test_multinomialRegression.R index 21ddb0e..0919856 100644 --- a/zeroSum/tests/testthat/test_multinomialRegression.R +++ b/zeroSum/tests/testthat/test_multinomialRegression.R @@ -41,7 +41,7 @@ test_that("multinomial regression equals glmnet", { expect_equal(eA$cost, eCompA$cost, tolerance = 1e-3) expect_equal(cor(as.numeric(coef(A)[-1, ]), - as.numeric(ref$test_multi$A[-1, ])), 1.0, tolerance = 1e-2) + as.numeric(ref$test_multi$A[-1, ])), 1.0, tolerance = 0.05) # ## multinomial Regression zerosum @@ -65,9 +65,9 @@ test_that("multinomial regression equals glmnet", { expect_lte(eB_LS2$cost, 0.337) expect_equal(cor(as.numeric(coef(B)), as.numeric(coef(B_LS))), 0.99, - tolerance = 1e-2) + tolerance = 0.05) expect_equal(cor(as.numeric(coef(B_LS)), as.numeric(coef(B_LS2))), 1.0, - tolerance = 1e-2) + tolerance = 0.05) expect_equal(sum(coef(B)[-1, 1]), sum(coef(B)[-1, 2]), tolerance = 1e-12) expect_equal(sum(coef(B)[-1, 1]), sum(coef(B)[-1, 3]), tolerance = 1e-12) diff --git a/zeroSum/tests/testthat/test_normal_cd.R b/zeroSum/tests/testthat/test_normal_cd.R index 86a05eb..da304d2 100644 --- a/zeroSum/tests/testthat/test_normal_cd.R +++ b/zeroSum/tests/testthat/test_normal_cd.R @@ -1,45 +1,44 @@ - context("Testing normal coordinate descent move") +context("Testing normal coordinate descent move") - test_that("coordinate descent move seems to work", { +test_that("coordinate descent move seems to work", { + data <- regressionObject(log2(exampleData$x + 1), exampleData$y, "gaussian", + 0.578, 1, 1, NULL, NULL, NULL, NULL, NULL, NULL, FALSE, TRUE, FALSE, + 1, 0.1 + ) - data <- regressionObject(log2(exampleData$x + 1), exampleData$y, - NULL, 0.578, 1, standardize = FALSE, useZeroSum = FALSE, nFold = 0) + normalCD <- function(data, k) { + ak <- data$x[, k]^2 %*% data$w + data$lambda * (1 - data$alpha) * data$v[k] - normalCD <- function(data, k) - { - ak <- data$x[, k]^2 %*% data$w + data$lambda * (1 - data$alpha) * data$v[k] + bk <- sum(data$x[, k] * data$w * (data$y - rep(data$beta[1], nrow(data$y)) - + data$x[, -k] %*% data$beta[-c(1, k + 1)])) - bk <- sum(data$x[, k] * data$w * (data$y - rep(data$beta[1], nrow(data$y)) - - data$x[, -k] %*% data$beta[-c(1, k + 1)])) + betak <- 0 + bk1 <- bk + data$lambda * data$alpha * data$v[k] + bk2 <- bk - data$lambda * data$alpha * data$v[k] + if (bk1 < 0) { + betak <- bk1 / ak + } else if (bk2 > 0) { + betak <- bk2 / ak + } else { betak <- 0 - bk1 <- bk + data$lambda * data$alpha * data$v[k] - bk2 <- bk - data$lambda * data$alpha * data$v[k] - - if(bk1 < 0) - { - betak <- bk1 / ak - } else if(bk2 > 0) - { - betak <- bk2 / ak - } else - { - betak <- 0 - } - - return(betak) } - k <- 5 + return(betak) + } - R <- as.numeric(normalCD(data, k)) + k <- 5 - C <- .Call("checkMoves", data, as.integer(0), - as.integer(k-1), as.integer(0), - as.integer(0), as.integer(0), PACKAGE = "zeroSum") + R <- as.numeric(normalCD(data, k)) + C <- .Call("checkMoves", data, as.integer(0), + as.integer(k - 1), as.integer(0), + as.integer(0), as.integer(0), + PACKAGE = "zeroSum" + ) - expect_that(R, equals(C, tolerance = 1e-10)) - }) + + expect_that(R, equals(C, tolerance = 1e-10)) +}) diff --git a/zeroSum/tests/testthat/test_offset_cd.R b/zeroSum/tests/testthat/test_offset_cd.R index 2bef585..ba14db8 100644 --- a/zeroSum/tests/testthat/test_offset_cd.R +++ b/zeroSum/tests/testthat/test_offset_cd.R @@ -1,8 +1,11 @@ context("Testing intercept") test_that("intercept update seems to work", { - data <- regressionObject(log2(exampleData$x + 1), exampleData$y, NULL, - 0.578, 1, standardize = FALSE, useZeroSum = FALSE, nFold = 0) + data <- regressionObject( + log2(exampleData$x + 1), exampleData$y, "gaussian", + 0.578, 1, 1, NULL, NULL, NULL, NULL, NULL, NULL, FALSE, TRUE, FALSE, + 1, 0.1 + ) updateOffset <- function(data) { a <- (data$w %*% (data$y - data$x %*% data$beta[-1])) / sum(data$w) diff --git a/zeroSum/tests/testthat/test_rt_zs_cd.R b/zeroSum/tests/testthat/test_rt_zs_cd.R index bc23507..c555655 100644 --- a/zeroSum/tests/testthat/test_rt_zs_cd.R +++ b/zeroSum/tests/testthat/test_rt_zs_cd.R @@ -1,119 +1,121 @@ context("Testing rotated zerosum coordinate descent move") - test_that("rotated zerosum coordinate descent move seems to work", { - - data <- regressionObject(log2(exampleData$x + 1), exampleData$y, NULL, - 0.578, 1, standardize = FALSE, useZeroSum = FALSE, nFold = 0) - - zeroSumCdRT <- function(data, n, m, s, theta) - { - rtW <- (data$u[n] * cos(theta) - data$u[m] * sin(theta)) / data$u[s] - part1 <- data$x[, m] * sin(theta) - data$x[, n] * cos(theta) + data$x[, s] * rtW - - a <- as.numeric(part1^2 %*% data$w) + data$lambda * (1 - data$alpha) * - (data$v[n] * cos(theta)^2 + data$v[m] * sin(theta)^2 + data$v[s] * rtW^2) - - t1 <- data$beta[n + 1] - t2 <- data$beta[m + 1] - - part2 <- (data$cSum - data$u[-c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - - data$u[n] * t1 - data$u[m] * t2) / data$u[s] - part2 <- as.numeric(part2) - part3 <- data$y - data$beta[1] - data$x[, -c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - - data$x[, n] * t1 - data$x[, m] * t2 - data$x[, s] * part2 - - - b <- -sum(part1 * part3 * data$w) - as.numeric(data$lambda * (1 - data$alpha) * - (data$v[n] * t1 * cos(theta) - data$v[m] * t2 * sin(theta) + data$v[s] * part2 * - (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) / data$u[s])) - - c1 <- data$v[n] * cos(theta) - data$v[m] * sin(theta) + data$v[s] * (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) * data$v[s] / data$u[s] - c2 <- data$v[n] * cos(theta) - data$v[m] * sin(theta) + data$v[s] * ( data$u[n] * cos(theta) - data$u[m] * sin(theta)) * data$v[s] / data$u[s] - c3 <- data$v[n] * cos(theta) + data$v[m] * sin(theta) + data$v[s] * (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) * data$v[s] / data$u[s] - c4 <- data$v[n] * cos(theta) + data$v[m] * sin(theta) + data$v[s] * ( data$u[n] * cos(theta) - data$u[m] * sin(theta)) * data$v[s] / data$u[s] - c5 <- -data$v[n] * cos(theta) - data$v[m] * sin(theta) + data$v[s] * (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) * data$v[s] / data$u[s] - c6 <- -data$v[n] * cos(theta) - data$v[m] * sin(theta) + data$v[s] * ( data$u[n] * cos(theta) - data$u[m] * sin(theta)) * data$v[s] / data$u[s] - c7 <- -data$v[n] * cos(theta) + data$v[m] * sin(theta) + data$v[s] * (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) * data$v[s] / data$u[s] - c8 <- -data$v[n] * cos(theta) + data$v[m] * sin(theta) + data$v[s] * ( data$u[n] * cos(theta) - data$u[m] * sin(theta)) * data$v[s] / data$u[s] - - betan <- (b - data$lambda * data$alpha * c1) / a * cos(theta) + data$beta[n + 1] - betam <- (-(b - data$lambda * data$alpha * c1) / a) * sin(theta) + data$beta[m + 1] - betas <- (data$cSum - data$u[-c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] - - if(betan > 0 && betam > 0 && betas > 0) { - return(c(betan, betam, betas)) - } - betan <- (b - data$lambda * data$alpha * c2) / a * cos(theta) + data$beta[n + 1] - betam <- (-(b - data$lambda * data$alpha * c2) / a) * sin(theta) + data$beta[m + 1] - betas <- (data$cSum - data$u[-c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] - - if(betan > 0 && betam > 0 && betas < 0) { - return(c(betan, betam, betas)) - } - - betan <- (b - data$lambda * data$alpha * c3) / a * cos(theta) + data$beta[n + 1] - betam <- (-(b - data$lambda * data$alpha * c3) / a) * sin(theta) + data$beta[m + 1] - betas <- (data$cSum - data$u[-c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] - - if(betan > 0 && betam < 0 && betas > 0) { - return(c(betan, betam, betas)) - } - - betan <- (b - data$lambda * data$alpha * c4) / a * cos(theta) + data$beta[n + 1] - betam <- (-(b - data$lambda * data$alpha * c4) / a) * sin(theta) + data$beta[m + 1] - betas <- (data$cSum - data$u[-c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] - - if(betan > 0 && betam < 0 && betas < 0) { - return(c(betan, betam, betas)) - } - - betan <- (b - data$lambda * data$alpha * c5) / a * cos(theta) + data$beta[n + 1] - betam <- (-(b - data$lambda * data$alpha * c5) / a) * sin(theta) + data$beta[m + 1] - betas <- (data$cSum - data$u[-c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] - - if(betan < 0 && betam > 0 && betas > 0) { - return(c(betan, betam, betas)) - } - - betan <- (b - data$lambda * data$alpha * c6) / a * cos(theta) + data$beta[n + 1] - betam <- (-(b - data$lambda * data$alpha * c6) / a) * sin(theta) + data$beta[m + 1] - betas <- (data$cSum - data$u[-c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] - - if(betan < 0 && betam > 0 && betas < 0) { - return(c(betan, betam, betas)) - } - - betan <- (b - data$lambda * data$alpha * c7) / a * cos(theta) + data$beta[n + 1] - betam <- (-(b - data$lambda * data$alpha * c7) / a) * sin(theta) + data$beta[m + 1] - betas <- (data$cSum - data$u[-c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] - - if(betan < 0 && betam < 0 && betas > 0) { - return(c(betan, betam, betas)) - } - - betan <- (b - data$lambda * data$alpha * c8) / a * cos(theta) + data$beta[n + 1] - betam <- (-(b - data$lambda * data$alpha * c8) / a) * sin(theta) + data$beta[m + 1] - betas <- (data$cSum - data$u[-c(n, m,s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] - - if(betan < 0 && betam < 0 && betas < 0) { - return(c(betan, betam, betas)) - } - - return(c(NA, NA, NA)) +test_that("rotated zerosum coordinate descent move seems to work", { + data <- regressionObject( + log2(exampleData$x + 1), exampleData$y, "gaussian", + 0.578, 1, 1, NULL, NULL, NULL, NULL, NULL, NULL, FALSE, TRUE, FALSE, + 1, 0.1 + ) + + zeroSumCdRT <- function(data, n, m, s, theta) { + rtW <- (data$u[n] * cos(theta) - data$u[m] * sin(theta)) / data$u[s] + part1 <- data$x[, m] * sin(theta) - data$x[, n] * cos(theta) + data$x[, s] * rtW + + a <- as.numeric(part1^2 %*% data$w) + data$lambda * (1 - data$alpha) * + (data$v[n] * cos(theta)^2 + data$v[m] * sin(theta)^2 + data$v[s] * rtW^2) + + t1 <- data$beta[n + 1] + t2 <- data$beta[m + 1] + + part2 <- (data$cSum - data$u[-c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - + data$u[n] * t1 - data$u[m] * t2) / data$u[s] + part2 <- as.numeric(part2) + part3 <- data$y - data$beta[1] - data$x[, -c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - + data$x[, n] * t1 - data$x[, m] * t2 - data$x[, s] * part2 + + + b <- -sum(part1 * part3 * data$w) - as.numeric(data$lambda * (1 - data$alpha) * + (data$v[n] * t1 * cos(theta) - data$v[m] * t2 * sin(theta) + data$v[s] * part2 * + (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) / data$u[s])) + + c1 <- data$v[n] * cos(theta) - data$v[m] * sin(theta) + data$v[s] * (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) * data$v[s] / data$u[s] + c2 <- data$v[n] * cos(theta) - data$v[m] * sin(theta) + data$v[s] * (data$u[n] * cos(theta) - data$u[m] * sin(theta)) * data$v[s] / data$u[s] + c3 <- data$v[n] * cos(theta) + data$v[m] * sin(theta) + data$v[s] * (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) * data$v[s] / data$u[s] + c4 <- data$v[n] * cos(theta) + data$v[m] * sin(theta) + data$v[s] * (data$u[n] * cos(theta) - data$u[m] * sin(theta)) * data$v[s] / data$u[s] + c5 <- -data$v[n] * cos(theta) - data$v[m] * sin(theta) + data$v[s] * (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) * data$v[s] / data$u[s] + c6 <- -data$v[n] * cos(theta) - data$v[m] * sin(theta) + data$v[s] * (data$u[n] * cos(theta) - data$u[m] * sin(theta)) * data$v[s] / data$u[s] + c7 <- -data$v[n] * cos(theta) + data$v[m] * sin(theta) + data$v[s] * (-data$u[n] * cos(theta) + data$u[m] * sin(theta)) * data$v[s] / data$u[s] + c8 <- -data$v[n] * cos(theta) + data$v[m] * sin(theta) + data$v[s] * (data$u[n] * cos(theta) - data$u[m] * sin(theta)) * data$v[s] / data$u[s] + + betan <- (b - data$lambda * data$alpha * c1) / a * cos(theta) + data$beta[n + 1] + betam <- (-(b - data$lambda * data$alpha * c1) / a) * sin(theta) + data$beta[m + 1] + betas <- (data$cSum - data$u[-c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] + + if (betan > 0 && betam > 0 && betas > 0) { + return(c(betan, betam, betas)) } + betan <- (b - data$lambda * data$alpha * c2) / a * cos(theta) + data$beta[n + 1] + betam <- (-(b - data$lambda * data$alpha * c2) / a) * sin(theta) + data$beta[m + 1] + betas <- (data$cSum - data$u[-c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] - n <- 5 - m <- 7 - s <- 2 - theta <- 37.32 + if (betan > 0 && betam > 0 && betas < 0) { + return(c(betan, betam, betas)) + } + + betan <- (b - data$lambda * data$alpha * c3) / a * cos(theta) + data$beta[n + 1] + betam <- (-(b - data$lambda * data$alpha * c3) / a) * sin(theta) + data$beta[m + 1] + betas <- (data$cSum - data$u[-c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] + + if (betan > 0 && betam < 0 && betas > 0) { + return(c(betan, betam, betas)) + } + + betan <- (b - data$lambda * data$alpha * c4) / a * cos(theta) + data$beta[n + 1] + betam <- (-(b - data$lambda * data$alpha * c4) / a) * sin(theta) + data$beta[m + 1] + betas <- (data$cSum - data$u[-c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] + + if (betan > 0 && betam < 0 && betas < 0) { + return(c(betan, betam, betas)) + } + + betan <- (b - data$lambda * data$alpha * c5) / a * cos(theta) + data$beta[n + 1] + betam <- (-(b - data$lambda * data$alpha * c5) / a) * sin(theta) + data$beta[m + 1] + betas <- (data$cSum - data$u[-c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] + + if (betan < 0 && betam > 0 && betas > 0) { + return(c(betan, betam, betas)) + } + + betan <- (b - data$lambda * data$alpha * c6) / a * cos(theta) + data$beta[n + 1] + betam <- (-(b - data$lambda * data$alpha * c6) / a) * sin(theta) + data$beta[m + 1] + betas <- (data$cSum - data$u[-c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] + + if (betan < 0 && betam > 0 && betas < 0) { + return(c(betan, betam, betas)) + } + + betan <- (b - data$lambda * data$alpha * c7) / a * cos(theta) + data$beta[n + 1] + betam <- (-(b - data$lambda * data$alpha * c7) / a) * sin(theta) + data$beta[m + 1] + betas <- (data$cSum - data$u[-c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] + + if (betan < 0 && betam < 0 && betas > 0) { + return(c(betan, betam, betas)) + } + + betan <- (b - data$lambda * data$alpha * c8) / a * cos(theta) + data$beta[n + 1] + betam <- (-(b - data$lambda * data$alpha * c8) / a) * sin(theta) + data$beta[m + 1] + betas <- (data$cSum - data$u[-c(n, m, s)] %*% data$beta[-c(1, n + 1, m + 1, s + 1)] - data$u[n] * betan - data$u[m] * betam) / data$u[s] + + if (betan < 0 && betam < 0 && betas < 0) { + return(c(betan, betam, betas)) + } + + return(c(NA, NA, NA)) + } + + n <- 5 + m <- 7 + s <- 2 + theta <- 37.32 - R <- zeroSumCdRT(data, n,m, s,theta) + R <- zeroSumCdRT(data, n, m, s, theta) - R <- as.numeric(R) + R <- as.numeric(R) - C <- .Call("checkMoves", data, as.integer(3), as.integer(n-1), - as.integer(m-1), as.integer(s-1), as.integer(0), - PACKAGE = "zeroSum") + C <- .Call("checkMoves", data, as.integer(3), as.integer(n - 1), + as.integer(m - 1), as.integer(s - 1), as.integer(0), + PACKAGE = "zeroSum" + ) - expect_that(R, equals(C, tolerance = 1e-10)) - }) + expect_that(R, equals(C, tolerance = 1e-10)) +}) diff --git a/zeroSum/tests/testthat/test_weights.R b/zeroSum/tests/testthat/test_weights.R index 44f0e1e..84bdcc5 100644 --- a/zeroSum/tests/testthat/test_weights.R +++ b/zeroSum/tests/testthat/test_weights.R @@ -1,7 +1,6 @@ context("Testing regression weights") test_that("regression weights seem to be okay", { - set.seed(10) # an example data set is included in the package @@ -11,16 +10,34 @@ test_that("regression weights seem to be okay", { P <- ncol(x) N <- nrow(x) - alpha <- 1.0 - - w <- runif(N) + alpha <- 1.0 + + w <- c( + 0.507478203158826, 0.306768506066874, 0.426907666493207, 0.693102080840617, + 0.0851359688676894, 0.225436616456136, 0.274530522990972, 0.272305066231638, + 0.615829307818785, 0.429671525489539, 0.651655666995794, 0.567737752571702, + 0.113508982118219, 0.595925305271521, 0.358049975009635, 0.428809418343008, + 0.0519033221062273, 0.264177667442709, 0.398790730861947, 0.836134143406525, + 0.864721225807443, 0.615352416876704, 0.775109896436334, 0.355568691389635, + 0.405849972041324, 0.706646913895383, 0.838287665275857, 0.239589131204411, + 0.770771533250809, 0.355897744419053, 0.535597037756816 + ) u <- rep(1, P) v <- rep(1, P) v[1] <- 0 u[1] <- 0 - A <- zeroSum(x, y, alpha = alpha, weights = w, zeroSum.weights = u, - penalty.factor = v, threads = 4) + + fi <- c( + 3L, 6L, 7L, 2L, 1L, 2L, 4L, 7L, 8L, 7L, 5L, 1L, 4L, 9L, 1L, + 10L, 8L, 2L, 1L, 8L, 3L, 5L, 3L, 9L, 9L, 10L, 10L, 5L, 4L, 6L, + 6L + ) + + A <- zeroSum(x, y, + alpha = alpha, weights = w, zeroSum.weights = u, foldid = fi, + penalty.factor = v, threads = 4 + ) expect_equal(sum(coef(A)[-c(1, 2), ]), 0, tolerance = 1e-12) expect_equal(as.numeric(coef(A)[2, 1]), -2.297, tolerance = 1e-2) @@ -30,29 +47,34 @@ test_that("regression weights seem to be okay", { u <- rep(1, P) v <- rep(1, P) - v[1] <- 0 + v[1] <- 0 v[19] <- 0 - u[1] <- 0 + u[1] <- 0 u[19] <- 0 - fit1 <- zeroSum(x, y, alpha = alpha, lambda = lambda, family = "binomial", - penalty.factor = v, zeroSum.weights = u) - fit2 <- zeroSum(x, y, alpha = alpha, lambda = lambda, family = "binomial", - algorithm = "LS", penalty.factor = v, zeroSum.weights = u) + fit1 <- zeroSum(x, y, + alpha = alpha, lambda = lambda, family = "binomial", + penalty.factor = v, zeroSum.weights = u + ) + fit2 <- zeroSum(x, y, + alpha = alpha, lambda = lambda, family = "binomial", + algorithm = "LS", penalty.factor = v, zeroSum.weights = u + ) expect_equal(sum(coef(fit1)[-c(1, 2, 20), ]), 0, tolerance = 1e-12) expect_equal(sum(coef(fit2)[-c(1, 2, 20), ]), 0, tolerance = 1e-12) - e1 <- extCostFunction(x, y, as.numeric(coef(fit1)), alpha, lambda, - family = "binomial", penalty.factor = v) - e2 <- extCostFunction(x, y, as.numeric(coef(fit2)), alpha, lambda, - family = "binomial", penalty.factor = v) + e1 <- extCostFunction(x, y, as.numeric(coef(fit1)), alpha, lambda, + family = "binomial", penalty.factor = v + ) + e2 <- extCostFunction(x, y, as.numeric(coef(fit2)), alpha, lambda, + family = "binomial", penalty.factor = v + ) cbind(e1, e2) diff <- as.numeric(e1) - as.numeric(e2) expect_lte(diff[1], 1e-2) expect_lte(diff[3], 1e-2) expect_lte(diff[4], 1e-2) - }) diff --git a/zeroSum/tests/testthat/test_zs_cd.R b/zeroSum/tests/testthat/test_zs_cd.R index f38333f..a02e17a 100644 --- a/zeroSum/tests/testthat/test_zs_cd.R +++ b/zeroSum/tests/testthat/test_zs_cd.R @@ -1,75 +1,78 @@ - context("Testing zerosum coordinate descent move") +context("Testing zerosum coordinate descent move") - test_that("zerosum coordinate descent move seems to work", { +test_that("zerosum coordinate descent move seems to work", { + data <- regressionObject( + log2(exampleData$x + 1), exampleData$y, "gaussian", + 0.578, 1, 1, NULL, NULL, NULL, NULL, NULL, NULL, FALSE, TRUE, FALSE, + 1, 0.1 + ) - data <- regressionObject(log2(exampleData$x + 1), exampleData$y, NULL, - 0.578, 1, standardize = FALSE, useZeroSum = FALSE, nFold = 0) + zeroSumCd <- function(data, k, s) { + part1 <- data$x[, s] * data$u[k] / data$u[s] - data$x[, k] - zeroSumCd <- function(data, k, s) { - part1 <- data$x[, s] * data$u[k] / data$u[s] - data$x[, k] + ak <- as.numeric(part1^2 %*% data$w) + ak <- ak + data$lambda * (1 - data$alpha) * (data$v[k] + data$v[s] + * data$u[k]^2 / data$u[s]^2) - ak <- as.numeric(part1^2 %*% data$w) - ak <- ak + data$lambda * (1 - data$alpha) * (data$v[k] + data$v[s] - * data$u[k]^2 / data$u[s]^2) - - part2 <- data$y - data$beta[1] - data$x[, -c(k, s)] %*% - data$beta[-c(1, k + 1, s + 1)] - data$x[, s] / data$u[s] * - as.numeric(data$cSum - data$u[-c(s, k)] %*% + part2 <- data$y - data$beta[1] - data$x[, -c(k, s)] %*% + data$beta[-c(1, k + 1, s + 1)] - data$x[, s] / data$u[s] * + as.numeric(data$cSum - data$u[-c(s, k)] %*% data$beta[-c(1, s + 1, k + 1)]) - bk <- - sum(part1 * part2 * data$w) + data$lambda * - (1 - data$alpha) * data$v[s] * (data$u[k] / - (data$u[s]^2)) * (data$cSum - data$u[-c(s, k)] %*% - data$beta[-c(1, s + 1, k + 1)]) - - - bk1 <- (bk - data$lambda * data$alpha * (data$v[k] - data$v[s] * - data$u[k] / data$u[s])) / ak - bk2 <- (bk - data$lambda * data$alpha * (data$v[k] + data$v[s] * - data$u[k] / data$u[s])) / ak - bk3 <- (bk + data$lambda * data$alpha * (data$v[k] + data$v[s] * - data$u[k] / data$u[s])) / ak - bk4 <- (bk + data$lambda * data$alpha * (data$v[k] - data$v[s] * - data$u[k] / data$u[s])) / ak - - bs1 <- (data$cSum - data$u[-c(s, k)] %*% - data$beta[-c(1, s + 1, k + 1)] - data$u[k] * bk1) / data$u[s] - bs2 <- (data$cSum - data$u[-c(s, k)] %*% - data$beta[-c(1, s + 1, k + 1)] - data$u[k] * bk2) / data$u[s] - bs3 <- (data$cSum - data$u[-c(s, k)] %*% - data$beta[-c(1, s + 1, k + 1)] - data$u[k] * bk3) / data$u[s] - bs4 <- (data$cSum - data$u[-c(s, k)] %*% - data$beta[-c(1, s + 1, k + 1)] - data$u[k] * bk4) / data$u[s] - - betak <- NA - betas <- NA - if (bk1 > 0 && bs1 > 0) { - betak <- bk1 - betas <- bs1 - } else if (bk2 > 0 && bs2 < 0) { - betak <- bk2 - betas <- bs2 - } else if (bk3 < 0 && bs3 > 0) { - betak <- bk3 - betas <- bs3 - } else if (bk4 < 0 && bs4 < 0) { - betak <- bk4 - betas <- bs4 - } - - return(c(betak, betas)) + bk <- -sum(part1 * part2 * data$w) + data$lambda * + (1 - data$alpha) * data$v[s] * (data$u[k] / + (data$u[s]^2)) * (data$cSum - data$u[-c(s, k)] %*% + data$beta[-c(1, s + 1, k + 1)]) + + + bk1 <- (bk - data$lambda * data$alpha * (data$v[k] - data$v[s] * + data$u[k] / data$u[s])) / ak + bk2 <- (bk - data$lambda * data$alpha * (data$v[k] + data$v[s] * + data$u[k] / data$u[s])) / ak + bk3 <- (bk + data$lambda * data$alpha * (data$v[k] + data$v[s] * + data$u[k] / data$u[s])) / ak + bk4 <- (bk + data$lambda * data$alpha * (data$v[k] - data$v[s] * + data$u[k] / data$u[s])) / ak + + bs1 <- (data$cSum - data$u[-c(s, k)] %*% + data$beta[-c(1, s + 1, k + 1)] - data$u[k] * bk1) / data$u[s] + bs2 <- (data$cSum - data$u[-c(s, k)] %*% + data$beta[-c(1, s + 1, k + 1)] - data$u[k] * bk2) / data$u[s] + bs3 <- (data$cSum - data$u[-c(s, k)] %*% + data$beta[-c(1, s + 1, k + 1)] - data$u[k] * bk3) / data$u[s] + bs4 <- (data$cSum - data$u[-c(s, k)] %*% + data$beta[-c(1, s + 1, k + 1)] - data$u[k] * bk4) / data$u[s] + + betak <- NA + betas <- NA + if (bk1 > 0 && bs1 > 0) { + betak <- bk1 + betas <- bs1 + } else if (bk2 > 0 && bs2 < 0) { + betak <- bk2 + betas <- bs2 + } else if (bk3 < 0 && bs3 > 0) { + betak <- bk3 + betas <- bs3 + } else if (bk4 < 0 && bs4 < 0) { + betak <- bk4 + betas <- bs4 } - k <- 5 - s <- 2 + return(c(betak, betas)) + } + + k <- 5 + s <- 2 - R <- as.numeric(zeroSumCd(data, k, s)) - C <- .Call("checkMoves", data, as.integer(2), as.integer(k - 1), - as.integer(s - 1), as.integer(0), as.integer(0), - PACKAGE = "zeroSum") + R <- as.numeric(zeroSumCd(data, k, s)) + C <- .Call("checkMoves", data, as.integer(2), as.integer(k - 1), + as.integer(s - 1), as.integer(0), as.integer(0), + PACKAGE = "zeroSum" + ) - expect_that(R, equals(C, tolerance = 1e-10)) - }) + expect_that(R, equals(C, tolerance = 1e-10)) +}) diff --git a/zeroSum/vignettes/README.Rmd b/zeroSum/vignettes/README.Rmd index 0a22f5d..170ec1c 100644 --- a/zeroSum/vignettes/README.Rmd +++ b/zeroSum/vignettes/README.Rmd @@ -94,13 +94,13 @@ with or without the zero-sum constraint can be easily compared. A binary package (*.zip) is available, which can be installed in R using: - install.packages("https://github.com/rehbergT/zeroSum/raw/master/zeroSum_2.0.2.zip", repos = NULL) + install.packages("https://github.com/rehbergT/zeroSum/raw/master/zeroSum_2.0.3.zip", repos = NULL) ## Linux and macOS The source package (*.tar.gz) can be installed in R using: - install.packages("https://github.com/rehbergT/zeroSum/raw/master/zeroSum_2.0.2.tar.gz", repos = NULL) + install.packages("https://github.com/rehbergT/zeroSum/raw/master/zeroSum_2.0.3.tar.gz", repos = NULL) ## Devtools @@ -113,7 +113,7 @@ Git clone or download this repository and open a terminal within the zeroSum fol The source package can be build and installed with: R CMD build zeroSum - R CMD INSTALL zeroSum_2.0.2.tar.gz + R CMD INSTALL zeroSum_2.0.3.tar.gz # Quick start diff --git a/zeroSum_2.0.2.tar.gz b/zeroSum_2.0.2.tar.gz deleted file mode 100644 index 3fd775f..0000000 Binary files a/zeroSum_2.0.2.tar.gz and /dev/null differ diff --git a/zeroSum_2.0.2.zip b/zeroSum_2.0.2.zip deleted file mode 100644 index 4268dc0..0000000 Binary files a/zeroSum_2.0.2.zip and /dev/null differ diff --git a/zeroSum_2.0.3.tar.gz b/zeroSum_2.0.3.tar.gz new file mode 100644 index 0000000..84c933d Binary files /dev/null and b/zeroSum_2.0.3.tar.gz differ diff --git a/zeroSum_2.0.3.zip b/zeroSum_2.0.3.zip new file mode 100644 index 0000000..3dc373f Binary files /dev/null and b/zeroSum_2.0.3.zip differ