ChiSquared Goodness of Fit Test
The chisquare goodness of fit test assesses whether the observed frequencies fit a specified distribution.
Example:
A University conducted a survey of its recent graduates to collect demographic and health information for future planning purposes as well as to assess students' satisfaction with their undergraduate experiences. The survey revealed that a substantial proportion of students were not engaging in regular exercise, many felt their nutrition was poor and a substantial number were smoking. In response to a question on regular exercise, 60% of all graduates reported getting no regular exercise, 25% reported exercising sporadically and 15% reported exercising regularly as undergraduates. The next year the University launched a health promotion campaign on campus in an attempt to increase health behaviors among undergraduates. The program included modules on exercise, nutrition and smoking cessation. To evaluate the impact of the program, the University again surveyed graduates and asked the same questions. The survey was completed by 470 graduates and the following data were collected on the exercise question:

No Regular Exercise 
Sporadic Exercise 
Regular Exercise 
Total 
Observed # 
255 
125 
90 
470 
Based on the data, is there evidence of a shift in the distribution of responses to the exercise question following the implementation of the health promotion campaign on campus?
In this example, we have one sample and a discrete (ordinal) outcome variable (with three response options). We specifically want to compare the distribution of responses in the sample to the distribution reported the previous year (i.e., 60%, 25%, 15% reporting no, sporadic and regular exercise, respectively). We now run the test using the fivestep approach.
First, we set up the hypotheses and determine level of significance. The null hypothesis again represents the "no change" or "no difference" situation. If the health promotion campaign has no impact then we expect the distribution of responses to the exercise question to be the same as that measured prior to the implementation of the program.
H_{0}: p_{1}=0.60, p_{2}=0.25, p_{3}=0.15, or equivalently H_{0}: The distribution of responses is 0.60, 0.25, 0.15
H_{1}: H_{0} is false. α =0.05
Notice that the research hypothesis is written in words rather than in symbols. The research hypothesis as stated captures any difference in the distribution of responses from that specified in the null hypothesis. We do not specify a specific alternative distribution, instead we are testing whether the sample data "fit" the distribution in H_{0} or not. With the χ^{2} goodnessoffit test there is no upper or lower tailed version of the test.
Based on the expected distribution, we can calculate the number of students we would have expected to see in each exercise category.

No Exercise 
Sporadic Exercise 
Regular Exercise 
Total 

# Observed 
255 
125 
90 
470 
# Expected 
282 
117.5 
70.5 
470 
Then, for each category, we compute (OE)^{2}/E.

No Exercise 
Sporadic Exercise 
Regular Exercise 
Total 

# Observed 
255 
125 
90 
470 
# Expected 
282 
117.5 
70.5 
470 
(OE)^{2}/E 
2.59 
0.48 
5.39 
8.46 
Since there are three categories, the degrees of freedom = 2. (df=k1=31=2).
From the chisquared table above we find that the critical value is 5.99 for 2 degrees of freedom at the α=0.05 level. Since 8.46 is greater than the critical value, we reject the null hypothesis, and conclude that the distribution of exercise has changed; it is no longer 60%, 25%, 15%.
Goodness of Fit ChiSquared Test with R
The analysis above could also be performed using R by providing the observed number of responses and the expected frequencies as follows:
> obs <c(255,125,90)
> null_p<c(0.60,0.25,0.15)
> chisq.test(obs,p=null_p)
Chisquared test for given probabilities
data: obs
Xsquared = 8.4574, df = 2, pvalue = 0.01457