2.5  Examples in SAS/R: Dice Rolls & Tomato
Example  Dice Rolls
Suppose that we roll dice 30 times and observe the following table showing the number of times each face ends up on top.
Face

Count

1

3

2

7

3

5

4

10

5

2

6

3

Total

30

We want to test the null hypothesis that the dice is fair. Under this hypothesis, X ∼ Mult(n = 30, π_{0}) where π_{0} = (1/6 , 1/6 , 1/6 , 1/6 , 1/6 , 1/6 ).
This is our assumed model, and under this H_{0}, the expected counts are E_{j} = 30*1/6= 5 for each cell j. One way to calculate the difference between what we are assuming and what we are observing is to evaluate the the goodnessoffit statistics as discussed in the previous section.
\begin{eqnarray*}
X^2 &= & \dfrac{(35)^2}{5}+\dfrac{(75)^2}{5}+\dfrac{(55)^2}{5}\\
& & +\dfrac{(105)^2}{5}+\dfrac{(25)^2}{5}+\dfrac{(35)^2}{5}\\
&=& 9.2
\end{eqnarray*}
\begin{eqnarray*}
G^2 &=& 2\left(3\text{log}\dfrac{3}{5}+7\text{log}\dfrac{7}{5}+5\text{log}\dfrac{5}{5}\right.\\
& & \left.+ 10\text{log}\dfrac{10}{5}+2\text{log}\dfrac{2}{5}+3\text{log}\dfrac{3}{5}\right)\\
&=& 8.8
\end{eqnarray*}
Note that numerical values of Χ^{2} and G^{2} can be different. We can calculate the pvalues for these statistics in order to help determine how well this model fits. The pvalues are P(χ^{2}_{5} ≥ 9.2) = .10 and P(χ^{2}_{5} ≥ 8.8) = .12. Given these pvalues, with the critical value or Type I error of α=0.05, we fail to reject the null hypothesis. But rather than relying on pvalues, we should also think about the actual values of X^{2} and G^{2}statistics. Small values imply a good fit here, i.e., the observed values are close to what you had assumed. Large values are going to imply the opposite. In this case, the fairdice model doesn't fit the data very well, but the fit isn't bad enough to conclude beyond a reasonable doubt that the dice is unfair.
Next, we show how to do this in SAS and R; you can run either.
The following SAS code, dice_rolls.sas, will perform the goodnessoffit test for the example above.
Here is the output generated by this program, and the headings are selfexplanatory:
Notice that this SAS code only computes the Pearson chisquare statistic and not the deviance statistic.
Can you identify the relevant statistics and the pvalue in the output? What does the column labeled "Percent" represent? 
The following R code, dice_rolls.R will perform the same analysis as what we just did in SAS. You can copy the entire code in the R window and your output will be saved into two files, dice_rolls.out and dice_rolls_Results. If you are new to R, however, I suggest you run this line by line to get more familiar with the commands; you can simply copy and past into the Rterminal. For other options, see a very basic intro to R.
dice_rolls.out (part of the output)

Here are a few additional comments regarding the above R code and its output. If you run Inspect, the data there will be labeled as "ex7" where in the updated code the label is "dice", but it's the same data. The last part of the R code does the same analysis, but gives an output that looks more like what SAS produces; we did this to show a bit more programming in R and how you can play with creating different outputs.
IMPORTANT! The Pearson chisquared statistic is X^{2} = 9.2 (pvalue=0.101), the deviance statistic (from the R output) is G^{2}=8.78 (pvalue=0.118), and they both follow the chisquared sampling distribution with df=5. It is not uncommon to observe the discrepancy in the values between these two statistics especially for the smaller sample sizes. Notice, however, that in this case they do lead to the same conclusion  there is a moderate evidence that the dice is fair.
Can you identify the relevant statistics and the pvalue in the output? What does the column labeled "Percentage" in dice_rolls.out represent? 
Example  Tomato Phenotypes
Tall cutleaf tomatoes were crossed with dwarf potatoleaf tomatoes, and n = 1611 offspring were classified by their phenotypes.
Phenotypes

Count

tall cutleaf

926

tall potatoleaf

288

dwarf cutleaf

293

dwarf potatoleaf

104

Genetic theory says that the four phenotypes should occur with relative frequencies 9 : 3 : 3 : 1, and thus are not all equally as likely to be observed. The dwarf potatoleaf is less likely to observed than the others. Do the observed data support this theory?
Under the null hypothesis, the probabilities are
π_{1} = 9/16 , π_{2} = π_{3} = 3/16 , π_{4} = 1/16 ,
and the expected frequencies are
E_{1} = 1611(9/16) = 906.2,
E_{2} = E_{3} = 1611(3/16) = 302.1, and
E_{4} = 1611(1/16) = 100.7.
We calculate the fit statistics and find that X^{2} = 1.47 and G^{2} = 1.48, which are nearly identical. The pvalues based on the χ^{2} distribution with 3 d.f. are approximately equal to 0.69. Therefore, we fail to reject the null hypothesis and the data are consistent with the genetic theory. Again, the small values of these statistics imply that the fit between the data and the proposed model is good!
Here is how to do the computations in R using the following code :
This has stepbystep calculations and using of builtin chisq.test() with producing some nice output including Pearson and deviance residuals.
Here is the above analysis done in SAS:
Do you recall what the residuals are from linear regression? How would you define them in this context? What do they tell you about the tomato example? 