Learn R Programming

gRbase (version 1.8-5.1)

array-distribution: Marginalize and condition in multidimensional array.

Description

Marginalize and condition in a multidimensional array which is assumed to represent a discrete multivariate distribution.

Usage

ar_dist(tab, marg = NULL, cond = NULL, normalize = TRUE)

Arguments

tab

Multidimensional array with dimnames.

marg

A specification of the desired margin; a character vector, a numeric vector or a right hand sided formula.

cond

A specification of what is conditioned on. Can take two forms: Form one is a a character vector, a numeric vector or a right hand sided formula. Form two is as a simple slice of the array, which is a list of the form var1=value1, var2=value2 etc.

normalize

Should the result be normalized to sum to 1.

Value

A multidimensional array.

See Also

ar_new, ar_marg, ar_slice etc.

Examples

Run this code
# NOT RUN {
hec <- HairEyeColor

is.named.array( hec )
## We need dimnames, and names on the dimnames

## Marginalize:
ar_dist(hec, marg= ~Hair + Eye)
ar_dist(hec, marg= ~Hair:Eye)
ar_dist(hec, marg= c("Hair", "Eye"))
ar_dist(hec, marg= 1:2)

ar_dist(hec, marg= ~Hair + Eye, normalize=FALSE)

## Condition
ar_dist(hec, cond= ~Sex + Hair)
ar_dist(hec, cond= ~Sex:Hair)
ar_dist(hec, cond= c("Sex", "Hair"))
ar_dist(hec, cond= c(3,1))

ar_dist(hec, cond= list(Hair="Black"))
ar_dist(hec, cond= list(Hair=1))

# }
# NOT RUN {
## This will fail
ar_dist(hec, cond= list(Hair=c("Black", "Brown")))
ar_dist(hec, cond= list(Hair=1:2))
# }
# NOT RUN {
## But this will do the trick
a <- ar_slice(hec, slice=list(Hair=c("Black", "Brown")))
ar_dist(a, cond=~Hair)

## Combined
ar_dist(hec, marg=~Hair+Eye, cond=~Sex)
ar_dist(hec, marg=~Hair+Eye, cond="Sex")

ar_dist(hec, marg=~Hair+Eye, cond=list(Sex="Male"))
ar_dist(hec, marg=~Hair+Eye, cond=list(Sex="Male"), normalize=FALSE)

ar_dist(hec, cond=list(Sex="Male"))
ar_dist(hec, cond=list(Sex="Male"), normalize=FALSE)

# }

Run the code above in your browser using DataLab