chi-square test

25
FPP 28 Chi-square test

Upload: sidone

Post on 05-Jan-2016

40 views

Category:

Documents


2 download

DESCRIPTION

Chi-square test. FPP 28. More types of inference for nominal variables. Nominal data is categorical with more than two categories Compare observed frequencies of nominal variable to hypothesized probabilities One categorical variable with more than two categories - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chi-square test

FPP 28

Chi-square test

Page 2: Chi-square test

More types of inference for nominal variablesNominal data is categorical with more than two

categories

Compare observed frequencies of nominal variable to hypothesized probabilitiesOne categorical variable with more than two

categoriesChi-squared goodness of fit test

Test if two nominal variables are independentTwo categorical variables with at least one

having more than two categoriesChi-squared test of independence

Page 3: Chi-square test

Goodness of fit testDo people admit

themselves to hospitals more frequently close to their birthday?

Data from a random sample of 200 people admitted to hospitals

Days from birthday

Number of admissions

within 7 11

8-30 24

31-90 69

91+ 96

Page 4: Chi-square test

Goodness of fit testAssume there is no birthday effect, that is,

people admit randomly. Then, Pr (within 7) = = .0411

Pr (8 - 30) = = .1260 Pr (31-90) = = .3288 Pr (91+) = = .5041

So, in a sample of 200 people, we’d expect

to be in “within 7” to be in “8 - 30” to be in “31 - 90” to be in “91+”

Page 5: Chi-square test

Goodness of fit testIf admissions are random, we expect the

sample frequencies and hypothesized probabilities to be similar

But, as always, the sample frequencies are affected by chance error

So, we need to see whether the sample frequencies could have been a plausible result from a chance error if the hypothesized probabilities are true.

Let’s build a hypothesis test

Page 6: Chi-square test

Goodness of fit testHypothesis

Claim (alternative hyp.) is admission probabilities change according to days since birthday

Opposite of claim (null hyp.) is probabilities in accordance with random admissions.

H0 : Pr (within 7) = .0411 Pr (8 - 30) = .1260 Pr (31-90) = .3288 Pr (91+) = .5041

HA : probabilities different than those in H0 .

Page 7: Chi-square test

Goodness of fit test: Test statisticChi-squared test statistic

X 2 = sum(observed - expected)2

expected

⎝ ⎜

⎠ ⎟

Page 8: Chi-square test

Goodness of fit test: Test statistic

X 2 = sum(observed - expected)2

expected

⎝ ⎜

⎠ ⎟

= .94 + .057 + .16 + .23 =1.397

Cell Obs Exp Dif Dif2 Dif2/Exp

In 7

8-30

31-90

91+

Page 9: Chi-square test

Goodness of fit test: Calculate p-valueX2 has a chi-squared distribution with

degrees of freedom equal to number of categories minus 1.

In this case, df = 4 – 1 = 3.

Page 10: Chi-square test

Goodness of fit test: Calculate p-valueTo get a p-value, calculate the area under

the chi-squared curve to the right of 1.397

Using JMP, this area is 0.703. If the null hypothesis is true, there is a 70% chance of observing a value of X2 as or more extreme than 1.397

Using the table the p-value is between 0.9 and 0.70

Page 11: Chi-square test

Chi-squared table

Page 12: Chi-square test

JMP output admissions

31 - 90 8 - 30 91+ Within 7

31 - 90 8 - 30 91+ Within 7

31 - 90

8 - 30

91+

Within 7

Total

Level

69

24

96

11

200

Count

0.34500

0.12000

0.48000

0.05500

1.00000

Prob

4 Levels

Frequencies

31 - 90

8 - 30

91+

Within 7

Level

0.34500

0.12000

0.48000

0.05500

Estim Prob

0.32900

0.12600

0.50400

0.04100

Hypoth Prob

Likelihood Ratio

Pearson

Test

1.3063

1.3974

ChiSquare

3

3

DF

0.7276

0.7061

Prob>Chisq

Method: Fix hypothesized values, rescale omitted

Test Probabilities

Days

Distributions

Page 13: Chi-square test

Goodness of fit test: Judging p-valueThe .70 is a large p-value, indicating that

the difference between the observed and expected counts could well occur by random chance when the null hypothesis is true. Therefore, we cannot reject the null hypothesis. There is not enough evidence to conclude that admissions rates change according to days from birthday.

Page 14: Chi-square test

Independence testIs birth order related

to delinquency?

Nye (1958) randomly sampled 1154 high school girls and asked if they had been “delinquent”.

Eldest 24 450

In Between 29 312

Youngest 35 211

Only 23 70

Page 15: Chi-square test

Sample of conditional frequencies% Delinquent for each

birth order statusBased on conditional

frequencies, it appears that youngest are more delinquent

Could these sample frequencies have plausibly occurred by chance if there is no relationship between birth order and delinqeuncy

Oldest .05

Middle .085

Youngest .14

Only .25

Page 16: Chi-square test

Test of independenceHypotheses

Want to show that there is some relationship between birth order and delinquency.

Opposite is that there is no relationship.

H0 : birth order and delinquency are independent.

HA : birth order and delinquency are dependent.

Page 17: Chi-square test

Implications of independenceExpected counts

Under independence, Pr(oldest and delinquent) = Pr(oldest)*Pr(delinquent)

Estimate Pr(oldest) as marginal frequency of oldest

Estimate Pr(delinquent) as marginal frequency of delinquent

Hence, estimate Pr(oldest and delinquent) as

The expected number of oldest and delinquent, under independence, equals

This is repeated for all the other cells in table

Page 18: Chi-square test

Test of independenceExpected counts

Next we compare the observed counts with the expected to get a test statistic

Oldest 45.59 428.41

In Between

32.80 308.2

Youngest 23.66 222.34

Only 8.95 84.05

Page 19: Chi-square test

Use the X2 statistic as the test statistic:

245.4205.84

)05.8470(

95.8

)95.823(

34.222

)34.222211(

66.23

)66.2335(

2.308

)2.308312(

80.32

)80.3229(

41.428

)41.428450(

49.45

)59.4524(

2222

22222

X

Page 20: Chi-square test

Test of independence:Calculate the p-valueX 2 has a chi-squared distribution with degrees

of freedom:

df = (number rows – 1) * (number columns – 1)

In delinquency problem, df = (4 - 1) * (2 - 1) = 3.

The area under the chi-squared curve to the right of 42.245 is less than .0001. There is only a very small chance of getting an X2 as or more extreme than 42.245.

Page 21: Chi-square test
Page 22: Chi-square test

JMP output for chi-squared test

Freq: Column 3

Birth

Order

Eldest

In Betwe

Only Child

Youngest

450

38.99

43.14

94.94

428.407

1.0883

24

2.08

21.62

5.06

45.5927

10.2263

312

27.04

29.91

91.50

308.2

0.0468

29

2.51

26.13

8.50

32.7998

0.4402

70

6.07

6.71

75.27

84.0546

2.3500

23

1.99

20.72

24.73

8.94541

22.0819

211

18.28

20.23

85.77

222.338

0.5782

35

3.03

31.53

14.23

23.662

5.4327

474

41.07

341

29.55

93

8.06

246

21.32

1043

90.38

111

9.62

1154

Delinquent?

Count

Total %

Col %

Row %

Expected

Cell Chi 2

No Yes

Contingency Table

Model

Error

C. Total

N

Source

3

1150

1153

1154

DF

18.54974

346.83395

365.38369

-LogLike

0.0508

RSquare (U)

Likelihood Ratio

Pearson

Test

37.099

42.245

ChiSquare

<.0001

<.0001

Prob>ChiSq

Tests

Contingency Analysis of Delinquent? By Birth Order

This is a small p-value. It is unlikely we’d observe data like this if the null hypothesis is true. There does appear to be an association between delinquency and birth order.

Page 23: Chi-square test

Chi-squared test detailsRequires simple random samples.Works best when expected frequencies in

each cell are at least 5.Should not have zero countsHow one specifies categories can affect

results.

Page 24: Chi-square test

Chi-squared test items What do I do when expected counts are less than 5? Try to get more data. Barring that, you can collapse

categories.Example: Is baldness related to heart disease? (see JMP for data set)

Baldness Disease Number of people None Yes 251 None No 331 Little Yes 165 Little No 221 Some Yes 195 Some No 185 Combine “extreme” and “much”

categories Much Yes 50 Much or extreme Yes 52 Much No 34 Much or extreme No 35 Extreme Yes 2 Extreme No 1

This changes the question slightly, since we have a new category.

Page 25: Chi-square test

Chi-squared testfor collapsed data for

baldness exampleBased on p-value,

baldness and heart disease are not independent.

We see that increasing baldness is associated with increased incidence of heart disease.

Freq: Count

Bal

dnes

s

Little

Much+Extreme

None

Some

221

15.40

28.63

57.25

165

11.50

24.89

42.75

35

2.44

4.53

40.23

52

3.62

7.84

59.77

331

23.07

42.88

56.87

251

17.49

37.86

43.13

185

12.89

23.96

48.68

195

13.59

29.41

51.32

386

26.90

87

6.06

582

40.56

380

26.48

772

53.80

663

46.20

1435

HeartDisease

Count

Total %

Col %

Row %

No Yes

Contingency Table

Model

Error

C. Total

N

Source

3

1431

1434

1435

DF

7.25181

983.27068

990.52249

-LogLike

0.0073

RSquare (U)

Likelihood Ratio

Pearson

Test

14.504

14.510

ChiSquare

0.0023

0.0023

Prob>ChiSq

Tests

Contingency Analysis of HeartDisease By Baldness