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

tabDist(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.

Author

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

Examples


hec <- HairEyeColor

is.named.array( hec )
#> [1] TRUE
## We need dimnames, and names on the dimnames

## Marginalize:
tabDist(hec, marg= ~Hair + Eye)
#>        Eye
#> Hair         Brown       Blue      Hazel       Green
#>   Black 0.11486486 0.03378378 0.02533784 0.008445946
#>   Brown 0.20101351 0.14189189 0.09121622 0.048986486
#>   Red   0.04391892 0.02871622 0.02364865 0.023648649
#>   Blond 0.01182432 0.15878378 0.01689189 0.027027027
tabDist(hec, marg= ~Hair:Eye)
#>        Eye
#> Hair         Brown       Blue      Hazel       Green
#>   Black 0.11486486 0.03378378 0.02533784 0.008445946
#>   Brown 0.20101351 0.14189189 0.09121622 0.048986486
#>   Red   0.04391892 0.02871622 0.02364865 0.023648649
#>   Blond 0.01182432 0.15878378 0.01689189 0.027027027
tabDist(hec, marg= c("Hair", "Eye"))
#>        Eye
#> Hair         Brown       Blue      Hazel       Green
#>   Black 0.11486486 0.03378378 0.02533784 0.008445946
#>   Brown 0.20101351 0.14189189 0.09121622 0.048986486
#>   Red   0.04391892 0.02871622 0.02364865 0.023648649
#>   Blond 0.01182432 0.15878378 0.01689189 0.027027027
tabDist(hec, marg= 1:2)
#>        Eye
#> Hair         Brown       Blue      Hazel       Green
#>   Black 0.11486486 0.03378378 0.02533784 0.008445946
#>   Brown 0.20101351 0.14189189 0.09121622 0.048986486
#>   Red   0.04391892 0.02871622 0.02364865 0.023648649
#>   Blond 0.01182432 0.15878378 0.01689189 0.027027027

tabDist(hec, marg= ~Hair + Eye, normalize=FALSE)
#>        Eye
#> Hair    Brown Blue Hazel Green
#>   Black    68   20    15     5
#>   Brown   119   84    54    29
#>   Red      26   17    14    14
#>   Blond     7   94    10    16

## Condition
tabDist(hec, cond= ~Sex + Hair)
#> , , Hair = Black
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.57142857 0.69230769
#>   Blue  0.19642857 0.17307692
#>   Hazel 0.17857143 0.09615385
#>   Green 0.05357143 0.03846154
#> 
#> , , Hair = Brown
#> 
#>        Sex
#> Eye          Male    Female
#>   Brown 0.3706294 0.4615385
#>   Blue  0.3496503 0.2377622
#>   Hazel 0.1748252 0.2027972
#>   Green 0.1048951 0.0979021
#> 
#> , , Hair = Red
#> 
#>        Sex
#> Eye          Male    Female
#>   Brown 0.2941176 0.4324324
#>   Blue  0.2941176 0.1891892
#>   Hazel 0.2058824 0.1891892
#>   Green 0.2058824 0.1891892
#> 
#> , , Hair = Blond
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.06521739 0.04938272
#>   Blue  0.65217391 0.79012346
#>   Hazel 0.10869565 0.06172840
#>   Green 0.17391304 0.09876543
#> 
tabDist(hec, cond= ~Sex:Hair)
#> , , Hair = Black
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.57142857 0.69230769
#>   Blue  0.19642857 0.17307692
#>   Hazel 0.17857143 0.09615385
#>   Green 0.05357143 0.03846154
#> 
#> , , Hair = Brown
#> 
#>        Sex
#> Eye          Male    Female
#>   Brown 0.3706294 0.4615385
#>   Blue  0.3496503 0.2377622
#>   Hazel 0.1748252 0.2027972
#>   Green 0.1048951 0.0979021
#> 
#> , , Hair = Red
#> 
#>        Sex
#> Eye          Male    Female
#>   Brown 0.2941176 0.4324324
#>   Blue  0.2941176 0.1891892
#>   Hazel 0.2058824 0.1891892
#>   Green 0.2058824 0.1891892
#> 
#> , , Hair = Blond
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.06521739 0.04938272
#>   Blue  0.65217391 0.79012346
#>   Hazel 0.10869565 0.06172840
#>   Green 0.17391304 0.09876543
#> 
tabDist(hec, cond= c("Sex", "Hair"))
#> , , Hair = Black
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.57142857 0.69230769
#>   Blue  0.19642857 0.17307692
#>   Hazel 0.17857143 0.09615385
#>   Green 0.05357143 0.03846154
#> 
#> , , Hair = Brown
#> 
#>        Sex
#> Eye          Male    Female
#>   Brown 0.3706294 0.4615385
#>   Blue  0.3496503 0.2377622
#>   Hazel 0.1748252 0.2027972
#>   Green 0.1048951 0.0979021
#> 
#> , , Hair = Red
#> 
#>        Sex
#> Eye          Male    Female
#>   Brown 0.2941176 0.4324324
#>   Blue  0.2941176 0.1891892
#>   Hazel 0.2058824 0.1891892
#>   Green 0.2058824 0.1891892
#> 
#> , , Hair = Blond
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.06521739 0.04938272
#>   Blue  0.65217391 0.79012346
#>   Hazel 0.10869565 0.06172840
#>   Green 0.17391304 0.09876543
#> 
tabDist(hec, cond= c(3,1))
#> , , Hair = Black
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.57142857 0.69230769
#>   Blue  0.19642857 0.17307692
#>   Hazel 0.17857143 0.09615385
#>   Green 0.05357143 0.03846154
#> 
#> , , Hair = Brown
#> 
#>        Sex
#> Eye          Male    Female
#>   Brown 0.3706294 0.4615385
#>   Blue  0.3496503 0.2377622
#>   Hazel 0.1748252 0.2027972
#>   Green 0.1048951 0.0979021
#> 
#> , , Hair = Red
#> 
#>        Sex
#> Eye          Male    Female
#>   Brown 0.2941176 0.4324324
#>   Blue  0.2941176 0.1891892
#>   Hazel 0.2058824 0.1891892
#>   Green 0.2058824 0.1891892
#> 
#> , , Hair = Blond
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.06521739 0.04938272
#>   Blue  0.65217391 0.79012346
#>   Hazel 0.10869565 0.06172840
#>   Green 0.17391304 0.09876543
#> 

tabDist(hec, cond= list(Hair="Black"))
#>        Sex
#> Eye           Male     Female
#>   Brown 0.29629630 0.33333333
#>   Blue  0.10185185 0.08333333
#>   Hazel 0.09259259 0.04629630
#>   Green 0.02777778 0.01851852
tabDist(hec, cond= list(Hair=1))
#>        Sex
#> Eye           Male     Female
#>   Brown 0.29629630 0.33333333
#>   Blue  0.10185185 0.08333333
#>   Hazel 0.09259259 0.04629630
#>   Green 0.02777778 0.01851852

if (FALSE) { # \dontrun{
## This will fail
tabDist(hec, cond= list(Hair=c("Black", "Brown")))
tabDist(hec, cond= list(Hair=1:2))
} # }
## But this will do the trick
a <- tabSlice(hec, slice=list(Hair=c("Black", "Brown")))
tabDist(a, cond=~Hair)
#> , , Hair = Black
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.29629630 0.33333333
#>   Blue  0.10185185 0.08333333
#>   Hazel 0.09259259 0.04629630
#>   Green 0.02777778 0.01851852
#> 
#> , , Hair = Brown
#> 
#>        Sex
#> Eye           Male     Female
#>   Brown 0.18531469 0.23076923
#>   Blue  0.17482517 0.11888112
#>   Hazel 0.08741259 0.10139860
#>   Green 0.05244755 0.04895105
#> 

## Combined
tabDist(hec, marg=~Hair+Eye, cond=~Sex)
#> , , Sex = Male
#> 
#>        Eye
#> Hair         Brown       Blue      Hazel      Green
#>   Black 0.11469534 0.03942652 0.03584229 0.01075269
#>   Brown 0.18996416 0.17921147 0.08960573 0.05376344
#>   Red   0.03584229 0.03584229 0.02508961 0.02508961
#>   Blond 0.01075269 0.10752688 0.01792115 0.02867384
#> 
#> , , Sex = Female
#> 
#>        Eye
#> Hair         Brown       Blue      Hazel       Green
#>   Black 0.11501597 0.02875399 0.01597444 0.006389776
#>   Brown 0.21086262 0.10862620 0.09265176 0.044728435
#>   Red   0.05111821 0.02236422 0.02236422 0.022364217
#>   Blond 0.01277955 0.20447284 0.01597444 0.025559105
#> 
tabDist(hec, marg=~Hair+Eye, cond="Sex")
#> , , Sex = Male
#> 
#>        Eye
#> Hair         Brown       Blue      Hazel      Green
#>   Black 0.11469534 0.03942652 0.03584229 0.01075269
#>   Brown 0.18996416 0.17921147 0.08960573 0.05376344
#>   Red   0.03584229 0.03584229 0.02508961 0.02508961
#>   Blond 0.01075269 0.10752688 0.01792115 0.02867384
#> 
#> , , Sex = Female
#> 
#>        Eye
#> Hair         Brown       Blue      Hazel       Green
#>   Black 0.11501597 0.02875399 0.01597444 0.006389776
#>   Brown 0.21086262 0.10862620 0.09265176 0.044728435
#>   Red   0.05111821 0.02236422 0.02236422 0.022364217
#>   Blond 0.01277955 0.20447284 0.01597444 0.025559105
#> 

tabDist(hec, marg=~Hair+Eye, cond=list(Sex="Male"))
#>        Eye
#> Hair         Brown       Blue      Hazel      Green
#>   Black 0.11469534 0.03942652 0.03584229 0.01075269
#>   Brown 0.18996416 0.17921147 0.08960573 0.05376344
#>   Red   0.03584229 0.03584229 0.02508961 0.02508961
#>   Blond 0.01075269 0.10752688 0.01792115 0.02867384
tabDist(hec, marg=~Hair+Eye, cond=list(Sex="Male"), normalize=FALSE)
#>        Eye
#> Hair    Brown Blue Hazel Green
#>   Black    32   11    10     3
#>   Brown    53   50    25    15
#>   Red      10   10     7     7
#>   Blond     3   30     5     8

tabDist(hec, cond=list(Sex="Male"))
#>        Eye
#> Hair         Brown       Blue      Hazel      Green
#>   Black 0.11469534 0.03942652 0.03584229 0.01075269
#>   Brown 0.18996416 0.17921147 0.08960573 0.05376344
#>   Red   0.03584229 0.03584229 0.02508961 0.02508961
#>   Blond 0.01075269 0.10752688 0.01792115 0.02867384
tabDist(hec, cond=list(Sex="Male"), normalize=FALSE)
#>        Eye
#> Hair    Brown Blue Hazel Green
#>   Black    32   11    10     3
#>   Brown    53   50    25    15
#>   Red      10   10     7     7
#>   Blond     3   30     5     8