DIFFERENTIAL ITEM FUNCTIONING (DIF) METHODS
Compare knowledge distributions for different respondents
genderMC<-subset(gmo, select=c("pk_medmarij", "pk_nafta", "pk_parisagree", "pk_guantanamo", 
                               "pk_deficit2018", "pk_gdpgrow", "female", "rep"))
genderMC<-na.omit(genderMC) # Remover NAs

genderTF<-subset(gmo, select=c("pkT_medmarij_01", "pkF_nafta_01", "pkT_parisagree_01", 
                               "pkF_guantanamo_01", "pkF_deficit2018_01", "pkT_gdpgrow_01", 
                               "female", "rep"))
genderTF<-na.omit(genderTF) # Remover NAs
             
know.scaleMC <- apply(genderMC, 1, mean) 
know.scaleTF <- apply(genderTF, 1, mean) 

par(mfrow=c(2,2)) 
# Focal group: 1 (green); Reference group: 0 (red)
sm::sm.density.compare(know.scaleMC, genderMC$female, xlab="Gender: Women")
sm::sm.density.compare(know.scaleMC, genderMC$rep, xlab="PID: Republican")

sm::sm.density.compare(know.scaleTF, genderTF$female, xlab="Gender: Women")
sm::sm.density.compare(know.scaleTF, genderTF$rep, xlab="PID: Republican")
Mantel-Haenszel DIF diagnostics (difR package)
# Reference and Focal Groups
femaleMC<-genderMC$female # women = 1 men = 0
#genderMC<-subset(genderMC, select=c("pk_medmarij", "pk_nafta", "pk_parisagree", "pk_guantanamo", 
                           #    "pk_deficit2018", "pk_gdpgrow"))
PIDMC<-genderMC$rep # rep = 1

femaleTF<-genderTF$female # women = 1 men = 0
PIDTF<-genderTF$rep # rep = 1
# M.H. DIF
# The function used, difMH, requires that the dataset appear first, followed by the name of the grouping variable, in this case gender and rep.

# One of the genders must be identified as the focal group. We selected females (coded as 1 in the data).
names(genderMC)

gender.MH <- difR::difMH(genderMC, group=femaleMC, focal.name=1, purify=TRUE, 
                         p.adjust.method = "BH")  
print(gender.MH) # chi-square values (alphaMH), large values indicate DIF.
# we see the log of the odds ratio for each item (alphaMH) 
# as well as the ETS Δ mentioned previously (deltaMH).

# # We requested that the matching scale scores be purified. 
# When nonpurified items are included in the matching score, the accuracy of DIF detection 
# can be greatly diminished and false positives are more likely. 

# Finally, it is easy to obtain a graphical display for the MH chi-square test results, 
# which provides an easy-to-interpret visual display. 
difR::plot.MH(gender.MH)
Logistic regression (LR) DIF diagnotics (difR)
# Note: here is also the 'lordif' package, that exclusively uses the logistic function. 

# The logistic regression method (Swaminathan & Rogers, 1990) allows for detecting both uniform 
# and non-uniform DIF without requiring an item response model approach. 

# It has an advantage over MH when the researcher is interested in checking the data for both 
# uniform and nonuniform DIF, as it can easily be adapted for both. 

# It consists in fitting a logistic model with the matching criterion, 
# the group membership and an interaction between both as covariates.

genderMClr<-genderMC[,1:6]

dlgMC <- difR::difLogistic(genderMClr, group=femaleMC, focal.name=1, 
                           p.adjust.method = "BH", type="both") # "nudif" or "udif"
difR::print.Logistic(dlgMC)

dlPIDMC <- difR::difLogistic(genderMClr, group=PIDMC, focal.name=1, 
                             p.adjust.method = "BH", type="both") # "nudif" or "udif"
difR::print.Logistic(dlPIDMC)

# The likelihood ratio statistics are displayed on the Y axis, for each item.
# The detection threshold is displayed by a horizontal line, and items flagged as DIF are 
# printed with the color defined by argument col.

plot(dlgMC, plot="lrStat", itemFit = "best", 
     pch = 8, number = F, col = "red", colIC = rep("black", 2), ltyIC = c(1, 2))

plot(dlPIDMC, plot="lrStat", itemFit = "best", 
     pch = 8, number = F, col = "red", colIC = rep("black", 2), ltyIC = c(1, 2))

genderTFlr<-genderTF[,1:6]

dlgTF <- difR::difLogistic(genderTFlr, group=femaleTF, focal.name=1, 
                           p.adjust.method = "BH", type="both")
difR::print.Logistic(dlgTF)

# For PID:

dlPIDTF <- difR::difLogistic(genderTFlr, group=PIDTF, focal.name=1, 
                             p.adjust.method = "BH", type="both") 
dlPIDTF

plot(dlgTF, plot="lrStat", itemFit = "best", 
     pch = 8, number = F, col = "red", colIC = rep("black", 2), ltyIC = c(1, 2))

plot(dlPIDTF, plot="lrStat", itemFit = "best", 
     pch = 8, number = F, col = "red", colIC = rep("black", 2), ltyIC = c(1, 2))
Lord Chi-square DIF
lordMC.outG<-difR::difLord(genderMC, group="female", focal.name=1, purify=TRUE, 
                           model="1PL", p.adjust.method = "BH")
lordMC.outG
plot(lordMC.outG)

lordTF.outG<-difR::difLord(genderTF, group="female", focal.name=1, purify=TRUE, 
                           model="1PL", p.adjust.method = "BH")
lordTF.outG
plot(lordTF.outG)

# Now for the 2-PL Model:

lordMC.outR<-difR::difLord(genderMC, group="rep", focal.name=1, purify=TRUE, 
                           model="2PL", p.adjust.method = "BH")
lordMC.outR

lordTF.outR<-difR::difLord(genderTF, group="rep", focal.name=1, purify=TRUE, 
                           model="2PL", p.adjust.method = "BH")
lordTF.outR