Homer White, Georgetown College

Always remember to make sure the necessary packages are loaded:

```
require(mosaic)
require(tigerstats)
```

```
data(ledgejump)
View(ledgejump)
help(ledgejump)
```

Research Question:

Does the weather have any effect on crowd behavior?

```
WeBe <- xtabs(~weather+crowd.behavior,
data=ledgejump)
WeBe
```

```
crowd.behavior
weather baiting polite
cool 2 7
warm 8 4
```

```
rowPerc(WeBe)
```

```
baiting polite Total
cool 22.22 77.78 100
warm 66.67 33.33 100
```

Looks like crowds were nastier in warm weather!

Is the pattern in the data due to a real association between weather and crowd behavior?

Or did it just occur by chance?

After all, many things can affect crowd behavior:

- how long incident lasts
- number of people in crowd
- personalities of the onlookers who gather to watch
- personality/behavior of would-be jumper

These things vary from one incident to another. We model them as “chance.”

Null and Alternative hypotheses:

\( H_0 \): Weather and crowd behavior are not related.

\( H_a \): Weather and crowd behavior ARE related.

- The Null thinks the relationship in the data occurred by chance.
- The Alternative thinks the relationship in the data is too strong to be explained (reasonably) in terms of chance alone.

```
crowd.behavior
weather baiting polite
cool 2 7
warm 8 4
```

- Crowd baited in 10 cases
- Crowd was polite in 11 cases
- \( \frac{10}{21} \times 100 = 47.6 \)% baiting
- \( \frac{11}{21} \times 100 = 52.4 \)% polite

```
crowd.behavior
weather baiting polite
cool 2 7
warm 8 4
```

- Cool weather 9 times. Null expects:
- baiting 47.6% of 9 times = 4.28 times
- politeness 52.4% of 9 times = 4.72 times

- Warm Weather 12 times. Null expects:
- baiting 47.6% of 12 times = 5.71 times
- politeness 52.4% of 12 times = 6.29 times

So the Null expects a table about like this …

```
baiting polite
cool 4.29 4.71
warm 5.71 6.29
```

… give or take some for chance variation in all of the factors that might affect crowd behavior.

In each cell, the count expected by the Null is:

\[ \frac{\text{rowSum} \times \text{colSum}}{\text{Grand Total}}. \]

```
bait polite
cool 2 7
warm 8 4
```

Example:

For cool-baiting cell,

\[ \frac{(2+7) \times (2+8)}{21} = 4.29 \]

Observed:

```
bait polite
cool 2 7
warm 8 4
```

Expected By Null:

```
baiting polite
cool 4.29 4.71
warm 5.71 6.29
```

Observed - Expected:

```
bait polite
cool -2.29 2.29
warm 2.29 -2.29
```

One Number to measure overall difference:

\[ \chi^2=\sum_{\text{all cells}} \frac{(\text{Observed}-\text{Expected})^2}{\text{Expected}}. \]

This works out to:

\[ \frac{(2-4.29)^2}{4.29}+\frac{(7-4.71)^2}{4.71} \\ +\frac{(8-5.71)^2}{5.71}+\frac{(4-6.29)^2}{6.29} \\ \approx 4.07 \]

- \( \chi^2 \) statistic is always \( \geq 0 \)
- the bigger it is, the more the data differ from what the Null expects

Our Question is:

What's the chance of getting 4.07 or more, if the Null is right?

This chance is called a *P-value.*

Our strategy is *simulation*:

- repeat the study many times, in a situation where the Null is correct
- compute the \( \chi^2 \)-statistic each time
- keep track of how often it is \( \geq 4.07 \)

The P-value is \( \approx \) the proportion of times the simulated \( \chi^2 \)-statistic exceeds 4.07, the statistic we actually got.

Here is an app to perform the simulation

```
require(manipulate)
ChisqSimSlow(~weather+crowd.behavior,
data=ledgejump,
effects="fixed")
```

A common convention is:

- If \( P < 0.05 \), we reject \( H_0 \)
- If \( P \geq 0.05 \), we fail to reject \( H_0 \)

```
data(gss02)
View(gss02)
help(gss02)
```

(More information: General Social Survey)

Our Research Question:

Is there any association between one's race and whether or not one owns a gun?

```
RaceGun <- xtabs(
~race+owngun,
data=gss02
)
```

```
RaceGun
```

```
owngun
race No Yes
AfrAm 106 16
Hispanic 20 3
Other 25 7
White 454 284
```

```
rowPerc(RaceGun)
```

```
No Yes
AfrAm 86.89 13.11
Hispanic 86.96 13.04
Other 78.12 21.88
White 61.52 38.48
```

Null and Alternative hypotheses:

\( H_0 \): In the U.S. population, race and gun ownership are not related.

\( H_a \): In the U.S. population, race and gun ownership ARE related.

Try it out:

```
ChisqSimSlow(~race+owngun,
data=gss02,effects="random")
```

For a two-way table, the “degrees of freedom” (\( df \)) are:

\[ df = (rows -1) \times (columns-1). \]

If the Null is right, then:

- \( \chi^2 \) should be about \( df \),
- give or take \( \sqrt{2 \times df} \) or so.

In the GSS 2002 race and gun study,

\[ df=(4-1) \times (2-1) = 3, \]

so if the Null is right then:

- \( \chi^2 \) should be about 3,
- give or take \( \sqrt{2 \times 3}=2.45 \) or so.

The actual value was \( \chi^2=36.98 \) — far above what the Null expects!

… awfully slow!

(And unless you repeat many, many times, the approximation to the P-value is not so good!)

chisqtestGC() computes chi-square statistic and P-value for you.

We apply it in five steps.

Research Question:

Is there any relationship, in the Georgetown College population, between sex and seating preference?

\( H_0 \): In the GC population, sex and seating preference are not related.

\( H_a \): In the GC population, sex and seating preference ARE related.

- Step Two is computation of the \( \chi^2 \)-statistic
- Step Three is the computation of the P-value

Usually these steps happen together:

```
chisqtestGC(~sex+seat,
data=m111survey)
```

\( \chi^2 = 3.73 \), and \( P=0.155 \).

Since \( P=0.155 > 0.05 \), we fail to reject \( H_0 \).

This study did not provide strong evidence that sex and seating preference are related, in the Georgetown College population.

A well-worded conclusion is:

- non-technical (no “P-value”, “\( H_0 \)”, etc.)
- says how much evidence the study provides for \( H_a \)

```
chisqtestGC(~weather+crowd.behavior,
data=ledgejump)
```

You get a warning:

“Some expected cell counts are low: the approximation of the P-value may be unreliable. Consider using simulation.”

This warning occurs when some expected cell counts are less than 5.

Consider using the `simulate.p.value`

argument.

In `ledgejump`

, the 21 incidents remain the fixed during re-sampling:

- 9 cool-weather
- 12 warm-weather

Only the response (baiting,polite) varied.

For this type of simulation, use:

```
chisqtestGC(~weather+crowd.behavior,
data=ledgejump,
simulate.p.value="fixed",
B=2500,
graph=TRUE)
```

In `m111survey`

, the 71 students were a random sample from the GC population.

In re-sampling, both **sex** *and* the response **seat** will vary.

For this type of simulation, use:

```
chisqtestGC(~sex+seat,
data=m111survey,
simulate.p.value="random",
B=2500,
graph=TRUE)
```

If you are in doubt, just use:

```
chisqtestGC(~sex+seat,
data=m111survey,
simulate.p.value=TRUE,
B=2500)
```

R will run a re-sampling routine of its own.