Introduction to Statistics (MAT/SST 115.03 2008S)

R Notes for Topic 24: Goodness-of-Fit Tests


To do chi-squared tests in R, you use the chisq.test function. A typical form of chi-squared test (and the kind that matches the form we see in this book) looks like the following:

chisq.test(vector-of-observations, p=vector-of-probabilities)

For example, for the first chi-squared test of birthdays, we might write

chisq.test(c(17,26,22,23,19,15,25), p=c(1/7,1/7,1/7,1/7,1/7,1/7,1/7))

Similarly, for the second test (Activity 24-2), we might write

chisq.test(c(17,26,22,23,19,15,25), p=c(1/6,1/6,1/6,1/6,1/6,1/12,1/12))

Similarly, for the third test (Activity 24-3), we might write

chisq.test(c(170,260,220,230,190,150,250), p=c(1/7,1/7,1/7,1/7,1/7,1/7,1/7))

By the time you hit 24-4 d, you should know what to do.

24-5

We'll start by creating vectors of counts and probabilities. (Just so you know, cld stands for “country population leading digit”.)

cpld = c(55,36,22,23,15,14,12,12,5)
benford = c(.301,.176,.125,.097,.079,.067,.058,.051,.046)

Those vectors make many things easier. To compute proportions, we just divide each value in cpld by the total population.

cpld/194

Similarly, to compute expected values, we multiply the Benford probabilities by 194.

benford*194

Calculating the test statistic and p-value:

chisq.test(cpld, p=benford)

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-8 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.