# Categorical independent variable and binary dependent variable

Which test can I use for analyzing the effect of a categorical independent variable, such as preoperative ASA score (1/2/3/4), on a binary dependent variable, such as postoperative complication (yes/no)?

I know that the chi squared test is an option, but this test does not tell me if the significant difference lies between ASA 1 and ASA 2 or between ASA 2 and ASA 3,...

Is there a statistical test I can use to know between which independent categories the significant difference is situated?

You can use logistic regression with an ordinal predictor variable. By choosing the encoding system for the predictor, you can get the information presented in a useful form. Here is a useful UCLA page overview for different categorical encoding systems, using R (there are similar pages for other languages.) For your purposes, maybe a successive differences coding is natural. Below I will simulate some data (using R) and present analysis, coding in different ways.

But first, similar questions have been asked before, see Logistic regression? multiple categorical and ordinal values to predict binary outcome, Encoding of categorical data/feature/predictor for binary classification

set.seed(7*11*13)
ncats <- 5
k <- 100
ordvar <- factor(rep(1:ncats, each=k), ordered=TRUE)
Y  <- rbinom(ncats*k, 1, rep(seq.int(from=0.1, to=0.9, len=ncats), each=k))
ordnum <- rep(1:ncats, each=k)
mydf <- data.frame(Y, ordvar, ordnum)

mod.ordvar0 <- glm(Y  ~ ordvar, family=binomial, data=mydf)

mod.ordvar1 <- glm(Y  ~ ordvar, family=binomial, data=mydf, contrasts=list(ordvar=MASS::contr.sdif))

mod.ordnum <- glm(Y  ~ ordnum + I(ordnum^2), family=binomial, data=mydf)


The first model we estimated used R's standard encoding for ordinal predictors, which is orthogonal polynomials:

 summary(mod.ordvar0)

Call:
glm(formula = Y ~ ordvar, family = binomial, data = mydf)

Deviance Residuals:
Min       1Q   Median       3Q      Max
-2.1945  -0.8782   0.4343   0.7585   2.0200

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)   0.1435     0.1183   1.213    0.225
ordvar.L      3.2513     0.3068  10.598   <2e-16 ***
ordvar.Q      0.1498     0.2805   0.534    0.593
ordvar.C      0.1612     0.2465   0.654    0.513
ordvar^4     -0.1442     0.2151  -0.670    0.503
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 692.35  on 499  degrees of freedom
Residual deviance: 514.22  on 495  degrees of freedom
AIC: 524.22


But I guess that often (and in your case ...) successive differences (comparing level $$i+1$$ with level $$i$$) is more informative:

summary(mod.ordvar1)

Call:
glm(formula = Y ~ ordvar, family = binomial, data = mydf, contrasts = list(ordvar = MASS::contr.sdif))

Deviance Residuals:
Min       1Q   Median       3Q      Max
-2.1945  -0.8782   0.4343   0.7585   2.0200

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)   0.1435     0.1183   1.213 0.225046
ordvar2-1     1.1472     0.3666   3.130 0.001751 **
ordvar3-2     0.7138     0.2932   2.434 0.014920 *
ordvar4-3     1.1386     0.3055   3.727 0.000194 ***
ordvar5-4     1.2150     0.4188   2.901 0.003716 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 692.35  on 499  degrees of freedom
Residual deviance: 514.22  on 495  degrees of freedom
AIC: 524.22


But note that these two models are equivalent, they give exactly the same fitted probabilities. In mathematical thers, the only difference is in the choice of a basis for a vector space. The last model is not equivalent, it is a reduced model, using the ordinal levels as numeric, and fitting only a linear+quadratic model, so two less parameters:

summary(mod.ordnum)

Call:
glm(formula = Y ~ ordnum + I(ordnum^2), family = binomial, data = mydf)

Deviance Residuals:
Min       1Q   Median       3Q      Max
-2.1697  -0.8215   0.4468   0.7548   1.9728

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.58216    0.58633  -4.404 1.06e-05 ***
ordnum       0.74624    0.43060   1.733   0.0831 .
I(ordnum^2)  0.04420    0.07192   0.615   0.5388
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 692.35  on 499  degrees of freedom
Residual deviance: 515.15  on 497  degrees of freedom
AIC: 521.15 $$$$
`