pr provides a set of probability classes built on vctrs and its rcrds class. The primary offerings include:
-
prob: A probability vector class -
prob_mat: Arcrdwhich describes a matrix of probabilities -
cnt_prob_mat: Arcrdwhich describes a matrix of probabilities with counts
It is designed to be lightweight so that it can be included as a dependency for other packages. Its only direct dependency is vctrs.
Installation
You can install the development version of pr from GitHub with:
# install.packages("pak")
pak::pak("christopherkenny/pr")Example
The primary component of the package is the prob class. This represents a vector of probabilities.
It supports creating record classes for sets of probabilities which sum to 1 row-wise. This is implemented via the vctrs rcrd type, which is a collection of same-length vectors.
A corresponding probability matrix can be created as follows:
p1 <- prob(c(0.4, 2 / 3, NA_real_, 1L))
p2 <- 1 - p1
prob_mat(pr_one = p1, pr_two = p2)
#> <probability matrix[4]>
#> pr_one pr_two
#> [1,] 0.400 0.600
#> [2,] 0.667 0.333
#> [3,] <NA> <NA>
#> [4,] 1.000 0.000Finally, a count probability matrix is provided which adds a count field to the probability matrix.
p1 <- prob(c(0.4, 2 / 3, NA_real_, 1L))
p2 <- 1 - p1
cnt_prob_mat(pr_one = p1, pr_two = p2, count = c(10, 20, 5, 15))
#> <count probability matrix[4]>
#> count pr_one pr_two
#> [1,] 10 0.400 0.600
#> [2,] 20 0.667 0.333
#> [3,] 5 <NA> <NA>
#> [4,] 15 1.000 0.000These can also be created without specifying the count, in which case it defaults to 1L.
p1 <- prob(c(0.4, 2 / 3, NA_real_, 1L))
p2 <- 1 - p1
cnt_prob_mat(pr_one = p1, pr_two = p2)
#> <count probability matrix[4]>
#> count pr_one pr_two
#> [1,] 1 0.400 0.600
#> [2,] 1 0.667 0.333
#> [3,] 1 <NA> <NA>
#> [4,] 1 1.000 0.000For both matrix types, these are represented as rcrd classes which require the columns to be named and each column to be a prob vector (or coercible to one).
