Title: | Sparse Tables |
---|---|
Description: | Fast Multiplication and Marginalization of Sparse Tables. |
Authors: | Mads Lindskou [aut, cre] |
Maintainer: | Mads Lindskou <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.8.4 |
Built: | 2024-11-03 03:35:28 UTC |
Source: | https://github.com/mlindsk/sparta |
Fast Multiplication and Marginalization of Sparse Tables.
Maintainer: Mads Lindskou [email protected]
Useful links:
A non-argument function, that outputs the classes that can be converted to a sparta object
allowed_class_to_sparta()
allowed_class_to_sparta()
Turn a sparse table into an array
as_array(x) ## S3 method for class 'sparta' as_array(x) ## S3 method for class 'sparta_unity' as_array(x)
as_array(x) ## S3 method for class 'sparta' as_array(x) ## S3 method for class 'sparta_unity' as_array(x)
x |
sparta object |
An array
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) as_array(as_sparta(x))
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) as_array(as_sparta(x))
Turn a sparta into a conditional probability table
as_cpt(x, y) ## S3 method for class 'sparta' as_cpt(x, y)
as_cpt(x, y) ## S3 method for class 'sparta' as_cpt(x, y)
x |
sparta object |
y |
the conditioning variables |
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) # A joint probability table p(a, b, c) as_cpt(sx, character(0)) # the same as normalize normalize(sx) # A conditional probability table p(a, c | b) pacb <- as_cpt(sx, "b") # The probability distribution when b = b1 slice(pacb, c(b = "b1"))
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) # A joint probability table p(a, b, c) as_cpt(sx, character(0)) # the same as normalize normalize(sx) # A conditional probability table p(a, c | b) pacb <- as_cpt(sx, "b") # The probability distribution when b = b1 slice(pacb, c(b = "b1"))
Turn a sparse table into a data frame
as_df(x, dense = FALSE) ## S3 method for class 'sparta' as_df(x, dense = FALSE)
as_df(x, dense = FALSE) ## S3 method for class 'sparta' as_df(x, dense = FALSE)
x |
sparta object |
dense |
Logical indicating if zero cells should be present or not |
A data frame
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) as_df(as_sparta(x))
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) as_df(as_sparta(x))
Turn an array-like object or a data.frame into a sparse representation
as_sparta(x) ## S3 method for class 'array' as_sparta(x) ## S3 method for class 'matrix' as_sparta(x) ## S3 method for class 'table' as_sparta(x) ## S3 method for class 'sparta' as_sparta(x) ## S3 method for class 'data.frame' as_sparta(x)
as_sparta(x) ## S3 method for class 'array' as_sparta(x) ## S3 method for class 'matrix' as_sparta(x) ## S3 method for class 'table' as_sparta(x) ## S3 method for class 'sparta' as_sparta(x) ## S3 method for class 'data.frame' as_sparta(x)
x |
array-like object or a data.frame |
A sparta object
# ---------- # Example 1) # ---------- x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) as_sparta(x) # ---------- # Example 2) # ---------- y <- mtcars[, c("gear", "carb")] y[] <- lapply(y, as.character) as_sparta(y)
# ---------- # Example 1) # ---------- x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) as_sparta(x) # ---------- # Example 2) # ---------- y <- mtcars[, c("gear", "carb")] y[] <- lapply(y, as.character) as_sparta(y)
Determine if two sparta objects are equivalent
equiv(x, y) ## S3 method for class 'sparta' equiv(x, y)
equiv(x, y) ## S3 method for class 'sparta' equiv(x, y)
x |
sparta object |
y |
sparta object |
Logical. TRUE
if x
and y
are equivalent
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) y <- array( c(2,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) sy <- as_sparta(y) equiv(sx, sy) equiv(sx, sx)
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) y <- array( c(2,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) sy <- as_sparta(y) equiv(sx, sy) equiv(sx, sx)
Find the value or the name of a cell
get_val(x, y) ## S3 method for class 'sparta' get_val(x, y) get_cell_name(x, y) ## S3 method for class 'sparta' get_cell_name(x, y)
get_val(x, y) ## S3 method for class 'sparta' get_val(x, y) get_cell_name(x, y) ## S3 method for class 'sparta' get_cell_name(x, y)
x |
sparta |
y |
named character vector or vector of cell indices |
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) get_val(sx, c(a = "a2", b = "b1", c = "c2")) get_cell_name(sx, sx[, 4])
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) get_val(sx, c(a = "a2", b = "b1", c = "c2")) get_cell_name(sx, sx[, 4])
Marginalize a sparse table given a vector of variables to marginalize out
marg(x, y, flow = "sum") ## S3 method for class 'sparta' marg(x, y, flow = "sum") ## S3 method for class 'numeric' marg(x, y, flow = "sum")
marg(x, y, flow = "sum") ## S3 method for class 'sparta' marg(x, y, flow = "sum") ## S3 method for class 'numeric' marg(x, y, flow = "sum")
x |
sparta object or a numeric. If numeric, the value is just returned. |
y |
character vector of the variables to marginalize out |
flow |
either "sum" or "max" |
A sparta object (or scalar if all variables are summed out)
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) marg(sx, c("c")) su <- sparta_unity_struct(dim_names(sx), rank = 3.14) marg(su, c("a", "b"))
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) marg(sx, c("c")) su <- sparta_unity_struct(dim_names(sx), rank = 3.14) marg(su, c("a", "b"))
Multiplication and division of sparse tables
mult(x, y) ## S3 method for class 'sparta' mult(x, y) ## S3 method for class 'numeric' mult(x, y) div(x, y) ## S3 method for class 'sparta' div(x, y) ## S3 method for class 'numeric' div(x, y)
mult(x, y) ## S3 method for class 'sparta' mult(x, y) ## S3 method for class 'numeric' mult(x, y) div(x, y) ## S3 method for class 'sparta' div(x, y) ## S3 method for class 'numeric' div(x, y)
x |
sparta object or scalar |
y |
sparta object or scalar |
A sparta object or a scalar
# ---------- # Example 1) # ---------- x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) y <- array( c(1,3,0,2,4,2,7,0, 1,8,0,1,6,2,1,0, 1,5,0,3,2,9,1,0), dim = c(2,2,2, 3), dimnames = list( b = c("b1", "b2"), d = c("d1", "d2"), a = c("a1", "a2"), e = c("e1", "e2", "e3") ) ) sx <- as_sparta(x) sy <- as_sparta(y) sparsity(sx) table_size(sx) dim_names(sx) names(sx) mult(sx, sy) div(sy, sx) # ---------- # Example 2) # ---------- d1 <- mtcars[, c("cyl", "vs", "am")] d1[] <- lapply(d1, as.character) d2 <- mtcars[, c("am", "gear", "carb")] d2[] <- lapply(d2, as.character) ds1 <- as_sparta(d1) ds2 <- as_sparta(d2) mult(ds1, ds2) # ---------- # Example 3) # ---------- su <- sparta_unity_struct(dim_names(sy), rank = 3.1415) sparta_rank(su) sum(su) sun <- normalize(su) sun sum(sun) mult(sx, sun) # ---------- # Example 4) # ---------- so <- sparta_ones(dim_names(sx)) mult(so, 2)
# ---------- # Example 1) # ---------- x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) y <- array( c(1,3,0,2,4,2,7,0, 1,8,0,1,6,2,1,0, 1,5,0,3,2,9,1,0), dim = c(2,2,2, 3), dimnames = list( b = c("b1", "b2"), d = c("d1", "d2"), a = c("a1", "a2"), e = c("e1", "e2", "e3") ) ) sx <- as_sparta(x) sy <- as_sparta(y) sparsity(sx) table_size(sx) dim_names(sx) names(sx) mult(sx, sy) div(sy, sx) # ---------- # Example 2) # ---------- d1 <- mtcars[, c("cyl", "vs", "am")] d1[] <- lapply(d1, as.character) d2 <- mtcars[, c("am", "gear", "carb")] d2[] <- lapply(d2, as.character) ds1 <- as_sparta(d1) ds2 <- as_sparta(d2) mult(ds1, ds2) # ---------- # Example 3) # ---------- su <- sparta_unity_struct(dim_names(sy), rank = 3.1415) sparta_rank(su) sum(su) sun <- normalize(su) sun sum(sun) mult(sx, sun) # ---------- # Example 4) # ---------- so <- sparta_ones(dim_names(sx)) mult(so, 2)
Normalize
normalize(x) ## S3 method for class 'sparta' normalize(x) ## S3 method for class 'numeric' normalize(x)
normalize(x) ## S3 method for class 'sparta' normalize(x) ## S3 method for class 'numeric' normalize(x)
x |
sparta |
A sparta object
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) normalize(sx)
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) normalize(sx)
Print method for sparta objects
## S3 method for class 'sparta' print(x, ...)
## S3 method for class 'sparta' print(x, ...)
x |
sparta object |
... |
For S3 compatability. Not used. |
Find the slice of a sparse table
slice(x, s, drop = FALSE) ## S3 method for class 'sparta' slice(x, s, drop = FALSE)
slice(x, s, drop = FALSE) ## S3 method for class 'sparta' slice(x, s, drop = FALSE)
x |
sparta object |
s |
a slice in form of a named character vector |
drop |
Logical. If |
A sparta object
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) # conditional probability table p(b,c|a) sx <- as_cpt(sx, "a") # the probability distriubtion when 'a' is 'a2' sxa2 <- slice(sx, c(a = "a2")) get_val(sxa2, c(a = "a2", b = "b1", c = "c2")) sxa2_drop <- slice(sx, c(a = "a2"), drop = TRUE) get_val(sxa2_drop, c(b = "b1", c = "c2")) u <- sparta_unity_struct(dim_names(sx)) slice(u, c(a = "a1"), drop = TRUE)
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) # conditional probability table p(b,c|a) sx <- as_cpt(sx, "a") # the probability distriubtion when 'a' is 'a2' sxa2 <- slice(sx, c(a = "a2")) get_val(sxa2, c(a = "a2", b = "b1", c = "c2")) sxa2_drop <- slice(sx, c(a = "a2"), drop = TRUE) get_val(sxa2_drop, c(b = "b1", c = "c2")) u <- sparta_unity_struct(dim_names(sx)) slice(u, c(a = "a1"), drop = TRUE)
Sparsity
sparsity(x) ## S3 method for class 'sparta' sparsity(x) ## S3 method for class 'sparta_unity' sparsity(x)
sparsity(x) ## S3 method for class 'sparta' sparsity(x) ## S3 method for class 'sparta_unity' sparsity(x)
x |
sparta |
The ratio of ncol(x)
and the total statespace of x
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) sparsity(sx)
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) sparsity(sx)
Construct a sparta object filled with ones
sparta_ones(dim_names)
sparta_ones(dim_names)
dim_names |
A named list of discrete levels |
A sparta object
sparta_ones(list(a = c("a1", "a2"), b = c("b1", "b2")))
sparta_ones(list(a = c("a1", "a2"), b = c("b1", "b2")))
Helper function to construct a sparta object with given values and dim names
sparta_struct(x, vals, dim_names)
sparta_struct(x, vals, dim_names)
x |
matrix where columns represents cells in an array-like object |
vals |
vector of values corresponding to x |
dim_names |
a named list |
A sparta object
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) sparta_struct(unclass(sx), vals(sx), dim_names(sx))
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) sparta_struct(unclass(sx), vals(sx), dim_names(sx))
Construct a sparse table of ones
sparta_unity_struct(dim_names, rank = 1L)
sparta_unity_struct(dim_names, rank = 1L)
dim_names |
A named list of discrete levels |
rank |
The value of each element. Default is |
A sparta object
s <- sparta_unity_struct(list(a = c("a1", "a2"), b = c("b1", "b2")), rank = 1) mult(s, 2)
s <- sparta_unity_struct(list(a = c("a1", "a2"), b = c("b1", "b2")), rank = 1) mult(s, 2)
Vector-like operations on sparta objects
## S3 method for class 'sparta' sum(x, ...) ## S3 method for class 'sparta' max(x, ...) ## S3 method for class 'sparta' min(x, ...) which_min_cell(x) ## S3 method for class 'sparta' which_min_cell(x) which_min_idx(x) ## S3 method for class 'sparta' which_min_idx(x) which_max_cell(x) ## S3 method for class 'sparta' which_max_cell(x) which_max_idx(x) ## S3 method for class 'sparta' which_max_idx(x)
## S3 method for class 'sparta' sum(x, ...) ## S3 method for class 'sparta' max(x, ...) ## S3 method for class 'sparta' min(x, ...) which_min_cell(x) ## S3 method for class 'sparta' which_min_cell(x) which_min_idx(x) ## S3 method for class 'sparta' which_min_idx(x) which_max_cell(x) ## S3 method for class 'sparta' which_max_cell(x) which_max_idx(x) ## S3 method for class 'sparta' which_max_idx(x)
x |
sparta |
... |
For S3 compatability. |
Number of elements in a table
table_size(x) ## S3 method for class 'sparta' table_size(x)
table_size(x) ## S3 method for class 'sparta' table_size(x)
x |
sparta |
The size of the sparta table x
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) table_size(sx)
x <- array( c(1,0,0,2,3,4,0,0), dim = c(2,2,2), dimnames = list( a = c("a1", "a2"), b = c("b1", "b2"), c = c("c1", "c2") ) ) sx <- as_sparta(x) table_size(sx)
Getter methods for sparta objects
vals(x) ## S3 method for class 'sparta' vals(x) get_values(x) ## S3 method for class 'sparta' get_values(x) dim_names(x) ## S3 method for class 'sparta' dim_names(x) ## S3 method for class 'sparta' names(x) sparta_rank(x) ## S3 method for class 'sparta_unity' sparta_rank(x)
vals(x) ## S3 method for class 'sparta' vals(x) get_values(x) ## S3 method for class 'sparta' get_values(x) dim_names(x) ## S3 method for class 'sparta' dim_names(x) ## S3 method for class 'sparta' names(x) sparta_rank(x) ## S3 method for class 'sparta_unity' sparta_rank(x)
x |
sparta object |