Session Ready
Exercise

Chi-Square I

So now you have your expected values saved as expDat, observed values saved as data and your residuals (i.e. the difference between your expected and observed values) saved as resid. These are more than enough ingredients to calculate your Chi square value!

If you remember, the formula for Chi-square is $$\sum\frac{(observed_i- expected_i)^2}{expected_i}$$. If we did this by hand, you would have to do a calculation for each cell of your table, and then sum it. Luckily for us, we can do it all in one go with R!

To square the expected value minus the observed value, and divide by the expected value, you can simply square the whole table of residuals and divide this by the table of observed values. If you do this inside the sum() function, it will sum these values, resulting in the Chi-square value. To use sum() you simply put your values between brackets. For instance sum(data) would add up all the values in data and give you 64!

Instructions
100 XP
  • Calculate the Chi-square value for data.
  • Remember, you can square a value using ^2 - for instance, 3^2 would return 9.