Skip to content

Commit

Permalink
Adds check for method paramter in markovchainFit. Addresses #166, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
ncordon committed Feb 3, 2019
1 parent ab9c905 commit c4d6a3a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
12 changes: 6 additions & 6 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions man/markovchainFit.Rd

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

47 changes: 23 additions & 24 deletions src/1_functions4Fitting.cpp → src/fittingFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -1607,55 +1611,50 @@ 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;
} else {
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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/testBasic1.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit c4d6a3a

Please sign in to comment.