Contingency Tables and Tests For Independance

data
##    Rk                     X AvAge GP  W  L OL PTS  PTS. GF GA SOW SOL   SRS
## 1   1   Philadelphia Flyers  27.2  4  3  1  0   6 0.750 15 11   0   0  0.38
## 2   2   Toronto Maple Leafs  29.0  4  3  1  0   6 0.750 14 12   0   0  1.85
## 3   3  Vegas Golden Knights  28.8  3  3  0  0   6 1.000 11  5   0   0  1.63
## 4   4   Washington Capitals  30.2  4  2  0  2   6 0.750 15 13   0   1 -0.38
## 5   5     New Jersey Devils  26.1  3  2  0  1   5 0.833  8  6   0   1  0.13
## 6   6    Montreal Canadiens  28.6  3  2  0  1   5 0.833 12  7   0   0  0.83
## 7   7        Calgary Flames  28.1  3  2  0  1   5 0.833 11  6   0   0  0.29
## 8   8    Colorado Avalanche  27.1  3  2  1  0   4 0.667 12  6   0   0  0.63
## 9   9       St. Louis Blues  28.3  3  2  1  0   4 0.667  9 13   0   0 -0.63
## 10 10         Winnipeg Jets  28.0  3  2  1  0   4 0.667  9  9   0   0  1.27
## 11 11     Detroit Red Wings  29.1  4  2  2  0   4 0.500  9 10   0   0 -0.39
## 12 12    New York Islanders  28.8  3  2  1  0   4 0.667  5  5   0   0 -0.13
## 13 13   Carolina Hurricanes  27.0  3  2  1  0   4 0.667  9  6   0   0  1.20
## 14 14      Florida Panthers  27.8  2  2  0  0   4 1.000 10  6   0   0 -0.75
## 15 15   Nashville Predators  28.7  3  2  1  0   4 0.667 10  7   0   0  0.51
## 16 16   Tampa Bay Lightning  27.6  2  2  0  0   4 1.000 10  3   0   0  0.75
## 17 17        Minnesota Wild  29.6  3  2  1  0   4 0.667  8  7   0   0 -0.38
## 18 18   Pittsburgh Penguins  28.7  4  2  2  0   4 0.500 13 18   1   0 -0.38
## 19 19         Anaheim Ducks  28.1  3  1  1  1   3 0.500  4  7   0   0  0.88
## 20 20 Columbus Blue Jackets  26.6  4  1  2  1   3 0.375  8 13   0   0 -0.90
## 21 21         Boston Bruins  28.8  3  1  1  1   3 0.500  3  5   1   0 -0.13
## 22 22       Ottawa Senators  26.9  3  1  1  1   3 0.500 10 10   0   0  1.66
## 23 23       Arizona Coyotes  28.5  3  1  1  1   3 0.500 10 10   0   1 -0.13
## 24 24        Buffalo Sabres  27.3  4  1  3  0   2 0.250 11 12   0   0  0.38
## 25 25       Edmonton Oilers  27.7  4  1  3  0   2 0.250 10 15   0   0 -2.19
## 26 26      New York Rangers  25.6  3  1  2  0   2 0.333  8  8   0   0  0.13
## 27 27     Vancouver Canucks  27.4  4  1  3  0   2 0.250  9 16   0   0 -2.69
## 28 28     Los Angeles Kings  28.2  3  0  1  2   2 0.333  8 11   0   0 -0.13
## 29 29       San Jose Sharks  29.3  3  1  2  0   2 0.333 10 13   1   0 -1.88
## 30 30    Chicago Blackhawks  26.9  4  0  3  1   1 0.125  9 20   0   0  0.00
## 31 31          Dallas Stars    NA NA NA NA NA  NA    NA NA NA  NA  NA    NA
##      SOS TG.G EVGF EVGA PP PPO   PP. PPA PPOA    PK. SH SHA PIM.G oPIM.G   S
## 1  -0.63 6.50   12    7  3  12 25.00   4   15  73.33  0   0   7.5    6.0  99
## 2   1.35 6.50    8    9  6  14 42.86   3   17  82.35  0   0  13.3    9.3 135
## 3  -0.38 5.33   10    3  0   7  0.00   1    8  87.50  1   1   5.3    4.7  90
## 4  -0.63 7.00   13    9  2   8 25.00   3   16  81.25  0   1   9.3    5.3  98
## 5  -0.21 4.67    7    1  1  10 10.00   4   13  69.23  0   1  11.0    9.0  81
## 6  -0.84 6.33    6    4  4  10 40.00   2   14  85.71  2   1  11.0    8.3 102
## 7  -1.38 5.67    5    4  6  16 37.50   1   12  91.67  0   1   8.7   11.3  93
## 8  -1.38 6.00    4    5  8  17 47.06   1    9  88.89  0   0   6.7   12.0  89
## 9   0.71 7.33    9    5  0   9  0.00   8   14  42.86  0   0  10.0    6.7  86
## 10  1.27 6.00    7    5  2  13 15.38   4    9  55.56  0   0   7.3   10.0  90
## 11 -0.14 4.75    8    8  1   7 14.29   2   11  81.82  0   0  11.8    9.8  99
## 12 -0.13 3.33    3    4  2  17 11.76   1   14  92.86  0   0  14.7   12.7  73
## 13  0.20 5.00    6    5  3  12 25.00   1    9  88.89  0   0   7.7    9.7  98
## 14 -2.75 8.00    7    3  3   8 37.50   3    5  40.00  0   0   5.0    8.0  64
## 15 -0.49 5.67    9    6  1  11  9.09   1    8  87.50  0   0   5.3    7.3 101
## 16 -2.75 6.50    8    1  2   7 28.57   2    7  71.43  0   0   7.0    7.0  70
## 17 -0.71 5.00    8    6  0  16  0.00   1   11  90.91  0   0   8.7   12.0 105
## 18  0.63 7.75    8   14  4  16 25.00   4   15  73.33  1   0   7.5    8.0 124
## 19  1.88 3.67    4    7  0   5  0.00   0    7 100.00  0   0   5.3    4.0  71
## 20  0.35 5.25    8   12  0   7  0.00   1    9  88.89  0   0   9.5    8.5 127
## 21  0.21 2.67    0    5  2  10 20.00   0   13 100.00  1   0  11.7    9.7  92
## 22  1.66 6.67    7    6  3  15 20.00   4   14  71.43  0   0  10.0   14.0  84
## 23  0.21 6.67    6    6  3  12 25.00   3   13  76.92  1   1  10.0    9.3  95
## 24  0.63 5.75    8   11  3  15 20.00   1    5  80.00  0   0   3.8    8.8 134
## 25 -0.94 6.25    7   11  2  18 11.11   2   14  85.71  1   2   9.5   11.5 138
## 26  0.13 5.33    5    5  3  17 17.65   3   14  78.57  0   0  10.0   16.0 106
## 27 -0.94 6.25    8    9  0  15  0.00   7   21  66.67  1   0  13.0   10.0 134
## 28  0.88 6.33    6    9  2  12 16.67   2   17  88.24  0   0  12.0    8.7  92
## 29 -1.21 7.67    5   11  5  11 45.45   2   10  80.00  0   0   8.0    8.7  95
## 30  2.75 7.25    4   15  5  12 41.67   5   15  66.67  0   0   7.5    6.0 124
## 31    NA   NA   NA   NA NA  NA    NA  NA   NA     NA NA  NA    NA     NA  NA
##      S.  SA   SV. SO
## 1  15.2 144 0.924  1
## 2  10.4 103 0.883  0
## 3  12.2  76 0.934  0
## 4  15.3 114 0.886  0
## 5   9.9 115 0.948  0
## 6  11.8  95 0.926  0
## 7  11.8  93 0.935  1
## 8  13.5  78 0.923  1
## 9  10.5  91 0.857  0
## 10 10.0 105 0.914  0
## 11  9.1 127 0.921  0
## 12  6.8  83 0.940  2
## 13  9.2  68 0.912  1
## 14 15.6  64 0.906  0
## 15  9.9  98 0.929  0
## 16 14.3  60 0.950  0
## 17  7.6  94 0.926  0
## 18 10.5  96 0.813  0
## 19  5.6  96 0.927  1
## 20  6.3 132 0.902  0
## 21  3.3  70 0.929  0
## 22 11.9  91 0.890  0
## 23 10.5  97 0.897  0
## 24  8.2 101 0.881  0
## 25  7.2 145 0.897  0
## 26  7.5  84 0.905  1
## 27  6.7 144 0.889  0
## 28  8.7  95 0.884  0
## 29 10.5  96 0.865  0
## 30  7.3 134 0.851  0
## 31   NA  NA    NA NA

This is a continuation of this project <https://rpubs.com/nurfnick/720194

This portion of the project is all about categorical variables. Let’s remind ourselves of what categorical variables we have. I had created one about who had more powerplays, so I’ll copy/pasta that code here

data$PPO[is.na(data$PPO)] <- 0
data$PPOA[is.na(data$PPOA)] <- 0
data[which(data$PPO <= data$PPOA),"PowerPlays"] = "Less"
data[which(data$PPO > data$PPOA),"PowerPlays"] = "More"
table(data$PowerPlays)
## 
## Less More 
##   17   14

Let’s add an indicator for Canadian teams too

data[which(data$X %in% c("Toronto Maple Leafs","Montreal Canadiens","Calgary Flames","Winnipeg Jets" ,"Ottawa Senators","Edmonton Oilers")),"Canadian"] = TRUE
data[which(!(data$X %in% c("Toronto Maple Leafs","Montreal Canadiens","Calgary Flames","Winnipeg Jets" ,"Ottawa Senators","Edmonton Oilers"))),"Canadian"] = FALSE
table(data$PowerPlays,data$Canadian)
##       
##        FALSE TRUE
##   Less    15    2
##   More    10    4

Test For Independance

test = chisq.test(table(data$PowerPlays), p = c(1,1)/2)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  table(data$PowerPlays)
## X-squared = 0.29032, df = 1, p-value = 0.59
test$expected
## Less More 
## 15.5 15.5
barplot(table(data$PowerPlays))

Contingency Table Test

test2 = chisq.test(table(data$PowerPlays,data$Canadian))
## Warning in chisq.test(table(data$PowerPlays, data$Canadian)): Chi-squared
## approximation may be incorrect
test2
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  table(data$PowerPlays, data$Canadian)
## X-squared = 0.52123, df = 1, p-value = 0.4703
test2$expected
##       
##           FALSE     TRUE
##   Less 13.70968 3.290323
##   More 11.29032 2.709677
mosaicplot(table(data$PowerPlays,data$Canadian))

I am so clever