Alex
Alex

Reputation: 65

How to calculate a Chi square in R programming?

This is my data frame called mydata.

                   Denomination     
Attendance    Protestant Catholic Jewish
Regular          182      213    203
Irregular        154      138    110

I want to calculate the hypothesis for this through a chi square test. So i set my hypothesis like this:

H0: There is no relation between denomination and attendance

H1: There is a relation between denomination and attendance.

I've tried by calculating actual value and expected value and compare them to calculate chi square. But didn't get the output.

The codes I've tried:

> rowSums(mydata,na.rm = FALSE,dims = 1L)
> colSums(mydata,na.rm = FALSE,dims = 1L)
> sum(mydata)
> e = rowSums(mydata) * colSums(mydata)/ 1000
> chisq.test(mydata) = sum((mydata-e)^2 / e)

But I didn't get the result. Please suggest?

Upvotes: 0

Views: 69

Answers (1)

akash87
akash87

Reputation: 3994

A couple of things to note here:

  1. chisq.test can only have numeric columns (or integer). The way you have displayed the data, it seems as though Attendance is its own column as opposed to the rowname.
  2. You don't need to calculate rowSums or colSums to use the test.

Here is the code I ran:

dfs <- data.frame(Attendance = c("Regular", "Irregular"), Protestant = c(182, 154), Catholic = c(213, 138), Jewish = c(203,110))
rownames(dfs) <- dfs[,1]
dfs$Attendance <- NULL
chisq.test(dfs)

    Pearson's Chi-squared test

data:  dfs
X-squared = 7.8782, df = 2, p-value = 0.01947

Upvotes: 2

Related Questions