From c4d6a3a97940a8bd3f9fc1b333e4f8c0d83ab156 Mon Sep 17 00:00:00 2001 From: ncordon Date: Sun, 3 Feb 2019 19:13:21 +0100 Subject: [PATCH] Adds check for method paramter in markovchainFit. Addresses #166, closes #165. --- R/RcppExports.R | 12 ++--- man/markovchainFit.Rd | 6 +-- ...tions4Fitting.cpp => fittingFunctions.cpp} | 47 +++++++++---------- tests/testthat/testBasic1.R | 2 +- 4 files changed, 33 insertions(+), 34 deletions(-) rename src/{1_functions4Fitting.cpp => fittingFunctions.cpp} (98%) diff --git a/R/RcppExports.R b/R/RcppExports.R index 2bfca2de..8b27584c 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -126,7 +126,7 @@ inferHyperparam <- function(transMatr = matrix(), scale = numeric(), data = char #' it fits the underlying Markov chain distribution using either MLE (also using a #' Laplacian smoother), bootstrap or by MAP (Bayesian) inference. #' -#' @param data It can be a character vector or a nx2 matrix or a nx2 data frame or a list +#' @param data It can be a character vector or a {n x n} matrix or a {n x n} data frame or a list #' @param method Method used to estimate the Markov chain. Either "mle", "map", "bootstrap" or "laplace" #' @param byrow it tells whether the output Markov chain should show the transition probabilities by row. #' @param nboot Number of bootstrap replicates in case "bootstrap" is used. @@ -138,10 +138,10 @@ inferHyperparam <- function(transMatr = matrix(), scale = numeric(), data = char #' Used only when \code{method} equal to "mle". #' @param confint a boolean to decide whether to compute Confidence Interval or not. #' @param hyperparam Hyperparameter matrix for the a priori distribution. If none is provided, -#' default value of 1 is assigned to each parameter. This must be of size kxk -#' where k is the number of states in the chain and the values should typically -#' be non-negative integers. -#' @param stringchar It can be a nx2 matrix or a character vector or a list +#' default value of 1 is assigned to each parameter. This must be of size +#' {k x k} where k is the number of states in the chain and the values +#' should typically be non-negative integers. +#' @param stringchar It can be a {n x n} matrix or a character vector or a list #' @param toRowProbs converts a sequence matrix into a probability matrix #' @param sanitize put 1 in all rows having rowSum equal to zero #' @param possibleStates Possible states which are not present in the given sequence @@ -164,7 +164,7 @@ inferHyperparam <- function(transMatr = matrix(), scale = numeric(), data = char #' package version 0.2.5 #' #' @author Giorgio Spedicato, Tae Seung Kang, Sai Bhargav Yalamanchi -#' @note This function has been rewritten in Rcpp. Bootstrap algorithm has been defined "euristically". +#' @note This function has been rewritten in Rcpp. Bootstrap algorithm has been defined "heuristically". #' In addition, parallel facility is not complete, involving only a part of the bootstrap process. #' When \code{data} is either a \code{data.frame} or a \code{matrix} object, only MLE fit is #' currently available. diff --git a/man/markovchainFit.Rd b/man/markovchainFit.Rd index 8b3de60a..d80ba987 100644 --- a/man/markovchainFit.Rd +++ b/man/markovchainFit.Rd @@ -14,7 +14,7 @@ markovchainFit(data, method = "mle", byrow = TRUE, nboot = 10L, sanitize = FALSE, possibleStates = character()) } \arguments{ -\item{stringchar}{It can be a nx2 matrix or a character vector or a list} +\item{stringchar}{It can be a {n x n} matrix or a character vector or a list} \item{toRowProbs}{converts a sequence matrix into a probability matrix} @@ -22,7 +22,7 @@ markovchainFit(data, method = "mle", byrow = TRUE, nboot = 10L, \item{possibleStates}{Possible states which are not present in the given sequence} -\item{data}{It can be a character vector or a nx2 matrix or a nx2 data frame or a list} +\item{data}{It can be a character vector or a {n x n} matrix or a {n x n} data frame or a list} \item{method}{Method used to estimate the Markov chain. Either "mle", "map", "bootstrap" or "laplace"} @@ -64,7 +64,7 @@ Disabling confint would lower the computation time on large datasets. If \code{d contain \code{NAs}, the related \code{NA} containing transitions will be ignored. } \note{ -This function has been rewritten in Rcpp. Bootstrap algorithm has been defined "euristically". +This function has been rewritten in Rcpp. Bootstrap algorithm has been defined "heuristically". In addition, parallel facility is not complete, involving only a part of the bootstrap process. When \code{data} is either a \code{data.frame} or a \code{matrix} object, only MLE fit is currently available. diff --git a/src/1_functions4Fitting.cpp b/src/fittingFunctions.cpp similarity index 98% rename from src/1_functions4Fitting.cpp rename to src/fittingFunctions.cpp index 357a8879..e0821d7e 100644 --- a/src/1_functions4Fitting.cpp +++ b/src/fittingFunctions.cpp @@ -1523,10 +1523,10 @@ List inferHyperparam(NumericMatrix transMatr = NumericMatrix(), NumericVector sc //' Used only when \code{method} equal to "mle". //' @param confint a boolean to decide whether to compute Confidence Interval or not. //' @param hyperparam Hyperparameter matrix for the a priori distribution. If none is provided, -//' default value of 1 is assigned to each parameter. This must be of size kxk -//' where k is the number of states in the chain and the values should typically -//' be non-negative integers. -//' @param stringchar It can be a nx2 matrix or a character vector or a list +//' default value of 1 is assigned to each parameter. This must be of size +//' {k x k} where k is the number of states in the chain and the values +//' should typically be non-negative integers. +//' @param stringchar It can be a {n x n} matrix or a character vector or a list //' @param toRowProbs converts a sequence matrix into a probability matrix //' @param sanitize put 1 in all rows having rowSum equal to zero //' @param possibleStates Possible states which are not present in the given sequence @@ -1549,7 +1549,7 @@ List inferHyperparam(NumericMatrix transMatr = NumericMatrix(), NumericVector sc //' package version 0.2.5 //' //' @author Giorgio Spedicato, Tae Seung Kang, Sai Bhargav Yalamanchi -//' @note This function has been rewritten in Rcpp. Bootstrap algorithm has been defined "euristically". +//' @note This function has been rewritten in Rcpp. Bootstrap algorithm has been defined "heuristically". //' In addition, parallel facility is not complete, involving only a part of the bootstrap process. //' When \code{data} is either a \code{data.frame} or a \code{matrix} object, only MLE fit is //' currently available. @@ -1581,6 +1581,10 @@ List markovchainFit(SEXP data, String method = "mle", bool byrow = true, int nbo double confidencelevel = 0.95, bool confint = true, NumericMatrix hyperparam = NumericMatrix(), bool sanitize = false, CharacterVector possibleStates = CharacterVector()) { + + if (method != "mle" && method != "bootstrap" && method != "map" && method != "laplace") { + stop ("method should be one of \"mle\", \"bootsrap\", \"map\" or \"laplace\""); + } // list to store the output List out; @@ -1607,20 +1611,19 @@ List markovchainFit(SEXP data, String method = "mle", bool byrow = true, int nbo // byrow assumes distinct observations (trajectiories) are per row // otherwise transpose - if (!byrow) { - mat = _transpose(mat); - } + if (!byrow) + mat = _transpose(mat); - S4 outMc =_matr2Mc(mat, laplacian, sanitize, possibleStates); + S4 outMc = _matr2Mc(mat, laplacian, sanitize, possibleStates); // whether to compute confidence interval or not if (confint) { // convert matrix to list int nrows = mat.nrow(); List manyseq(nrows); - for (int i = 0;i < nrows;i++) { - manyseq[i] = mat(i,_); - } + + for (int i = 0; i < nrows; i++) + manyseq[i] = mat(i, _); out = _mcFitMle(manyseq, byrow, confidencelevel, sanitize, possibleStates); out[0] = outMc; @@ -1628,34 +1631,30 @@ List markovchainFit(SEXP data, String method = "mle", bool byrow = true, int nbo out = List::create(_["estimate"] = outMc); } } - else if (TYPEOF(data) == VECSXP) { + else if (TYPEOF(data) == VECSXP) { if (method == "mle") { - out = _mcFitMle(data, byrow, confidencelevel, sanitize, possibleStates); + out = _mcFitMle(data, byrow, confidencelevel, sanitize, possibleStates); } else if (method == "map") { out = _mcFitMap(data, byrow, confidencelevel, hyperparam, sanitize, possibleStates); - } + } else + stop("method not available for a list"); } else { if (method == "mle") { out = _mcFitMle(data, byrow, confidencelevel, sanitize, possibleStates); - } - - if (method == "bootstrap") { + } else if (method == "bootstrap") { out = _mcFitBootStrap(data, nboot, byrow, parallel, confidencelevel, sanitize, possibleStates); - } - - if (method == "laplace") { + } else if (method == "laplace") { out = _mcFitLaplacianSmooth(data, byrow, laplacian, sanitize, possibleStates); - } - - if (method == "map") { + } else if (method == "map") { out = _mcFitMap(data, byrow, confidencelevel, hyperparam, sanitize, possibleStates); } } // markovchain object S4 estimate = out["estimate"]; + if (name != "") { estimate.slot("name") = name; } diff --git a/tests/testthat/testBasic1.R b/tests/testthat/testBasic1.R index 666a92df..8bed087e 100644 --- a/tests/testthat/testBasic1.R +++ b/tests/testthat/testBasic1.R @@ -201,7 +201,7 @@ test_that("createSequenceMatrix : Permutation of parameters",{ byrow = TRUE, dimnames = list(c("a", "b", "c"), c("a", "b", "c")))) }) -### Test for createSequenceMatrix : input nx2 matrix +### Test for createSequenceMatrix : input {n x n} matrix data <- matrix(c("a", "a", "b", "a", "b", "a", "b", "a", NA, "a", "a", "a", "a", "b", NA, "b"), ncol = 2, byrow = TRUE)