Debian Codegraph @

R/printTable.R

raw
1
2 printTable2 <- function(table2, digits = 3)
3 {
4 ##-- 2-weg Kontingenztafel mit allem zusammen ... -- ruft catCon(.) auf
5 ##-- Urspruneglich fuer NDK-Uebungen 1992
6 ##-- Verbessert und Fehler korrigiert! : M.Maechler, Feb.1993
7 d <- dim(table2)
8 if(length(d) != 2)
9 stop("Argument muss numerische Matrix sein: Die (2-Weg) Kontingenz Tafel")
10 N <- sum(table2)
11 cat("\nKontingenz-Tafel mit Randsummen:\n")
12 print(margin2table(table2), digits=0)
13 cat("\nGemeinsame Verteilung mit Randverteilungen:\n")
14 I <- d[1]; J <- d[2]; df <- (I-1)*(J-1)
15 r <- margin2table(table2/N)
16 print(r, digits)
17 joint <- r[1:I, 1:J]
18 xrand <- r[I+1, 1:J]
19 yrand <- r[1:I, J+1]
20 condy <- joint/yrand
21 condx <- t(t(joint)/xrand)
22 cat("Bedingte Verteilung gegen y:\n"); print(round(condy,digits)); cat("\n")
23 cat("Bedingte Verteilung gegen x:\n"); print(round(condx,digits)); cat("\n")
24 exp.ind <- N * outer(yrand,xrand)#- Expected under INDEPendence: n * p_i * p_j
25 cat("Freiheitsgrade: df =",df,"\n")
26 cat("Chi^2 - Annahmebereich: [0,", round(qchisq(0.95,df),1),
27 "] (alpha=0.05)\n\n\n", sep = "")
28 test.chisq <- sum((as.matrix(table2)-exp.ind)^2/exp.ind)
29 cat("Testwerte unter der Unabhaengigkeitshypothese:\n")
30 cat(" Test mit Chi^2: ",format(round(test.chisq,2)),
31 " (P-Wert: ",round(1-pchisq(test.chisq,df),4),")\n",sep = "")
32 is.pos <- table2 != 0
33 test.deviance <- 2*sum(table2[is.pos]*log(table2[is.pos]/exp.ind[is.pos]))
34 cat(" Test mit Devianz: ",format(round(test.deviance,2)),
35 " (P-Wert: ",round(1-pchisq(test.deviance,df),4),")\n\n",sep = "")
36 invisible(list(p.condx = condx, p.condy = condy, expected.indep = exp.ind,
37 df = df, chisq.test = test.chisq, deviance = test.deviance))
38 }
39
40 ### The original catCon() function did compute and print;
41 ### now separated :
42
43 margin2table <- function(x, totName = "sum", name.if.empty=FALSE) {
44 x <- as.matrix(x)
45 if(name.if.empty) x <- empty.dimnames(x)
46 r <- rowSums(x)
47 r <- rbind(cbind(x, r), c(colSums(x), sum(r)))
48 dimnames(r) <-
49 if(!is.null(dnx <- dimnames(x))) {
50 dn <- list(if(!is.null(dnx[[1]])) c(dnx[[1]], totName),
51 if(!is.null(dnx[[2]])) c(dnx[[2]], totName))
52 names(dn) <- names(dnx)
53 dn
54 } ## else NULL
55 class(r) <- c("margin2table", "table")
56 r
57 }
58
59 print.margin2table <- function(x, digits = 3, quote = FALSE, right = TRUE, ...)
60 {
61 if(is.null(d <- dim(x)) || length(d <- d - 1) !=2)
62 stop("'x' is not a matrix")
63 N <- d[1]; M <- d[2]
64 cx <- format(round(x, digits))[c(1:N,N+1,N+1), c(1:M,M+1,M+1)]
65 cx[N+1,] <- "--"; if(!is.null(rownames(cx))) rownames(cx)[N+1] <- "--"
66 cx[,M+1] <- "|" ; if(!is.null(colnames(cx))) colnames(cx)[M+1] <- "|"
67 ## TODO: think of implementing zero.print = "." -- as in print.table()
68 ## TODO(2): improve that in print.table(),
69 ## (e.g. replace "0.0" or "0e0" by ". "; "00.0" by " . ")
70 print(cx, quote=quote, right=right, ...)
71 invisible(x)
72 }