Homework 3.3 Problem 37
Hospitals <- matrix(c(120,680,10,40,10,190,20,230),ncol=4,byrow=TRUE)
colnames(Hospitals) <- c("LH delayed","LH discharged","SH delayed","SH discharged")
rownames(Hospitals) <- c("Major surgery","Minor sugery")
Hospitals <- as.table(Hospitals)
Hospitals
## LH delayed LH discharged SH delayed SH discharged
## Major surgery 120 680 10 40
## Minor sugery 10 190 20 230
Hospitals2 <- matrix(c(120,680,10,190),ncol=2,byrow=TRUE)
colnames(Hospitals2) <- c("LH delayed","LH discharged")
rownames(Hospitals2) <- c("Major surgery","Minor sugery")
Hospitals2 <- as.table(Hospitals)
Hospitals2
## LH delayed LH discharged SH delayed SH discharged
## Major surgery 120 680 10 40
## Minor sugery 10 190 20 230
Hospitals3 <- matrix(c(10,40,20,230),ncol=2,byrow=TRUE)
colnames(Hospitals3) <- c("SH delayed","SH discharged")
rownames(Hospitals3) <- c("Major surgery","Minor sugery")
Hospitals3 <- as.table(Hospitals)
Hospitals3
## LH delayed LH discharged SH delayed SH discharged
## Major surgery 120 680 10 40
## Minor sugery 10 190 20 230
CrossTable from the gmodels package does all these at once.
library(gmodels)
as.data.frame.matrix(Hospitals2)
## LH delayed LH discharged SH delayed SH discharged
## Major surgery 120 680 10 40
## Minor sugery 10 190 20 230
CrossTable(Hospitals2, prop.t=TRUE, prop.r=TRUE, prop.c=TRUE)
##
##
## Cell Contents
## |-------------------------|
## | N |
## | Chi-square contribution |
## | N / Row Total |
## | N / Col Total |
## | N / Table Total |
## |-------------------------|
##
##
## Total Observations in Table: 1300
##
##
## |
## | LH delayed | LH discharged | SH delayed | SH discharged | Row Total |
## --------------|---------------|---------------|---------------|---------------|---------------|
## Major surgery | 120 | 680 | 10 | 40 | 850 |
## | 14.412 | 21.720 | 4.713 | 105.602 | |
## | 0.141 | 0.800 | 0.012 | 0.047 | 0.654 |
## | 0.923 | 0.782 | 0.333 | 0.148 | |
## | 0.092 | 0.523 | 0.008 | 0.031 | |
## --------------|---------------|---------------|---------------|---------------|---------------|
## Minor sugery | 10 | 190 | 20 | 230 | 450 |
## | 27.222 | 41.026 | 8.903 | 199.470 | |
## | 0.022 | 0.422 | 0.044 | 0.511 | 0.346 |
## | 0.077 | 0.218 | 0.667 | 0.852 | |
## | 0.008 | 0.146 | 0.015 | 0.177 | |
## --------------|---------------|---------------|---------------|---------------|---------------|
## Column Total | 130 | 870 | 30 | 270 | 1300 |
## | 0.100 | 0.669 | 0.023 | 0.208 | |
## --------------|---------------|---------------|---------------|---------------|---------------|
##
##
library(gmodels)
as.data.frame.matrix(Hospitals2)
## LH delayed LH discharged SH delayed SH discharged
## Major surgery 120 680 10 40
## Minor sugery 10 190 20 230
CrossTable(Hospitals2, prop.t=TRUE, prop.r=TRUE, prop.c=TRUE)
##
##
## Cell Contents
## |-------------------------|
## | N |
## | Chi-square contribution |
## | N / Row Total |
## | N / Col Total |
## | N / Table Total |
## |-------------------------|
##
##
## Total Observations in Table: 1300
##
##
## |
## | LH delayed | LH discharged | SH delayed | SH discharged | Row Total |
## --------------|---------------|---------------|---------------|---------------|---------------|
## Major surgery | 120 | 680 | 10 | 40 | 850 |
## | 14.412 | 21.720 | 4.713 | 105.602 | |
## | 0.141 | 0.800 | 0.012 | 0.047 | 0.654 |
## | 0.923 | 0.782 | 0.333 | 0.148 | |
## | 0.092 | 0.523 | 0.008 | 0.031 | |
## --------------|---------------|---------------|---------------|---------------|---------------|
## Minor sugery | 10 | 190 | 20 | 230 | 450 |
## | 27.222 | 41.026 | 8.903 | 199.470 | |
## | 0.022 | 0.422 | 0.044 | 0.511 | 0.346 |
## | 0.077 | 0.218 | 0.667 | 0.852 | |
## | 0.008 | 0.146 | 0.015 | 0.177 | |
## --------------|---------------|---------------|---------------|---------------|---------------|
## Column Total | 130 | 870 | 30 | 270 | 1300 |
## | 0.100 | 0.669 | 0.023 | 0.208 | |
## --------------|---------------|---------------|---------------|---------------|---------------|
##
##
library(gmodels)
as.data.frame.matrix(Hospitals3)
## LH delayed LH discharged SH delayed SH discharged
## Major surgery 120 680 10 40
## Minor sugery 10 190 20 230
CrossTable(Hospitals3, prop.t=TRUE, prop.r=TRUE, prop.c=TRUE)
##
##
## Cell Contents
## |-------------------------|
## | N |
## | Chi-square contribution |
## | N / Row Total |
## | N / Col Total |
## | N / Table Total |
## |-------------------------|
##
##
## Total Observations in Table: 1300
##
##
## |
## | LH delayed | LH discharged | SH delayed | SH discharged | Row Total |
## --------------|---------------|---------------|---------------|---------------|---------------|
## Major surgery | 120 | 680 | 10 | 40 | 850 |
## | 14.412 | 21.720 | 4.713 | 105.602 | |
## | 0.141 | 0.800 | 0.012 | 0.047 | 0.654 |
## | 0.923 | 0.782 | 0.333 | 0.148 | |
## | 0.092 | 0.523 | 0.008 | 0.031 | |
## --------------|---------------|---------------|---------------|---------------|---------------|
## Minor sugery | 10 | 190 | 20 | 230 | 450 |
## | 27.222 | 41.026 | 8.903 | 199.470 | |
## | 0.022 | 0.422 | 0.044 | 0.511 | 0.346 |
## | 0.077 | 0.218 | 0.667 | 0.852 | |
## | 0.008 | 0.146 | 0.015 | 0.177 | |
## --------------|---------------|---------------|---------------|---------------|---------------|
## Column Total | 130 | 870 | 30 | 270 | 1300 |
## | 0.100 | 0.669 | 0.023 | 0.208 | |
## --------------|---------------|---------------|---------------|---------------|---------------|
##
##
barplot(Hospitals, main = "Hospital discharges", col=c("darkblue","red"), beside = T)