Category Archives: Statistics

Week 27: The Kruskal-Wallis One-Way Analysis of Variance by Ranks

Back to nonparametrics this week with the Kruskal-Wallis one-way analysis of variance by ranks!

When Would You Use It?
The Kruskal-Wallis one-way analysis of variance by ranks is a nonparametric test used to determine if, in a set of k (k  ≥ 2) independent samples, at least two of the samples represent populations with different median values.

What Type of Data?
The Kruskal-Wallis one-way analysis of variance by ranks requires ordinal data.

Test Assumptions

  • Each sample of subjects has been randomly chosen from the population it represents.
  • The k samples are independent of one another.
  • The dependent variable (the values being ranked) is a continuous random variable.
  • The distributions of the underlying populations are identical in shape (but do not have to be normal).

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the k population medians are equal. The alternative hypothesis claims that at least two of the k population medians are different.

Step 2: Compute the test statistic, a chi-square value (usually denoted as H). H is computed as follows:

07-03-2016-a

Step 3: Obtain the p-value associated with the calculated chi-square H statistic. The p-value indicates the probability of observing an H value equal to or larger than the observed H value from the sample under the assumption that the null hypothesis is true. The degrees of freedom for this test are k – 1.

Step 4: Determine the conclusion. If the p-value is larger than the prespecified α-level, fail to reject the null hypothesis (that is, retain the claim that the population medians are equal). If the p-value is smaller than the prespecified α-level, reject the null hypothesis in favor of the alternative.

Example
The example for this test comes from my music! Looking at my songs that are rated five stars, I wanted to see if there was a difference in the median playcounts for the different genres. Since my Five Star songs are mostly electronic and alternative, I decided to group the rest of the genres into an “other” category so that there are three genre categories total. Here, n = 50 and let α = 0.05.

H0: θelectronic = θalternative = θother
Ha: at least one pair of medians are different

To obtain the ranks of the songs, I did the following steps:

First, I sorted the songs by playcount.

Second, I ranked the songs from 1 to 50 based on their playcount, with 1 corresponding to the song with the highest playcount and 50 corresponding to the song with the lowest playcount. Note that I could have done this the opposite way (1 corresponding to the least-played song and 50 corresponding to the most-played song; the resulting H value would be the same).

Third, I adjusted the ranks for ties. Where there were ties in the playcount, I summed the ranks that were taken by the ties and then divided that value by the number of tied values. I then replaced the original ranks with the newly calculated value.

Finally, I summed the ranks within each of the three genre groups to obtain my Rj values. Here is a table of this final procedure:

07-03-2016-b

Computations:

07-03-2016-c

Here, our computed p-value is greater than our α-level, which leads us to fail to reject the null hypothesis, which is the claim that the median playcount is equal across the three genre groups.

Example in R
No example in R this week, as this is probably easier to do by hand than using R!

Advertisements

Week 26: The Single-Factor Between-Subjects Analysis of Variance

We’re back to parametric tests this week with the single-factor between-subjects analysis of variance (ANOVA)!

When Would You Use It?
The single-factor between-subjects ANOVA is a parametric tests used to determine if, in a set of k (k  ≥ 2) independent samples, at least two of the samples represent populations with different mean values.

What Type of Data?
The single-factor between-subjects ANOVA requires interval or ratio data.

Test Assumptions

  • Each sample of subjects has been randomly chosen from the population it represents.
  • For each sample, the distribution of the data in the underlying population is normal.
  • The variances of the k underlying populations are equal (homogeneity of variances).

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the k population means are equal. The alternative hypothesis claims that at least two of the k population means are different.

Step 2: Compute the test statistic, an F-value. To do so, calculate the following sums of squares values for between-groups (SSB) and within-groups (SSW):

06-26-2016-a

Then compute the mean squared difference scores for between-groups (MSG) and within-groups (MSE):

06-26-2016-b

Finally, compute the F statistic by calculating the ratio:

06-26-2016-c

Step 3: Obtain the p-value associated with the calculated F statistic. The p-value indicates the probability of a ratio of MSB to MSW equal to or larger than the observed ratio in the F statistic, under the assumption that the null hypothesis is true. Unless you have software, it probably isn’t possible to calculate the exact p-value of your F statistic. Instead, you can use an F table (such as this one) to obtain the critical F value for a prespecified α-level. To use this table, first determine the α-level. Find the degrees of freedom for the numerator (or MSB; the df are explained below) and locate the corresponding column on the table. Then find the degrees of freedom for the denominator (or MSE; the df are explained below) and locate the corresponding set of rows on the table. Find the row specific to your α-level. The value at the intersection of the row and column is your critical F value.

Step 4: Determine the conclusion. If the p-value is larger than the prespecified α-level (or the calculated F statistic is larger than the critical F value), fail to reject the null hypothesis (that is, retain the claim that the population means are all equal). If the p-value is smaller than the prespecified α-level, reject the null hypothesis in favor of the alternative.

Example
The example I want to look at today comes from a previous semester’s STAT 217 grades. This particular section of 217 had four labs associated with it. I wanted to determine if the average final grade was different for any one lab compared to the others. Here, n = 109 and let α = 0.05.

H0: µlab1 = µlab2 = µlab3 = µlab4
Ha: at least one pair of means are different

Computations:

06-26-2016-d

For this case, the critical F value, as obtained by the table, is 2.70. Since the computed F value is smaller than the critical F value, we fail to reject H0 and conclude that the average final grade is equal across all four labs.

Example in R

x=read.table('clipboard', header=T)
attach(x)
summary(fit)
             Df Sum Sq Mean Sq F value Pr(>F)
lab           3   1319   439.5   2.036  0.113
Residuals   105  22670   215.9

R will give you the exact p-value of your F statistic; in this case, p-value = 0.113.

Week 25: The McNemar Test

Ready for more nonparametric tests? Today we’re talking about the McNemar test!

When Would You Use It?
The McNemar test is a nonparametric test used to determine if two dependent samples represent two different populations.

What Type of Data?
The McNemar test requires two categorical or nominal data.

Test Assumptions

  • The sample of subjects has been randomly chosen from the population it represents.
  • Each observation in the contingency table is independent of other observations.
  • The scores of the subjects are measured as a dichotomous categorical measure with two mutually exclusive categories.
  • The sample size is not “extremely small” (though there is debate over what constitutes an extremely small sample size).

Test Process
Step 1: Formulate the null and alternative hypotheses. For the McNemar test, the data are usually displayed in a contingency table with the following setup:

06-19-2016-a

Here, Response 1 and Response 2 are the two possible outcomes of the first condition. Response A and Response B are the two possible outcomes of the second condition. Cell a represents the number of people in the sample who had both Response 1 and Response A, cell b represents the number of people in the sample who had both Response 1 and Response B, etc.

The null hypothesis of the test claims that in the underlying population represented by the sample, the proportion of observations in cell b is the same as the proportion of observations in cell c. The alternative hypothesis claims otherwise (one population proportion is greater than the other, less than the other, or that the proportions are simply not equal).

Step 2: Compute the test statistic, a chi-square. It is computed as follows:

06-19-2016-b

Step 3: Obtain the p-value associated with the calculated chi-square. The p-value indicates the probability of a difference in the two cell counts equal to or more extreme than the observed difference between the cell counts, under the assumption that the null hypothesis is true.

Step 4: Determine the conclusion. If the p-value is larger than the prespecified α-level, fail to reject the null hypothesis (that is, retain the claim that the cell proportions for cell b and cell c are equal). If the p-value is smaller than the prespecified α-level, reject the null hypothesis in favor of the alternative.

Example
The example for this test comes from a previous semester’s STAT 217 grades. In the semester in question, the professor offered the students a “bonus test” after their midterms. This was done by allowing the students to essentially re-take the midterm given in class, but doing so on their own time and using all the resources they wanted to. A (small) fraction of the points they would earn on this bonus test would be added to their actual in-class test points.

I wanted to determine if the proportion of students who passed the lab test and failed the bonus test was equal to the proportion of students who failed the lab test but passed the bonus test, using n = 109 students and α = 0.05.

H0: πpass/fail = πfail/pass
Ha: πpass/fail ≠ πfail/pass

The following table shows the breakdown for the four possible outcomes in this case.

06-19-2016-c

Computations:

06-19-2016-d

Since our p-value is smaller than our alpha-level, we reject H0 and claim that the proportions for cells b and c are significantly different.

Example in R
Since the calculations for this week’s test are quite easy, it’s probably faster to do them by hand than use R!

Week 23: The Wilcoxon Matched-Pairs Signed-Ranks Test

Yo! Today we’re going to talk about another nonparametric test: the Wilcoxon matched-pairs signed-ranks test!

When Would You Use It?
The Wilcoxon matched-pairs signed-ranks test is a nonparametric test used to determine if two dependent samples represent two different populations.

What Type of Data?
The Wilcoxon matched-pairs signed-ranks test requires ordinal data.

Test Assumptions

  • The sample of subjects has been randomly selected from the population it represents.
  • The original scores obtained for the subjects in the study are interval or ratio data.
  • The distribution of the difference of the scores in the populations represented by the samples is symmetric about the median population difference score.

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis states that in the two populations represented by the two samples, the median difference score between the two populations is zero. The alternative hypothesis claims otherwise (that the population median difference is greater than, less than, or simply not equal to zero).

Step 2: Compute the test statistic. The test statistic here is called the Wilcoxon T test statistic. Since the calculation is best demonstrated with data, please see the example shown below to see how this is done.

Step 3: Obtain the critical value. Unlike most of the tests we’ve done so far, you don’t get a precise p-value when computing the results here. Rather, you calculate your T test statistic value and then compare it to a specific value. This is done using a table (such as the one here). Find the number at the intersection of your sample size and the specified α-level. Compare this value with your T value.

Step 4: Determine the conclusion. If the calculated T value is larger than the table value, fail to reject the null hypothesis (that is, retain the claim that the samples do not represent different populations). If the calculated T value is equal to or smaller than the table value, reject the null hypothesis in favor of the alternative.

Example
The example for today’s test comes from one of the STAT 213 lab sections I taught last semester. I wanted to see if the students’ ranks in relation to their lab peers changed between midterm 1 and midterm 2. Set α = 0.05. The data is summarized in the following table, and an explanation of the columns can be found below.

H0: θD = 0
Ha: θD ≠ 0

06-05-2016-a

Column 1 is the student ID.
Column 2 is the student’s ranks on midterm 1, with “1” corresponding to the student with the highest grade and “23” corresponding to the student with the lowest grade.
Column 3 is the student’s ranks on midterm 2, with “1” corresponding to the student with the highest grade and “23” corresponding to the student with the lowest grade.
Column 4 is the differences between the rank on midterm 1 and the rank on midterm 2.
Column 5 is the absolute values of Column 4.
Column 6 is the ranks of the values in Column 5. If a Column 5 value is zero, it is not ranked. If there are multiple identical values in Column 5, the average of their ranks is assigned to each of those values for Column 6.
Column 7 is the signed ranks of the values in Column 5. It is the same as Column 6, except if a value was negative in Column 4, its rank becomes negative in Column 7.

To obtain the Wilcoxon T test statistic, find the sum of the positive signed ranks and the sum of the negative signed ranks (all in Column 7). The absolute value of the smaller of these sums is the Wilcoxon T. Here,

06-05-2016-b

So T = 99. The table value for a two-tailed test with n = 23 and α = 0.05 is 73. Since our calculated T is larger than the critical value, we fail to reject the null hypothesis and claim that the median difference in rank in the population is not different between midterm 1 and midterm 2.

Example in R
No R example this week, as this is probably easier to do by hand.

Week 22: The t Test for Two Dependent Samples

Today we’re going to talk about our first test involving dependent samples: the t test for two dependent samples!

When Would You Use It?
The t test for two dependent samples is a parametric test used to determine if two dependent samples represent two populations with different mean values.

What Type of Data?
The t test for two dependent samples requires interval or ratio data.

Test Assumptions

  • If each sample contains the same subjects (e.g., a setup that involves testing subjects at time A and then again at time B), order effects must be controlled for.
  • If a matched subjects design is employed, within each pair of matched subjects, the two subjects must be randomly assigned to one of the two experimental conditions.

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the two sample means are equal. The alternative hypothesis claims otherwise (one population mean is greater than the other, less than the other, or that the means are simply not equal).

Step 2: Compute the t-score. The t-score is computed as follows:

05-29-2016-a

Step 3: Obtain the p-value associated with the calculated t-score. The p-value indicates the probability of a difference in the two sample means that is equal to or more extreme than the observed difference between the sample means, under the assumption that the null hypothesis is true.

Step 4: Determine the conclusion. If the p-value is larger than the prespecified α-level, fail to reject the null hypothesis (that is, retain the claim that the population means are equal). If the p-value is smaller than the prespecified α-level, reject the null hypothesis in favor of the alternative.

Example
For the data for this example, I decided to compare the age at which the internet thought I would die in 2011 to the age at which the internet thinks I would die in 2016. That is, I took 8 different online “death tests” in 2011, then re-took them this evening. The data are in the following table:

05-29-2016-b

I wanted to see if there was a significant difference in the average “age of death” between 2011 and 2016, based on what information I gave these tests. Here, n = 8. Set α = 0.05.

H0: µ2011 = µ2016 (or µ2011 – µ2016 = 0)
Ha: µ2011 ≠ µ2016 (or µ2011 – µ2016 ≠ 0)

Computations:

05-29-2016-c

Since our p-value is smaller than our alpha-level, we reject Hand claim that the population means are significantly different (with evidence in favor of the mean being higher in 2011).

Example in R

dat=read.table('clipboard',header=T) #"dat" is the name of the imported raw data

diffs = y2011 - y2016

n=length(diffs)

D = sum(diffs)

sdev = sd(diffs)

t = D/(sdev/sqrt(n))        #t score      

pval = pt(t, n-1)*2         #p-value

                               #pt calculates the left-hand area

                               #multiply by two because it is a two-sided test

(Here’s a list of the tests, by the way.)

 

Week 21: The z Test for Two Independent Proportions

Hello, all! Today we’re going to talk about a two sample test involving proportions. Specifically, we’re going to talk about the z test for two independent proportions!

When Would You Use It?
The z test for two independent proportions is a nonparametric test used to determine if, in a 2 x 2 contingency table, the underlying populations represented by the samples have equal proportions of observations in one of the two categories of the dependent variable.

What Type of Data?
The z test for two independent proportions requires categorical or nominal data.

Test Assumptions

  • The data represent a random sample of independent observations.

Test Process
Step 1: Formulate the null and alternative hypotheses. The data appropriate for this type of test is usually summarized in a 2 x 2 table (see the example below to get a better understanding of this). The null hypothesis claims that for the category of interest of the dependent variable, the proportion of observations from the first category of the independent variable that belong to the category of interest is equal to the proportion of observations from the second category of the independent variable that belong to the category of interest.

Step 2: Compute the test statistic. The test statistic here is a z-score and is computed as follows:

05-23-2016-a

Step 3: Obtain the p-value associated with the calculated z-score. The p-value indicates the probability of observing a difference in proportions as extreme or more extreme than the observed sample difference, under the assumption that the null hypothesis is true.

Step 4: Determine the conclusion. If the p-value is larger than the prespecified α-level, fail to reject the null hypothesis (that is, retain the claim that the proportions are equal in both groups of the independent variable). If the p-value is smaller than the prespecified α-level, reject the null hypothesis in favor of the alternative.

Example
For today’s example, I wanted to see if there was a significant difference in the proportion of gold medals for European countries versus the rest of the world in the 2012 London Summer Olympics. I sampled a total of 55 countries (all countries that won at least one gold medal), then tallied the number of gold medals, the number of non-gold medals, and whether or not the country was in Europe. This data is summarized in the following table:

05-23-2016-b

Let’s test the claim that the proportion of gold medals for European and non-European countries is different. Set α = 0.05.

H0: π1 = π2
Ha: π1 ≠ π2

Here, n1 = 353, n2 = 516, p1 = 0.323, and p2 = 0.353. The values of p and z and the resulting p-value are calculated as:

05-23-2016-c

Since our p-value is larger than our alpha-level (0.3632 > 0.05), we fail to reject H0 and claim that the proportions are equal in the population.

Example in R
This example assumes that your data are in columns, with one column containing the number of gold medals per country, one column containing the number of total medals per country, and one coded column telling you whether a country belongs to Europe or not.

dat=read.table('clipboard', header=T) #'dat' is the name of the imported raw data
euro = subset(dat,europe == "y")
non = subset(dat,europe == "n")
a = sum(euro$gold)
b = sum(euro$total) - a
c = sum(non$gold)
d = sum(non$total) - c
n1 = sum(a + b)
n2 = sum(c + d)
goldsum = sum(dat$gold)
othersum = sum(total)
p1= a/n1
p2 = c/n2
p = (a + c)/(n1 + n2)
z = (p1 - p2)/(sqrt((p*(1-p))*((1/n1)+(1/n2))))
pval = (pnorm(z))*2          #p-value
                               #pnorm calculates the left-hand area
                               #multiply by two because it is a two-sided test

Week 20: The Chi-Square Test of Independence

Hello, people! Today we’re going to talk about another chi-square test: the chi-square test of independence!

When Would You Use It?
The chi-square test of independence is a nonparametric test used to determine if the two variables represented in a contingency table are independent of one another.

What Type of Data?
The chi-square test of independence requires categorical or nominal data.

Test Assumptions

  • The data represent a random sample of independent observations.
  • The expected frequency of each cell in the contingency table is at least 5.

Test Process
Step 1: Formulate the null and alternative hypotheses. The data appropriate for this type of test is usually summarized in an r x c table, where r is the number of rows of the table and c is the number of columns of the table (see the example below to get a better understanding of this). The null hypothesis claims that the in the population from which the sample was drawn, the observed frequency of each cell in the table is equal to the respective expected frequencies of each cell in the table. The alternative hypothesis claims that for at least one cell, the observed and expected frequencies are different.

Step 2: Compute the test statistic. The test statistic here, unsurprisingly, a chi-square value. To compute this value, use the following equation:

05-15-2016-a

Eij, the expected cell count for the ijth cell, is calculated as follows:

05-15-2016-b

Step 3: Obtain the critical value. The critical value can be obtained using a chi-square table (such as this one here). Find the column corresponding to your specified alpha-level, then find the row corresponding to your degrees of freedom. The degrees of freedom is calculated as df = (r – 1)(c – 1), where r is the number of rows in the table and c is the number of columns in the table. Compare your obtained chi-square value to the value at the intersection of your selected alpha-level and degrees of freedom.

Step 4: Determine the conclusion. If your test statistic is equal to or greater than the table value, reject the null hypothesis. If your test statistic is smaller than the table value, fail to reject the null (that is, claim that the observed cell frequencies match those of the expected cell frequencies).

Example
The example I’ll use today involves looking at some Nobel Prize data. Specifically, I want to see if the category of Nobel Prize (chemistry, physics, etc.) is independent of gender. The data come from here. The sample size I used was n = 761; I omitted organizations who had won the award and just looked at individuals. I also chose to omit the “Economics” category, as that had been the most recently added and did not have a lot of observations for either gender yet. Set α = 0.05.

H0: Nobel Prize category is independent of gender
Ha: Nobel Prize category is not independent of gender

Observed counts are in the following table:

05-15-2016-c

The expected cell counts, as calculated by the Eij formula above, are displayed in the following table:

05-15-2016-d

Calculating the chi-square value gives us:

05-15-2016-e

The degrees of freedom for this test is df = (5 – 1)(2 – 1) = 4, which gives us a critical chi-square value of 9.488 by the table. Since our calculated chi-square value, 32.894, is larger than the table value, this suggests that we reject the null and claim that prize category and gender are not independent.

Week 19: The Chi-Square Test for Homogeneity

What’s up, y’all? Today we’re going to talk about the chi-square test for homogeneity!

When Would You Use It?
The chi-square test for homogeneity is a nonparametric test used to determine whether or not r independent samples, categorized on a single dimension, are homogeneous with respect to the proportion of observations in each of the c categories.

What Type of Data?
The chi-square test for homogeneity requires categorical or nominal data.

Test Assumptions

  • The data represent a random sample of independent observations.
  • The expected frequency of each cell in the contingency table is at least 5.

Test Process
Step 1: Formulate the null and alternative hypotheses. The data appropriate for this type of test is usually summarized in an r x c table, where r is the number of rows of the table and c is the number of columns of the table (see the example below to get a better understanding of this). The null hypothesis claims that the in the population from which the sample was drawn, the observed frequency of each cell in the table is equal to the respective expected frequencies of each cell in the table. The alternative hypothesis claims that for at least one cell, the observed and expected frequencies are different.

Step 2: Compute the test statistic. The test statistic here, unsurprisingly, a chi-square value. To compute this value, use the following equation:

05-15-2016-a

Eij, the expected cell count for the ijth cell, is calculated as follows:

05-15-2016-b

Step 3: Obtain the critical value. The critical value can be obtained using a chi-square table (such as this one here). Find the column corresponding to your specified alpha-level, then find the row corresponding to your degrees of freedom. The degrees of freedom is calculated as df = (r – 1)(c – 1), where r is the number of rows in the table and c is the number of columns in the table. Compare your obtained chi-square value to the value at the intersection of your selected alpha-level and degrees of freedom.

Step 4: Determine the conclusion. If your test statistic is equal to or greater than the table value, reject the null hypothesis. If your test statistic is smaller than the table value, fail to reject the null (that is, claim that the observed cell frequencies match those of the expected cell frequencies).

Example
The example for this test comes from Amazon. Specifically, I want to see if the number of 4+ star ratings was homogeneous across the six different price ranges for laptop computers. I chose a random sample of n = 15 from each of the six price ranges and determined how many of the 15 laptops selected had four or more stars for their average review. The observed counts are in the following table:

05-15-2016-c

Set α = 0.05.

H0: The proportion of 4+ star ratings is homogeneous across all price ranges
Ha: The proportion of 4+ star ratings is not homogeneous across all price ranges

The expected cell counts, as calculated by the Eij formula above, are displayed in the following table:

05-15-2016-d

Calculating the chi-square value gives us:

05-15-2016-e

The degrees of freedom for this test is df = (6 – 1)(2 – 1) = 5, which gives us a critical chi-square value of 11.070 by the table. Since our calculated chi-square value, 3.54, is smaller than the table value, this suggests that we fail to reject the null and claim that the proportion of 4+ star ratings is the same for each price category.

 

Week 18: The Siegel-Tukey Test for Equal Variability

Today we’re going to talk about another nonparametric test: the Siegel-Tukey test for equal variability!

When Would You Use It?
The Siegel-Tukey test for equal variability is a nonparametric test used to determine if two independent samples represent two populations with different variances.

What Type of Data?
The Siegel-Tukey test for equal variability requires ordinal data.

Test Assumptions

  • Each sample is a simple random sample from the population it represents.
  • The two samples are independent.
  • The underlying distributions of the samples have equal medians.

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the two population variances are equal. The alternative hypothesis claims otherwise (one variance is greater than the other, or that they are simply not equal).

[Note that from here on out, the calculations are exactly the same as for the Mann-Whitney U test. The only thing that differs is how the data are ranked.]

Step 2: Compute the test statistics: U1 and U2. Since this is best done with data, please see the example shown below to see how this is done.

Step 3: Obtain the critical value. Unlike most of the tests we’ve done so far, you don’t get a precise p-value when computing the results here. Rather, you calculate your U values and then compare them to a specific value. This is done using a table (such as the one here). Find the number at the intersection of your sample sizes for both samples at the specified alpha-level. Compare this value with the smaller of your U1 and U2 values.

Step 4: Determine the conclusion. If your test statistic is equal to or less than the table value, reject the null hypothesis. If your test statistic is greater than the table value, fail to reject the null (that is, claim that the variances are equal in the population).

Example
Today’s data come from my 2012 music selection. I wanted to see if the median play counts for two genres—pop and electronic—were the same. I chose these two because I think most of my favorite songs are of one of the two genres. To keep things relatively simple for the example, I sampled n = 8 electronic songs and n = 8 pop songs. Set α = 0.05.

H0: σ2pop = σ2electronic
Ha: σ2pop ≠ σ2electronic

The following table shows several different columns of information. I will explain the columns below.

test17a

Column 1 is the genre of each song.
Column 2 is the play count for each song, ranked from least to greatest
Column 3 is the rank of each play count. In order to obtain the ranks for this test, start by giving a rank of “1” to the lowest play count value. Then a rank of “2” to the highest play count value, a rank of “3” to the second highest play count value, a rank of “4” to the second lowest play count value, etc. (that is, assign ranks by alternating from one extreme to the other).

To compute U1 and U2, use the following equations:

test15b

So here,

test17c

The test statistic itself is the smaller of the above values; in this case, they’re both the same, so we get U = 32. In the table, the critical value for n1 = 8 and n2 = 8 and α = 0.05 for a two-tailed test is 13. Since U > 13, we fail to reject the null and retain the claim that the population variances are equal.

Example in R
No R example this week; most of this is easy enough to do by hand for a small-ish sample.

AP

Yo, fools!

Awhile back (2011), I decided to try taking an online practice version of the AP Statistics exam. I think it went okay (I unfortunately didn’t record my exact final score on the multiple choice, ‘cause I’m a loser), but I decided to try it again because, you know, I’ve actually got a better background for it now.

Well, not the same practice test. I found a newer one (2010 I think?) on which there were fewer multiple choice questions but the questions were decidedly more difficult. This time I ended up getting 16/18 correct (I shouldn’t have missed one of the ones I did; I just completely blanked on how to do it, even though I’ve taught it in lab like 20 times).

Anyway. If you want to give it a shot, it’s located here!

 

Week 17: The Moses Test for Equal Variability

Today we’re going to talk about another nonparametric test: the Moses Test for equal variability!

When Would You Use It?
The Moses Test for equal variability is a nonparametric test used to determine if two independent samples represent two populations with different variances.

What Type of Data?
The Moses Test for equal variability requires ordinal data.

Test Assumptions

  • Each sample is a simple random sample from the population it represents.
  • The two samples are independent.

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the two population variances are equal. The alternative hypothesis claims otherwise (one variance is greater than the other, or that they are simply not equal).

Step 2: Compute the test statistics: U1 and U2. Since this is best done with data, please see the example shown below to see how this is done. [Note that the test statistic calculations are exactly the same as for the Mann-Whitney U test. The only thing that differs is the ranking procedure.]

Step 3: Obtain the critical value. Unlike most of the tests we’ve done so far, you don’t get a precise p-value when computing the results here. Rather, you calculate your U values and then compare them to a specific value. This is done using a table (such as the one here). Find the number at the intersection of your sample sizes for both samples at the specified alpha-level. Compare this value with the smaller of your U1 and U2 values.

Step 4: Determine the conclusion. If your test statistic is equal to or less than the table value, reject the null hypothesis. If your test statistic is greater than the table value, fail to reject the null (that is, claim that the variances are equal in the population).

Example
Today’s data come from my 2012 music selection (I’ll use this data next week, too!). I wanted to see if the median play counts for two genres—pop and electronic—were the same. I chose these two because I think most of my favorite songs are of one of the two genres. To keep things relatively simple for the example, I sampled n = 8 electronic songs and n = 8 pop songs. Set α = 0.05.

H0: σ2pop = σ2electronic
Ha: σ2pop ≠ σ2electronic

Here are the raw data:

17a

The following tables show several different columns of information. I will explain the columns below.

17b

The “Subsample” column: to obtain the rankings for this test, first divide the n1 scores in sample 1 into m1 subsamples (m1 > 1), with each subsample comprised of k scores. Then divide the n2 scores of sample 2 into m2 subsamples (m2 >2), with each subsample comprised of k scores. To form the subsamples, employ sampling without replacement within each of the samples. Ideally, m1, m2, and k should be chosen such that (m1)(k) = n1 and (m2)(k) = n2 if at all possible. Here, m1 = m2 = 4, and k = 2. The “subsample” columns list the four subsamples of play counts for each genre.

The second column contains the average of the k values for a given subset. It’s just the average of the values in the “subsample” column.

The third column contains the differences between each subsample value (X) and the subsample’s mean.

The fourth column is just the third column’s values squared.

The fifth column contains the sum of the values in the fourth column.

The sixth column contains the rank of the value in the fifth column over both genres. The smallest values is ranked as 1 and the largest is ranked as 8 (in this case).

To compute U1 and U2, use the following equations:

17c

So here,

17d

The test statistic itself is the smaller of the above values,  so we get U = 81. In the table, the critical value for n1 = 8 and n2 = 8 and α = 0.05 for a two-tailed test is 13. Since U > 13, we fail to reject the null and retain the claim that the population variances are equal.

Example in R
x=read.table('clipboard', header=T)              #data
attach(x)
elects=subset(x,genre=="Electronic")
pops=subset(x,genre=="Pop")
group1=matrix(rep(NaN,8),nrow=4)
group2=matrix(rep(NaN,8),nrow=4)


group1=as.matrix(sample(elects[,2],8,replace=F)) #subgroups for sample 1
sub1.1=group1[1:2,]
sub1.2=group1[3:4,]
sub1.3=group1[5:6,]
sub1.4=group1[7:8,]


group2=as.matrix(sample(pops[,2],8,replace=F))   #subgroups for sample 1
sub2.1=group2[1:2,]
sub2.2=group2[3:4,]
sub2.3=group2[5:6,]
sub2.4=group2[7:8,]
samps=rbind(sub1.1,sub1.2,sub1.3,sub1.4,sub2.1,sub2.2,sub2.3,sub2.4)


xbars=rep(NaN,8)                                 #column 2
for(i in 1:8){
xbars[i]=mean(samps[i,])}
xbars=as.matrix(xbars)


diffs1=rep(NaN,8)                                #column 3
for(i in 1:8){
diffs1[i]=(samps[i,1]-xbars[i])}
diffs2=rep(NaN,8)                               
for(i in 1:8){
diffs2[i]=(samps[i,2]-xbars[i])}


diffs=cbind(diffs1,diffs2)                       #column 4


diffs2=diffs^2                                   #column 5


sumdif=rep(NaN,8)
for(i in 1:8){
sumdif[i]=diffs2[i,1]+diffs2[i,2]}               #column 6

Week 16: The Kolmogorov-Smirnov Test for Two Independent Samples

[This is coming out on a Monday ’cause I was super busy yesterday and had no time to make this/post it.]

Today’s test is a non-parametric test for two samples: the Kolmogorov-Smirnov test for two independent samples!

When Would You Use It?
The Kolmogorov-Smirnov test for two independent samples is a nonparametric test used to determine if two independent samples represent two different populations.

What Type of Data?
The Kolmogorov-Smirnov test for two independent samples requires ordinal data.

Test Assumptions

  • All of the observations in the samples are randomly selected and independent of one another.
  • The scale of the measurement is ordinal.

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the the distribution underlying the population for one sample is the same as the distribution underlying the population for the other sample. The alternative claims that the distributions are not the same.

Step 2: Compute the test statistic. The test statistic, in the case of this test, is defined by the point that represents the greatest vertical distance at any point between the cumulative probability distribution constructed from the first sample and the cumulative probability distribution constructed from the second sample. I will refer you to the example shown below to show how these calculations are done in a specific testing situation.

Step 3: Obtain the critical value. Unlike most of the tests we’ve done so far, you don’t get a precise p-value when computing the results here. Rather, you calculate your test statistic and then compare it to a specific value. This is done using a table. Find the number at the intersection of your sample sizes for your specified alpha-level. Compare this value with your test statistic.

Step 4: Determine the conclusion. If your test statistic is equal to or larger than the table value, reject the null hypothesis (that is, claim that the distribution of the data is inconsistent with the hypothesized population distribution). If your test statistic is less than the table value, fail to reject the null.

Example
For this test’s example, I want to use some of my music data from 2012. I know that I tend to listen to music from the “electronic” genre and from the “dance” genre fairly equally, so I want to determine, based on play count, if I can say that the population distributions for these genres are similar. To keep things simple, I will use nelectronic = 6 and ndance = 6.

H0: Felectronic(X) = Fdance(X) for all values of X
Ha: Felectronic(X) ≠ Fdance(X) for at least one value of X

Computations:
For the computations section of this test, I will display a table of values for the data and describe what the values are and how the test statistic is obtained.

test16

Column A and Column C, together, show the ranked values of the play counts for electronic (Column A) and dance (Column C).
Column B represents the cumulative proportion in the sample for each play count in Column A. For example, for the play count = 7, the cumulative proportion of that value is just 1/6, since there is no smaller value in Column A.
Column D represents the same thing as column B, except for Column C.
Column E is Column B – Column D.

The test statistic is obtained by determining the largest value from Column E. Here, the test statistic is .5. This value is compared to the critical value at α = 0.05, n1 = 6, n2 = 6, which is .667. Since our test statistic is not larger than our critical value, we fail to reject the null and claim that the distributions of play counts for electronic and dance are similar.

Example in R
No R example this week, as this is pretty easy to do by hand, especially with having to rank things.

Week 15: The Mann-Whitney U Test

Today we’re going to talk about another nonparametric test: the Mann-Whitney U test!

When Would You Use It?
The Mann-Whitney U test is a nonparametric test used to determine if two independent samples represent two populations with different medians.

What Type of Data?
The Mann-Whitney U test requires ordinal data.

Test Assumptions

  • Each sample is a simple random sample from the population it represents.
  • The two samples are independent.
  • The original scores obtained are continuous random variables (which are later ranked).
  • The underlying distributions of the samples are identical in shape (but do not necessarily have to be normal).

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the two population medians are equal. The alternative hypothesis claims otherwise (one median is greater than the other, or that they are simply not equal).

Step 2: Compute the test statistics: U1 and U2. Since this is best done with data, please see the example shown below to see how this is done.

Step 3: Obtain the critical value. Unlike most of the tests we’ve done so far, you don’t get a precise p-value when computing the results here. Rather, you calculate your U values and then compare them to a specific value. This is done using a table (such as the one here). Find the number at the intersection of your sample sizes for both samples at the specified alpha-level. Compare this value with the smaller of your U1 and U2 values.

Step 4: Determine the conclusion. If your test statistic is equal to or less than the table value, reject the null hypothesis. If your test statistic is greater than the table value, fail to reject the null (that is, claim that the medians are equal in the population).

Example
Today’s data come from my 2012 music selection. I wanted to see if the median play counts for two genres—pop and electronic—were the same. I chose these two because I think most of my favorite songs are of one of the two genres. To keep things relatively simple for the example, I sampled n = 8 electronic songs and n = 8 pop songs. Set α = 0.05.

H0: θpop = θelectronic
Ha: θpop ≠ θelectronic

The following table shows several different columns of information. I will explain the columns below.

test15a

Column 1 is the genre of each song.
Column 2 is the play count for each song.
Column 3 is the overall rank of the play count, regardless of which genre it came from.

If there had been ties, I would have summed the number of ranks that were taken by the ties, and then divide that value by the number of ties.

To compute U1 and U2, use the following equations:

test15b

So here,

test15c

The test statistic itself is the smaller of the above values; in this case, we get U = 28. In the table, the critical value for n1 = 8 and n2 = 8 and α = 0.05 for a two-tailed test is 13. Since U > 13, we fail to reject the null and retain the claim that the population medians are equal.

Example in R
No R example this week; most of this is easy enough to do by hand for a small-ish sample.

Week 14: The Median Absolute Deviation Test for Identifying Outliers

Today we’re going do something a little bit different by talking about the median absolute deviation test for identifying outliers!

When Would You Use It?
The median absolute deviation test for identifying outliers is used to determine whether or not a specific sore in a sample of n observations should be classified as an outlier.

What Type of Data?
The median absolute deviation test for identifying outliers requires interval or ratio data.

Test Assumptions
None listed.

Test Process
The equation employed in this test is as follows:

Step 1: Compute the median M of the dataset.

Step 2: Compute the median absolute deviation, or MAD. To do so:

a) Calculate the absolute values of the difference between each score and the median.
b) Arrange these absolute deviations in order from lowest to highest.
c) Find the median of these absolute deviations; this is the MAD value.

Step 3: Determine the Max value. While the selection of this value is somewhat arbitrary, a recommended value is to set Max = 5. This is because if the data are assumed to come from an approximately normal distribution, this value will be very likely to identify extreme or outlier scores.

Step 4: Plug in each X value into the equation to determine if it is an outlier. X is an outlier if the left-hand side of the equation exceeds the Max value. If doing this test by hand, the best way to go about this step is to start with the X that deviates the most from the median and work down from there, but if using a program, it’s easy enough to just test them all at once.

Example
Today’s data is from my 2013 music. I have the lengths (in seconds) of all n = 365 songs from that year, and I want to determine which values are outliers.

Computations:
M = 226
MAD = 36

With Max = 5, I found that the songs with the following lengths are outliers:

891
564
636
516
580
534
597
574
537
595
486

This was done using R; the code is below.

Example in R

x = read.table('clipboard', header=T) #data
M = median(x)
absdev = abs(x-M)
MAD = median(absdev)
Max = 5
for (i in 1:length(x)){       # if an x value is an outlier, this loop will
dev = (abs(x[i]-M))/MAD       # print its value
if (dev > 5) { print(x[i]) }}

Week 13: F Test for Two Population Variances

Today we’re going to talk about variances. Specifically, the F test for two population variances!

When Would You Use It?
The F test for two population variances is a parametric test used to determine if two independent samples represent two populations with homogeneous (similar) variances.

What Type of Data?
The F test for two population variances requires interval or ratio data.

Test Assumptions
None listed.

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the two population variances are equal. The alternative hypothesis claims otherwise (one population variance is greater than the other, less than the other, or that the variances are simply not equal).

Step 2: Compute the test statistic, an F value. The test statistic is computed as follows:

test13a

Step 3: Obtain the p-value associated with the calculated F value. The p-value indicates the probability of a difference in the two sample variances that is equal to or more extreme than the observed difference between the sample variances, under the assumption that the null hypothesis is true. The two degrees of freedom associated with the F value are df1 = n1-1 and df2 = n2-1, where n1 and n2 are the respective sample sizes of the first and second sample.

Step 4: Determine the conclusion. If the p-value is larger than the prespecified α-level, fail to reject the null hypothesis (that is, retain the claim that the population means are equal). If the p-value is smaller than the prespecified α-level, reject the null hypothesis in favor of the alternative.

Example
The data for this example come from my walking data from 2013 and 2015. I want to see if there is a significant difference in the mileage variance for these two years (in other words, was I less consistent with the length of my walks in one year versus the other? Set α = 0.05.

H0: σ12 = σ22 (or σ12 – σ22 = 0)
Ha: σ12 ≠ σ22 (or σ12 – σ22 ≠ 0)

Computations:

test13b

Since our p-value is smaller than our alpha-level, we reject H0 and claim that the population variances are significantly different (with evidence in favor of the variance being higher for 2015).

Example in R

year2013=read.table('clipboard', header=F) #data from 2013
year2015=read.table('clipboard', header=F) #data from 2015
s1=var(year2013)
s2=var(year2015)
df1=length(year2013)-1
df2=length(year2015)-1
F=s2/s1                        #test statistic  
pval = (1-pf(F, df2, df1))     #p-value

Week 12: The t Test for Two Independent Samples

Today we’re going to talk about our first test involving two samples: the t test for two independent samples!

When Would You Use It?
The t test for two independent samples is a parametric test used to determine if two independent samples represent two populations with different mean values.

What Type of Data?
The t test for two independent samples requires interval or ratio data.

Test Assumptions

  • Each sample is a simple random sample from the populations they represent.
  • The distributions underlying each of the populations are normal.
  • The variances of the underlying populations are equal (homogeneity of variance; a formal test for this will come in a later week).

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the two sample means are equal. The alternative hypothesis claims otherwise (one population mean is greater than the other, less than the other, or that the means are simply not equal).

Step 2: Compute the t-score. The t-score is computed as follows:

test12

Step 3: Obtain the p-value associated with the calculated t-score. The p-value indicates the probability of a difference in the two sample means that is equal to or more extreme than the observed difference between the sample means, under the assumption that the null hypothesis is true.

Step 4: Determine the conclusion. If the p-value is larger than the prespecified α-level, fail to reject the null hypothesis (that is, retain the claim that the population means are equal). If the p-value is smaller than the prespecified α-level, reject the null hypothesis in favor of the alternative.

Example
The data for this example come from the midterm scores of my lab section for STAT 213. While lab attendance is technically optional, the students’ attendance is recorded for each lab (if they show up to lab, they basically get additional instructional materials unlocked to help them study more).

I wanted to see if there was a significant difference in the average midterm score for students who attended lab at least half the time (sample 1) and students who attended lab less than half the time (sample 2). Specifically, I wanted to test the claim that attending lab more frequently was associated with a higher midterm score. Here, n1 = 17 and n2 = 13. Set α = 0.05.

H0: µ1 = µ2 (or µ1 – µ2 = 0)
Ha: µ1 > µ2 (or µ1 – µ2 > 0)

Computations:

test12b

Since our p-value is smaller than our alpha-level, we reject H0 and claim that the population means are significantly different (with evidence in favor of the mean being higher for those attending labs more often).

Example in R

x=read.table('clipboard', header=T)
attach(x)
x1=subset(x,attended==1)[,1]                 #attended lab
x2=subset(x,attended==0)[,1]                 #did not attend lab
n1=length(x1)
n2=length(x2)
xbar1=mean(x1)
xbar2=mean(x2)
s1=((sum(x1^2)-(((sum(x1))^2)/n1))/(n1-1))
s2=((sum(x2^2)-(((sum(x2))^2)/n2))/(n2-1))
t = (xbar1 - xbar2)/sqrt(((((n1-1)*s1)
    +((n2-1)*s2))/(n1+n2-2))*((1/n1)+(1/n2))) #test statistic
pval = (1-pt(t, n-1))                         #p-value

Week 11: The Single-Sample Runs Test

Today we’re going to look at another nonparametric test: the single-sample runs test.

When Would You Use It?
The runs test is a nonparametric test used in a single sample situation to determine if the distribution of a series of binary events, in the population, is random.

What Type of Data?
The single sample runs test requires categorical (nominal) data.

Test Assumptions
None listed.

Test Process|
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the events in the underlying population (represented by the sample series) are distributed randomly. The alternative hypothesis claims that the events in the underlying population are distributed nonrandomly.

Step 2: Compute the number of times each of the two alternatives appears in the sample series (n1 and n2) and the number of runs, r, in the series. A run is a sequence within the series in which one of the alternatives occurs on consecutive trials.

Step 3: Obtain the critical value. Unlike most of the tests we’ve done so far, you don’t get a precise p-value when computing the runs test results. Rather, you calculate your r and then compare it to an upper and lower limit for your specific n1 and n2 values. This is done using a table (such as the one here*). For the values of n1 and n2 in your sample (labeled on this table as n and m), find the intersecting cell of the two values.

Step 4: Determine the conclusion. If your r is greater than or equal to the larger number or smaller than or equal to the smaller number in that cell, you have a statistically significant result, meaning you reject the null (that is, reject the claim that the distribution of the binary events in the population is nonrandom). If your r is between the smaller and larger number, fail to reject the null.

Example
For this example, I decided to see if the coin flips from the website Just Flip a Coin were, in fact, random. I “flipped” the coin a total of 30 times and recorded my results as “H” for heads and “T” for tails. This series is recorded below.

H0: The distribution of heads and tails in the population is random
Ha: The distribution of heads and tails in the population is nonrandom.

THTTHHHHHHTHTTHHTTHHTHHHTHHHHT

Computations:

T  H  TT  HHHHHH  T H  TT  HH  TT  HH  T  HHH  T  HHHH  T

n1 = 19 (heads)

n2 = 11 (tails)

r = 15

According to the table, my lower bound is 9 and my upper bound is 21. Since my r is in between these two values, I do not have a statistically significant result. I fail to reject H0 and claim that the distribution of heads and tails in the population is indeed random.

*Note that this table, like many others, only has a maximum of 20 for either n or m, and is constructed with α = 0.05 for the two-sided test and α = 0.025 for the one-sided test.

Example in R
No R example this week, since it’s probably more work to do this in R than it is to do it by hand, haha.

Week 10: The z Test for a Population Proportion

Today it’s time for yet another test: the z test for a population proportion!

When Would You Use It?
The z test for a population proportion is used to determine if, in an underlying population comprised of two categories, the proportion of observations (in a sample) in one of the two categories is equal to a specific value.

What Type of Data?
The z test for a population proportion requires categorical (nominal) data.

Test Assumptions

  • Each of the n independent observations is randomly selected from a population, and each observation can be classified into one of two mutually exclusive categories.
  • It is recommended that this test is employed when the sample is not too small (n > 11).

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the likelihood an observation will fall into Category 1 in the population is equal to a certain probability. The alternative hypothesis claims otherwise (that the population proportion for Category 1 is not equal to the value stated in the null).

Step 2: Compute the test statistic, a z-score. The test statistic is computed as follows:

test10

Step 3: Obtain the p-value associated with the calculated z-score. The p-value indicates the probability of observing a sample proportion as extreme or more extreme than the observed sample proportion, under the assumption that the null hypothesis is true.

Step 4: Determine the conclusion. If the p-value is larger than the prespecified α-level, fail to reject the null hypothesis (that is, retain the claim that the probability of falling into Category 1 in the population is equal to the value specified in the null hypothesis). If the p-value is smaller than the prespecified α-level, reject the null hypothesis in favor of the alternative.

Example
The data for this example come from my n = 30 students from one of my STAT 213 labs. I want to test the hypothesis, based on my lab, that the proportion of students who come to lab for ALL the 213 labs (for this particular instructor) is 40%. We take attendance, so we know who’s there and who’s not for a given week (note, however, that I’m going to randomly select an attendance sheet from one of the weeks of lab, AND I actually never pay attention to what the proportion of students who show up is, so I’m really just guessing about 40%). So let’s do it! Set α = 0.05 and let π denote the proportion of individuals to come to lab.

H0: π = .4
Ha: π ≠ .4

Computations:

test10b

Since our p-value is (only slightly) larger than our alpha-level, we fail to reject H0 and claim that the population proportion of students who attend labs is, in fact, .4.

Example in R

dat = read.table('clipboard',header=F) #'dat' is the name of the imported raw data
                                       #'dat' coded such that 0 = did not attend, 1 = attended
n = nrow(dat)
X = sum(dat)
p1 =  X/n
pi1 = .4
z = (p1-pi1)/(sqrt((pi1*(1-pi1))/n))  #z-score                  
pval = (1-pnorm(z))*2                 #p-value
                                      #pnorm calculates the left-hand area
                                      #multiply by two because it is a two-sided test

Week 9: The Binomial Sign Test for a Single Sample

Today we’re going to look at another nonparametric test: the binomial sign test for a single sample!

When Would You Use It?
The binomial sign test is used in a single sample situation to determine, in a population comprised of two categories, if the proportion of observations in one of the two categories is equal to a specific value.

What Type of Data?
The binomial sign test requires categorical (nominal) data.

Test Assumptions
None listed.

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the true proportion of observations in one of the two categories, in the population, is equal to a specific value. The alternative hypothesis claims otherwise (the proportion is either greater than, less than, or not equal to the value claimed in the null hypothesis.

Step 2: Compute the test statistic, a probability value. The test statistic is calculated as follows:

test9

 

That is, your test statistic is the probability of attaining ≥ r observations from “category 1” in a sample of size n, where r is the number of observations from “category 1” in your original sample.

While you can calculate this value by hand, it may be easier to either use a table or an online calculator.

Step 3: Obtain the p-value. If your alternative hypothesis is non-directional (that is, uses “≠”), your p-value is equal to α/2. If your alternative hypothesis is directional (uses “>” or “<”, your p-value is simply equal to α.

Step 4: Determine your conclusion. This depends on your alternative hypothesis. Let p1 denote the sample proportion of observations falling into “category 1”, and let P denote the test statistic value, as calculated above.

If it is nondirectional (≠) reject H0 if P < α/2.
If the alternative hypothesis is directional (>), reject H0 if p1 > π1 and P < α.
If the alternative hypothesis is directional (<), reject H0 if p1 < π1 and P < α.

Example
For this example, I decided to see if the coin flips from the website Just Flip a Coin were, binomially distributed with π1 = π2 = 0.5. I “flipped” the coin a total of 30 times and recorded my results for “category 1” (heads) and “category 2” (tails). The outcomes are displayed in the table below.

H0: π1 = 0.5
Ha: π1 ≠ 0.5

Set α = 0.05.

Computations:

test9b

 

test9c

Since P = 0.1002 > 0.025 (α/2 = 0.05/2 = 0.025), we fail to reject H0, the claim that proportion of the number of heads is equal to 0.5 in the population.

Example in R

x = read.table('clipboard', header=F)
pi1= 0.5                                 #hypothesized probability for "heads"
n = length(as.matrix(x))
tab = as.data.frame(table(x))            #observed frequencies
p1 = tab[1,2]
P = 1 - pbinom(p1, size = n, prob = pi1) #test statistic

Week 8: The Chi-Square Goodness-of-Fit Test

Today we’re continuing the theme of nonparametric tests with the chi-square goodness-of-fit test!

When Would You Use It?
The chi-square goodness-of-fit test is a nonparametric test used in a single sample situation to determine if a sample originates from a population for which the observed cell (category) frequencies different from the expected cell (category) frequencies. Basically, this test is used when a particular “distribution” is expected of the categories of a variable and a researcher wants to know if that distribution fits the data.

What Type of Data?
The chi-square goodness-of-fit test requires categorical (nominal) data.

Test Assumptions

  • Categorical or nominal data are used in the analysis (the data should represent frequencies for mutually exclusive categories).
  • The data consist of a random sample of n observations.
  • The expected frequency (as calculated below) of each cell is 5 or greater.

Test Process
Step 1: Formulate the null and alternative hypotheses. The null hypothesis claims that the observed frequency of each cell is equal to the expected frequency of that cell, while the alternative hypothesis claims that at least one cell has an observed frequency that differs from the expected frequency.

Step 2: Compute the test statistic, a chi-square value. The calculations are as follows:

Test8

Step 3: Obtain the p-value associated with the calculated chi-square. The p-value indicates the probability of observing deviations from the expected values that are larger than those in the observed sample, under the assumption that the null hypothesis is true.

Step 4: Determine the conclusion. If the p-value is larger than the prespecified α-level, fail to reject the null hypothesis (that is, retain the claim that the observed frequencies in the cells are equal to the expected frequencies of the cells). If the p-value is smaller than the prespecified α-level, reject the null hypothesis in favor of the alternative.

Example
For this test’s example, I wanted to determine if the dice on my iPod Yahtzee app were fair. That is, I wanted to see if there was an equal probability for all six sides to come up on any given roll. So I rolled the five dice 115 times (for a total of n = 575 individual die rolls) and simply recorded the faces showing (note that I did not actually “play” Yahtzee while doing this, which is to say that I just kept rolling all five dice no matter what happened and I didn’t “hold” any of them at any point). I’m going to claim in my null hypothesis that all six side values have an equal probability of showing on a given die. That is,

H0: p1 = p2 = p3 = p4 = p5 = p6 = 1/6
Ha: The probability for at least one value is not 1/6

Set α = 0.05.

Computations:

Test8b

Since our p-value is larger than our alpha level of .05, we fail to reject H0 and claim that the observed values are equal to the expected values and the dice are fair.

Example in R

x=read.table('clipboard', header=F)
n=length(x)
tab=as.data.frame(table(x))           #observed frequencies
p=1/length(tab$x)
Chi=rep(NaN,length(tab$x))
for (i in 1:length(tab$x)){
Chi[i]=((tab$Freq[i]-(p*n))^2)/(p*n)
}
Chisquare=sum(Chi)
1-pchisq(Chisquare,length(tab$x)-1)   #p-value