General representation of multidimensional arrays (with named dimnames, also called named arrays.)

parray(varNames, levels, values = 1, normalize = "none", smooth = 0)

as.parray(values, normalize = "none", smooth = 0)

data2parray(data, varNames = NULL, normalize = "none", smooth = 0)

makeDimNames(varNames, levels, sep = "")

Arguments

varNames

Names of variables defining table; can be a right hand sided formula.

levels

Either 1) a vector with number of levels of the factors in varNames or 2) a list with specification of the levels of the factors in varNames. See 'examples' below.

values

Values to go into the array

normalize

Either "none", "first" or "all". Should result be normalized, see 'Details' below.

smooth

Should values be smoothed, see 'Details' below.

data

Data to be coerced to a parray; can be data.frame, table, xtabs, matrix.

sep

Desired separator in dim names; defaults to "".

Value

A a named array.

Details

A named array object represents a table defined by a set of variables and their levels, together with the values of the table. E.g. f(a,b,c) can be a table with a,b,c representing levels of binary variable

If normalize="first" then for each configuration of all other variables than the first, the probabilities are normalized to sum to one. Thus f(a,b,c) becomes a conditional probability table of the form p(a|b,c).

If normalize="all" then the sum over all entries of f(a,b,c) is one.

If smooth is positive then smooth is added to values before normalization takes place.

See also

Author

Søren Højsgaard, sorenh@math.aau.dk

Examples

 
t1 <- parray(c("gender","answer"), list(c('male','female'),c('yes','no')), values=1:4)
t1 <- parray(~gender:answer, list(c('male','female'),c('yes','no')), values=1:4)
t1 <- parray(~gender:answer, c(2,2), values=1:4)

t2 <- parray(c("answer","category"), list(c('yes','no'),c(1,2)), values=1:4+10)
t3 <- parray(c("category","foo"), c(2,2), values=1:4+100)

varNames(t1)
#> [1] "gender" "answer"
nLevels(t1)
#> gender answer 
#>      2      2 
valueLabels(t1)
#> $gender
#> [1] "gender1" "gender2"
#> 
#> $answer
#> [1] "answer1" "answer2"
#> 

## Create 1-dimensional vector with dim and dimnames
x1 <- 1:5
as.parray(x1)
#> V1
#> V11 V12 V13 V14 V15 
#>   1   2   3   4   5 
x2 <- parray("x", levels=length(x1), values=x1)
dim(x2)
#> x 
#> 5 
dimnames(x2)
#> $x
#> [1] "x1" "x2" "x3" "x4" "x5"
#> 

## Matrix
x1 <- matrix(1:6, nrow=2)
as.parray(x1)
#>      V2
#> V1    V21 V22 V23
#>   V11   1   3   5
#>   V12   2   4   6
parray(~a:b, levels=dim(x1), values=x1)
#>     b
#> a    b1 b2 b3
#>   a1  1  3  5
#>   a2  2  4  6
#> attr(,"class")
#> [1] "parray" "array" 

## Extract parrays from data
## 1) a dataframe
data(cad1) 
data2parray(cad1, ~Sex:AngPec:AMI)
#> , , AMI = Definite
#> 
#>         AngPec
#> Sex      Atypical None Typical
#>   Female        2    4       4
#>   Male          4   10      39
#> 
#> , , AMI = NotCertain
#> 
#>         AngPec
#> Sex      Atypical None Typical
#>   Female        4   19      14
#>   Male         20   52      64
#> 
data2parray(cad1, c("Sex","AngPec","AMI"))
#> , , AMI = Definite
#> 
#>         AngPec
#> Sex      Atypical None Typical
#>   Female        2    4       4
#>   Male          4   10      39
#> 
#> , , AMI = NotCertain
#> 
#>         AngPec
#> Sex      Atypical None Typical
#>   Female        4   19      14
#>   Male         20   52      64
#> 
data2parray(cad1, c(1,2,3))
#> , , AMI = Definite
#> 
#>         AngPec
#> Sex      Atypical None Typical
#>   Female        2    4       4
#>   Male          4   10      39
#> 
#> , , AMI = NotCertain
#> 
#>         AngPec
#> Sex      Atypical None Typical
#>   Female        4   19      14
#>   Male         20   52      64
#> 
## 2) a table
data2parray(UCBAdmissions,c(1,2), normalize="first")
#>           Gender
#> Admit           Male    Female
#>   Admitted 0.4451877 0.3035422
#>   Rejected 0.5548123 0.6964578