diff --git a/DESCRIPTION b/DESCRIPTION index 158f9a84..b6b32465 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: markovchain Type: Package Title: Easy Handling Discrete Time Markov Chains -Version: 0.10.0 +Version: 0.10.0.9100 Authors@R: c( person("Giorgio Alfredo", "Spedicato", role = c("aut", "cre"), email = "spedicato_giorgio@yahoo.it",comment = c(ORCID = "0000-0002-0315-8888")), @@ -14,22 +14,22 @@ Authors@R: c( person("Toni", "Giorgino", role="ctb", comment = c(ORCID = "0000-0001-6449-0596")), person("Richèl J.C.", "Bilderbeek", role = "ctb", comment = c(ORCID = "0000-0003-1107-7049")), person("Daniel", "Ebbert", email = "daniel@ebbert.nrw", role = "ctb", comment = c(ORCID = "0000-0003-3666-7205")), - person("Shreyash", "Maheshwari", email = "coolshreysh1000@gmail.com", role = "ctb") + person("Shreyash", "Maheshwari", email = "coolshreysh1000@gmail.com", role = "ctb"), + person("Reinhold", "Koch", role = "ctb") ) Maintainer: Giorgio Alfredo Spedicato Description: Functions and S4 methods to create and manage discrete time Markov chains more easily. In addition functions to perform statistical (fitting and drawing random variates) and probabilistic (analysis of their structural proprieties) analysis are provided. See Spedicato (2017) . - Some functions for continuous times Markov chains depends on the suggested ctmcd package, that, - as May 2023, can be retrieved from . + Some functions for continuous times Markov chains depend on the suggested ctmcd package. License: MIT + file LICENSE Depends: R (>= 4.2.0), + Matrix (>= 1.5-0), methods Imports: igraph, - Matrix (>= 1.5-0), expm, stats4, parallel, diff --git a/R/classesAndMethods.R b/R/classesAndMethods.R index 0a011b9e..3a569c75 100644 --- a/R/classesAndMethods.R +++ b/R/classesAndMethods.R @@ -21,7 +21,8 @@ #' coerce,markovchain,data.frame-method coerce,table,markovchain-method #' coerce,markovchain,igraph-method coerce,markovchain,matrix-method #' coerce,markovchain,sparseMatrix-method coerce,sparseMatrix,markovchain-method -#' coerce,matrix,markovchain-method coerce,msm,markovchain-method +#' coerce,matrix,markovchain-method coerce,Matrix,markovchain-method +#' coerce,msm,markovchain-method #' coerce,msm.est,markovchain-method coerce,etm,markovchain-method #' dim,markovchain-method initialize,markovchain-method #' names<-,markovchain-method plot,markovchain,missing-method @@ -93,7 +94,7 @@ #' \item \code{markovchain} object are backed by S4 Classes. #' \item Validation method is used to assess whether either columns or rows totals to one. #' Rounding is used up to \code{.Machine$double.eps * 100}. If state names are not properly -#' defined for a probability \code{matrix}, coercing to \code{markovhcain} object leads +#' defined for a probability \code{matrix}, coercing to \code{markovchain} object leads #' to overriding states name with artificial "s1", "s2", ... sequence. In addition, operator #' overloading has been applied for \eqn{+,*,^,==,!=} operators. #' } @@ -204,7 +205,7 @@ setMethod( .Object, states = states, byrow = byrow, - transitionMatrix = transitionMatrix, + transitionMatrix = as.matrix(transitionMatrix), name = name, ... ) @@ -687,7 +688,7 @@ setMethod("plot", signature(x = "markovchain", y = "missing"), # firstly, check size if (ncol(matr) != nrow(matr)) { - if(verbose) stop("Error! Not a rectangular matrix") + if(verbose) stop("Error! Not a quadratic matrix") return(FALSE) } @@ -707,7 +708,9 @@ setMethod("plot", signature(x = "markovchain", y = "missing"), # Internal function to return a markovchain object given a matrix .matrix2Mc <- function(from) { - + # enforce class matrix + from <- as.matrix(from) + # whether given matrix is a transition matrix or not # if it is then how probabilities are stored # row-wise or columnwise @@ -751,6 +754,7 @@ NULL # coerce matrix to markovchain object using internal method # example: as("some matrix", "markovchain") setAs(from = "matrix", to = "markovchain", def = .matrix2Mc) +setAs(from = "Matrix", to = "markovchain", def = .matrix2Mc) # Function to transform a markovchain into a data.frame # Args: diff --git a/R/sysdata.rda b/R/sysdata.rda index 6bc79936..a4265651 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/data-raw/db4Tests.R b/data-raw/db4Tests.R index 47b45dd9..a2201543 100644 --- a/data-raw/db4Tests.R +++ b/data-raw/db4Tests.R @@ -90,7 +90,13 @@ M[4,3] <- M[4,5] <- 1/2 M[5,5] <- 1 mcDrunkard <- new("markovchain", transitionMatrix = M) +# sparse matrix example based on Fisher's iris data +data(iris) +sparsematrix <- Matrix::sparseMatrix(i=rep(seq.int(nrow(iris)),5), + j=FNN::get.knn(iris[,-5], k=5)$nn.index, + x=.2) + #SAVING usethis::use_data(simpleMcCiaoFit, checksAlofiRawTransitions, checkmarkovchainFitList, knownSteadyStatesMCs, - mcHitting, mcDrunkard, internal = TRUE, overwrite = TRUE) \ No newline at end of file + mcHitting, mcDrunkard, sparsematrix, internal = TRUE, overwrite = TRUE) \ No newline at end of file diff --git a/man/markovchain-class.Rd b/man/markovchain-class.Rd index 57664e99..112edfa0 100644 --- a/man/markovchain-class.Rd +++ b/man/markovchain-class.Rd @@ -38,6 +38,7 @@ \alias{coerce,markovchain,sparseMatrix-method} \alias{coerce,sparseMatrix,markovchain-method} \alias{coerce,matrix,markovchain-method} +\alias{coerce,Matrix,markovchain-method} \alias{coerce,msm,markovchain-method} \alias{coerce,msm.est,markovchain-method} \alias{coerce,etm,markovchain-method} @@ -72,7 +73,7 @@ The S4 class that describes \code{markovchain} objects. \item \code{markovchain} object are backed by S4 Classes. \item Validation method is used to assess whether either columns or rows totals to one. Rounding is used up to \code{.Machine$double.eps * 100}. If state names are not properly -defined for a probability \code{matrix}, coercing to \code{markovhcain} object leads +defined for a probability \code{matrix}, coercing to \code{markovchain} object leads to overriding states name with artificial "s1", "s2", ... sequence. In addition, operator overloading has been applied for \eqn{+,*,^,==,!=} operators. } diff --git a/tests/testthat/test-sparse.R b/tests/testthat/test-sparse.R new file mode 100644 index 00000000..3dc36e41 --- /dev/null +++ b/tests/testthat/test-sparse.R @@ -0,0 +1,3 @@ +test_that("sparse transition matrix is accepted", { + expect_is(as(sparsematrix, "markovchain"), "markovchain") +}) diff --git a/vignettes/an_introduction_to_markovchain_package.Rmd b/vignettes/an_introduction_to_markovchain_package.Rmd index 8c14fbff..12441d51 100644 --- a/vignettes/an_introduction_to_markovchain_package.Rmd +++ b/vignettes/an_introduction_to_markovchain_package.Rmd @@ -325,7 +325,7 @@ defaultMc <- new("markovchain") The quicker way to create `markovchain` objects is made possible thanks to the implemented `initialize` S4 method that checks that: - * the `transitionMatrix` to be a transition matrix, i.e., all entries to be probabilities and either all rows or all columns to sum up to one. + * the `transitionMatrix`, either of class matrix or Matrix, to be a transition matrix, i.e., all entries to be probabilities and either all rows or all columns to sum up to one. * the columns and rows names of `transitionMatrix` to be defined and to coincide with `states` vector slot. The `markovchain` objects can be collected in a list within `markovchainList` S4 objects as following example shows.