# Chi-squared test for frequencies. # # R's 'chisq.test' provides a p-value for the chi-squared test for frequencies # by taking in a table of frequencies and an optional list of expected frequencies. # Here we'll run the two examples in the chi_2_test_frequencies_tutorial # Load in the survey data survey <-read.csv("http://www.courses.washington.edu/psy315/datasets/Psych315W19survey.csv") # Example 1: left vs. right handers in our class, compared to 10% left handers fo <- table(survey\$hand) # observed frequencies fe = c(.1,.9) # expected frequencies # run the chi-squared test: out <- chisq.test(fo,p=fe) # The chi-squared statistic is: out\$statistic # The degrees of freedom is: out\$parameter # And the p-value is: out\$p.value # Writing in APA format can be done like this: sprintf('Chi-Squared(%d,N=%d) = %5.2f, p = %5.4f',out\$parameter,sum(fo),out\$statistic,out\$p.value) # Plot the results: barplot(fo) # Example 2: Birthdays by month fo <- table(survey\$month) # Run the chi-squared test. If we don't specify the expected frequency the # test assumes that expected frequencies are equal across categories. out <- chisq.test(fo) # result in APA format: sprintf('Chi-Squared(%d,N=%d) = %5.2f, p = %5.4f',out\$parameter,sum(fo),out\$statistic,out\$p.value) # Plotting: first rearrange months in order fo <- fo[c(5,4,8,1,9,7,6,2,12,11,10,3)] # Then plot: barplot(fo) # If you only have the chi-squared statistic and degrees of freedom # you can use the 'pchisq' function to get the p-value. We use # lower.tail = FALSE to reject H0 for large values of chi-squared: chi.squared <- out\$statistic df <- out\$parameter p.value <- pchisq(chi.squared,df,lower.tail = FALSE) p.value