Running a Chi-Square Test of Independence – Data Analysis and Intrepretation
Running a Chi-Square Test of Independence
For the purpose of running Chi Square Test of Independence
on the Gapminder dataset I am working on, as indicated in the assignment
instruction, I will have to categorize some of my Gapminder quantitative
variables into categorical variables.
In this sense, I have added another quantitative variable from the Gapminder data to the dataset csv file I will be working with; the new quantitative variable is exports.
The new exports variable:
exports: Exports of goods and services represent the value of all
goods and other market services provided to the rest of the world. They include
the value of merchandise, freight, insurance, transport, travel, royalties,
license fees, and other services, such as communication, construction,
financial, information, business, personal, and government services. They
exclude compensation of employees and investment income (formerly called factor services) and transfer payments.
It is this exports variable I will categorise to 2 level categorical response variable in order to be able to run Chi Square Test of Independence on my work.
I have subsequently, updated My Personal Codebook to include this new variable.
See updated codebook here:
https://docs.google.com/document/d/177YfOjdk4oekFu20OLt4fgmu-n7cgRULAJ_Kd9KdYaM/edit?usp=sharing
Secondly, the purpose of the Chi Square Test of Independence,
I have slightly changed the research question.
The new research question is:
Is there an association or relation between Inflation and Exports of Ghana?
Hypothesis Testing
The Null and Alternate Hypotheses:
From the above research question, the Null Hypothesis (Ho) is that there is no association / relation between Inflation and Exports of the people of Ghana.
Whereas the Alternate Hypothesis (Ha) states that there is an association /
relation between Inflation and Exports of the people of Ghana.
Sample:
Sample is the data from the Gapminder dataset with focus on Ghana
Assessing the evidence:
This is done by Running Chi Square Test of Independence on the hypotheses.
I do this by using running the test using the Python program.
MY PYTHON PROGRAM CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# -*- coding: utf-8 -*- """ Created on Mon Jan 4 00:59:30 2016 @author: Bernard """ import pandas import scipy.stats import seaborn import matplotlib.pyplot as plt #load the gapminder_ghana_updated dataset csv into the program data = pandas.read_csv('gapminder_ghana_updated.csv', low_memory = False) #Converting data to numeric data['incomeperperson'] = pandas.to_numeric(data['incomeperperson'], errors='coerce') data['lifeexpectancy'] = pandas.to_numeric(data['lifeexpectancy'], errors='coerce') data['literacyrate'] = pandas.to_numeric(data['literacyrate'], errors='coerce') data['Inflation'] = pandas.to_numeric(data['Inflation'], errors='coerce') data['exports'] = pandas.to_numeric(data['exports'], errors='coerce') #create a variable for inflationCategory data["inflationCategory"] = data["Inflation"] #categorical groupings for inflation. This is to get one categorical variable for the #Chi Square test data["inflationCategory"] = pandas.cut(data.inflationCategory, [-4, 32, 64, 96, 128]) #create a variable for exportsCategory data["exportsCategory"] = data["exports"] #categorical groupings for exports. this is to get a two level categorical varible # for the CHI SQUARE Test data["exportsCategory"] = pandas.qcut(data["exports"], 2) #including only data relevant for our testing by droping irrelavant data dataSub = data[["inflationCategory", "exportsCategory"]].dropna() #describe inflation category print("describe inflation Category") desc1 = dataSub["inflationCategory"].describe() print(desc1) print("") print("") #inflationCategory count print("inflation category") c1 = dataSub["inflationCategory"].value_counts(sort=False, dropna=True) print(c1) print("") print("") #describe exportsCategory print("describe Exports Category") desc1 = dataSub["exportsCategory"].describe() print(desc1) print("") print("") #Exports category count print("Exports category") c1 = dataSub["exportsCategory"].value_counts(sort=False, dropna=True) print(c1) print("") print("") # contingency table of observed counts count1=pandas.crosstab(dataSub['exportsCategory'], dataSub['inflationCategory']) print (count1) print("") print("") # column percentages colmnSum=count1.sum(axis=0) colPercent=count1/colmnSum print(colPercent) print("") print("") # chi-square print ('chi-square value, p value, expected counts') chiSq1= scipy.stats.chi2_contingency(count1) print (chiSq1) print("") print("") #Change format of inflationCategory from numberic to categorical dataSub["inflationCategory"] = dataSub["inflationCategory"].astype("category") # make exportsCategory numberic dataSub["exportsCategory"] = pandas.to_numeric(dataSub['exportsCategory'], errors='coerce') # graph percent with export level within each inflation group seaborn.factorplot(x="inflationCategory", y="exportsCategory", data=dataSub, kind="bar", ci=None) plt.xlabel("Inflation group level ") plt.ylabel("Proportion Export level") #compare [-4, 32] and [32, 64] recode1 = {[-4, 32]: [-4, 32], [32, 64]: [32, 64]} dataSub['COMP-4v32']= dataSub['inflationCategory'].map(recode1) # contingency table of observed counts count2=pandas.crosstab(dataSub['inflationCategory'], dataSub['COMP-4v32']) print (count2) # column percentages colmnSum2=count2.sum(axis=0) columnPerc2=count2/colmnSum2 print(columnPerc2) print ('chi-square value, p value, expected counts') chis2= scipy.stats.chi2_contingency(count2) print (chis2) #compare [-4, 32] and [64, 96] recode2 = {[-4, 32]: [-4, 32], [64, 96]: [64, 96]} dataSub['COMP-4v64']= dataSub['inflationCategory'].map(recode2) # contingency table of observed counts count2=pandas.crosstab(dataSub['inflationCategory'], dataSub['COMP-4v64']) print (count2) # column percentages colmnSum2=count2.sum(axis=0) columnPerc2=count2/colmnSum2 print(columnPerc2) print ('chi-square value, p value, expected counts') chis2= scipy.stats.chi2_contingency(count2) print (chis2) #compare [-4, 32] and [96, 128] recode3 = {[-4, 32]: [-4, 32], [96, 128]: [96, 128]} dataSub['COMP-4v96']= dataSub['inflationCategory'].map(recode3) # contingency table of observed counts count3=pandas.crosstab(dataSub['inflationCategory'], dataSub['COMP-4v96']) print (count3) # column percentages colmnSum3=count3.sum(axis=0) columnPerc3=count3/colmnSum3 print(columnPerc3) print ('chi-square value, p value, expected counts') chis3= scipy.stats.chi2_contingency(count3) print (chis3) #compare [32, 64] and [64, 96] recode4 = {[32, 64]: [32, 64], [64, 96]: [64, 96]} dataSub['COMP32v64']= dataSub['inflationCategory'].map(recode4) # contingency table of observed counts count4=pandas.crosstab(dataSub['inflationCategory'], dataSub['COMP32v64']) print (count4) # column percentages colmnSum4=count4.sum(axis=0) columnPerc4=count4/colmnSum4 print(columnPerc4) print ('chi-square value, p value, expected counts') chis4= scipy.stats.chi2_contingency(count4) print (chis4) #compare [32, 64] and [96, 128] recode5 = {[32, 64]: [32, 64], [96, 128]: [96, 128]} dataSub['COMP32v96']= dataSub['inflationCategory'].map(recode5) # contingency table of observed counts count5=pandas.crosstab(dataSub['inflationCategory'], dataSub['COMP32v96']) print (count5) # column percentages colmnSum5=count5.sum(axis=0) columnPerc5=count5/colmnSum5 print(columnPerc5) print ('chi-square value, p value, expected counts') chis5= scipy.stats.chi2_contingency(count5) print (chis5) #compare [64, 96] and [96, 128] recode6 = {[64, 96]: [64, 96], [96, 128]: [96, 128]} dataSub['COMP64v96']= dataSub['inflationCategory'].map(recode6) # contingency table of observed counts count6=pandas.crosstab(dataSub['inflationCategory'], dataSub['COMP64v96']) print (count6) # column percentages colmnSum6=count6.sum(axis=0) columnPerc6=count6/colmnSum6 print(columnPerc6) print ('chi-square value, p value, expected counts') chis6= scipy.stats.chi2_contingency(count6) print (chis6) |
CODE OUTPUT:
<<<<<<<<<<<<< CODE OUTPUT BEGIN >>>>>>>>>>>>>>>>>>>
describe inflation Category
count 51
unique 4
top (-4, 32]
freq 37
Name: inflationCategory, dtype:
object
inflation category
(-4, 32] 37
(32, 64] 9
(64, 96] 4
(96, 128] 1
dtype: int64
describe Exports Category
count 51
unique 2
top [3.338, 20.254]
freq 26
Name: exportsCategory, dtype:
object
Exports category
[3.338, 20.254] 26
(20.254, 48.802] 25
dtype: int64
inflationCategory (-4, 32]
(32, 64] (64, 96] (96, 128]
exportsCategory
[3.338, 20.254] 16 6 3 1
(20.254, 48.802] 21 3 1 0
inflationCategory (-4, 32]
(32, 64] (64, 96] (96, 128]
exportsCategory
[3.338, 20.254] 0.432432
0.666667 0.75 1
(20.254, 48.802] 0.567568
0.333333 0.25 0
chi-square value, p value,
expected counts
(3.6574740124740135,
0.30090520911852703, 3, array([[ 18.8627451 ,
4.58823529, 2.03921569, 0.50980392],
[ 18.1372549 , 4.41176471,
1.96078431, 0.49019608]]))
please find graph for analysing further the values
https://drive.google.com/file/d/0B2KfPRxy4ootVzR3R3RoNW9iOEk/view?usp=sharing
<<<<<<<<<<<<<CODE OUTPUT ENDED>>>>>>>>>>>>>>>>>>>
DRAWING CONCLUSION (SUMMARY):
Model Interpretation for Chi-Square Tests:
When examining the relation /association between Inflation (categorical explanatory variable) and Exports (categorical response variable) of Ghana, we can see that the p-value from the Chi-Square Test results is p= 0.3009 with an associated chi-square value, X2 = 3.657
From this p-value and its associated chi-square value the test is significant and therefore I will accept the Null Hypothesis (Ho) that there is no association / relation between Inflation and Exports of the people of Ghana
Model Interpretation for post hoc Chi-Square Test results:
However, since there are different categories of inflation rate, to reduce a Type Error between each of the various exports levels with respect to the inflation
I ran post hoc Chi-Square Test for each of the values of the explanatory variable – inflation. Hence two by two Chi-Square Tests were run for each of the values in my explanatory categorical variable.
There were a total of six combinations of these individual tests.
With the help of the Bonferroni Adjustment test method most of these compared variables were not very far or different from each other. Hence I will accept the Null Hypothesis (Ho) that there is no association / relation between
Inflation and Exports of the people of Ghana.