Skip to content

Commit 6e69e0c

Browse files
committed
Adds check for method paramter in markovchainFit. Addresses #166, closes #165.
1 parent ab9c905 commit 6e69e0c

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/1_functions4Fitting.cpp renamed to src/fittingFunctions.cpp

+23-24
Original file line numberDiff line numberDiff line change
@@ -1523,10 +1523,10 @@ List inferHyperparam(NumericMatrix transMatr = NumericMatrix(), NumericVector sc
15231523
//' Used only when \code{method} equal to "mle".
15241524
//' @param confint a boolean to decide whether to compute Confidence Interval or not.
15251525
//' @param hyperparam Hyperparameter matrix for the a priori distribution. If none is provided,
1526-
//' default value of 1 is assigned to each parameter. This must be of size kxk
1527-
//' where k is the number of states in the chain and the values should typically
1528-
//' be non-negative integers.
1529-
//' @param stringchar It can be a nx2 matrix or a character vector or a list
1526+
//' default value of 1 is assigned to each parameter. This must be of size
1527+
//' {k x k} where k is the number of states in the chain and the values
1528+
//' should typically be non-negative integers.
1529+
//' @param stringchar It can be a {n x n} matrix or a character vector or a list
15301530
//' @param toRowProbs converts a sequence matrix into a probability matrix
15311531
//' @param sanitize put 1 in all rows having rowSum equal to zero
15321532
//' @param possibleStates Possible states which are not present in the given sequence
@@ -1549,7 +1549,7 @@ List inferHyperparam(NumericMatrix transMatr = NumericMatrix(), NumericVector sc
15491549
//' package version 0.2.5
15501550
//'
15511551
//' @author Giorgio Spedicato, Tae Seung Kang, Sai Bhargav Yalamanchi
1552-
//' @note This function has been rewritten in Rcpp. Bootstrap algorithm has been defined "euristically".
1552+
//' @note This function has been rewritten in Rcpp. Bootstrap algorithm has been defined "heuristically".
15531553
//' In addition, parallel facility is not complete, involving only a part of the bootstrap process.
15541554
//' When \code{data} is either a \code{data.frame} or a \code{matrix} object, only MLE fit is
15551555
//' currently available.
@@ -1581,6 +1581,10 @@ List markovchainFit(SEXP data, String method = "mle", bool byrow = true, int nbo
15811581
double confidencelevel = 0.95, bool confint = true,
15821582
NumericMatrix hyperparam = NumericMatrix(), bool sanitize = false,
15831583
CharacterVector possibleStates = CharacterVector()) {
1584+
1585+
if (method != "mle" && method != "bootstrap" && method != "map" && method != "laplace") {
1586+
stop ("method should be one of \"mle\", \"bootsrap\", \"map\" or \"laplace\"");
1587+
}
15841588

15851589
// list to store the output
15861590
List out;
@@ -1607,55 +1611,50 @@ List markovchainFit(SEXP data, String method = "mle", bool byrow = true, int nbo
16071611

16081612
// byrow assumes distinct observations (trajectiories) are per row
16091613
// otherwise transpose
1610-
if (!byrow) {
1611-
mat = _transpose(mat);
1612-
}
1614+
if (!byrow)
1615+
mat = _transpose(mat);
16131616

1614-
S4 outMc =_matr2Mc(mat, laplacian, sanitize, possibleStates);
1617+
S4 outMc = _matr2Mc(mat, laplacian, sanitize, possibleStates);
16151618

16161619
// whether to compute confidence interval or not
16171620
if (confint) {
16181621
// convert matrix to list
16191622
int nrows = mat.nrow();
16201623
List manyseq(nrows);
1621-
for (int i = 0;i < nrows;i++) {
1622-
manyseq[i] = mat(i,_);
1623-
}
1624+
1625+
for (int i = 0; i < nrows; i++)
1626+
manyseq[i] = mat(i, _);
16241627

16251628
out = _mcFitMle(manyseq, byrow, confidencelevel, sanitize, possibleStates);
16261629
out[0] = outMc;
16271630
} else {
16281631
out = List::create(_["estimate"] = outMc);
16291632
}
16301633
}
1631-
else if (TYPEOF(data) == VECSXP) {
1634+
else if (TYPEOF(data) == VECSXP) {
16321635
if (method == "mle") {
1633-
out = _mcFitMle(data, byrow, confidencelevel, sanitize, possibleStates);
1636+
out = _mcFitMle(data, byrow, confidencelevel, sanitize, possibleStates);
16341637
} else if (method == "map") {
16351638
out = _mcFitMap(data, byrow, confidencelevel, hyperparam, sanitize, possibleStates);
1636-
}
1639+
} else
1640+
stop("method not available for a list");
16371641
}
16381642
else {
16391643
if (method == "mle") {
16401644
out = _mcFitMle(data, byrow, confidencelevel, sanitize, possibleStates);
1641-
}
1642-
1643-
if (method == "bootstrap") {
1645+
} else if (method == "bootstrap") {
16441646
out = _mcFitBootStrap(data, nboot, byrow, parallel,
16451647
confidencelevel, sanitize, possibleStates);
1646-
}
1647-
1648-
if (method == "laplace") {
1648+
} else if (method == "laplace") {
16491649
out = _mcFitLaplacianSmooth(data, byrow, laplacian, sanitize, possibleStates);
1650-
}
1651-
1652-
if (method == "map") {
1650+
} else if (method == "map") {
16531651
out = _mcFitMap(data, byrow, confidencelevel, hyperparam, sanitize, possibleStates);
16541652
}
16551653
}
16561654

16571655
// markovchain object
16581656
S4 estimate = out["estimate"];
1657+
16591658
if (name != "") {
16601659
estimate.slot("name") = name;
16611660
}

0 commit comments

Comments
 (0)