Add and Print Marginals for 2-way Contingency Tables
printTable2.RdprintTable2() prints a 2-way contingency table “with all
bells and whistles” (currently using German labeling).
margin2table() computes marginals, adds them to the table and
returns a margin2table object the print method for which adds
text decorations (using "-" and "|").
Usage
printTable2(table2, digits = 3)
margin2table(x, totName = "sum", name.if.empty=FALSE)
# S3 method for class 'margin2table'
print(x, digits = 3, quote = FALSE, right = TRUE, ...)Arguments
- x
a matrix; for
print(), the result ofmargin2table.- digits
Anzahl Dezimalstellen, auf die die Häufigkeiten gerundet werden sollen.
- quote, right
logicals passed to
print.default(), but with different default values.- totName
string to use as row- and column- name if
xhas correspondingdimnames.- name.if.empty
logical indicating if the margin “totals” should be named in any case.
- ...
further potential arguments, unused currently.
Value
margin2table returns a matrix with added marginals,
i.e., an extra row and column, and is of class "margin2table"
(and "table" still) which has a nice print method.
printTable2 is just producing output.
Examples
margin2table(diag(4),,TRUE)
#> | sum
#> 1 0 0 0 | 1
#> 0 1 0 0 | 1
#> 0 0 1 0 | 1
#> 0 0 0 1 | 1
#> -- -- -- -- -- | --
#> sum 1 1 1 1 | 4
m <- diag(3); colnames(m) <- letters[1:3]
margin2table(m)
#> a b c | sum
#> [1,] 1 0 0 | 1
#> [2,] 0 1 0 | 1
#> [3,] 0 0 1 | 1
#> [4,] -- -- -- | --
#> [5,] 1 1 1 | 3
margin2table(m / sum(m))
#> a b c | sum
#> [1,] 0.333 0.000 0.000 | 0.333
#> [2,] 0.000 0.333 0.000 | 0.333
#> [3,] 0.000 0.000 0.333 | 0.333
#> [4,] -- -- -- | --
#> [5,] 0.333 0.333 0.333 | 1.000
data(HairEyeColor)
margin2table(HairEyeColor[,, "Male"])
#> Eye
#> Hair Brown Blue Hazel Green | sum
#> Black 32 11 10 3 | 56
#> Brown 53 50 25 15 | 143
#> Red 10 10 7 7 | 34
#> Blond 3 30 5 8 | 46
#> -- -- -- -- -- | --
#> sum 98 101 47 33 | 279
printTable2(HairEyeColor[,, "Male"])
#>
#> Kontingenz-Tafel mit Randsummen:
#> Eye
#> Hair Brown Blue Hazel Green | sum
#> Black 32 11 10 3 | 56
#> Brown 53 50 25 15 | 143
#> Red 10 10 7 7 | 34
#> Blond 3 30 5 8 | 46
#> -- -- -- -- -- | --
#> sum 98 101 47 33 | 279
#>
#> Gemeinsame Verteilung mit Randverteilungen:
#> Eye
#> Hair Brown Blue Hazel Green | sum
#> Black 0.115 0.039 0.036 0.011 | 0.201
#> Brown 0.190 0.179 0.090 0.054 | 0.513
#> Red 0.036 0.036 0.025 0.025 | 0.122
#> Blond 0.011 0.108 0.018 0.029 | 0.165
#> -- -- -- -- -- | --
#> sum 0.351 0.362 0.168 0.118 | 1.000
#> Bedingte Verteilung gegen y:
#> Eye
#> Hair Brown Blue Hazel Green
#> Black 0.571 0.196 0.179 0.054
#> Brown 0.371 0.350 0.175 0.105
#> Red 0.294 0.294 0.206 0.206
#> Blond 0.065 0.652 0.109 0.174
#>
#> Bedingte Verteilung gegen x:
#> Eye
#> Hair Brown Blue Hazel Green
#> Black 0.327 0.109 0.213 0.091
#> Brown 0.541 0.495 0.532 0.455
#> Red 0.102 0.099 0.149 0.212
#> Blond 0.031 0.297 0.106 0.242
#>
#> Freiheitsgrade: df = 9
#> Chi^2 - Annahmebereich: [0,16.9] (alpha=0.05)
#>
#>
#> Testwerte unter der Unabhaengigkeitshypothese:
#> Test mit Chi^2: 41.3 (P-Wert: 0)
#> Test mit Devianz: 44.4 (P-Wert: 0)
#>
printTable2(HairEyeColor[,, "Female"])
#>
#> Kontingenz-Tafel mit Randsummen:
#> Eye
#> Hair Brown Blue Hazel Green | sum
#> Black 36 9 5 2 | 52
#> Brown 66 34 29 14 | 143
#> Red 16 7 7 7 | 37
#> Blond 4 64 5 8 | 81
#> -- -- -- -- -- | --
#> sum 122 114 46 31 | 313
#>
#> Gemeinsame Verteilung mit Randverteilungen:
#> Eye
#> Hair Brown Blue Hazel Green | sum
#> Black 0.115 0.029 0.016 0.006 | 0.166
#> Brown 0.211 0.109 0.093 0.045 | 0.457
#> Red 0.051 0.022 0.022 0.022 | 0.118
#> Blond 0.013 0.204 0.016 0.026 | 0.259
#> -- -- -- -- -- | --
#> sum 0.390 0.364 0.147 0.099 | 1.000
#> Bedingte Verteilung gegen y:
#> Eye
#> Hair Brown Blue Hazel Green
#> Black 0.692 0.173 0.096 0.038
#> Brown 0.462 0.238 0.203 0.098
#> Red 0.432 0.189 0.189 0.189
#> Blond 0.049 0.790 0.062 0.099
#>
#> Bedingte Verteilung gegen x:
#> Eye
#> Hair Brown Blue Hazel Green
#> Black 0.295 0.079 0.109 0.065
#> Brown 0.541 0.298 0.630 0.452
#> Red 0.131 0.061 0.152 0.226
#> Blond 0.033 0.561 0.109 0.258
#>
#> Freiheitsgrade: df = 9
#> Chi^2 - Annahmebereich: [0,16.9] (alpha=0.05)
#>
#>
#> Testwerte unter der Unabhaengigkeitshypothese:
#> Test mit Chi^2: 107 (P-Wert: 0)
#> Test mit Devianz: 112 (P-Wert: 0)
#>