##############################################################################
#----------------------------------------------------------------------------#
################################## LIBRARIES #################################
#----------------------------------------------------------------------------#
##############################################################################

library(TAM)
library(doMC)
library(parallel)
library(pbmcapply)
library(funprog)
library(dplyr)
library(readxl)
library(furrr)
library(tibble)

lastChar <- function(str){
  substr(str, nchar(str), nchar(str))
}

##############################################################################
#----------------------------------------------------------------------------#
################################### RESALI ###################################
#----------------------------------------------------------------------------#
##############################################################################

generate_resali <- function(scenario=NULL,grp=NULL) {
  scen <- as.numeric(gsub("[A,B,C,_]","",substr(scenario,0,3)))
  if (substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50") {
    N <- 50
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="300") {
    N <- 300
  }
  dat <- read.csv(paste0('/home/corentin/Documents/These/Recherche/ROSALI-SIM/Data/N',N,'/scenario_',scenario,'.csv'))
  if (scen%in%c(4,16)) {
  res <- resali(df=dat[dat$replication==1,],items = seq(1,7),group=grp,verbose=FALSE)
  df_res <- data.frame(dif.detect.1=ifelse(length(res$dif.items)>=1,res$dif.items[1],NA),
                       dif.detect.2=ifelse(length(res$dif.items)>=2,res$dif.items[2],NA),
                       dif.detect.3=ifelse(length(res$dif.items)>=3,res$dif.items[3],NA),
                       dif.detect.4=ifelse(length(res$dif.items)>=4,res$dif.items[4],NA),
                       dif.detect.5=ifelse(length(res$dif.items)>=5,res$dif.items[5],NA),
                       dif.detect.6=ifelse(length(res$dif.items)>=6,res$dif.items[6],NA),
                       dif.detect.7=ifelse(length(res$dif.items)>=7,res$dif.items[7],NA),
                       dif.detect.unif.1=ifelse(length(res$uniform)>=1,res$uniform[1],NA),
                       dif.detect.unif.2=ifelse(length(res$uniform)>=2,res$uniform[2],NA),
                       dif.detect.unif.3=ifelse(length(res$uniform)>=3,res$uniform[3],NA),
                       dif.detect.unif.4=ifelse(length(res$uniform)>=4,res$uniform[4],NA),
                       dif.detect.unif.5=ifelse(length(res$uniform)>=5,res$uniform[5],NA),
                       dif.detect.unif.6=ifelse(length(res$uniform)>=6,res$uniform[6],NA),
                       dif.detect.unif.7=ifelse(length(res$uniform)>=7,res$uniform[7],NA),
                       N=N,
                       nbdif=ifelse(scen==16,2,0),
                       true.dif.1=ifelse(scen==16,unique(dat[dat$replication==1,]$dif1),NA),
                       true.dif.2=ifelse(scen==16,unique(dat[dat$replication==1,]$dif2),NA)
                      )
  for (k in 2:1000) {
    if (k%%100==0) {
      cat(paste0('N=',k,'/1000\n'))
    }
    res <- resali(df=dat[dat$replication==k,],items = seq(1,7),group=grp,verbose=FALSE)
    df_res2 <- data.frame(dif.detect.1=ifelse(length(res$dif.items)>=1,res$dif.items[1],NA),
                         dif.detect.2=ifelse(length(res$dif.items)>=2,res$dif.items[2],NA),
                         dif.detect.3=ifelse(length(res$dif.items)>=3,res$dif.items[3],NA),
                         dif.detect.4=ifelse(length(res$dif.items)>=4,res$dif.items[4],NA),
                         dif.detect.5=ifelse(length(res$dif.items)>=5,res$dif.items[5],NA),
                         dif.detect.6=ifelse(length(res$dif.items)>=6,res$dif.items[6],NA),
                         dif.detect.7=ifelse(length(res$dif.items)>=7,res$dif.items[7],NA),
                         dif.detect.unif.1=ifelse(length(res$uniform)>=1,res$uniform[1],NA),
                         dif.detect.unif.2=ifelse(length(res$uniform)>=2,res$uniform[2],NA),
                         dif.detect.unif.3=ifelse(length(res$uniform)>=3,res$uniform[3],NA),
                         dif.detect.unif.4=ifelse(length(res$uniform)>=4,res$uniform[4],NA),
                         dif.detect.unif.5=ifelse(length(res$uniform)>=5,res$uniform[5],NA),
                         dif.detect.unif.6=ifelse(length(res$uniform)>=6,res$uniform[6],NA),
                         dif.detect.unif.7=ifelse(length(res$uniform)>=7,res$uniform[7],NA),
                         N=N,
                         nbdif=ifelse(scen==16,2,0),
                         true.dif.1=ifelse(scen==16,unique(dat[dat$replication==k,]$dif1),NA),
                         true.dif.2=ifelse(scen==16,unique(dat[dat$replication==k,]$dif2),NA))
    df_res <- rbind(df_res,df_res2)
  }
  }
  else if (scen%in%c(2,8)) {
    res <- resali(df=dat[dat$replication==1,],items = seq(1,4),group=grp,verbose=FALSE)
    df_res <- data.frame(dif.detect.1=ifelse(length(res$dif.items)>=1,res$dif.items[1],NA),
                         dif.detect.2=ifelse(length(res$dif.items)>=2,res$dif.items[2],NA),
                         dif.detect.3=ifelse(length(res$dif.items)>=3,res$dif.items[3],NA),
                         dif.detect.4=ifelse(length(res$dif.items)>=4,res$dif.items[4],NA),
                         dif.detect.unif.1=ifelse(length(res$uniform)>=1,res$uniform[1],NA),
                         dif.detect.unif.2=ifelse(length(res$uniform)>=2,res$uniform[2],NA),
                         dif.detect.unif.3=ifelse(length(res$uniform)>=3,res$uniform[3],NA),
                         dif.detect.unif.4=ifelse(length(res$uniform)>=4,res$uniform[4],NA),
                         N=N,
                         nbdif=ifelse(scen==8,1,0),
                         true.dif=ifelse(scen==8,unique(dat[dat$replication==1,]$dif1),NA)
    )
    for (k in 2:1000) {
      if (k%%100==0) {
        cat(paste0('N=',k,'/1000\n'))
      }
      res <- resali(df=dat[dat$replication==k,],items = seq(1,4),group=grp,verbose=FALSE)
      df_res2 <- data.frame(dif.detect.1=ifelse(length(res$dif.items)>=1,res$dif.items[1],NA),
                            dif.detect.2=ifelse(length(res$dif.items)>=2,res$dif.items[2],NA),
                            dif.detect.3=ifelse(length(res$dif.items)>=3,res$dif.items[3],NA),
                            dif.detect.4=ifelse(length(res$dif.items)>=4,res$dif.items[4],NA),
                            dif.detect.unif.1=ifelse(length(res$uniform)>=1,res$uniform[1],NA),
                            dif.detect.unif.2=ifelse(length(res$uniform)>=2,res$uniform[2],NA),
                            dif.detect.unif.3=ifelse(length(res$uniform)>=3,res$uniform[3],NA),
                            dif.detect.unif.4=ifelse(length(res$uniform)>=4,res$uniform[4],NA),
                            N=N,
                            nbdif=ifelse(scen==8,1,0),
                            true.dif=ifelse(scen==8,unique(dat[dat$replication==k,]$dif1),NA))
      df_res <- rbind(df_res,df_res2)
    }
  }
  return(df_res)
}






results <- c(sapply(c(2,4),function(x) paste0(x,c('A','B','C'))),sapply(8,function(x) paste0(x,c('A','B','C'))))

results2 <- c(sapply(16,function(x) paste0(x,c('A','B','C'))))

results <- c(sapply(c(50,300),function(x) paste0(results,'_',x)))

results2 <- c(sapply(c(50,300),function(x) paste0(results2,'_',x)))

results <- sort(results)

results2 <- sort(results2)

results <- c(results,results2)

for (r in results) {
  cat(paste0(r,"\n"))
  cat(paste0("-------------------------------------------","\n"))
  write.csv(generate_resali(r,"TT"),paste0("/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/resali/",r,".csv"))
  cat(paste0("-------------------------------------------","\n"))
}



##############################################################################
#----------------------------------------------------------------------------#
########################### AGGREGATION DIF MATRICES #########################
#----------------------------------------------------------------------------#
##############################################################################

#### Create data.frame

results <- c(sapply(c(2,4),function(x) paste0(x,c('A','B','C'))),sapply(8,function(x) paste0(x,c('A','B','C'))))

results2 <- c(sapply(16,function(x) paste0(x,c('A','B','C'))))

results <- c(sapply(c(50,300),function(x) paste0(results,'_',x)))

results2 <- c(sapply(c(50,300),function(x) paste0(results2,'_',x)))

results <- sort(results)

results2 <- sort(results2)

results <- c(results,results2)

results <- c(sapply(c("noBF","noLRT","noLRT_noBF","original","resali"),function(x) paste0(results,'_',x)))

#### Compiler function

compile_simulation2 <- function(scenario) {
  name <- as.numeric(gsub("[^0-9.-]", "", substr(scenario,start=0,stop=2)))
  if (substr(scenario,start=nchar(scenario)-5,stop=nchar(scenario))=="0_noBF") {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/noBF/',scenario,'.xls'))
    s$version <- "noBF"
  }
  if (substr(scenario,start=nchar(scenario)-4,stop=nchar(scenario))=="noLRT") {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/noLRT/',scenario,'.xls'))
    s$version <- "noLRT"
  }
  if (substr(scenario,start=nchar(scenario)-9,stop=nchar(scenario))=="noLRT_noBF") {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/noLRTnoBF/',scenario,'.xls'))
    s$version <- "noLRTnoBF"
  }
  if (substr(scenario,start=nchar(scenario)-7,stop=nchar(scenario))=="original") {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/original/',scenario,'.xls'))
    s$version <- "original"
  }
  if (substr(scenario,start=nchar(scenario)-5,stop=nchar(scenario))=="resali") {
    s <- read.csv(paste0('/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/resali/',gsub("_resali","",scenario),'.csv'),row.names = 1,header = T)
    if (as.numeric(gsub("[A-Z]","", substr(scenario,start=0,stop=2)))%in%c(2,4)) {
      s <- s[,-c(ncol(s))]
    }
    if (as.numeric(gsub("[A-Z]","", substr(scenario,start=0,stop=2)))%in%c(4)) {
      s <- s[,-c(ncol(s))]
    }
    if (as.numeric(gsub("[A-Z]","", substr(scenario,start=0,stop=2)))%in%c(2,8)) {
      s <- add_column(s,J=4,.after='N')
    }
    if (as.numeric(gsub("[A-Z]","", substr(scenario,start=0,stop=2)))%in%c(4,16)) {
      s <- add_column(s,J=7,.after='N')
    }
    s$version <- "resali"
  }
  name <- gsub("_", "", substr(scenario,start=0,stop=3))
  if (as.numeric(gsub("[A,B,C]","",name))==2){
    colnames(s) <- c("dif.detect.1","dif.detect.2","dif.detect.3","dif.detect.4",
                     "dif.detect.unif.1","dif.detect.unif.2","dif.detect.unif.3","dif.detect.unif.4",
                     "N","J","nb.dif","version")
    s$dif.detected <- sapply(1:1000,function(x) any(!is.na(s[x,1:4])))
    s$prop.perfect <- NA
    s$prop.flexible <- NA
    s$prop.moreflexible <- NA
  }
  if (as.numeric(gsub("[A,B,C]","",name))==4){
    colnames(s) <- c("dif.detect.1","dif.detect.2","dif.detect.3","dif.detect.4",
                     "dif.detect.5","dif.detect.6","dif.detect.7",
                     "dif.detect.unif.1","dif.detect.unif.2","dif.detect.unif.3","dif.detect.unif.4",
                     "dif.detect.unif.5","dif.detect.unif.6","dif.detect.unif.7",
                     "N","J","nb.dif","version")
    s$dif.detected <- sapply(1:1000,function(x) any(!is.na(s[x,1:7])))
    s$prop.perfect <- NA
    s$prop.flexible <- NA
    s$prop.moreflexible <- NA
  }
  if (as.numeric(gsub("[A,B,C]","",name))==8){
    colnames(s) <- c("dif.detect.1","dif.detect.2","dif.detect.3","dif.detect.4",
                     "dif.detect.unif.1","dif.detect.unif.2","dif.detect.unif.3","dif.detect.unif.4",
                     "N","J","nb.dif","true.dif","version")
    s$dif.detected <- sapply(1:1000,function(x) any(!is.na(s[x,1:4])))
    s$perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,1:4]))==1,s[x,"dif.detect.unif.1"]==1 & s[x,which(sapply(1:4,function(y) !is.na(s[x,y])))]==s[x,"true.dif"],0)  )
    s$prop.perfect <- mean(s$perfect.detection)
    s$flexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,1:4]))==1,s[x,which(sapply(1:4,function(y) !is.na(s[x,y])))]==s[x,"true.dif"],0)  )
    s$prop.flexible <- mean(s$flexible.detect)
    s$moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,1:4]))!=0,s[x,"true.dif"]%in%c(s[x,which(sapply(1:4,function(y) !is.na(s[x,y])))]),0)  )
    s$prop.moreflexible <- mean(s$moreflexible.detect)
  }
  if (as.numeric(gsub("[A,B,C]","",name))==16){
    colnames(s) <- c("dif.detect.1","dif.detect.2","dif.detect.3","dif.detect.4",
                     "dif.detect.5","dif.detect.6","dif.detect.7",
                     "dif.detect.unif.1","dif.detect.unif.2","dif.detect.unif.3","dif.detect.unif.4",
                     "dif.detect.unif.5","dif.detect.unif.6","dif.detect.unif.7",
                     "N","J","nb.dif","true.dif.1","true.dif.2","version")
    s$dif.detected <- sapply(1:1000,function(x) any(!is.na(s[x,1:7])))
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,1:7]))==2,s[x,"dif.detect.unif.1"]==1 & s[x,"dif.detect.unif.2"]==1 & s[x,which(sapply(1:7,function(y) !is.na(s[x,y])))[1]]%in%c(s[x,c("true.dif.1","true.dif.2")]) & s[x,which(sapply(1:7,function(y) !is.na(s[x,y])))[2]]%in%c(s[x,c("true.dif.1","true.dif.2")]) 
                                                            ,0)  )
    s$prop.perfect <- mean(perfect.detection)
    s$flexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,1:7]))==2,s[x,which(sapply(1:7,function(y) !is.na(s[x,y])))[1]]%in%c(s[x,c("true.dif.1","true.dif.2")]) & s[x,which(sapply(1:7,function(y) !is.na(s[x,y])))[2]]%in%c(s[x,c("true.dif.1","true.dif.2")]),0 ))
    s$prop.flexible <- mean(s$flexible.detect)
    s$moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,1:7]))!=0,s[x,"true.dif.1"]%in%c(s[x,which(sapply(1:7,function(y) !is.na(s[x,y])))]) &
                                                            s[x,"true.dif.2"]%in%c(s[x,which(sapply(1:7,function(y) !is.na(s[x,y])))]),0)  )
    s$prop.moreflexible <- mean(s$moreflexible.detect)
  }
  d <- data.frame(scenario=name,
                  version=unique(s$version),
                  N=unique(s$N),
                  J=unique(s$J),
                  M=4,
                  nb.dif=unique(s$nb.dif),
                  prop.dif.detect=mean(s$dif.detected),
                  prop.perfect=ifelse(substr(name,start=nchar(name),stop=nchar(name))!="D",unique(s$prop.perfect),NA),
                  prop.flexible=ifelse(substr(name,start=nchar(name),stop=nchar(name))!="D",unique(s$prop.flexible),NA),
                  prop.moreflexible=ifelse(substr(name,start=nchar(name),stop=nchar(name))!="D",unique(s$prop.moreflexible),NA)
          )
  return(d)
}


#### Compiled results

res.dat.dif <- compile_simulation2("2A_300_noBF")

for (x in results[seq(2,length(results))]) {
  y <- compile_simulation2(x)
  res.dat.dif <- bind_rows(res.dat.dif,y)
}

res.dat.dif


##############################################################################
#----------------------------------------------------------------------------#
############################### PLOTS OF RESULTS #############################
#----------------------------------------------------------------------------#
##############################################################################

# False positives

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif==0 & res.dat.dif$version=='original',]$prop.dif.detect) )
)
plot(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,1),lty=1,pch=3,xlab='N',ylab="Proportions of scenarios where DIF was detected")
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1)
title('Proportion of DIF detection in scenarios without DIF')

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif==0 & res.dat.dif$version=='noBF',]$prop.dif.detect) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="blue",pch=2)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="blue")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif==0 & res.dat.dif$version=='noLRTnoBF',]$prop.dif.detect) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="red",pch=4)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="red")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif==0 & res.dat.dif$version=='noLRT',]$prop.dif.detect) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="green",pch=5)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="green")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif==0 & res.dat.dif$version=='resali',]$prop.dif.detect) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="purple",pch=6)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="purple")

legend(x='topleft',legend=c('original','no Bonferroni correction',"no LRT","no LRT and no Bonferroni",'Residuals'),
       col=c('black',"blue","green","red",'purple'),lty=c(1,1,1,1,1),pch=c(3,2,4,5,6))

par(mfrow=c(2,2))

# Perfect detection

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='original',]$prop.perfect) )
)
plot(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,1),lty=1,pch=3,xlab='N',ylab="Proportions of scenarios with perfect DIF detection")
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1)
title('Proportion of perfect DIF detection in scenarios with DIF')

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='noBF',]$prop.perfect) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="blue",pch=2)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="blue")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='noLRTnoBF',]$prop.perfect) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="red",pch=4)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="red")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='noLRT',]$prop.perfect) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="green",pch=5)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="green")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='resali',]$prop.perfect) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="purple",pch=6)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="purple")
legend(x='topleft',legend=c('original','no Bonferroni correction',"no LRT","no LRT and no Bonferroni",'Residuals'),
       col=c('black',"blue","green","red",'purple'),lty=c(1,1,1,1,1),pch=c(3,2,4,5,6))



# Flexible detection

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='original',]$prop.flexible) )
)
plot(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,1),lty=1,pch=3,xlab='N',ylab="Proportions of scenarios with flexible DIF detection")
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1)
title('Proportion of flexible DIF detection in scenarios with DIF')

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='noBF',]$prop.flexible) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="blue",pch=2)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="blue")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='noLRTnoBF',]$prop.flexible) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="red",pch=4)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="red")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='noLRT',]$prop.flexible) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="green",pch=5)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="green")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='resali',]$prop.flexible) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="purple",pch=6)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="purple")
legend(x='topleft',legend=c('original','no Bonferroni correction',"no LRT","no LRT and no Bonferroni",'Residuals'),
       col=c('black',"blue","green","red",'purple'),lty=c(1,1,1,1,1),pch=c(3,2,4,5,6))


# Most flexible detection

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='original',]$prop.moreflexible) )
)
plot(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,1),lty=1,pch=3,xlab='N',ylab="Proportions of scenarios with most flexible DIF detection")
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1)
title('Proportion of most flexible DIF detection in scenarios with DIF')

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='noBF',]$prop.moreflexible) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="blue",pch=2)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="blue")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='noLRTnoBF',]$prop.moreflexible) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="red",pch=4)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="red")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='noLRT',]$prop.moreflexible) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="green",pch=5)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="green")

plot.dat <- data.frame(N=c(50,300),
                       prop.dif.detect=sapply(c(50,300),function(x) mean(res.dat.dif[res.dat.dif$N==x & res.dat.dif$nb.dif!=0 & res.dat.dif$version=='resali',]$prop.moreflexible) )
)
points(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="purple",pch=6)
lines(plot.dat$prop.dif.detect~plot.dat$N,ylim=c(0,0.05),lty=1,col="purple")
legend(x='topleft',legend=c('original','no Bonferroni correction',"no LRT","no LRT and no Bonferroni",'Residuals'),
       col=c('black',"blue","green","red",'purple'),lty=c(1,1,1,1,1),pch=c(3,2,4,5,6))