chi2test in MATLAB -- vector different length

1.8k Views Asked by At

I have 3 data vectors representing the gender (0=male, 1=female) of 3 groups A, B, C.

for example

A = [0 0 0 0 1 1 1 1 0 0];
B = [1 1 1 1 1 1 1 0];
C = [1 0 0 1 0 1 1 0 1 1 1 1 1];

and relative number of male and female

n_maleA =6;
n_femaleA =4;
n_maleB = 1;
n_femaleB = 7;
n_maleC = 4;
n_femaleC = 9;

I would like to know if there are significant differences in gender between the 3 groups. To do this I read that is possible to use

[tbl,chi2stat,pval] = crosstab(x1,x2)

How can I use this with more than 2 groups of data and with data that have different length? Is there any other way to perform the chi-squared test in MATLAB that suits with my case?

Thanks in advance

1

There are 1 best solutions below

0
On

It sounds like you want to test whether the proportion of males/females differs across groups. One way to do this would be to use a logistic regression model with male/female as the response variable and the group membership as a categorical predictor. Then look at the chi-square test for the model as a whole vs. a constant model.

>> mf = [A';B';C'];
>> group = [ones(length(A),1);2*ones(length(B),1);3*ones(length(C),1)];
>> group = nominal(group);
>> fitglm(group,mf,'distribution','binomial')
ans = 
Generalized Linear regression model:
    logit(y) ~ 1 + x1
    Distribution = Binomial

Estimated Coefficients:
                   Estimate      SE        tStat       pValue 
                   ________    _______    ________    ________
    (Intercept)    -0.40547     0.6455    -0.62814     0.52991
    x1_2             2.3514     1.2488      1.8829    0.059715
    x1_3             1.2164    0.88192      1.3793     0.16781

31 observations, 28 error degrees of freedom
Dispersion: 1
Chi^2-statistic vs. constant model: 4.79, p-value = 0.0913