# Testing whether the means within group are different from each other (R)

My Y variable is binary and takes the values 0 and 1. The same applies for the independent variable X. I've been able to create the means within the group, here:

 If Y = 0 -> X = .51
If Y = 1 -> X = .69


What I want to know is, whether 0.51 and 0.69 are that much different from each other. In other words, do people with illness (Y) really die (X) on average more than people without illness. I'm not sure, whether to use ANOVA or a pairwise t-test.

My approach is

t.test(data$$Y, data$$X, data = data, paired = TRUE)


and the other approach is

pairwise.t.test(data$$Y, data$$X, p.adjust = "bonferroni")


Am I on the right track?

• Does it make sense to take the mean of a binary variable? Why would you be comparing the mean of values in the X vector to the mean of values in the Y vector? ... In any case, it sounds like the simplest approach is to use a chi-square test of association. – Sal Mangiafico May 2 at 10:11
• If Y takes the value 1 if one person has illness and 0 if not. X takes the value 1 if he or she died, otherwise 0. I want to figure out, whether there is a pattern between the means of X within the Y class. I mean it would be a huge information gain, if we would know that all ill people died and all healthy people didn't, right? I want to make sure that the values .51 and .69 didn't occur randomly and that this difference is so large, that we can finally tell, that ill people tend to die more than healthy people. – Textime May 2 at 10:23
• I get that. I mean to say, if you want to compare Group (Y=0) to Group (Y=1) then you would compare those two groups, not compare X to Y... In any case it sounds like you want a chi-square test of association. – Sal Mangiafico May 2 at 10:28
• I just mentioned that, because I have more than one independent binary variable (X1, X2, .., X140). So that's why I want to compare X1 in Y, then X2 in Y etc. But still, thank you for your advice. I hope that your approach leads to the desired result. – Textime May 2 at 10:50
• A couple of resources that may be helpful. One here and one here – Sal Mangiafico May 2 at 16:18

## 1 Answer

If you are counting the number of Y and then number of X with and without the condition then a simple contingency table and the Fisher exact test could be used.

If counted one hundred Y=0 and Y=1 then the problem is:

mat<-matrix(c(49, 51, 31, 69), nrow=2)
fisher.test(mat)

Fisher's Exact Test for Count Data

data:  mat
p-value = 0.0139
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
1.154704 3.974051
sample estimates:
odds ratio
2.130247


So there is a significant difference, but if your sample size is smaller say only only 35 each. Then the results are not significant.

mat<-matrix(c(17, 18, 11, 24), nrow=2)
fisher.test(mat)


The Fisher's test was designed for a 2x2 design but can be extended to many more levels. See the help for fisher.test for more information.