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

library(TAM)
library(doMC)
library(parallel)
library(pbmcapply)
library(funprog)
library(plyr)
library(dplyr)
library(readxl)

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

source(paste0(getwd(),"/Scripts/Analysis/functions/resali.R"))

##############################################################################
#----------------------------------------------------------------------------#
############################# ANALYSIS FUNCTIONS #############################
#----------------------------------------------------------------------------#
##############################################################################
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)
}


replicate_pcm_analysis_m4 <- function(df=NULL,treatment='TT',irtmodel='PCM2',method='MML',sequence='replication',eff.size=0,difsize=NA,nbdif=0) {
  nbitems <- sum(sapply(1:20,function(x) paste0('item',x)) %in% colnames(df))
  resp <- df[,sapply(seq(1,nbitems),function(x) paste0('item',x))]
  truebeta <- eff.size
  if (method=='MML') {
    n <- max(df[,sequence])
    print(n)
    tam1 <- lapply(seq(1,n),
                   function(x) pcm_analysis(df=df[df[,sequence]==x,],treatment=treatment,irtmodel=irtmodel)
    )
  }
  listitems <- c(sapply(c('_1','_2','_3'),function(x) paste0(sapply(seq(1,nbitems),function(x) paste0('item',x)),x)))
  returndat <- data.frame(matrix(nrow=max(df[,sequence]),ncol=length(listitems)))
  colnames(returndat) <- listitems
  for (s in seq(1,max(df[,sequence]))) {
    for (k in seq(1,nbitems)) {
      returndat[s,paste0('item',k,'_1')] <- tam1[[s]]$item[k,'AXsi_.Cat1']
      returndat[s,paste0('item',k,'_2')] <- tam1[[s]]$item[k,'AXsi_.Cat2']-tam1[[s]]$item[k,'AXsi_.Cat1']
      returndat[s,paste0('item',k,'_3')] <- tam1[[s]]$item[k,'AXsi_.Cat3']-tam1[[s]]$item[k,'AXsi_.Cat2']
    }
  }
  returndat <- returndat[,sort_by(listitems, lastChar)]
  returndat$beta <- sapply(seq(1,max(df[,sequence])),function(k) tam1[[k]]$beta[2])
  returndat$se.beta <- 1.413612*sapply(seq(1,max(df[,sequence])),function(k) tam.se(tam1[[k]])$beta$se.Dim1[2] )
  returndat$low.ci.beta <- returndat$beta-1.96*returndat$se.beta
  returndat$high.ci.beta <- returndat$beta+1.96*returndat$se.beta
  returndat$true.value.in.ci <- 1*(truebeta>returndat$low.ci.beta & truebeta<returndat$high.ci.beta)
  returndat$h0.rejected <- 1*(0<returndat$low.ci.beta | 0>returndat$high.ci.beta)
  if (truebeta==0) {
    returndat$beta.same.sign.truebeta <- NA
  } else {
    returndat$beta.same.sign.truebeta <- 1*(sign(truebeta)==sign(returndat$beta))
  }
  returndat2 <- data.frame(J=rep(nbitems,max(df[,sequence])),
                           M=1+max(df$item1),
                           N=nrow(df[df$replication==1,])/2,
                           eff.size=truebeta,
                           dif.size= difsize,
                           nb.dif= nbdif
  )
  returndat <- cbind(returndat2,returndat)
  return(returndat)
}





replicate_pcm_analysis_m2 <- function(df=NULL,treatment='TT',irtmodel='PCM2',method='MML',sequence='replication',eff.size=0,difsize=NA,nbdif=0) {
  truebeta <- eff.size
  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') {
    n <- max(df[,sequence])
    print(n)
    tam1 <- lapply(seq(1,n),
                   function(x) pcm_analysis(df=df[df[,sequence]==x,],treatment=treatment,irtmodel=irtmodel)
    )
  }
  listitems <- sapply(seq(1,nbitems),function(x) paste0('item',x))
  returndat <- data.frame(matrix(nrow=max(df[,sequence]),ncol=length(listitems)))
  colnames(returndat) <- listitems
  for (s in seq(1,max(df[,sequence]))) {
    for (k in seq(1,nbitems)) {
      returndat[s,paste0('item',k)] <- tam1[[s]]$xsi$xsi[k]
    }
  }
  returndat$beta <- sapply(seq(1,max(df[,sequence])),function(k) tam1[[k]]$beta[2])
  returndat$se.beta <- 1.413612*sapply(seq(1,max(df[,sequence])),function(k) tam.se(tam1[[k]])$beta$se.Dim1[2] )
  returndat$low.ci.beta <- returndat$beta-1.96*returndat$se.beta
  returndat$high.ci.beta <- returndat$beta+1.96*returndat$se.beta
  returndat$true.value.in.ci <- 1*(truebeta>returndat$low.ci.beta & truebeta<returndat$high.ci.beta)
  returndat$h0.rejected <- 1*(0<returndat$low.ci.beta | 0>returndat$high.ci.beta)
  if (truebeta==0) {
    returndat$beta.same.sign.truebeta <- NA
  } else {
    returndat$beta.same.sign.truebeta <- 1*(sign(truebeta)==sign(returndat$beta))
  }
  returndat2 <- data.frame(J=rep(nbitems,max(df[,sequence])),
                           M=1+max(df$item1),
                           N=nrow(df[df$replication==1,])/2,
                           eff.size=truebeta,
                           dif.size= difsize,
                           nb.dif= nbdif
  )
  returndat <- cbind(returndat2,returndat)
  return(returndat)
}


replicate_pcm_analysis<- function(df=NULL,treatment='TT',irtmodel='PCM2',method='MML',sequence='replication',eff.size=0,difsize=NA,nbdif=0) {
  j <- max(df$item1)
  if(j==1) {
    return(replicate_pcm_analysis_m2(df=df,treatment=treatment,irtmodel=irtmodel,method=method,sequence=sequence,eff.size=eff.size,difsize=difsize,nbdif=nbdif))
  } else {
    return(replicate_pcm_analysis_m4(df=df,treatment=treatment,irtmodel=irtmodel,method=method,sequence=sequence,eff.size=eff.size,difsize=difsize,nbdif=nbdif))
  }
}

##############################################################################
#----------------------------------------------------------------------------#
################################# AGGREGATION ################################
#----------------------------------------------------------------------------#
##############################################################################

#### Create data.frame

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

#results2 <- c(sapply(seq(10,20,2),function(x) paste0(x,c('A','B','C','D','E'))))

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

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

#results <- c(results,results2)

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

results2 <- c(sapply(seq(10,20,2),function(x) paste0(x,c('A','B','C','D','E'))))

results <- c(sapply(c("050",100,300),function(x) paste0(results,'_',x)))

results2 <- c(sapply(c("050",100,300),function(x) paste0(results2,'_',x)))

results <- sort(results)

results2 <- sort(results2)

results <- c(results,results2)

results <- gsub('050',"50",results)

# results <- c(sapply(1:16,function(x) c(results[x],results[x+16],results[x+32])),
#             sapply(1:30,function(x) c(results[x+48],results[x+30+48],results[x+60+48]))
#             )
#### Compiler function

compile_simulation <- function(scenario) {
  name <- as.numeric(gsub("[^0-9.-]", "", substr(scenario,start=0,stop=2)))
  if (substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50" & name<=4) {
    s <- read.csv(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/NoDIF/N50/scenario_',scenario,'.csv'))
  }
  if (substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50" & name>4) {
    s <- read.csv(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/NoDIF/N50/scenario_',scenario,'.csv'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="100" & name<=4) {
    s <- read.csv(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/NoDIF/N100/scenario_',scenario,'.csv'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="100" & name>4) {
    s <- read.csv(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/NoDIF/N100/scenario_',scenario,'_nodif.csv'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="200" & name<=4) {
    s <- read.csv(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/NoDIF/N200/scenario_',scenario,'.csv'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="200" & name>4) {
    s <- read.csv(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/NoDIF/N200/scenario_',scenario,'_nodif.csv'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="300" & name<=4) {
    s <- read.csv(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/NoDIF/N300/scenario_',scenario,'.csv'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="300" & name>4) {
    s <- read.csv(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/NoDIF/N300/scenario_',scenario,'_nodif.csv'))
  }
  if (unique(s$J)==4) {
    if (unique(s$M)==2) {
      a <- data.frame(m.item1=mean(s$item1),m.item2=mean(s$item2),m.item3=mean(s$item3),m.item4=mean(s$item4))
    } else {
      a <- data.frame(m.item1_1=mean(s$item1_1),m.item1_2=mean(s$item1_2),m.item1_3=mean(s$item1_3),
                      m.item2_1=mean(s$item2_1),m.item2_2=mean(s$item2_2),m.item2_3=mean(s$item2_3),
                      m.item3_1=mean(s$item3_1),m.item3_2=mean(s$item3_2),m.item3_3=mean(s$item3_3),
                      m.item4_1=mean(s$item4_1),m.item4_2=mean(s$item4_2),m.item4_3=mean(s$item4_3)
                      )
    }
  } else {
    if (unique(s$M)==2) {
      a <- data.frame(m.item1=mean(s$item1),m.item2=mean(s$item2),m.item3=mean(s$item3),m.item4=mean(s$item4),
                      m.item5=mean(s$item5),m.item6=mean(s$item6),m.item7=mean(s$item7))
    } else {
      a <- data.frame(m.item1_1=mean(s$item1_1),m.item1_2=mean(s$item1_2),m.item1_3=mean(s$item1_3),
                      m.item2_1=mean(s$item2_1),m.item2_2=mean(s$item2_2),m.item2_3=mean(s$item2_3),
                      m.item3_1=mean(s$item3_1),m.item3_2=mean(s$item3_2),m.item3_3=mean(s$item3_3),
                      m.item4_1=mean(s$item4_1),m.item4_2=mean(s$item4_2),m.item4_3=mean(s$item4_3),
                      m.item5_1=mean(s$item5_1),m.item5_2=mean(s$item5_2),m.item5_3=mean(s$item5_3),
                      m.item6_1=mean(s$item6_1),m.item6_2=mean(s$item6_2),m.item6_3=mean(s$item6_3),
                      m.item7_1=mean(s$item7_1),m.item7_2=mean(s$item7_2),m.item7_3=mean(s$item7_3)
      )
    }
  }
  N <- ifelse(substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50","50",substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario)))
  zz <- ifelse(N=="50",substr(scenario,start=0,stop=nchar(scenario)-3),substr(scenario,start=0,stop=nchar(scenario)-4)) 
  zz.type <- substr(zz,start=nchar(zz),stop=nchar(zz))
  
  if (unique(is.na(s$dif.size))) {
    s$dif.size <- 0
  }
  if(zz=="10B") {
    s$dif.size <- 0.3
  }
  if (substr(zz,1,1)%in%c("6","8")) {
    s$nb.dif <- 1
  }
  if (substr(zz,1,2)%in%c("10","12","14",'16')) {
    s$nb.dif <- 2
  }
  if (substr(zz,1,1)%in%c("18","20")) {
    s$nb.dif <- 3
  }
  
  if (zz.type!="A") {
  if (zz.type=="B") {
    s$eff.size <- 0.2
  } else if (zz.type=="C" & unique(s$dif.size)==0) {
    s$eff.size <- 0.4
  } else if (zz.type=="C" & unique(s$dif.size)!=0) {
    s$eff.size <- 0.2
  } else if (zz.type=="D" & unique(s$dif.size)!=0) {
    s$eff.size <- 0.4
  } else if (zz.type=="E" & unique(s$dif.size)!=0) {
    s$eff.size <- 0.4
  }
  } else {
    s$eff.size <- 0
    s$dif.size <- -1*s$dif.size
  }
  
  b <- data.frame(scenario=zz,
                  scenario.type=substr(zz,start=nchar(zz),stop=nchar(zz)),
                  N=N,
                  J=unique(s$J),
                  M=unique(s$M),
                  eff.size=unique(s$eff.size),
                  nb.dif=unique(s$nb.dif),
                  dif.size=unique(s$dif.size)
                  )
  z <- data.frame(m.beta=mean(s$beta),
                  se.empirical.beta=sd(s$beta),
                  se.analytical.beta=mean(s$se.beta),
                  m.low.ci.beta=mean(s$low.ci.beta),
                  m.high.ci.beta=mean(s$high.ci.beta),
                  bias=mean(s$beta-s$eff.size),
                  true.value.in.ci.p=mean(s$true.value.in.ci),
                  h0.rejected.p=mean(s$h0.rejected),
                  beta.same.sign.truebeta.p=mean(s$beta.same.sign.truebeta,na.rm=T),
                  beta.same.sign.truebeta.signif.p=mean(s[s$h0.rejected==1,]$beta.same.sign.truebeta,na.rm=T))
  d <- cbind(b,a,z)
  d$prop.
  return(d)
}

#### Compiled results

res.dat <- compile_simulation('2A_50')

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

res.dat[res.dat$N==50,"dif.size"] <- res.dat[which(res.dat$N==50)+1,"dif.size"]


is.nan.data.frame <- function(x) {
  do.call(cbind, lapply(x, is.nan))
}


res.dat[is.nan(res.dat)] <- NA


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

#### Create data.frame

results <- c(sapply(c(6,8),function(x) paste0(x,c('A','B','C','D','E'))))

results2 <- c(sapply(seq(10,20,2),function(x) paste0(x,c('A','B','C','D','E'))))

results <- c(sapply(c("050",100,300),function(x) paste0(results,'_',x)))

results2 <- c(sapply(c("050",100,300),function(x) paste0(results2,'_',x)))

results <- sort(results)

results2 <- sort(results2)

results <- c(results,results2)

results <- gsub('050',"50",results)


#### Compiler function

compile_simulation2 <- function(scenario) {
  name <- as.numeric(gsub("[^0-9.-]", "", substr(scenario,start=0,stop=2)))
  if (substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50" & name>4) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N50/',scenario,'.xls'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="100" & name>4) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N100/',scenario,'.xls'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="200" & name>4) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N200/',scenario,'.xls'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="300" & name>4) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N300/',scenario,'.xls'))
  }
  J <- max(which(sapply(1:7,function(x) paste0('item',x) %in% colnames(s) | paste0('item',x,'_1') %in% colnames(s))))
  M <- 1+sum(sapply(1:3,function(x) paste0('item1_',x) %in% colnames(s) ))
  if (M==1) {M <- 2}
  nb.dif <- max(which(sapply(1:3,function(x) paste0('dif',x) %in% colnames(s) | paste0('dif',x,'_1') %in% colnames(s))))
  if (J==4) {
    if (M==2) {
      a <- data.frame(m.item1=mean(s$item1),m.item2=mean(s$item2),m.item3=mean(s$item3),m.item4=mean(s$item4))
    } else {
      a <- data.frame(m.item1_1=mean(s$item1_1),m.item1_2=mean(s$item1_2),m.item1_3=mean(s$item1_3),
                      m.item2_1=mean(s$item2_1),m.item2_2=mean(s$item2_2),m.item2_3=mean(s$item2_3),
                      m.item3_1=mean(s$item3_1),m.item3_2=mean(s$item3_2),m.item3_3=mean(s$item3_3),
                      m.item4_1=mean(s$item4_1),m.item4_2=mean(s$item4_2),m.item4_3=mean(s$item4_3)
      )
    }
  } else {
    if (M==2) {
      a <- data.frame(m.item1=mean(s$item1),m.item2=mean(s$item2),m.item3=mean(s$item3),m.item4=mean(s$item4),
                      m.item5=mean(s$item5),m.item6=mean(s$item6),m.item7=mean(s$item7))
    } else {
      a <- data.frame(m.item1_1=mean(s$item1_1),m.item1_2=mean(s$item1_2),m.item1_3=mean(s$item1_3),
                      m.item2_1=mean(s$item2_1),m.item2_2=mean(s$item2_2),m.item2_3=mean(s$item2_3),
                      m.item3_1=mean(s$item3_1),m.item3_2=mean(s$item3_2),m.item3_3=mean(s$item3_3),
                      m.item4_1=mean(s$item4_1),m.item4_2=mean(s$item4_2),m.item4_3=mean(s$item4_3),
                      m.item5_1=mean(s$item5_1),m.item5_2=mean(s$item5_2),m.item5_3=mean(s$item5_3),
                      m.item6_1=mean(s$item6_1),m.item6_2=mean(s$item6_2),m.item6_3=mean(s$item6_3),
                      m.item7_1=mean(s$item7_1),m.item7_2=mean(s$item7_2),m.item7_3=mean(s$item7_3)
      )
    }
  }
  N <- ifelse(substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50","50",substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario)))
  zz <- ifelse(N=="50",substr(scenario,start=0,stop=nchar(scenario)-3),substr(scenario,start=0,stop=nchar(scenario)-4))
  eff.size <- unique(res.dat[res.dat$scenario==zz & res.dat$N==N,'eff.size'])
  dif.size <- unique(res.dat[res.dat$scenario==zz & res.dat$N==N,'dif.size'])
  b <- data.frame(scenario=zz,
                  scenario.type=substr(zz,start=nchar(zz),stop=nchar(zz)),
                  N=N,
                  J=J,
                  M=M,
                  eff.size=eff.size,
                  nb.dif=nb.dif,
                  dif.size=dif.size
  )
  true.value.in.ci <- eff.size <= s$beta+1.96*s$se_beta & eff.size >= s$beta-1.96*s$se_beta
  beta.same.sign.truebeta.p <- ifelse(rep(eff.size,nrow(s))==0,NA,(rep(eff.size,nrow(s))/s$beta)>0)
  num.reject <- which((s$beta-1.96*s$se_beta)>0 | (s$beta+1.96*s$se_beta)<0)
  z <- data.frame(m.beta=mean(s$beta),
                  se.empirical.beta=sd(s$beta),
                  se.analytical.beta=mean(s$se_beta),
                  m.low.ci.beta=mean(s$beta-1.96*s$se_beta),
                  m.high.ci.beta=mean(s$beta+1.96*s$se_beta),
                  true.value.in.ci.p=mean(true.value.in.ci),
                  h0.rejected.p=mean( (s$beta-1.96*s$se_beta)>0 | (s$beta+1.96*s$se_beta)<0 ),
                  beta.same.sign.truebeta.p=mean(beta.same.sign.truebeta.p),
                  beta.same.sign.truebeta.signif.p=mean(beta.same.sign.truebeta.p[num.reject])
  )
  d <- cbind(b,a,z)
  d$prop.
  return(d)
}


#### Compiled results

res.dat.dif <- compile_simulation2('6A_50')

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

res.dat.dif[is.na(res.dat.dif$dif.size),'dif.size'] <- 0
res.dat.dif[substr(res.dat.dif$scenario,1,1)%in%c("6","8"),'nb.dif'] <- 1
res.dat.dif[substr(res.dat.dif$scenario,1,2)%in%seq(10,16,2),'nb.dif'] <- 2
res.dat.dif[substr(res.dat.dif$scenario,1,2)%in%seq(18,20,2),'nb.dif'] <- 3
res.dat.dif[res.dat.dif$N==50,"dif.size"] <- res.dat.dif[which(res.dat.dif$N==50)+1,"dif.size"]
res.dat.dif[res.dat.dif$scenario.type=="B",]$eff.size <- 0.2
res.dat.dif[res.dat.dif$scenario.type=="C" & res.dat.dif$dif.size!=0,]$eff.size <- 0.2
res.dat.dif[res.dat.dif$scenario.type=="D" & res.dat.dif$dif.size!=0,]$eff.size <- 0.4
res.dat.dif[res.dat.dif$scenario.type=="E" & res.dat.dif$dif.size!=0,]$eff.size <- 0.4
res.dat.dif[res.dat.dif$scenario=="10B",]$dif.size <- 0.3 
res.dat.dif$bias <- res.dat.dif$eff.size-res.dat.dif$m.beta

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

#### Create data.frame


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

results2 <- c(sapply(seq(10,20,2),function(x) paste0(x,c('A','B','C','D','E'))))

results <- c(sapply(c("050",100,300),function(x) paste0(results,'_',x)))

results2 <- c(sapply(c("050",100,300),function(x) paste0(results2,'_',x)))

results <- sort(results)

results2 <- sort(results2)

results <- c(results,results2)

results <- gsub('050',"50",results)


#### Compiler function

compile_simulation2_rosali <- function(scenario) {
  name <- as.numeric(gsub("[^0-9.-]", "", substr(scenario,start=0,stop=2)))
  if (substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50" & name>0) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/ROSALI-DIF/N50/',scenario,'_original.xls'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="100" & name>0) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/ROSALI-DIF/N100/',scenario,'_original.xls'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="200" & name>0) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/ROSALI-DIF/N200/',scenario,'_original.xls'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="300" & name>0) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/ROSALI-DIF/N300/',scenario,'_original.xls'))
  }
  J <- max(which(sapply(1:7,function(x) paste0('item',x) %in% colnames(s) | paste0('item',x,'_1') %in% colnames(s))))
  M <- 1+sum(sapply(1:3,function(x) paste0('item1_',x) %in% colnames(s) ))
  if (M==1) {M <- 2}
  nb.dif.true <- ifelse(name<=4,0,ifelse(name<=8,1,ifelse(name<=16,2,3)))
  if (name %in% c(3,4,13:20)) {
    m.nb.dif.detect <- mean(ifelse(is.na(s$dif_1_1),0,
                            ifelse(is.na(s$dif_2_1),1,
                                   ifelse(is.na(s$dif_3_1),2,
                                          ifelse(is.na(s$dif_4_1),3,
                                                 ifelse(is.na(s$dif_5_1),4,
                                                        ifelse(is.na(s$dif_6_1),5,
                                                               ifelse(is.na(s$dif_7_1),6,7))))))))
  }
  if (!(name %in% c(3,4,13:20))) {
    m.nb.dif.detect <- mean(ifelse(is.na(s$dif_1_1),0,
                            ifelse(is.na(s$dif_2_1),1,
                                   ifelse(is.na(s$dif_3_1),2,
                                          ifelse(is.na(s$dif_4_1),3,4)))))
  }
  if (J==4) {
    if (M==2) {
      a <- data.frame(m.item1=mean(s$item1_1),m.item2=mean(s$item2_1),m.item3=mean(s$item3_1),m.item4=mean(s$item4_1))
    } else {
      a <- data.frame(m.item1_1=mean(s$item1_1),m.item1_2=mean(s$item1_2),m.item1_3=mean(s$item1_3),
                      m.item2_1=mean(s$item2_1),m.item2_2=mean(s$item2_2),m.item2_3=mean(s$item2_3),
                      m.item3_1=mean(s$item3_1),m.item3_2=mean(s$item3_2),m.item3_3=mean(s$item3_3),
                      m.item4_1=mean(s$item4_1),m.item4_2=mean(s$item4_2),m.item4_3=mean(s$item4_3)
      )
    }
  } else {
    if (M==2) {
      a <- data.frame(m.item1=mean(s$item1_1),m.item2=mean(s$item2_1),m.item3=mean(s$item3_1),m.item4=mean(s$item4_1),
                      m.item5=mean(s$item5_1),m.item6=mean(s$item6_1),m.item7=mean(s$item7_1))
    } else {
      a <- data.frame(m.item1_1=mean(s$item1_1),m.item1_2=mean(s$item1_2),m.item1_3=mean(s$item1_3),
                      m.item2_1=mean(s$item2_1),m.item2_2=mean(s$item2_2),m.item2_3=mean(s$item2_3),
                      m.item3_1=mean(s$item3_1),m.item3_2=mean(s$item3_2),m.item3_3=mean(s$item3_3),
                      m.item4_1=mean(s$item4_1),m.item4_2=mean(s$item4_2),m.item4_3=mean(s$item4_3),
                      m.item5_1=mean(s$item5_1),m.item5_2=mean(s$item5_2),m.item5_3=mean(s$item5_3),
                      m.item6_1=mean(s$item6_1),m.item6_2=mean(s$item6_2),m.item6_3=mean(s$item6_3),
                      m.item7_1=mean(s$item7_1),m.item7_2=mean(s$item7_2),m.item7_3=mean(s$item7_3)
      )
    }
  }
  N <- ifelse(substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50","50",substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario)))
  zz <- ifelse(N=="50",substr(scenario,start=0,stop=nchar(scenario)-3),substr(scenario,start=0,stop=nchar(scenario)-4))
  eff.size <- unique(res.dat[res.dat$scenario==zz & res.dat$N==N,'eff.size'])
  dif.size <- unique(res.dat[res.dat$scenario==zz & res.dat$N==N,'dif.size'])
  b <- data.frame(scenario=zz,
                  scenario.type=substr(zz,start=nchar(zz),stop=nchar(zz)),
                  N=N,
                  J=J,
                  M=M,
                  eff.size=eff.size,
                  nb.dif=nb.dif.true,
                  m.nb.dif.detect=m.nb.dif.detect,
                  dif.size=dif.size
  )
  true.value.in.ci <- eff.size <= s$beta+1.96*s$se_beta & eff.size >= s$beta-1.96*s$se_beta
  beta.same.sign.truebeta.p <- ifelse(rep(eff.size,nrow(s))==0,NA,(rep(eff.size,nrow(s))/s$beta)>0)
  num.reject <- which((s$beta-1.96*s$se_beta)>0 | (s$beta+1.96*s$se_beta)<0)
  dif.d <- mean(sapply(1:1000,function(x) any(!is.na(s[x,paste0("dif_",1:unique(b$J),"_1")]))))
  if (nb.dif.true==0 & unique(b$J)==4) {
    prop.perfect <- NA
    flexible.detect <- NA
    moreflexible.detect <- NA
    any.detect <- NA
    thay.tpr <- NA
    thay.fpr <- mean(sapply(1:1000,function(x) sum( !is.na(s[x,paste0("dif_detect_",1:4)]) )/4 ))
  }
  if (nb.dif.true==0 & unique(b$J)==7) {
    prop.perfect <- NA
    flexible.detect <- NA
    moreflexible.detect <- NA
    any.detect <- NA
    thay.tpr <- NA
    thay.fpr <- mean(sapply(1:1000,function(x) sum( !is.na(s[x,paste0("dif_detect_",1:7)]) )/7 ))
  }
  if (nb.dif.true==1 & unique(b$J)==4 & unique(b$M)==4) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==1,s[x,"dif_detect_unif_1"]==1  & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1")])
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==1,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1")])
                                                        ,0)  )
    flexible.detect <- mean(flexible.detect)
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- moreflexible.detect
    thay.tpr <- moreflexible.detect
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:4)])!=s[x,"real_dif_1"],na.rm=F)/4,0)  ))
  }
  if (nb.dif.true==2 & unique(b$J)==4 & unique(b$M)==4) {
      perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==2,s[x,"dif_detect_unif_1"]==1 & s[x,"dif_detect_unif_2"]==1 & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                            ,0)  )
      prop.perfect <- mean(perfect.detection)
      flexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==2,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                          ,0)  )
      flexible.detect <- mean(flexible.detect)
      moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]) &
                                                                s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
      moreflexible.detect <- mean(moreflexible.detect)
      any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]) |
                                                       s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:4)]),0) )
      any.detect <- mean(any.detect)
      thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:4)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"]),na.rm=F)/2,0)  ))
      thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:4)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"])),na.rm=F)/4,0)  ))
      
  }
  if (nb.dif.true==2 & unique(b$J)==7 & unique(b$M)==4) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==2,s[x,"dif_detect_unif_1"]==1 & s[x,"dif_detect_unif_2"]==1 & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==2,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                        ,0)  )
    flexible.detect <- mean(flexible.detect)
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) |
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]),0)  )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"]),na.rm=F)/2,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"])),na.rm=F)/7,0)  ))
    
  }
  if (nb.dif.true==3 & unique(b$J)==7 & unique(b$M)==4) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==3,s[x,"dif_detect_unif_1"]==1 & s[x,"dif_detect_unif_2"]==1 & s[x,"dif_detect_unif_3"]==1 & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[3])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <-  sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==3,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[3])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) 
                                                         ,0)  )
    flexible.detect <- mean(flexible.detect)
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]) & s[x,"real_dif_3"]%in%c(s[x,paste0("dif_detect_",1:7)]),0) )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) |
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]) | s[x,"real_dif_3"]%in%c(s[x,paste0("dif_detect_",1:7)]),0) )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"],s[x,"real_dif_3"]),na.rm=F)/3,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"],s[x,"real_dif_3"])),na.rm=F)/7,0)  ))
    
  }
  if (nb.dif.true==1 & unique(b$J)==4 & unique(b$M)==2) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==1, s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1")])
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- prop.perfect
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- moreflexible.detect
    thay.tpr <- moreflexible.detect
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:4)])!=s[x,"real_dif_1"],na.rm=F)/4,0)  ))
  }
  if (nb.dif.true==2 & unique(b$J)==4 & unique(b$M)==2) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==2,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- prop.perfect
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]) |
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:4)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"]),na.rm=F)/2,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:4)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"])),na.rm=F)/4,0)  ))
    
  }
  if (nb.dif.true==2 & unique(b$J)==7 & unique(b$M)==2) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==2,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- prop.perfect
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) |
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]),0)  )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"]),na.rm=F)/2,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"])),na.rm=F)/7,0)  ))
    
  }
  if (nb.dif.true==3 & unique(b$J)==7 & unique(b$M)==2) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==3,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[3])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- prop.perfect
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]) & s[x,"real_dif_3"]%in%c(s[x,paste0("dif_detect_",1:7)]),0) )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) |
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]) | s[x,"real_dif_3"]%in%c(s[x,paste0("dif_detect_",1:7)]),0) )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"],s[x,"real_dif_3"]),na.rm=F)/3,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"],s[x,"real_dif_3"])),na.rm=F)/7,0)  ))
    
  }
  lrt.pass <- mean(s$lrt_passed)
  z <- data.frame(m.beta=mean(s$beta),
                  se.empirical.beta=sd(s$beta),
                  se.analytical.beta=mean(s$se_beta),
                  m.low.ci.beta=mean(s$beta-1.96*s$se_beta),
                  m.high.ci.beta=mean(s$beta+1.96*s$se_beta),
                  true.value.in.ci.p=mean(true.value.in.ci),
                  h0.rejected.p=mean( (s$beta-1.96*s$se_beta)>0 | (s$beta+1.96*s$se_beta)<0 ),
                  beta.same.sign.truebeta.p=mean(beta.same.sign.truebeta.p),
                  beta.same.sign.truebeta.signif.p=mean(beta.same.sign.truebeta.p[num.reject]),
                  dif.detected=dif.d,
                  prop.perfect=prop.perfect,
                  flexible.detect=flexible.detect,
                  moreflexible.detect=moreflexible.detect,
                  any.detect=any.detect,
                  thay.tpr=thay.tpr,
                  thay.fpr=thay.fpr,
                  lrt.pass=lrt.pass
  )
  d <- cbind(b,a,z)
  d$prop.
  return(d)
}


#### Compiled results

res.dat.dif.rosali <- compile_simulation2_rosali('2A_50')

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

res.dat.dif.rosali[res.dat.dif.rosali$scenario.type=='A','dif.size'] <- -res.dat.dif.rosali[res.dat.dif.rosali$scenario.type=='A','dif.size']
res.dat.dif.rosali[is.na(res.dat.dif.rosali$dif.size),'dif.size'] <- 0
res.dat.dif.rosali[substr(res.dat.dif.rosali$scenario,1,1)%in%c("6","8"),'nb.dif'] <- 1
res.dat.dif.rosali[substr(res.dat.dif.rosali$scenario,1,2)%in%seq(10,16,2),'nb.dif'] <- 2
res.dat.dif.rosali[substr(res.dat.dif.rosali$scenario,1,2)%in%seq(18,20,2),'nb.dif'] <- 3
res.dat.dif.rosali[res.dat.dif.rosali$N==50,"dif.size"] <- res.dat.dif.rosali[which(res.dat.dif.rosali$N==50)+1,"dif.size"]
res.dat.dif.rosali[res.dat.dif.rosali$scenario.type=="B",]$eff.size <- 0.2
res.dat.dif.rosali[res.dat.dif.rosali$scenario.type=="C" & res.dat.dif.rosali$dif.size==0,]$eff.size <- 0.4
res.dat.dif.rosali[res.dat.dif.rosali$scenario.type=="C" & res.dat.dif.rosali$dif.size!=0,]$eff.size <- 0.2
res.dat.dif.rosali[res.dat.dif.rosali$scenario.type=="D" & res.dat.dif.rosali$dif.size!=0,]$eff.size <- 0.4
res.dat.dif.rosali[res.dat.dif.rosali$scenario.type=="E" & res.dat.dif.rosali$dif.size!=0,]$eff.size <- 0.4
res.dat.dif.rosali[res.dat.dif.rosali$scenario=="10B",]$dif.size <- 0.3 

is.nan.data.frame <- function(x) {
  do.call(cbind, lapply(x, is.nan))
}


res.dat.dif.rosali[is.nan(res.dat.dif.rosali)] <- NA

res.dat.dif.rosali$bias <- res.dat.dif.rosali$eff.size-res.dat.dif.rosali$m.beta


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

#### Create data.frame


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

#results2 <- c(sapply(seq(10,20,2),function(x) paste0(x,c('A','B','C','D','E'))))

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

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

#results <- c(results,results2)

#results <- c(sapply(1:16,function(x) c(results[x],results[x+16],results[x+32])),
#             sapply(1:30,function(x) c(results[x+48],results[x+30+48],results[x+60+48]))
#)


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

results2 <- c(sapply(seq(10,20,2),function(x) paste0(x,c('A','B','C','D','E'))))

results <- c(sapply(c("050",100,300),function(x) paste0(results,'_',x)))

results2 <- c(sapply(c("050",100,300),function(x) paste0(results2,'_',x)))

results <- sort(results)

results2 <- sort(results2)

results <- c(results,results2)

results <- gsub('050',"50",results)


#### Compiler function

compile_simulation2_resali <- function(scenario) {
  name <- as.numeric(gsub("[^0-9.-]", "", substr(scenario,start=0,stop=2)))
  if (substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50" & name>0) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/RESALI/Results/N50/',scenario,'_original.xls'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="100" & name>0) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/RESALI/Results/N100/',scenario,'_original.xls'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="200" & name>0) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/RESALI/Results/N200/',scenario,'_original.xls'))
  }
  if (substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario))=="300" & name>0) {
    s <- read_excel(paste0('/home/corentin/Documents/These/Recherche/Simulations/Analysis/RESALI/Results/N300/',scenario,'_original.xls'))
  }
  J <- max(which(sapply(1:7,function(x) paste0('item',x) %in% colnames(s) | paste0('item',x,'_1') %in% colnames(s))))
  M <- 1+sum(sapply(1:3,function(x) paste0('item1_',x) %in% colnames(s) ))
  if (M==1) {M <- 2}
  nb.dif.true <- ifelse(name<=4,0,ifelse(name<=8,1,ifelse(name<=16,2,3)))
  if (name %in% c(3,4,13:20)) {
    m.nb.dif.detect <- mean(ifelse(is.na(s$dif_1_1),0,
                                   ifelse(is.na(s$dif_2_1),1,
                                          ifelse(is.na(s$dif_3_1),2,
                                                 ifelse(is.na(s$dif_4_1),3,
                                                        ifelse(is.na(s$dif_5_1),4,
                                                               ifelse(is.na(s$dif_6_1),5,
                                                                      ifelse(is.na(s$dif_7_1),6,7))))))))
  }
  if (!(name %in% c(3,4,13:20))) {
    m.nb.dif.detect <- mean(ifelse(is.na(s$dif_1_1),0,
                                   ifelse(is.na(s$dif_2_1),1,
                                          ifelse(is.na(s$dif_3_1),2,
                                                 ifelse(is.na(s$dif_4_1),3,4)))))
  }
  if (J==4) {
    if (M==2) {
      a <- data.frame(m.item1=mean(s$item1_1),m.item2=mean(s$item2_1),m.item3=mean(s$item3_1),m.item4=mean(s$item4_1))
    } else {
      a <- data.frame(m.item1_1=mean(s$item1_1),m.item1_2=mean(s$item1_2),m.item1_3=mean(s$item1_3),
                      m.item2_1=mean(s$item2_1),m.item2_2=mean(s$item2_2),m.item2_3=mean(s$item2_3),
                      m.item3_1=mean(s$item3_1),m.item3_2=mean(s$item3_2),m.item3_3=mean(s$item3_3),
                      m.item4_1=mean(s$item4_1),m.item4_2=mean(s$item4_2),m.item4_3=mean(s$item4_3)
      )
    }
  } else {
    if (M==2) {
      a <- data.frame(m.item1=mean(s$item1_1),m.item2=mean(s$item2_1),m.item3=mean(s$item3_1),m.item4=mean(s$item4_1),
                      m.item5=mean(s$item5_1),m.item6=mean(s$item6_1),m.item7=mean(s$item7_1))
    } else {
      a <- data.frame(m.item1_1=mean(s$item1_1),m.item1_2=mean(s$item1_2),m.item1_3=mean(s$item1_3),
                      m.item2_1=mean(s$item2_1),m.item2_2=mean(s$item2_2),m.item2_3=mean(s$item2_3),
                      m.item3_1=mean(s$item3_1),m.item3_2=mean(s$item3_2),m.item3_3=mean(s$item3_3),
                      m.item4_1=mean(s$item4_1),m.item4_2=mean(s$item4_2),m.item4_3=mean(s$item4_3),
                      m.item5_1=mean(s$item5_1),m.item5_2=mean(s$item5_2),m.item5_3=mean(s$item5_3),
                      m.item6_1=mean(s$item6_1),m.item6_2=mean(s$item6_2),m.item6_3=mean(s$item6_3),
                      m.item7_1=mean(s$item7_1),m.item7_2=mean(s$item7_2),m.item7_3=mean(s$item7_3)
      )
    }
  }
  N <- ifelse(substr(scenario,start=nchar(scenario)-1,stop=nchar(scenario))=="50","50",substr(scenario,start=nchar(scenario)-2,stop=nchar(scenario)))
  zz <- ifelse(N=="50",substr(scenario,start=0,stop=nchar(scenario)-3),substr(scenario,start=0,stop=nchar(scenario)-4))
  eff.size <- unique(res.dat[res.dat$scenario==zz & res.dat$N==N,'eff.size'])
  dif.size <- unique(res.dat[res.dat$scenario==zz & res.dat$N==N,'dif.size'])
  b <- data.frame(scenario=zz,
                  scenario.type=substr(zz,start=nchar(zz),stop=nchar(zz)),
                  N=N,
                  J=J,
                  M=M,
                  eff.size=eff.size,
                  nb.dif=nb.dif.true,
                  m.nb.dif.detect=m.nb.dif.detect,
                  dif.size=dif.size
  )
  true.value.in.ci <- eff.size <= s$beta+1.96*s$se_beta & eff.size >= s$beta-1.96*s$se_beta
  beta.same.sign.truebeta.p <- ifelse(rep(eff.size,nrow(s))==0,NA,(rep(eff.size,nrow(s))/s$beta)>0)
  num.reject <- which((s$beta-1.96*s$se_beta)>0 | (s$beta+1.96*s$se_beta)<0)
  dif.d <- mean(sapply(1:1000,function(x) any(!is.na(s[x,paste0("dif_",1:unique(b$J),"_1")]))))
  if (nb.dif.true==0 & unique(b$J)==4) {
    prop.perfect <- NA
    flexible.detect <- NA
    moreflexible.detect <- NA
    any.detect <- NA
    thay.tpr <- NA
    thay.fpr <- mean(sapply(1:1000,function(x) sum( !is.na(s[x,paste0("dif_detect_",1:4)]) )/4 ))
  }
  if (nb.dif.true==0 & unique(b$J)==7) {
    prop.perfect <- NA
    flexible.detect <- NA
    moreflexible.detect <- NA
    any.detect <- NA
    thay.tpr <- NA
    thay.fpr <- mean(sapply(1:1000,function(x) sum( !is.na(s[x,paste0("dif_detect_",1:7)]) )/7 ))
  }
  if (nb.dif.true==1 & unique(b$J)==4 & unique(b$M)==4) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==1,s[x,"dif_detect_unif_1"]==1  & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1")])
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==1,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1")])
                                                        ,0)  )
    flexible.detect <- mean(flexible.detect)
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- moreflexible.detect
    thay.tpr <- moreflexible.detect
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:4)])!=s[x,"real_dif_1"],na.rm=F)/4,0)  ))
  }
  if (nb.dif.true==2 & unique(b$J)==4 & unique(b$M)==4) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==2,s[x,"dif_detect_unif_1"]==1 & s[x,"dif_detect_unif_2"]==1 & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==2,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                        ,0)  )
    flexible.detect <- mean(flexible.detect)
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]) |
                                                     s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:4)]),0) )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:4)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"]),na.rm=F)/2,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:4)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"])),na.rm=F)/4,0)  ))
    
  }
  if (nb.dif.true==2 & unique(b$J)==7 & unique(b$M)==4) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==2,s[x,"dif_detect_unif_1"]==1 & s[x,"dif_detect_unif_2"]==1 & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==2,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                        ,0)  )
    flexible.detect <- mean(flexible.detect)
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) |
                                                     s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]),0)  )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"]),na.rm=F)/2,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"])),na.rm=F)/7,0)  ))
    
  }
  if (nb.dif.true==3 & unique(b$J)==7 & unique(b$M)==4) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==3,s[x,"dif_detect_unif_1"]==1 & s[x,"dif_detect_unif_2"]==1 & s[x,"dif_detect_unif_3"]==1 & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[3])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <-  sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==3,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[3])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) 
                                                         ,0)  )
    flexible.detect <- mean(flexible.detect)
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]) & s[x,"real_dif_3"]%in%c(s[x,paste0("dif_detect_",1:7)]),0) )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) |
                                                     s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]) | s[x,"real_dif_3"]%in%c(s[x,paste0("dif_detect_",1:7)]),0) )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"],s[x,"real_dif_3"]),na.rm=F)/3,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"],s[x,"real_dif_3"])),na.rm=F)/7,0)  ))
    
  }
  if (nb.dif.true==1 & unique(b$J)==4 & unique(b$M)==2) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==1, s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1")])
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- prop.perfect
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- moreflexible.detect
    thay.tpr <- moreflexible.detect
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:4)])!=s[x,"real_dif_1"],na.rm=F)/4,0)  ))
  }
  if (nb.dif.true==2 & unique(b$J)==4 & unique(b$M)==2) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))==2,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:4),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- prop.perfect
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:4)]) |
                                                     s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:4)]),0)  )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:4)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"]),na.rm=F)/2,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:4)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:4)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"])),na.rm=F)/4,0)  ))
    
  }
  if (nb.dif.true==2 & unique(b$J)==7 & unique(b$M)==2) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==2,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- prop.perfect
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]),0)  )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) |
                                                     s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]),0)  )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"]),na.rm=F)/2,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"])),na.rm=F)/7,0)  ))
    
  }
  if (nb.dif.true==3 & unique(b$J)==7 & unique(b$M)==2) {
    perfect.detection <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))==3,s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[1])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[2])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) & s[x,paste0('dif_detect_',which(sapply(paste0("dif_detect_",1:7),function(y) !is.na(s[x,y])))[3])]%in%c(s[x,c("real_dif_1","real_dif_2","real_dif_3")]) 
                                                          ,0)  )
    prop.perfect <- mean(perfect.detection)
    flexible.detect <- prop.perfect
    moreflexible.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) &
                                                              s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]) & s[x,"real_dif_3"]%in%c(s[x,paste0("dif_detect_",1:7)]),0) )
    moreflexible.detect <- mean(moreflexible.detect)
    any.detect <- sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,s[x,"real_dif_1"]%in%c(s[x,paste0("dif_detect_",1:7)]) |
                                                     s[x,"real_dif_2"]%in%c(s[x,paste0("dif_detect_",1:7)]) | s[x,"real_dif_3"]%in%c(s[x,paste0("dif_detect_",1:7)]),0) )
    any.detect <- mean(any.detect)
    thay.tpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"],s[x,"real_dif_3"]),na.rm=F)/3,0)  ))
    thay.fpr <- mean(sapply(1:1000,function(x) ifelse(sum(!is.na(s[x,paste0("dif_detect_",1:7)]))!=0,sum(!(unlist(s[x,paste0("dif_detect_",1:7)])%in%c(s[x,"real_dif_1"],s[x,"real_dif_2"],s[x,"real_dif_3"])),na.rm=F)/7,0)  ))
    
  }
  z <- data.frame(m.beta=mean(s$beta),
                  se.empirical.beta=sd(s$beta),
                  se.analytical.beta=mean(s$se_beta),
                  m.low.ci.beta=mean(s$beta-1.96*s$se_beta),
                  m.high.ci.beta=mean(s$beta+1.96*s$se_beta),
                  true.value.in.ci.p=mean(true.value.in.ci),
                  h0.rejected.p=mean( (s$beta-1.96*s$se_beta)>0 | (s$beta+1.96*s$se_beta)<0 ),
                  beta.same.sign.truebeta.p=mean(beta.same.sign.truebeta.p),
                  beta.same.sign.truebeta.signif.p=mean(beta.same.sign.truebeta.p[num.reject]),
                  dif.detected=dif.d,
                  prop.perfect=prop.perfect,
                  flexible.detect=flexible.detect,
                  moreflexible.detect=moreflexible.detect,
                  any.detect=any.detect
  )
  d <- cbind(b,a,z)
  d$prop.
  return(d)
}


#### Compiled results

res.dat.dif.resali <- compile_simulation2_resali('2A_50')

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

res.dat.dif.resali[res.dat.dif.resali$scenario.type=='A','dif.size'] <- -res.dat.dif.resali[res.dat.dif.resali$scenario.type=='A','dif.size']
res.dat.dif.resali[is.na(res.dat.dif.resali$dif.size),'dif.size'] <- 0
res.dat.dif.resali[substr(res.dat.dif.resali$scenario,1,1)%in%c("6","8"),'nb.dif'] <- 1
res.dat.dif.resali[substr(res.dat.dif.resali$scenario,1,2)%in%seq(10,16,2),'nb.dif'] <- 2
res.dat.dif.resali[substr(res.dat.dif.resali$scenario,1,2)%in%seq(18,20,2),'nb.dif'] <- 3
res.dat.dif.resali[res.dat.dif.resali$N==50,"dif.size"] <- res.dat.dif.resali[which(res.dat.dif.resali$N==50)+1,"dif.size"]
res.dat.dif.resali[res.dat.dif.resali$scenario.type=="B",]$eff.size <- 0.2
res.dat.dif.resali[res.dat.dif.resali$scenario=="20E" & res.dat.dif.resali$N==50,]$dif.size <- -0.5
res.dat.dif.resali[res.dat.dif.resali$scenario.type=="C" & res.dat.dif.resali$dif.size==0,]$eff.size <- 0.4
res.dat.dif.resali[res.dat.dif.resali$scenario.type=="C" & res.dat.dif.resali$dif.size!=0,]$eff.size <- 0.2
res.dat.dif.resali[res.dat.dif.resali$scenario.type=="D" & res.dat.dif.resali$dif.size!=0,]$eff.size <- 0.4
res.dat.dif.resali[res.dat.dif.resali$scenario.type=="E" & res.dat.dif.resali$dif.size!=0,]$eff.size <- 0.4
res.dat.dif.resali[res.dat.dif.resali$scenario=="10B",]$dif.size <- 0.3 

is.nan.data.frame <- function(x) {
  do.call(cbind, lapply(x, is.nan))
}


res.dat.dif.resali[is.nan(res.dat.dif.resali)] <- NA

res.dat.dif.resali$bias <- res.dat.dif.resali$eff.size-res.dat.dif.resali$m.beta



##############################################################################
#----------------------------------------------------------------------------#
################################## RASCHPOWER ################################
#----------------------------------------------------------------------------#
##############################################################################

###### Puissance théorique

res.dat$theoretical.power <- 0

### Scénarios N=100

## Scénarios J=4 / M=4

res.dat[res.dat$scenario %in% paste0(c(2,6,8,10,12),'A') & res.dat$N==100,]$theoretical.power <- 0.05
res.dat[res.dat$scenario %in% paste0(c(2,6,8,10,12),'B') & res.dat$N==100,]$theoretical.power <- 0.2177
res.dat[res.dat$scenario %in% paste0(c(6,8,10,12),'C') & res.dat$N==100,]$theoretical.power <- 0.2177
res.dat[res.dat$scenario %in% paste0(c(6,8,10,12),'D') & res.dat$N==100,]$theoretical.power <- 0.6586
res.dat[res.dat$scenario %in% paste0(c(6,8,10,12),'E') & res.dat$N==100,]$theoretical.power <- 0.6586

res.dat[res.dat$scenario %in% paste0(2,'C') & res.dat$N==100,]$theoretical.power <- 0.6586

## Scénarios J=7 / M=4

res.dat[res.dat$scenario %in% paste0(c(4,14,16,18,20),'A') & res.dat$N==100,]$theoretical.power <- 0.05
res.dat[res.dat$scenario %in% paste0(c(4,14,16,18,20),'B') & res.dat$N==100,]$theoretical.power <- 0.2450
res.dat[res.dat$scenario %in% paste0(c(14,16,18,20),'C') & res.dat$N==100,]$theoretical.power <- 0.2450
res.dat[res.dat$scenario %in% paste0(c(14,16,18,20),'D') & res.dat$N==100,]$theoretical.power <- 0.7136
res.dat[res.dat$scenario %in% paste0(c(14,16,18,20),'E') & res.dat$N==100,]$theoretical.power <- 0.7136

res.dat[res.dat$scenario %in% paste0(4,'C') & res.dat$N==100,]$theoretical.power <- 0.7136



### Scénarios N=300

## Scénarios J=4 / M=4

res.dat[res.dat$scenario %in% paste0(c(2,6,8,10,12),'A') & res.dat$N==300,]$theoretical.power <- 0.05
res.dat[res.dat$scenario %in% paste0(c(2,6,8,10,12),'B') & res.dat$N==300,]$theoretical.power <- 0.5373
res.dat[res.dat$scenario %in% paste0(c(6,8,10,12),'C') & res.dat$N==300,]$theoretical.power <- 0.5373
res.dat[res.dat$scenario %in% paste0(c(6,8,10,12),'D') & res.dat$N==300,]$theoretical.power <- 0.9834
res.dat[res.dat$scenario %in% paste0(c(6,8,10,12),'E') & res.dat$N==300,]$theoretical.power <- 0.9834

res.dat[res.dat$scenario %in% paste0(2,'C') & res.dat$N==300,]$theoretical.power <- 0.9834

## Scénarios J=7 / M=4

res.dat[res.dat$scenario %in% paste0(c(4,14,16,18,20),'A') & res.dat$N==300,]$theoretical.power <- 0.05
res.dat[res.dat$scenario %in% paste0(c(4,14,16,18,20),'B') & res.dat$N==300,]$theoretical.power <- 0.5907
res.dat[res.dat$scenario %in% paste0(c(14,16,18,20),'C') & res.dat$N==300,]$theoretical.power <- 0.5907
res.dat[res.dat$scenario %in% paste0(c(14,16,18,20),'D') & res.dat$N==300,]$theoretical.power <- 0.9919
res.dat[res.dat$scenario %in% paste0(c(14,16,18,20),'E') & res.dat$N==300,]$theoretical.power <- 0.9919

res.dat[res.dat$scenario %in% paste0(4,'C') & res.dat$N==300,]$theoretical.power <- 0.9919


### Scénarios N=50

## Scénarios J=4 / M=4

res.dat[res.dat$scenario %in% paste0(c(2,6,8,10,12),'A') & res.dat$N==50,]$theoretical.power <- 0.05
res.dat[res.dat$scenario %in% paste0(c(2,6,8,10,12),'B') & res.dat$N==50,]$theoretical.power <- 0.1339
res.dat[res.dat$scenario %in% paste0(c(6,8,10,12),'C') & res.dat$N==50,]$theoretical.power <- 0.1339
res.dat[res.dat$scenario %in% paste0(c(6,8,10,12),'D') & res.dat$N==50,]$theoretical.power <- 0.3863
res.dat[res.dat$scenario %in% paste0(c(6,8,10,12),'E') & res.dat$N==50,]$theoretical.power <- 0.3863

res.dat[res.dat$scenario %in% paste0(2,'C') & res.dat$N==50,]$theoretical.power <- 0.3863

## Scénarios J=7 / M=4

res.dat[res.dat$scenario %in% paste0(c(4,14,16,18,20),'A') & res.dat$N==50,]$theoretical.power <- 0.05
res.dat[res.dat$scenario %in% paste0(c(4,14,16,18,20),'B') & res.dat$N==50,]$theoretical.power <- 0.1448
res.dat[res.dat$scenario %in% paste0(c(14,16,18,20),'C') & res.dat$N==50,]$theoretical.power <- 0.1448
res.dat[res.dat$scenario %in% paste0(c(14,16,18,20),'D') & res.dat$N==50,]$theoretical.power <- 0.4328
res.dat[res.dat$scenario %in% paste0(c(14,16,18,20),'E') & res.dat$N==50,]$theoretical.power <- 0.4328

res.dat[res.dat$scenario %in% paste0(4,'C') & res.dat$N==50,]$theoretical.power <- 0.4328

### DIF scenarios

res.dat.dif$theoretical.power <- res.dat[res.dat$dif.size!=0,]$theoretical.power
res.dat.dif.rosali$theoretical.power <- res.dat$theoretical.power
res.dat.dif.resali$theoretical.power <- res.dat$theoretical.power


##############################################################################
#----------------------------------------------------------------------------#
######################### AGGREGATION OF ALL METHODS #########################
#----------------------------------------------------------------------------#
##############################################################################

# Items dichotomiques

res.dat$method <- "NONE"
res.dat.dif$method <- "PERFECT"
res.dat.dif.rosali$method <- "ROSALI"
res.dat.dif.resali$method <- "RESIDUS"


# Items polytomiques

res.dat.full <- res.dat[res.dat$M==4,]
res.dat.full <- rbind(res.dat.full,res.dat.dif[res.dat.dif$M==4,])
res.dat.full <- rbind.fill(res.dat.full,res.dat.dif.rosali[res.dat.dif.rosali$M==4,])
res.dat.full <- rbind.fill(res.dat.full,res.dat.dif.resali[res.dat.dif.resali$M==4,])

##############################################################################
#----------------------------------------------------------------------------#
############################ ARTICLE TABLE OUTPUT ############################
#----------------------------------------------------------------------------#
##############################################################################

# STRATEGY 1 - IGNORE DIF

res.dat.article <- res.dat[,c("N","J","eff.size","nb.dif","dif.size",
                              "m.beta","bias","true.value.in.ci.p","h0.rejected.p",
                              "theoretical.power")]
colnames(res.dat.article)[3] <- "true.beta"
colnames(res.dat.article)[5] <- "true.gamma"
colnames(res.dat.article)[6] <- "betahat"
colnames(res.dat.article)[8] <- "coverage"
colnames(res.dat.article)[9] <- "power"
res.dat.article[,6:10] <- round(res.dat.article[,6:10],2)
res.dat.article[res.dat.article$true.beta==0,"typeIerror"] <- res.dat.article[res.dat.article$true.beta==0,"power"]
res.dat.article[res.dat.article$true.beta==0,"power"] <- NA
res.dat.article <- res.dat.article[,c(1:7,11,9:10,8)]
res.dat.article[res.dat.article$nb.dif==0,"true.gamma"] <- NA
res.dat.article[is.na(res.dat.article)] <- " "
res.dat.article$bias <- -1*res.dat.article$bias
res.dat.article.ignore <- reshape(res.dat.article[res.dat.article$nb.dif>0,],
                                  direction = "wide", idvar = c("J","true.beta","nb.dif",'true.gamma'),timevar = "N" )
write.csv(res.dat.article.ignore,"/home/corentin/Documents/These/Valorisation/Articles/Simulations 1/Tables/res_ignoreDIF.csv")
res.dat.article.nodif.long <- res.dat.article[res.dat.article$nb.dif==0,]
res.dat.article.nodif <- reshape(res.dat.article[res.dat.article$nb.dif==0,],
                                  direction = "wide", idvar = c("J","true.beta","nb.dif",'true.gamma'),timevar = "N" )
write.csv(res.dat.article.nodif,"/home/corentin/Documents/These/Valorisation/Articles/Simulations 1/Tables/res_noDIF.csv")
res.dat.article <- reshape(res.dat.article[res.dat.article$nb.dif==0,],
                                 direction = "wide", idvar = c("J","true.beta","nb.dif",'true.gamma'),timevar = "N" )
write.csv(res.dat.article.nodif,"/home/corentin/Documents/These/Valorisation/Articles/Simulations 1/Tables/res_noDIF.csv")



res.dat.article.2 <- res.dat[,c("N","J","eff.size","nb.dif","dif.size",
                              "m.beta","bias","true.value.in.ci.p","h0.rejected.p",
                              "theoretical.power")]
colnames(res.dat.article.2)[3] <- "true.beta"
colnames(res.dat.article.2)[5] <- "true.gamma"
colnames(res.dat.article.2)[6] <- "betahat"
colnames(res.dat.article.2)[8] <- "coverage"
colnames(res.dat.article.2)[9] <- "power"
res.dat.article.2[,6:10] <- round(res.dat.article.2[,6:10],2)
res.dat.article.2[res.dat.article.2$true.beta==0,"typeIerror"] <- res.dat.article.2[res.dat.article.2$true.beta==0,"power"]
res.dat.article.2[res.dat.article.2$true.beta==0,"power"] <- NA
res.dat.article.2 <- res.dat.article.2[,c(1:7,11,9:10,8)]
res.dat.article.2[res.dat.article.2$nb.dif==0,"true.gamma"] <- NA
res.dat.article.2[is.na(res.dat.article.2)] <- " "
res.dat.article.2$bias <- -1*res.dat.article.2$bias
res.dat.article.nodif.2 <- res.dat.article.2[res.dat.article.2$nb.dif==0,]
  
# STRATEGY 2 - ROSALI

res.dat.article.rosali <- res.dat.dif.rosali[,c("N","J","eff.size","nb.dif","dif.size",
                              "m.beta","bias","true.value.in.ci.p","h0.rejected.p",
                              "theoretical.power")]
colnames(res.dat.article.rosali)[3] <- "true.beta"
colnames(res.dat.article.rosali)[5] <- "true.gamma"
colnames(res.dat.article.rosali)[6] <- "betahat"
colnames(res.dat.article.rosali)[8] <- "coverage"
colnames(res.dat.article.rosali)[9] <- "power"
res.dat.article.rosali[res.dat.article.rosali$true.beta==0,"typeIerror"] <- res.dat.article.rosali[res.dat.article.rosali$true.beta==0,"power"]
res.dat.article.rosali[res.dat.article.rosali$true.beta==0,"power"] <- NA
res.dat.article.rosali <- res.dat.article.rosali[,c(1:7,11,9:10,8)]
res.dat.article.rosali[res.dat.article.rosali$nb.dif==0,"true.gamma"] <- NA
res.dat.article.rosali[is.na(res.dat.article.rosali)] <- " "
res.dat.article.rosali$bias <- -1*res.dat.article.rosali$bias
res.dat.article.rosali <- reshape(res.dat.article.rosali,
                                   direction = "wide", idvar = c("J","true.beta","nb.dif",'true.gamma'),timevar = "N" )
res.dat.article.rosali.dif <- res.dat.article.rosali[res.dat.article.rosali$nb.dif>0,]
write.csv(res.dat.article.rosali.dif,"/home/corentin/Documents/These/Valorisation/Articles/Simulations 1/Tables/res_ROSALI_DIF.csv")
res.dat.article.rosali.nodif <- res.dat.article.rosali[res.dat.article.rosali$nb.dif==0,]
write.csv(res.dat.article.rosali.nodif,"/home/corentin/Documents/These/Valorisation/Articles/Simulations 1/Tables/res_ROSALI_noDIF.csv")


res.dat.article.rosali.2 <- res.dat.dif.rosali[,c("N","J","eff.size","nb.dif","dif.size",
                                                "m.beta","bias","true.value.in.ci.p","h0.rejected.p",
                                                "theoretical.power")]
colnames(res.dat.article.rosali.2)[3] <- "true.beta"
colnames(res.dat.article.rosali.2)[5] <- "true.gamma"
colnames(res.dat.article.rosali.2)[6] <- "betahat"
colnames(res.dat.article.rosali.2)[8] <- "coverage"
colnames(res.dat.article.rosali.2)[9] <- "power"
res.dat.article.rosali.2[res.dat.article.rosali.2$true.beta==0,"typeIerror"] <- res.dat.article.rosali.2[res.dat.article.rosali.2$true.beta==0,"power"]
res.dat.article.rosali.2[res.dat.article.rosali.2$true.beta==0,"power"] <- NA
res.dat.article.rosali.2 <- res.dat.article.rosali.2[,c(1:7,11,9:10,8)]
res.dat.article.rosali.2[res.dat.article.rosali.2$nb.dif==0,"true.gamma"] <- NA
res.dat.article.rosali.2[is.na(res.dat.article.rosali.2)] <- " "
res.dat.article.rosali.2$bias <- -1*res.dat.article.rosali.2$bias
res.dat.article.rosali.2.nodif <- res.dat.article.rosali.2[res.dat.article.rosali.2$nb.dif==0,]

# STRATEGY 3 - RESIDIF
res.dat.dif.resali[1,"N"] <- 50
res.dat.dif.resali$dif.size <- res.dat.dif.rosali$dif.size
res.dat.article.residif <- res.dat.dif.resali[,c("N","J","eff.size","nb.dif","dif.size",
                                         "m.beta","bias","true.value.in.ci.p","h0.rejected.p",
                                         "theoretical.power")]
colnames(res.dat.article.residif)[3] <- "true.beta"
colnames(res.dat.article.residif)[5] <- "true.gamma"
colnames(res.dat.article.residif)[6] <- "betahat"
colnames(res.dat.article.residif)[8] <- "coverage"
colnames(res.dat.article.residif)[9] <- "power"
res.dat.article.residif[res.dat.article.residif$true.beta==0,"typeIerror"] <- res.dat.article.residif[res.dat.article.residif$true.beta==0,"power"]
res.dat.article.residif[res.dat.article.residif$true.beta==0,"power"] <- NA
res.dat.article.residif <- res.dat.article.residif[,c(1:7,11,9:10,8)]
res.dat.article.residif[res.dat.article.residif$nb.dif==0,"true.gamma"] <- NA
res.dat.article.residif[is.na(res.dat.article.residif)] <- " "
res.dat.article.residif$bias <- -1*res.dat.article.residif$bias
res.dat.article.residif <- reshape(res.dat.article.residif,
                               direction = "wide", idvar = c("J","true.beta","nb.dif",'true.gamma'),timevar = "N")
res.dat.article.residif.dif <- res.dat.article.residif[res.dat.article.residif$nb.dif>0,]
write.csv(res.dat.article.residif.dif,"/home/corentin/Documents/These/Valorisation/Articles/Simulations 1/Tables/res_RESIDIF_DIF.csv")
res.dat.article.residif.nodif <- res.dat.article.residif[res.dat.article.residif$nb.dif==0,]
write.csv(res.dat.article.residif.nodif,"/home/corentin/Documents/These/Valorisation/Articles/Simulations 1/Tables/res_RESIDIF_noDIF.csv")

res.dat.article.residif.2 <- res.dat.dif.resali[,c("N","J","eff.size","nb.dif","dif.size",
                                                 "m.beta","bias","true.value.in.ci.p","h0.rejected.p",
                                                 "theoretical.power")]
colnames(res.dat.article.residif.2)[3] <- "true.beta"
colnames(res.dat.article.residif.2)[5] <- "true.gamma"
colnames(res.dat.article.residif.2)[6] <- "betahat"
colnames(res.dat.article.residif.2)[8] <- "coverage"
colnames(res.dat.article.residif.2)[9] <- "power"
res.dat.article.residif.2[res.dat.article.residif.2$true.beta==0,"typeIerror"] <- res.dat.article.residif.2[res.dat.article.residif.2$true.beta==0,"power"]
res.dat.article.residif.2[res.dat.article.residif.2$true.beta==0,"power"] <- NA
res.dat.article.residif.2 <- res.dat.article.residif.2[,c(1:7,11,9:10,8)]
res.dat.article.residif.2[res.dat.article.residif.2$nb.dif==0,"true.gamma"] <- NA
res.dat.article.residif.2[is.na(res.dat.article.residif.2)] <- " "
res.dat.article.residif.2$bias <- -1*res.dat.article.residif.2$bias
res.dat.article.residif.2.nodif <- res.dat.article.residif.2[res.dat.article.residif.2$nb.dif==0,]
res.dat.article.residif.dif

# STRATEGY 4 - PERFECT-DIF

res.dat.article.dif <- res.dat.dif[,c("N","J","eff.size","nb.dif","dif.size",
                                         "m.beta","bias","true.value.in.ci.p","h0.rejected.p",
                                         "theoretical.power")]
colnames(res.dat.article.dif)[3] <- "true.beta"
colnames(res.dat.article.dif)[5] <- "true.gamma"
colnames(res.dat.article.dif)[6] <- "betahat"
colnames(res.dat.article.dif)[8] <- "coverage"
colnames(res.dat.article.dif)[9] <- "power"
res.dat.article.dif[res.dat.article.dif$true.beta==0,"typeIerror"] <- res.dat.article.dif[res.dat.article.dif$true.beta==0,"power"]
res.dat.article.dif[res.dat.article.dif$true.beta==0,"power"] <- NA
res.dat.article.dif <- res.dat.article.dif[,c(1:7,11,9:10,8)]
res.dat.article.dif[res.dat.article.dif$nb.dif==0,"true.gamma"] <- NA
res.dat.article.dif[is.na(res.dat.article.dif)] <- " "
res.dat.article.dif$bias <- -1*res.dat.article.dif$bias
write.csv(res.dat.article.dif,"/home/corentin/Documents/These/Valorisation/Articles/Simulations 1/Tables/res_perfect.csv")
res.dat.article.dif <- reshape(res.dat.article.dif,
                                  direction = "wide", idvar = c("J","true.beta","nb.dif",'true.gamma'),timevar = "N" )
write.csv(res.dat.article.dif,"/home/corentin/Documents/These/Valorisation/Articles/Simulations 1/Tables/res_perfect.csv")


res.dat.article.dif.2 <- res.dat.dif[,c("N","J","eff.size","nb.dif","dif.size",
                                      "m.beta","bias","true.value.in.ci.p","h0.rejected.p",
                                      "theoretical.power")]
colnames(res.dat.article.dif.2)[3] <- "true.beta"
colnames(res.dat.article.dif.2)[5] <- "true.gamma"
colnames(res.dat.article.dif.2)[6] <- "betahat"
colnames(res.dat.article.dif.2)[8] <- "coverage"
colnames(res.dat.article.dif.2)[9] <- "power"
res.dat.article.dif.2[res.dat.article.dif.2$true.beta==0,"typeIerror"] <- res.dat.article.dif.2[res.dat.article.dif.2$true.beta==0,"power"]
res.dat.article.dif.2[res.dat.article.dif.2$true.beta==0,"power"] <- NA
res.dat.article.dif.2 <- res.dat.article.dif.2[,c(1:7,11,9:10,8)]
res.dat.article.dif.2[res.dat.article.dif.2$nb.dif==0,"true.gamma"] <- NA
res.dat.article.dif.2[is.na(res.dat.article.dif.2)] <- " "
res.dat.article.dif.2$bias <- -1*res.dat.article.dif.2$bias
