1
$\begingroup$

So I have data in form (in real data I have more rows, of course so this is just a sample)

>df <- structure(list(cake.eated = c(1, 0, 0, 1, 1, 1), is.it.weekend = c(1, 1, 1, 0, 1, 0)), 
+ .Names = c("is.cake.eated","is.it.weekend"), row.names = c(NA, -6L), class = "data.frame")
>df

 is.cake.eated is.it.weekend
         1             1
         0             1
         0             1
         1             0
         1             1
         1             0

and I woulf like to perfotm Fisher's test to it. In contigency-table form my data looks like

>c = matrix(c(2205,2605,1442,1042), ncol=2)
>rownames(c) <- c("cake.eated", "cake.not.eated")
>colnames(c) <- c("not.weekend", "weekend")
>c

                  not.weekend  weekend
 cake.eated            2205    1442
 cake.not.eated        2605    1042

So my question is, which one of these is the right way to use Fisher's test in R;

> fisher.test(c)

      Fisher's Exact Test for Count Data

data:  c
p-value < 2.2e-16
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.5539554 0.6753183
sample estimates:
odds ratio 
0.6117068 

OR

>fisher.test(df$is.cake.eated, df$is.it.weekend)
      Fisher's Exact Test for Count Data

data:  df$is.cake.eated and df$is.it.weekend
p-value = 1
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.8607512 1.1611740
sample estimates:
odds ratio 
     1 

Is Fisher test even okay to use in this case an this kind of data or is some else like chi-squared better? Why are the results from fisher tests so different and what am I doing wrong?

> chisq.test(df$is.cake.eated, df$is.it.weekend)

    Pearson's Chi-squared test with Yates' continuity correction

data:  df$is.cake.eated and df$is.it.weekend
X-squared = 1.781e-06, df = 1, p-value = 0.9989

Also, by Barnard test my results look like

> barnard.test(2205, 1442, 2605, 1042, dp=0.01)

       Barnard's Unconditional Test

       Treatment I Treatment II
Outcome I         2205         1442
Outcome II        2605         1042

Null hypothesis: Treatments have no effect on the outcomes
Score statistic = 9.88313
Nuisance parameter = 0.03 (One sided), 0.97 (Two sided)
P-value = 7.65658e-22 (One sided), 7.65658e-22 (Two sided)
$\endgroup$
  • $\begingroup$ Where does the rain.zeros data come from (where you run fisher.test(df$is.cake.eated, df$is.it.weekend) the output data does not match the input.) $\endgroup$ – Matthew Jul 14 '16 at 9:15
  • $\begingroup$ sorry, I copied wrong stuff from my console. Should be okay now $\endgroup$ – praseodyymi Jul 14 '16 at 9:33
  • $\begingroup$ Thanks. Will try to replicate it and update my answer. Is that the output you get from running it on your real data? $\endgroup$ – Matthew Jul 14 '16 at 9:37
  • $\begingroup$ Yes these were the results from my real data. I will also check this now again couple of times if I haven't noticed something relevant $\endgroup$ – praseodyymi Jul 14 '16 at 9:46
  • $\begingroup$ And is the table c the real data? An odds ratio of 1 means the outcome is the same in the two groups so I suspect something has gone wrong with the data. $\endgroup$ – Matthew Jul 14 '16 at 9:51
0
$\begingroup$

fisher.test

With fisher.test you can run it either on a table, as in your example fisher.test(c) or you can run it on two vectors, as in your second example. It looks like the output you have provided from fisher.test(df$is.cake.eated, df$is.it.weekend) does not match the input. They should correspond if the correct data are used:

df <- structure(list(cake.eated = c(1, 0, 0, 1, 1, 1), is.it.weekend = c(1, 1, 1, 0, 1, 0)),
                .Names = c("is.cake.eated","is.it.weekend"), row.names = c(NA, -6L), class = "data.frame")


fisher.test(df$is.cake.eated, df$is.it.weekend)

Gives

    Fisher's Exact Test for Count Data

data:  table(df)
p-value = 0.4667
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
  0.00000 11.81149
sample estimates:
odds ratio 
         0

And fisher.test(table(df)) gives

    Fisher's Exact Test for Count Data

data:  table(df)
p-value = 0.4667
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
  0.00000 11.81149
sample estimates:
odds ratio 
         0 

Chi-squared test

The chi-squared test and Fisher's exact test are used to do similar things: test the association between two categorical variable. However, they go about it in different ways. You can find details in standard statistics text books, but basically the chi-squared test approximates the p value (it is said to be 'asymptotic') whereas the exact test gives an exact p value. For this reason I tend to always use Fisher's exact but you could use chi-squared here.

The rule of thumb about using chi-squared is that no cell in the table should have an expected cell count of less than 1, and no more than 20% of the cells should have an expected count of less than 5. You can access the expected cell counts in R:

chisq.test(c)$exp

In your example, this is satisfied and the chi-squared approximation is valid:

chisq.test(c)$exp
               not.weekend weekend
cake.eated            2405    1242
cake.not.eated        2405    1242

chisq.test(c)

    Pearson's Chi-squared test with Yates' continuity correction

data:  c
X-squared = 97.189, df = 1, p-value < 2.2e-16
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.