*=================================================================================================================================================
* Date : 2024-03-04
* Stata version : Stata 18 SE
*
* This program evaluates DIF detection performance for multiple ROSALI versions
*
* ado-files needed : - rosali_nolrt, rosali_nobf, rosali_nolrt_nobf, rosali_original
*
*
*================================================================================================================================================

* Load pcm.ado
adopath+"/home/corentin/Documents/These/Recherche/ROSALI-SIM/Modules/rosali_custom"




  ************************************************** Version 1: original
  local N = "50 300"
  local ss = "2 4 8 16"
  foreach s in `ss' {
  foreach Nnn in `N' {
  local Nn =  `Nnn'
  local path_data = "/home/corentin/Documents/These/Recherche/ROSALI-SIM/Data/N`Nn'"
  local path_res = "/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/original/N`Nn'"
  local scenarios = "A B C"
  foreach scen in `scenarios' {
  clear
  import delim "`path_data'/scenario_`s'`scen'_`Nn'.csv", encoding(ISO-8859-2)  case(preserve) clear

  if (`s'<=2) {
    local nbitems=4
  }
  else if (`s'<=4) {
    local nbitems=7
  }
  else if (`s'<=12) {
    local nbitems=4
  }
  else {
    local nbitems=7
  }

  if (`s'<=4) {
    local nbdif=0
  }
  else if (`s'<=8) {
    local nbdif=1
  }
  else if (`s'<=16) {
    local nbdif=2
  }
  else {
    local nbdif=3
  }
  local taillemat=2*`nbitems'+3+`nbdif'

  mat outmat = J(1000,`taillemat',.)
  di "Scenario `s'`scen' / N=`Nnn'"
  forvalues k=1/1000 {
    if (mod(`k',100)==0) {
      di "`k'/1000"
    }
    preserve
    qui keep if replication==`k'
    qui rosali_original item1-item`nbitems' item1-item`nbitems', group(TT)
    qui mat resmat=r(difitems)
    local nbitems2 = 2*`nbitems'
    forvalues jj=1/`nbitems2' {
        qui mat outmat[`k',`jj']=resmat[1,`jj']
    }
    qui mat outmat[`k',2*`nbitems'+1]=`Nn'
    qui mat outmat[`k',2*`nbitems'+2]=`nbitems'
    qui mat outmat[`k',2*`nbitems'+3]=`nbdif'
    if (`nbdif' > 0) {
      qui levelsof dif1
      local ldif1 = r(levels)
      local diff1: word 1 of `ldif1'
      qui mat outmat[`k',2*`nbitems'+4]=`diff1'
      if (`nbdif' > 1) {
        qui levelsof dif2
        local ldif2 = r(levels)
        local diff2: word 1 of `ldif2'
        qui mat outmat[`k',2*`nbitems'+5]=`diff2'
        if (`nbdif' > 2) {
          qui levelsof dif3
          local ldif3 = r(levels)
          local diff3: word 1 of `ldif3'
          qui mat outmat[`k',2*`nbitems'+6]=`diff3'
        }
      }
    }
    restore
  }
  putexcel set "`path_res'/`s'`scen'_`Nn'_original.xls", sheet("outmat") replace
  putexcel A1=matrix(outmat), colnames
}
}
}



  ************************************************** Version 2: noBF
  local N = "50 300"
  local ss = "2 4 8 16"
  foreach s in `ss' {
  foreach Nnn in `N' {
  local Nn =  `Nnn'
  local path_data = "/home/corentin/Documents/These/Recherche/ROSALI-SIM/Data/N`Nn'"
  local path_res = "/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/noBF/N`Nn'"
  local scenarios = "A B C"
  foreach scen in `scenarios' {
  clear
  import delim "`path_data'/scenario_`s'`scen'_`Nn'.csv", encoding(ISO-8859-2)  case(preserve) clear

  if (`s'<=2) {
    local nbitems=4
  }
  else if (`s'<=4) {
    local nbitems=7
  }
  else if (`s'<=12) {
    local nbitems=4
  }
  else {
    local nbitems=7
  }

  if (`s'<=4) {
    local nbdif=0
  }
  else if (`s'<=8) {
    local nbdif=1
  }
  else if (`s'<=16) {
    local nbdif=2
  }
  else {
    local nbdif=3
  }
  local taillemat=2*`nbitems'+3+`nbdif'

  mat outmat = J(1000,`taillemat',.)
  di "Scenario `s'`scen' / N=`Nnn'"
  forvalues k=1/1000 {
    if (mod(`k',100)==0) {
      di "`k'/1000"
    }
    preserve
    qui keep if replication==`k'
    qui rosali_nobf item1-item`nbitems' item1-item`nbitems', group(TT)
    qui mat resmat=r(difitems)
    local nbitems2 = 2*`nbitems'
    forvalues jj=1/`nbitems2' {
        qui mat outmat[`k',`jj']=resmat[1,`jj']
    }
    qui mat outmat[`k',2*`nbitems'+1]=`Nn'
    qui mat outmat[`k',2*`nbitems'+2]=`nbitems'
    qui mat outmat[`k',2*`nbitems'+3]=`nbdif'
    if (`nbdif' > 0) {
      qui levelsof dif1
      local ldif1 = r(levels)
      local diff1: word 1 of `ldif1'
      qui mat outmat[`k',2*`nbitems'+4]=`diff1'
      if (`nbdif' > 1) {
        qui levelsof dif2
        local ldif2 = r(levels)
        local diff2: word 1 of `ldif2'
        qui mat outmat[`k',2*`nbitems'+5]=`diff2'
        if (`nbdif' > 2) {
          qui levelsof dif3
          local ldif3 = r(levels)
          local diff3: word 1 of `ldif3'
          qui mat outmat[`k',2*`nbitems'+6]=`diff3'
        }
      }
    }
    restore
  }
  putexcel set "`path_res'/`s'`scen'_`Nn'_noBF.xls", sheet("outmat") replace
  putexcel A1=matrix(outmat), colnames
}
}
}



  ************************************************** Version 3: noLRT
  local N = "50 300"
  local ss = "2 4 8 16"
  foreach s in `ss' {
  foreach Nnn in `N' {
  local Nn =  `Nnn'
  local path_data = "/home/corentin/Documents/These/Recherche/ROSALI-SIM/Data/N`Nn'"
  local path_res = "/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/noLRT/N`Nn'"
  local scenarios = "A B C"
  foreach scen in `scenarios' {
  clear
  import delim "`path_data'/scenario_`s'`scen'_`Nn'.csv", encoding(ISO-8859-2)  case(preserve) clear

  if (`s'<=2) {
    local nbitems=4
  }
  else if (`s'<=4) {
    local nbitems=7
  }
  else if (`s'<=12) {
    local nbitems=4
  }
  else {
    local nbitems=7
  }

  if (`s'<=4) {
    local nbdif=0
  }
  else if (`s'<=8) {
    local nbdif=1
  }
  else if (`s'<=16) {
    local nbdif=2
  }
  else {
    local nbdif=3
  }
  local taillemat=2*`nbitems'+3+`nbdif'

  mat outmat = J(1000,`taillemat',.)
  di "Scenario `s'`scen' / N=`Nnn'"
  forvalues k=1/1000 {
    if (mod(`k',100)==0) {
      di "`k'/1000"
    }
    preserve
    qui keep if replication==`k'
    qui rosali_nolrt item1-item`nbitems' item1-item`nbitems', group(TT)
    qui mat resmat=r(difitems)
    local nbitems2 = 2*`nbitems'
    forvalues jj=1/`nbitems2' {
        qui mat outmat[`k',`jj']=resmat[1,`jj']
    }
    qui mat outmat[`k',2*`nbitems'+1]=`Nn'
    qui mat outmat[`k',2*`nbitems'+2]=`nbitems'
    qui mat outmat[`k',2*`nbitems'+3]=`nbdif'
    if (`nbdif' > 0) {
      qui levelsof dif1
      local ldif1 = r(levels)
      local diff1: word 1 of `ldif1'
      qui mat outmat[`k',2*`nbitems'+4]=`diff1'
      if (`nbdif' > 1) {
        qui levelsof dif2
        local ldif2 = r(levels)
        local diff2: word 1 of `ldif2'
        qui mat outmat[`k',2*`nbitems'+5]=`diff2'
        if (`nbdif' > 2) {
          qui levelsof dif3
          local ldif3 = r(levels)
          local diff3: word 1 of `ldif3'
          qui mat outmat[`k',2*`nbitems'+6]=`diff3'
        }
      }
    }
    restore
  }
  putexcel set "`path_res'/`s'`scen'_`Nn'_noLRT.xls", sheet("outmat") replace
  putexcel A1=matrix(outmat), colnames
}
}
}


  ************************************************** Version 4: noLRT noBF
  local N = "50 300"
  local ss = "2 4 8 16"
  foreach s in `ss' {
  foreach Nnn in `N' {
  local Nn =  `Nnn'
  local path_data = "/home/corentin/Documents/These/Recherche/ROSALI-SIM/Data/N`Nn'"
  local path_res = "/home/corentin/Documents/These/Recherche/ROSALI-SIM/Analysis/noLRTnoBF/N`Nn'"
  local scenarios = "A B C"
  foreach scen in `scenarios' {
  clear
  import delim "`path_data'/scenario_`s'`scen'_`Nn'.csv", encoding(ISO-8859-2)  case(preserve) clear

  if (`s'<=2) {
    local nbitems=4
  }
  else if (`s'<=4) {
    local nbitems=7
  }
  else if (`s'<=12) {
    local nbitems=4
  }
  else {
    local nbitems=7
  }

  if (`s'<=4) {
    local nbdif=0
  }
  else if (`s'<=8) {
    local nbdif=1
  }
  else if (`s'<=16) {
    local nbdif=2
  }
  else {
    local nbdif=3
  }
  local taillemat=2*`nbitems'+3+`nbdif'

  mat outmat = J(1000,`taillemat',.)
  di "Scenario `s'`scen' / N=`Nnn'"
  forvalues k=1/1000 {
    if (mod(`k',100)==0) {
      di "`k'/1000"
    }
    preserve
    qui keep if replication==`k'
    qui rosali_nolrt_nobf item1-item`nbitems' item1-item`nbitems', group(TT)
    qui mat resmat=r(difitems)
    local nbitems2 = 2*`nbitems'
    forvalues jj=1/`nbitems2' {
        qui mat outmat[`k',`jj']=resmat[1,`jj']
    }
    qui mat outmat[`k',2*`nbitems'+1]=`Nn'
    qui mat outmat[`k',2*`nbitems'+2]=`nbitems'
    qui mat outmat[`k',2*`nbitems'+3]=`nbdif'
    if (`nbdif' > 0) {
      qui levelsof dif1
      local ldif1 = r(levels)
      local diff1: word 1 of `ldif1'
      qui mat outmat[`k',2*`nbitems'+4]=`diff1'
      if (`nbdif' > 1) {
        qui levelsof dif2
        local ldif2 = r(levels)
        local diff2: word 1 of `ldif2'
        qui mat outmat[`k',2*`nbitems'+5]=`diff2'
        if (`nbdif' > 2) {
          qui levelsof dif3
          local ldif3 = r(levels)
          local diff3: word 1 of `ldif3'
          qui mat outmat[`k',2*`nbitems'+6]=`diff3'
        }
      }
    }
    restore
  }
  putexcel set "`path_res'/`s'`scen'_`Nn'_noLRT_noBF.xls", sheet("outmat") replace
  putexcel A1=matrix(outmat), colnames
}
}
}
