library(TAM)

pcm_analysis <- function(df=NULL,treatment='TT',irtmodel='PCM2',method='MML') {
  nbitems <- sum(sapply(1:20,function(x) paste0('item',x)) %in% colnames(df))
  resp <- df[,sapply(seq(1,nbitems),function(x) paste0('item',x))]
  if (method=='MML') {
    tam1 <-  tam.mml(resp=resp,Y=df[,treatment],irtmodel = irtmodel,est.variance = T,verbose=F)
  }
  if (method=='JML') {
    tam1 <- tam.jml(resp=resp,group=1+df[,treatment])
  }
  if (method!='MML' & method!='JML') {
    stop('Invalid method. Please choose among MML or JML')
  }
  return(tam1)
}

dff <- read.csv(file = '/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N100/scenario_5A_100.csv')
dfff <- dff[dff$replication==1,]
facets <- dfff$TT
dfff$item4_noTT <- NA
dfff$item4_TT <- NA
dfff[dfff$TT==0,]$item4_noTT <- dfff[dfff$TT==0,"item4"]
dfff[dfff$TT==1,]$item4_TT <- dfff[dfff$TT==1,"item4"]

mml.mod <- tam.mml(resp=dfff[,c('item1','item2',"item3","item4_noTT",'item4_TT')],Y=dfff$TT,constraint='cases'
                     ,irtmodel = "PCM2",est.variance = T,verbose=F)