You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
5.6 KiB
R
167 lines
5.6 KiB
R
9 months ago
|
library(TAM)
|
||
|
|
||
|
resali <- function(df=NULL,items=NULL,group=NULL,verbose=T) {
|
||
|
if (verbose) {
|
||
9 months ago
|
cat('-----------------------------------------------------------\n')
|
||
|
cat('COMPUTING INITIAL PCM\n')
|
||
|
cat('-----------------------------------------------------------\n')
|
||
9 months ago
|
}
|
||
|
nbitems <- length(items)
|
||
|
nbitems_o <- nbitems
|
||
|
items_n <- paste0('item',items)
|
||
|
resp <- df[,items_n]
|
||
|
grp <- df[,group]
|
||
|
pcm_initial <- TAM::tam.mml(resp=resp,Y=grp,irtmodel = "PCM",est.variance = T,verbose=F)
|
||
|
dat <- df
|
||
|
dat$score <- rowSums(dat[,items_n])
|
||
9 months ago
|
nqt <- ifelse(length(unique(quantile(dat$score,seq(0,1,0.2))))==6,5,length(unique(quantile(dat$score,seq(0,1,0.2))))-1)
|
||
8 months ago
|
while (length(unique(quantile(dat$score,seq(0,1,1/nqt))))!=nqt+1) {
|
||
|
nqt <- nqt-1
|
||
|
}
|
||
8 months ago
|
# ITEM POLYTOMIQUE
|
||
|
if (max(resp)>1) {
|
||
9 months ago
|
dat$score_q5 <- cut(dat$score,unique(quantile(dat$score,seq(0,1,1/nqt))),labels=1:nqt,include.lowest=T)
|
||
9 months ago
|
res.anova <- rep(NA,nbitems)
|
||
|
pval <- rep(NA,nbitems)
|
||
|
fval <- rep(NA,nbitems)
|
||
|
for (i in items) {
|
||
9 months ago
|
dat[,paste0('res_',i)] <- IRT.residuals(pcm_initial)$stand_residuals[,i]
|
||
9 months ago
|
res.anova[i] <- summary(aov(dat[,paste0('res_',i)]~TT*score_q5,data=dat))
|
||
|
pval[i] <- res.anova[[i]][1,"Pr(>F)"]
|
||
|
fval[i] <- res.anova[[i]][1,'F value']
|
||
|
}
|
||
|
if (verbose) {
|
||
9 months ago
|
cat('DONE\n')
|
||
|
cat('-----------------------------------------------------------\n')
|
||
9 months ago
|
}
|
||
|
res.items <- c()
|
||
|
res.uniform <- c()
|
||
|
k <- 1
|
||
9 months ago
|
while(any(pval<0.05/(nbitems_o*3))) {
|
||
9 months ago
|
k <- k+1
|
||
|
if (verbose) {
|
||
9 months ago
|
cat(paste('COMPUTING STEP',k,'\n'))
|
||
|
cat('-----------------------------------------------------------\n')
|
||
9 months ago
|
}
|
||
|
res.item <- gsub("[a-z]", "",colnames(resp)[which.max(fval)])
|
||
|
res.items <- c(res.items,res.item)
|
||
|
res.uni <- res.anova[[which.max(fval)]][3,"Pr(>F)"]>0.05
|
||
|
res.uniform <- c(res.uniform,res.uni)
|
||
|
items_n <- c(items_n[items_n!=paste0('item',res.item)],paste0("item",res.item,c("noTT","TT")))
|
||
8 months ago
|
dat[dat$TT==1,paste0("item",res.item,'TT')] <- dat[dat$TT==1,paste0('item',res.item)]
|
||
|
dat[dat$TT==0,paste0("item",res.item,'noTT')] <- dat[dat$TT==0,paste0('item',res.item)]
|
||
9 months ago
|
resp <- dat[,items_n]
|
||
|
grp <- dat[,group]
|
||
|
pcm_while <- TAM::tam.mml(resp=resp,Y=grp,irtmodel = "PCM",est.variance = T,verbose=F)
|
||
|
nbitems <- length(items_n)
|
||
|
res.anova <- rep(NA,nbitems)
|
||
|
pval <- rep(NA,nbitems)
|
||
|
fval <- rep(NA,nbitems)
|
||
|
for (i in 1:nbitems) {
|
||
9 months ago
|
dat[,paste0('res_',i)] <- IRT.residuals(pcm_while)$stand_residuals[,i]
|
||
9 months ago
|
res.anova[i] <- summary(aov(dat[,paste0('res_',i)]~TT*score_q5,data=dat))
|
||
|
pval[i] <- res.anova[[i]][1,"Pr(>F)"]
|
||
|
fval[i] <- res.anova[[i]][1,'F value']
|
||
|
}
|
||
8 months ago
|
zz <- 0
|
||
|
for (name_i in items_n) {
|
||
|
zz <- zz+1
|
||
|
if (grepl("TT",name_i)) {
|
||
|
pval[zz] <- 1
|
||
|
fval[zz] <- 0
|
||
|
}
|
||
|
}
|
||
9 months ago
|
if (verbose) {
|
||
9 months ago
|
cat('DONE\n')
|
||
|
cat('-----------------------------------------------------------\n')
|
||
|
}
|
||
|
}
|
||
|
if (verbose) {
|
||
|
cat("DETECTED DIF ITEMS\n")
|
||
|
cat('-----------------------------------------------------------\n')
|
||
|
}
|
||
|
if (length(res.items>0)) {
|
||
|
results <- data.frame(dif.items=res.items,
|
||
|
uniform=1*res.uniform)
|
||
|
return(results)
|
||
|
}
|
||
|
else {
|
||
|
if (verbose) {
|
||
|
cat("No DIF was detected\n")
|
||
|
}
|
||
|
return(NULL)
|
||
|
}
|
||
8 months ago
|
# ITEM DICHOTOMIQUE
|
||
|
} else {
|
||
|
|
||
9 months ago
|
res.anova <- rep(NA,nbitems)
|
||
|
pval <- rep(NA,nbitems)
|
||
|
fval <- rep(NA,nbitems)
|
||
8 months ago
|
for (i in items) {
|
||
|
dat[,paste0('res_',i)] <- IRT.residuals(pcm_initial)$stand_residuals[,i]
|
||
|
res.anova[i] <- summary(aov(dat[,paste0('res_',i)]~TT,data=dat))
|
||
9 months ago
|
pval[i] <- res.anova[[i]][1,"Pr(>F)"]
|
||
|
fval[i] <- res.anova[[i]][1,'F value']
|
||
|
}
|
||
|
if (verbose) {
|
||
|
cat('DONE\n')
|
||
|
cat('-----------------------------------------------------------\n')
|
||
9 months ago
|
}
|
||
8 months ago
|
res.items <- c()
|
||
|
k <- 1
|
||
|
while(any(pval<0.05/(nbitems_o))) {
|
||
9 months ago
|
k <- k+1
|
||
|
if (verbose) {
|
||
|
cat(paste('COMPUTING STEP',k,'\n'))
|
||
|
cat('-----------------------------------------------------------\n')
|
||
|
}
|
||
|
res.item <- gsub("[a-z]", "",colnames(resp)[which.max(fval)])
|
||
|
res.items <- c(res.items,res.item)
|
||
|
items_n <- c(items_n[items_n!=paste0('item',res.item)],paste0("item",res.item,c("noTT","TT")))
|
||
8 months ago
|
dat[dat$TT==1,paste0("item",res.item,'TT')] <- dat[dat$TT==1,paste0('item',res.item)]
|
||
|
dat[dat$TT==0,paste0("item",res.item,'noTT')] <- dat[dat$TT==0,paste0('item',res.item)]
|
||
9 months ago
|
resp <- dat[,items_n]
|
||
|
grp <- dat[,group]
|
||
|
pcm_while <- TAM::tam.mml(resp=resp,Y=grp,irtmodel = "PCM",est.variance = T,verbose=F)
|
||
|
nbitems <- length(items_n)
|
||
|
res.anova <- rep(NA,nbitems)
|
||
|
pval <- rep(NA,nbitems)
|
||
|
fval <- rep(NA,nbitems)
|
||
|
for (i in 1:nbitems) {
|
||
8 months ago
|
dat[,paste0('res_',i)] <- IRT.residuals(pcm_while)$stand_residuals[,i]
|
||
|
res.anova[i] <- summary(aov(dat[,paste0('res_',i)]~TT,data=dat))
|
||
9 months ago
|
pval[i] <- res.anova[[i]][1,"Pr(>F)"]
|
||
|
fval[i] <- res.anova[[i]][1,'F value']
|
||
|
}
|
||
8 months ago
|
zz <- 0
|
||
|
for (name_i in items_n) {
|
||
|
zz <- zz+1
|
||
|
if (grepl("TT",name_i)) {
|
||
|
pval[zz] <- 1
|
||
|
fval[zz] <- 0
|
||
|
}
|
||
|
}
|
||
9 months ago
|
if (verbose) {
|
||
|
cat('DONE\n')
|
||
|
cat('-----------------------------------------------------------\n')
|
||
|
}
|
||
|
}
|
||
|
if (verbose) {
|
||
|
cat("DETECTED DIF ITEMS\n")
|
||
|
cat('-----------------------------------------------------------\n')
|
||
|
}
|
||
|
if (length(res.items>0)) {
|
||
|
results <- data.frame(dif.items=res.items,
|
||
8 months ago
|
uniform=rep(1,length(res.items)))
|
||
9 months ago
|
return(results)
|
||
|
}
|
||
|
else {
|
||
|
if (verbose) {
|
||
|
cat("No DIF was detected\n")
|
||
|
}
|
||
|
return(NULL)
|
||
|
}
|
||
8 months ago
|
|
||
9 months ago
|
}
|
||
8 months ago
|
}
|