table
function. The input is eithertable
function. The input is eithertable
function. The input is eithertable
function. The input is eithertable
function. The input is eitherload('Beer.RData')Beer %>% select(light,avail)%>% table%>% #Creates Tables addmargins()-> #Includes totals crosstab
print(crosstab)
## avail## light National Regional Sum## NONLIGHT 7 21 28## LIGHT 5 2 7## Sum 12 23 35
BE | CER | FRF | MUE | POR | STF | TT | YOG | |
---|---|---|---|---|---|---|---|---|
Economical | 3 | 24 | 7 | 3 | 20 | 3 | 16 | 7 |
Expensive | 27 | 6 | 9 | 33 | 5 | 18 | 3 | 10 |
Family Favourite | 31 | 14 | 7 | 4 | 10 | 2 | 5 | 5 |
Healthy | 18 | 14 | 31 | 38 | 25 | 28 | 8 | 34 |
Long Prepare | 35 | 0 | 0 | 0 | 9 | 10 | 1 | 0 |
Nutritious | 25 | 14 | 32 | 28 | 25 | 26 | 7 | 31 |
Quick | 2 | 54 | 26 | 33 | 8 | 8 | 15 | 20 |
Summer | 13 | 42 | 37 | 22 | 11 | 16 | 7 | 35 |
Tasteless | 2 | 8 | 0 | 6 | 2 | 2 | 0 | 1 |
Tasty | 34 | 24 | 33 | 21 | 16 | 26 | 11 | 26 |
Treat | 31 | 5 | 4 | 3 | 3 | 16 | 4 | 17 |
Weekdays | 9 | 47 | 11 | 24 | 15 | 6 | 13 | 10 |
Weekends | 56 | 12 | 10 | 5 | 8 | 23 | 16 | 18 |
Winter | 26 | 10 | 11 | 10 | 32 | 19 | 6 | 8 |
We can visualise this using Correspondence Analysis which requires the ca
package.
library(ca) caout<-ca(breakfastct) plot(caout)
You need to install the ca
package first
## Warning: package 'ca' was built under R version 3.6.2
summary(caout,row=FALSE,column=FALSE)
## ## Principal inertias (eigenvalues): ## ## dim value % cum% scree plot ## 1 0.193095 52.5 52.5 ************* ## 2 0.077731 21.1 73.6 ***** ## 3 0.043854 11.9 85.6 *** ## 4 0.032804 8.9 94.5 ** ## 5 0.012257 3.3 97.8 * ## 6 0.005687 1.5 99.4 ## 7 0.002363 0.6 100.0 ## -------- ----- ## Total: 0.367791 100.0
V1 \ V2 | X | Y | Total |
---|---|---|---|
A | 50 | ||
B | 50 | ||
Total | 20 | 80 | 100 |
If variable 1 and variable 2 are independent then what numbers do you expect to be in the empty cells?
V1 \ V2 | X | Y | Total |
---|---|---|---|
A | 10 | 40 | 50 |
B | 10 | 40 | 50 |
Total | 20 | 80 | 100 |
Under independence
V1 \ V2 | X | Y | Total |
---|---|---|---|
A | 12 | 38 | 50 |
B | 8 | 42 | 50 |
Total | 20 | 80 | 100 |
For the chi square test, in each cell we compute
$$\frac{(O_{ij}-E_{ij})^2}{E_{ij}}$$
where \(O_{ij}\) is the observed count in each cell and \(E_{ij}\) is the expected count in each cell.
The chi square statistic is
$$\chi^2=\sum\limits_{i=1}^{r}\sum\limits_{j=1}^{c}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}$$
where \(r\) and \(c\) are the number of rows and columns in the cross tab respectively.
summary(caout,row=FALSE,column=FALSE)
## ## Principal inertias (eigenvalues): ## ## dim value % cum% scree plot ## 1 0.193095 52.5 52.5 ************* ## 2 0.077731 21.1 73.6 ***** ## 3 0.043854 11.9 85.6 *** ## 4 0.032804 8.9 94.5 ** ## 5 0.012257 3.3 97.8 * ## 6 0.005687 1.5 99.4 ## 7 0.002363 0.6 100.0 ## -------- ----- ## Total: 0.367791 100.0
$$m_{ij}=\frac{o_{ij}-e_{ij}}{\sqrt{e_{ij}}}$$
Adelphi | Citiclub | CrownTowers | FlagstaffCity | HotelSophia | Larwill | Mercure | QT | |
---|---|---|---|---|---|---|---|---|
amazing | 24 | 1 | 24 | 1 | 0 | 15 | 1 | 33 |
bar | 13 | 4 | 1 | 1 | 2 | 5 | 8 | 48 |
bathroom | 3 | 7 | 8 | 6 | 14 | 4 | 19 | 6 |
bed | 13 | 11 | 3 | 9 | 15 | 25 | 19 | 19 |
booked | 6 | 21 | 9 | 15 | 10 | 1 | 5 | 1 |
breakfast | 5 | 5 | 6 | 6 | 17 | 5 | 9 | 9 |
city | 15 | 4 | 17 | 17 | 9 | 17 | 11 | 12 |
clean | 8 | 22 | 7 | 32 | 37 | 21 | 19 | 7 |
close | 11 | 9 | 2 | 14 | 11 | 12 | 17 | 7 |
club | 1 | 64 | 25 | 0 | 0 | 0 | 0 | 0 |
comfortable | 15 | 4 | 17 | 6 | 17 | 24 | 20 | 27 |
cross | 0 | 0 | 0 | 2 | 18 | 0 | 0 | 0 |
crown | 0 | 0 | 89 | 0 | 0 | 0 | 0 | 2 |
crystal | 0 | 0 | 24 | 0 | 0 | 0 | 0 | 0 |
dear | 97 | 0 | 47 | 0 | 0 | 6 | 41 | 0 |
enjoyed | 28 | 51 | 18 | 1 | 2 | 20 | 8 | 46 |
experience | 36 | 2 | 18 | 1 | 3 | 11 | 11 | 39 |
fantastic | 46 | 4 | 10 | 1 | 1 | 17 | 5 | 21 |
feedback | 32 | 66 | 17 | 0 | 1 | 6 | 15 | 39 |
forward | 1 | 51 | 5 | 0 | 0 | 4 | 13 | 4 |
free | 10 | 4 | 2 | 14 | 14 | 6 | 5 | 2 |
friendly | 23 | 17 | 16 | 9 | 9 | 30 | 19 | 35 |
good | 12 | 67 | 9 | 21 | 29 | 21 | 27 | 14 |
great | 71 | 76 | 31 | 14 | 19 | 52 | 19 | 95 |
hear | 23 | 7 | 32 | 1 | 3 | 17 | 9 | 44 |
hello | 1 | 0 | 0 | 0 | 0 | 9 | 0 | 62 |
helpful | 13 | 12 | 4 | 9 | 7 | 26 | 15 | 16 |
hi | 0 | 0 | 0 | 0 | 0 | 23 | 1 | 29 |
hotel | 74 | 172 | 49 | 32 | 61 | 51 | 65 | 67 |
leave | 41 | 2 | 5 | 2 | 0 | 11 | 1 | 14 |
location | 51 | 25 | 17 | 10 | 28 | 26 | 19 | 35 |
lovely | 21 | 2 | 10 | 0 | 1 | 31 | 3 | 30 |
melbourne | 36 | 7 | 49 | 22 | 28 | 19 | 26 | 77 |
much | 45 | 61 | 11 | 4 | 8 | 18 | 3 | 40 |
my | 26 | 15 | 40 | 17 | 21 | 28 | 26 | 27 |
night | 18 | 53 | 16 | 26 | 17 | 8 | 14 | 13 |
nights | 7 | 27 | 8 | 16 | 9 | 10 | 14 | 4 |
noise | 2 | 64 | 0 | 3 | 5 | 1 | 6 | 1 |
north | 0 | 0 | 1 | 0 | 0 | 1 | 25 | 0 |
old | 1 | 0 | 0 | 6 | 5 | 0 | 21 | 3 |
park | 0 | 0 | 0 | 2 | 0 | 24 | 1 | 1 |
parking | 2 | 2 | 5 | 18 | 2 | 15 | 13 | 2 |
place | 9 | 10 | 9 | 17 | 12 | 9 | 8 | 11 |
positive | 1 | 55 | 7 | 1 | 1 | 9 | 2 | 6 |
really | 13 | 10 | 5 | 4 | 11 | 21 | 3 | 43 |
receive | 1 | 55 | 2 | 0 | 0 | 0 | 0 | 0 |
reception | 0 | 4 | 6 | 7 | 27 | 5 | 9 | 3 |
review | 49 | 98 | 39 | 1 | 1 | 37 | 23 | 32 |
room | 34 | 61 | 70 | 59 | 81 | 64 | 61 | 51 |
rooms | 30 | 53 | 23 | 31 | 10 | 34 | 33 | 35 |
service | 41 | 57 | 30 | 2 | 9 | 10 | 16 | 40 |
southern | 0 | 0 | 0 | 2 | 18 | 0 | 0 | 0 |
staff | 72 | 25 | 35 | 26 | 25 | 44 | 41 | 76 |
station | 1 | 2 | 0 | 4 | 21 | 0 | 0 | 0 |
stay | 86 | 100 | 71 | 24 | 21 | 60 | 58 | 66 |
stayed | 21 | 32 | 33 | 31 | 21 | 26 | 29 | 23 |
taking | 61 | 99 | 7 | 1 | 0 | 15 | 21 | 37 |
team | 44 | 0 | 21 | 0 | 0 | 3 | 9 | 8 |
thank | 89 | 127 | 51 | 0 | 0 | 38 | 27 | 44 |
time | 86 | 99 | 15 | 5 | 9 | 36 | 29 | 51 |
towers | 0 | 0 | 69 | 0 | 0 | 0 | 0 | 1 |
walk | 5 | 2 | 5 | 13 | 14 | 5 | 5 | 4 |
wonderful | 48 | 2 | 16 | 1 | 1 | 11 | 4 | 27 |
The data can be loaded and correspondence analysis can be carried out using
load('hotels.RData') hoteltable%>%ca%>%plot
## ## Principal inertias (eigenvalues): ## ## dim value % cum% scree plot ## 1 0.199004 27.1 27.1 ******* ## 2 0.184450 25.1 52.2 ****** ## 3 0.155095 21.1 73.3 ***** ## 4 0.075622 10.3 83.6 *** ## 5 0.058206 7.9 91.5 ** ## 6 0.038432 5.2 96.7 * ## 7 0.024115 3.3 100.0 * ## -------- ----- ## Total: 0.734923 100.0
You must enable Javascript to view this page properly.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |