If I understand correctly, it sounds like you should be able to take your data and produce a table like this one (I'm using fake data here):
x |
A |
B |
level_1 |
500 |
450 |
level_2 |
300 |
200 |
level_3 |
200 |
350 |
Basically a table showing the counts across each level and group. Based on this table, we see that we have 1,000 people in each group. We can also see that, in the sample, $\frac{500}{500+300+200}=50\%$ of people in group A have x = level_1
. You want to find out (i.e. test) whether this proportion is significantly different from the proportion of x = level_1
in group B, which in this case is $\frac{450}{450+200+350}=45\%$. And then you want to repeat this test for each level
.
A simple way to do this is by carrying out a test of proportions using prop.test()
in R
. For example, for level_1
it would be:
prop.test(c(500, 450), c(500+300+200, 450+200+350))
### Output ###
2-sample test for equality of proportions with continuity correction
data: c(500, 450) out of c(500 + 300 + 200, 450 + 200 + 350)
X-squared = 4.814, df = 1, p-value = 0.02823
alternative hypothesis: two.sided
95 percent confidence interval:
0.005283576 0.094716424
sample estimates:
prop 1 prop 2
0.50 0.45
This gives you:
- The estimated proportions at the bottom (0.50 vs 0.45)
- The 95% confidence interval for the difference in proportions (0.005283576 0.094716424)
- The $\chi^2$ statistic for your test (4.814)
- The p-value for your test (0.02823)
So with this made-up data, we would conclude that the proportion of people with x = level_1
in group A is significantly different from group B's (at the 5% significance level). Then we would repeat this for the other two levels.
Note that if you were only interested in whether a proportion was greater in group A than in group B, you would need to set the alternative = 'greater'
in prop.test()
. This would make your test a one-sided one (the default test is two-sided).