(a)Carry out a ??2 test for association between the two variables.

data("Hospital", package="vcd")
str(Hospital)
##  'table' num [1:3, 1:3] 43 6 9 16 11 18 3 10 16
##  - attr(*, "dimnames")=List of 2
##   ..$ Visit frequency: chr [1:3] "Regular" "Less than monthly" "Never"
##   ..$ Length of stay : chr [1:3] "2-9" "10-19" "20+"
Hospital
##                    Length of stay
## Visit frequency     2-9 10-19 20+
##   Regular            43    16   3
##   Less than monthly   6    11  10
##   Never               9    18  16
HS <- margin.table(Hospital, 2:1)
chisq.test(HS)
## 
##  Pearson's Chi-squared test
## 
## data:  HS
## X-squared = 35.171, df = 4, p-value = 4.284e-07

(b)Use assocstats () to compute association statistics. How would you describe the strength of association here?

library(vcdExtra)
## Warning: package 'vcdExtra' was built under R version 3.5.3
## Loading required package: vcd
## Warning: package 'vcd' was built under R version 3.5.3
## Loading required package: grid
## Loading required package: gnm
## Warning: package 'gnm' was built under R version 3.5.3
assocstats(HS)
##                     X^2 df   P(> X^2)
## Likelihood Ratio 38.353  4 9.4755e-08
## Pearson          35.171  4 4.2842e-07
## 
## Phi-Coefficient   : NA 
## Contingency Coeff.: 0.459 
## Cramer's V        : 0.365

The chi square value is 35.171, 4 degree of freedom noted. There is evidence of relationship at the 0.005 level of significance. As the chi square value is large means there is a strong relationship between length of stay and visit frequency than does a small chi square value.

(c)Produce an association plot for these data, with visit frequency as the vertical variable. Describe the pattern of the relation you see here.

sieve(HS, shade=TRUE, labeling = labeling_values, goem_text=gpar(fontface=2))

Greater portion of regular visits lasted 2-9 days. During the length of stay, people have the tendency to stay 2-9 days. There are others that tend to stay longer at 10-19 days, wile there are fewer more that stay more than 20 days. Majority people visit regularly. While some never visit, there are a few others that visit once less than a month.

assoc(Hospital, shade=TRUE)

(d)Both variables can be considered ordinal, so CMHtest () may be useful here. Carry out that analysis. Do any of the tests lead to different conclusions?

CMHtest(HS)
## Cochran-Mantel-Haenszel Statistics for Length of stay by Visit frequency 
## 
##                  AltHypothesis  Chisq Df       Prob
## cor        Nonzero correlation 29.138  1 6.7393e-08
## rmeans  Row mean scores differ 29.607  2 3.7233e-07
## cmeans  Col mean scores differ 34.391  2 3.4044e-08
## general    General association 34.905  4 4.8596e-07

The ‘p-value’ is less than 0.05. Henc both variables can be considered ordinal, the association between two variables still stay true. All results lead to the same conclusion that there is a significant association between the visit frequency and the length of stay