From 510aca9bac4d1ec825d00c22a2510e7262cd8796 Mon Sep 17 00:00:00 2001 From: corentinchoisy Date: Wed, 28 Feb 2024 14:43:43 +0100 Subject: [PATCH] Completed n=50 DIF analysis --- Scripts/Analysis/DIF/pcm_dif_50_J4.do | 1331 +++++++++++++ .../DIF/{pcm_dif_50.do => pcm_dif_50_J7.do} | 1645 ++++------------- Scripts/Analysis/DIF/temp/temp.do | 1014 +++------- Scripts/Analysis/DIF/temp/temp_problem.do | 516 ++++++ 4 files changed, 2400 insertions(+), 2106 deletions(-) create mode 100644 Scripts/Analysis/DIF/pcm_dif_50_J4.do rename Scripts/Analysis/DIF/{pcm_dif_50.do => pcm_dif_50_J7.do} (60%) diff --git a/Scripts/Analysis/DIF/pcm_dif_50_J4.do b/Scripts/Analysis/DIF/pcm_dif_50_J4.do new file mode 100644 index 0000000..1fe406f --- /dev/null +++ b/Scripts/Analysis/DIF/pcm_dif_50_J4.do @@ -0,0 +1,1331 @@ + *================================================================================================================================================= + * Date : 2024-01-23 + * Stata version : Stata 18 SE + * + * This program analyses simulated data accounting for DIF through a partial credit model + * + * ado-files needed : - pcm (version 5.5 October 25, 2023, available on gitea) + * + * outputs : for N=100 + * + * + *================================================================================================================================================ + + * Load pcm.ado + adopath+"/home/corentin/Documents/These/Recherche/Simulations/Modules/" + + *========================== + * Scenarios with : J=4 + *========================== + + *************************************************** Scenarios with DIF on 1 item + + ************************************************** Scenario 5: J = 4 items / M = 2 modalities / DIF size 0.3 + local N = "50" + foreach Nnn in `N' { + local Nn = `Nnn' + local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" + local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" + local scenarios = "A B C D E F G" + foreach scen in `scenarios' { + clear + import delim "`path_data'/scenario_5`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear + rename TT tt + + * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif + local nbitems = 4 + local nbdif = 1 + local taillemat = `nbitems'+`nbdif'+3 + mat outmat = J(1000,`taillemat',.) + mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "beta" "se_beta" "dif_item_1" + di "Scenario 5`scen' / N=`Nnn'" + forvalues k=1/1000 { + if (mod(`k',100)==0) { + di "`k'/1000" + } + preserve + qui keep if replication==`k' + local difitems1=dif1 + local mod "gsem " + forvalues i=1/`nbitems' { + if (`i'==`difitems1') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } + } + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + qui `mod' + mat V=r(table) + mat W=V[1..2,1...] + forvalues j=1/`nbitems' { + if (`j'<`difitems1') { + mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif + } + else { + mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif + } + } + mat outmat[`k',`nbitems'+1] = W[1,3*`difitems1'-1] // coef de dif + mat outmat[`k',`nbitems'+2] = W[1,3*`nbitems'+2] // beta + mat outmat[`k',`nbitems'+3] = W[2,3*`nbitems'+2] // se beta + mat outmat[`k',`nbitems'+4] = `difitems1' // numéro item de dif + restore + } + putexcel set "`path_res'/out/5`scen'_`Nn'.xls", sheet("outmat") replace + putexcel A1=matrix(outmat), colnames +} +} + + +************************************************** Scenario 6: J = 4 items / M = 4 modalities / DIF size 0.3 +local N = "50" +foreach Nnn in `N' { +local Nn = `Nnn' +local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" +local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" +local scenarios = "A B C D E F G" +foreach scen in `scenarios' { +clear +import delim "`path_data'/scenario_6`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear +rename TT tt + +* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif +local nbitems = 4 +local nbdif = 1 +local taillemat = 3*`nbitems'+`nbdif'+3+2 +mat outmat = J(1000,`taillemat',.) +mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "beta" "se_beta" "dif_item_1" +di "Scenario 6`scen' / N=`Nnn'" +forvalues k=1/1000 { + if (mod(`k',100)==0) { + di "`k'/1000" + } + preserve + qui keep if replication==`k' + local difitems1=dif1 + forvalues i=1/`nbitems' { + if (`i'==`difitems1') { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + } + local mod "gsem " + forvalues i=1/`nbitems' { + if (`i'==`difitems1') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } + } + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" + qui `constrnt' + qui `constrnt2' + qui `mod' + mat V=r(table) + mat W=V[1..2,1...] + forvalues j=1/`nbitems' { + if (`j'<`difitems1') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+3] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+5] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+7] // items avant le premier dif + } + else if (`j'==`difitems1') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+4] // items du le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+7] // items du le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items du le premier dif } + } + else { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+6] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+8] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items avant le premier dif + } + } + mat outmat[`k',3*`nbitems'+1] = W[1,7*(`difitems1'-1)+2] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,7*(`difitems1'-1)+5] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,7*(`difitems1'-1)+8] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,7*`nbitems'+4] // beta + mat outmat[`k',3*`nbitems'+5] = W[2,7*`nbitems'+4] // se beta + mat outmat[`k',3*`nbitems'+6] = `difitems1' // numéro item de dif + restore +} +putexcel set "`path_res'/out/6`scen'_`Nn'.xls", sheet("outmat") replace +putexcel A1=matrix(outmat), colnames +} +} + + +************************************************** Scenario 7: J = 4 items / M = 2 modalities / DIF size 0.5 + local N = "50" + foreach Nnn in `N' { + local Nn = `Nnn' + local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" + local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" + local scenarios = "A B C D E F G" + foreach scen in `scenarios' { + clear + import delim "`path_data'/scenario_7`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear + rename TT tt + + * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif + local nbitems = 4 + local nbdif = 1 + local taillemat = `nbitems'+`nbdif'+3 + mat outmat = J(1000,`taillemat',.) + mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "beta" "se_beta" "dif_item_1" + di "Scenario 7`scen' / N=`Nnn'" + forvalues k=1/1000 { + if (mod(`k',100)==0) { + di "`k'/1000" + } + preserve + qui keep if replication==`k' + local difitems1=dif1 + local mod "gsem " + forvalues i=1/`nbitems' { + if (`i'==`difitems1') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } + } + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + qui `mod' + mat V=r(table) + mat W=V[1..2,1...] + forvalues j=1/`nbitems' { + if (`j'<`difitems1') { + mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif + } + else { + mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif + } + } + mat outmat[`k',`nbitems'+1] = W[1,3*`difitems1'-1] // coef de dif + mat outmat[`k',`nbitems'+2] = W[1,3*`nbitems'+2] // beta + mat outmat[`k',`nbitems'+3] = W[2,3*`nbitems'+2] // se beta + mat outmat[`k',`nbitems'+4] = `difitems1' // numéro item de dif + restore + } + putexcel set "`path_res'/out/7`scen'_`Nn'.xls", sheet("outmat") replace + putexcel A1=matrix(outmat), colnames +} +} + + + +************************************************** Scenario 8: J = 4 items / M = 4 modalities / DIF size 0.5 +local N = "50" +foreach Nnn in `N' { +local Nn = `Nnn' +local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" +local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" +local scenarios = "A B C D E F G" +foreach scen in `scenarios' { +clear +import delim "`path_data'/scenario_8`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear +rename TT tt + +* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif +local nbitems = 4 +local nbdif = 1 +local taillemat = 3*`nbitems'+`nbdif'+3+2 +mat outmat = J(1000,`taillemat',.) +mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "beta" "se_beta" "dif_item_1" +di "Scenario 8`scen' / N=`Nnn'" +forvalues k=1/1000 { + if (mod(`k',100)==0) { + di "`k'/1000" + } + preserve + qui keep if replication==`k' + +* MERGE des modalités si non représentées + local com_z = 0 + qui gen comz = 0 + forvalues j = 1 / `nbitems' { + local recoda_`j' = 0 + qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') + local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] + local minm`j'_t1_g0 = rect1_g0_`j'[1,1] + + qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') + local minm`j'_t1_g1 = rect1_g1_`j'[1,1] + local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] + + local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') + local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') + local nbm_`j' = `=`maxm_`j''-`minm_`j''' + + if `minm_`j'' != 0 & `com_z' == 0 { + local com_z = 1 + } + + qui count if item`j' == 3 & tt == 0 + local mod3plac = r(N) + qui count if item`j' == 3 & tt == 1 + local mod3tt = r(N) + local nb_rn3 = min(`mod3plac',`mod3tt') + if `nb_rn3'==0 { + qui replace comz = 1 + } + + forvalues m = 0/`=`nbm_`j''-1' { + qui count if item`j' == `m' & tt == 0 + local nb_rn1_g0 = r(N) + qui count if item`j' == `m' & tt == 1 + local nb_rn1_g1 = r(N) + local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') + if `nb_rn' == 0 { + qui replace comz = 1 + local recoda_`j' = 1 + if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { + local stop = 1 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' + `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'+`kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { + local stop = 1 + forvalues kk = 1/`=`m'' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' - `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m' - `kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else { + if runiform()>0.5{ + local stop = 1 + forvalues kk = 1/`m' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' - `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'-`kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else { + local stop = 1 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' + `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ + qui replace item`j'=`=`m' + `kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + else { + if `stop' != 0 { + qui replace item`j'= `nbm_`j'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + } + } + } + } + qui levelsof item`j' + local val = r(levels) + local checker: word 1 of `val' + local checker2: word 2 of `val' + local checker3: word 3 of `val' + if (`checker'!=0) { + qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) + } + else if (`checker2'!=1) { + qui recode item`j' (`checker2'=1) (`checker3'=2) + } + else if (`checker3'!=2) { + qui recode item`j' (`checker3'=2) + } + } + +qui valuesof comz +local val = r(values) +local checker: word 1 of `val' + +* replications avec 3 mod +if (`checker' == 1) { +local difitems1=dif1 +local difcheck = 0 +forvalues i=1/`nbitems' { + qui sum item`i' + local checker2 = r(max) + if (`i'==`difitems1') { + if (`checker2'==3) { + local difcheck = 1 + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + else { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + } + } +} +local mod "gsem " +forvalues i=1/`nbitems' { + qui sum item`i' + local checker2 = r(max) + if (`i'==`difitems1') { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" + } +} + else { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" + } + } +} +if (`difcheck'==1) { +local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" +} +if (`difcheck'==0) { +local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" +} +qui `constrnt' +if (`difcheck'==1) { + qui `constrnt2' +} +qui `mod' +mat V=r(table) +mat W=V[1..2,1...] +forvalues j=1/`nbitems' { + + mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif +} + mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"THETA:tt")] // beta + mat outmat[`k',3*`nbitems'+5] = W[2,colnumb(W,"THETA:tt")] // se beta + mat outmat[`k',3*`nbitems'+6] = `difitems1' // numéro item de dif + restore +} + +* replications sans problèmes +else { +local difitems1=dif1 +forvalues i=1/`nbitems' { + if (`i'==`difitems1') { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } +} +local mod "gsem " +forvalues i=1/`nbitems' { + if (`i'==`difitems1') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } +} +local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" +qui `constrnt' +qui `constrnt2' +qui `mod' +mat V=r(table) +mat W=V[1..2,1...] +forvalues j=1/`nbitems' { +if (`j'<`difitems1') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+3] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+5] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+7] // items avant le premier dif + } +else if (`j'==`difitems1') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+4] // items du le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+7] // items du le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items du le premier dif } + } +else { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+6] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+8] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items avant le premier dif + } +} + mat outmat[`k',3*`nbitems'+1] = W[1,7*(`difitems1'-1)+2] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,7*(`difitems1'-1)+5] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,7*(`difitems1'-1)+8] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,7*`nbitems'+4] // beta + mat outmat[`k',3*`nbitems'+5] = W[2,7*`nbitems'+4] // se beta + mat outmat[`k',3*`nbitems'+6] = `difitems1' // numéro item de dif + restore +} +} +putexcel set "`path_res'/out/8`scen'_`Nn'.xls", sheet("outmat") replace +putexcel A1=matrix(outmat), colnames +} +} + + + +*************************************************** Scenarios with DIF on 2 items + +* Load pcm.ado +adopath+"/home/corentin/Documents/These/Recherche/Simulations/Modules/" + +************************************************** Scenario 9: J = 4 items / M = 2 modalities / DIF size 0.3 x2 + local N = "50" + foreach Nnn in `N' { + local Nn = `Nnn' + local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" + local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" + local scenarios = "A B C D E F G" + foreach scen in `scenarios' { + clear + import delim "`path_data'/scenario_9`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear + rename TT tt + + * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif + local nbitems = 4 + local nbdif = 2 + local taillemat = `nbitems'+`nbdif'+4 + mat outmat = J(1000,`taillemat',.) + mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "dif2" "beta" "se_beta" "dif_item_1" "dif_item_2" + di "Scenario 9`scen' / N=`Nnn'" + forvalues k=1/1000 { + if (mod(`k',100)==0) { + di "`k'/1000" + } + preserve + qui keep if replication==`k' + local difitems1=dif1 + local difitems2=dif2 + if (`difitems1'<`difitems2') { + local difitemsmin `difitems1' + local difitemsmax `difitems2' + } + else { + local difitemsmin `difitems2' + local difitemsmax `difitems1' + } + local mod "gsem " + forvalues i=1/`nbitems' { + if (`i'==`difitemsmin' | `i'==`difitemsmax') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } + } + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + qui `mod' + mat V=r(table) + mat W=V[1..2,1...] + forvalues j=1/`nbitems' { + if (`j'<`difitemsmin') { + mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif + } + else if (`j'<`difitemsmax') { + mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif + } + else { + mat outmat[`k',`j'] = W[1,2+3*`j'] // items après le deuxieme dif + } + } + mat outmat[`k',`nbitems'+1] = W[1,3*`difitemsmin'-1] // coef de dif + mat outmat[`k',`nbitems'+2] = W[1,3*`difitemsmax'] // coef de dif + mat outmat[`k',`nbitems'+3] = W[1,3*`nbitems'+3] // beta + mat outmat[`k',`nbitems'+4] = W[2,3*`nbitems'+3] // se beta + mat outmat[`k',`nbitems'+5] = `difitemsmin' // numéro item de dif + mat outmat[`k',`nbitems'+6] = `difitemsmax' // numéro item de dif + restore + } + putexcel set "`path_res'/out/9`scen'_`Nn'.xls", sheet("outmat") replace + putexcel A1=matrix(outmat), colnames +} +} + + + +************************************************** Scenario 10: J = 4 items / M = 4 modalities / DIF size 0.3x2 +local N = "50" +foreach Nnn in `N' { +local Nn = `Nnn' +local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" +local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" +local scenarios = "A B C D E F G" +foreach scen in `scenarios' { +clear +import delim "`path_data'/scenario_10`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear +rename TT tt + +* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif +local nbitems = 4 +local nbdif = 2 +local taillemat = 3*`nbitems'+`nbdif'+6+2 +mat outmat = J(1000,`taillemat',.) +mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "dif2_1" "dif2_2" "dif2_3" "beta" "se_beta" "dif_item_1" "dif_item_2" +di "Scenario 10`scen' / N=`Nnn'" +forvalues k=1/1000 { + if (mod(`k',100)==0) { + di "`k'/1000" + } + preserve + qui keep if replication==`k' + + +* MERGE des modalités si non représentées + local com_z = 0 + qui gen comz = 0 + forvalues j = 1 / `nbitems' { + local recoda_`j' = 0 + qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') + local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] + local minm`j'_t1_g0 = rect1_g0_`j'[1,1] + + qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') + local minm`j'_t1_g1 = rect1_g1_`j'[1,1] + local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] + + local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') + local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') + local nbm_`j' = `=`maxm_`j''-`minm_`j''' + + if `minm_`j'' != 0 & `com_z' == 0 { + local com_z = 1 + } + + qui count if item`j' == 3 & tt == 0 + local mod3plac = r(N) + qui count if item`j' == 3 & tt == 1 + local mod3tt = r(N) + local nb_rn3 = min(`mod3plac',`mod3tt') + if `nb_rn3'==0 { + qui replace comz = 1 + } + + forvalues m = 0/`=`nbm_`j''-1' { + qui count if item`j' == `m' & tt == 0 + local nb_rn1_g0 = r(N) + qui count if item`j' == `m' & tt == 1 + local nb_rn1_g1 = r(N) + local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') + if `nb_rn' == 0 { + qui replace comz = 1 + local recoda_`j' = 1 + if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { + local stop = 1 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' + `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'+`kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { + local stop = 1 + forvalues kk = 1/`=`m'' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' - `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m' - `kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else { + if runiform()>0.5{ + local stop = 1 + forvalues kk = 1/`m' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' - `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'-`kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else { + local stop = 1 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' + `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ + qui replace item`j'=`=`m' + `kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + else { + if `stop' != 0 { + qui replace item`j'= `nbm_`j'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + } + } + } + } + qui levelsof item`j' + local val = r(levels) + local checker: word 1 of `val' + local checker2: word 2 of `val' + local checker3: word 3 of `val' + if (`checker'!=0) { + qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) + } + else if (`checker2'!=1) { + qui recode item`j' (`checker2'=1) (`checker3'=2) + } + else if (`checker3'!=2) { + qui recode item`j' (`checker3'=2) + } + } + +qui valuesof comz +local val = r(values) +local checker: word 1 of `val' + +* replications avec 3 mod +if (`checker' == 1) { + local difitems1=dif1 + local difitems2=dif2 + local difcheck = 0 + local difcheck2 = 0 +forvalues i=1/`nbitems' { + qui sum item`i' + local checker2 = r(max) + + if (`i'==`difitems1') { + if (`checker2'==3) { + local difcheck = 1 + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + else { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + } + } + if (`i'==`difitems2') { + if (`checker2'==3) { + local difcheck2=1 + local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + else { + local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + } + } +} +local mod "gsem " +forvalues i=1/`nbitems' { + qui sum item`i' + local checker2 = r(max) + if (`i'==`difitems1'| `i'==`difitems2') { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" + } + } + else { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" + } +} +} +if (`difcheck'==1) { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" + } + else { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" + } +} +else { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" + } + else { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" + } +} +qui `constrnt' +qui `constrnt2' +if (`difcheck'==1) { + qui `constrnt3' +} +if (`difcheck2'==1) { + qui `constrnt4' +} +qui `mod' +mat V=r(table) +mat W=V[1..2,1...] +forvalues j=1/`nbitems' { + + mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif +} + mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta + mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta + mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif + mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif + restore +} + +* replications sans problèmes +else { + local difitems1=dif1 + local difitems2=dif2 +forvalues i=1/`nbitems' { + if (`i'==`difitems1') { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + if (`i'==`difitems2') { + local constrnt3 = "constraint 3 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } +} +local mod "gsem " +forvalues i=1/`nbitems' { + if (`i'==`difitems1' | `i'==`difitems2') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } +} +local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" +qui `constrnt' +qui `constrnt2' +qui `constrnt3' +qui `constrnt4' +qui `mod' +mat V=r(table) +mat W=V[1..2,1...] +forvalues j=1/`nbitems' { + mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif + } + mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta + mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta + mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif + mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif + restore +} +} +putexcel set "`path_res'/out/10`scen'_`Nn'.xls", sheet("outmat") replace +putexcel A1=matrix(outmat), colnames +} +} + + +[ + +************************************************** Scenario 11: J = 4 items / M = 2 modalities / DIF size 0.5 x2 + local N = "50" + foreach Nnn in `N' { + local Nn = `Nnn' + local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" + local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" + local scenarios = "A B C D E F G" + foreach scen in `scenarios' { + clear + import delim "`path_data'/scenario_11`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear + rename TT tt + + * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif + local nbitems = 4 + local nbdif = 2 + local taillemat = `nbitems'+`nbdif'+4 + mat outmat = J(1000,`taillemat',.) + mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "dif2" "beta" "se_beta" "dif_item_1" "dif_item_2" + di "Scenario 11`scen' / N=`Nnn'" + forvalues k=1/1000 { + if (mod(`k',100)==0) { + di "`k'/1000" + } + preserve + qui keep if replication==`k' + local difitems1=dif1 + local difitems2=dif2 + if (`difitems1'<`difitems2') { + local difitemsmin `difitems1' + local difitemsmax `difitems2' + } + else { + local difitemsmin `difitems2' + local difitemsmax `difitems1' + } + local mod "gsem " + forvalues i=1/`nbitems' { + if (`i'==`difitemsmin' | `i'==`difitemsmax') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } + } + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + qui `mod' + mat V=r(table) + mat W=V[1..2,1...] + forvalues j=1/`nbitems' { + if (`j'<`difitemsmin') { + mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif + } + else if (`j'<`difitemsmax') { + mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif + } + else { + mat outmat[`k',`j'] = W[1,2+3*`j'] // items après le deuxieme dif + } + } + mat outmat[`k',`nbitems'+1] = W[1,3*`difitemsmin'-1] // coef de dif + mat outmat[`k',`nbitems'+2] = W[1,3*`difitemsmax'] // coef de dif + mat outmat[`k',`nbitems'+3] = W[1,3*`nbitems'+3] // beta + mat outmat[`k',`nbitems'+4] = W[2,3*`nbitems'+3] // se beta + mat outmat[`k',`nbitems'+5] = `difitemsmin' // numéro item de dif + mat outmat[`k',`nbitems'+6] = `difitemsmax' // numéro item de dif + restore + } + putexcel set "`path_res'/out/11`scen'_`Nn'.xls", sheet("outmat") replace + putexcel A1=matrix(outmat), colnames +} +} + + + +************************************************** Scenario 12: J = 4 items / M = 4 modalities / DIF size 0.5 x2 +local N = "50" +foreach Nnn in `N' { +local Nn = `Nnn' +local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" +local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" +local scenarios = "A B C D E F G" +foreach scen in `scenarios' { +clear +import delim "`path_data'/scenario_12`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear +rename TT tt + +* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif +local nbitems = 4 +local nbdif = 2 +local taillemat = 3*`nbitems'+`nbdif'+6+2 +mat outmat = J(1000,`taillemat',.) +mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "dif2_1" "dif2_2" "dif2_3" "beta" "se_beta" "dif_item_1" "dif_item_2" +di "Scenario 12`scen' / N=`Nnn'" +forvalues k=1/1000 { + if (mod(`k',100)==0) { + di "`k'/1000" + } + preserve + qui keep if replication==`k' + + +* MERGE des modalités si non représentées + local com_z = 0 + qui gen comz = 0 + forvalues j = 1 / `nbitems' { + local recoda_`j' = 0 + qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') + local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] + local minm`j'_t1_g0 = rect1_g0_`j'[1,1] + + qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') + local minm`j'_t1_g1 = rect1_g1_`j'[1,1] + local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] + + local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') + local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') + local nbm_`j' = `=`maxm_`j''-`minm_`j''' + + if `minm_`j'' != 0 & `com_z' == 0 { + local com_z = 1 + } + + qui count if item`j' == 3 & tt == 0 + local mod3plac = r(N) + qui count if item`j' == 3 & tt == 1 + local mod3tt = r(N) + local nb_rn3 = min(`mod3plac',`mod3tt') + if `nb_rn3'==0 { + qui replace comz = 1 + } + + forvalues m = 0/`=`nbm_`j''-1' { + qui count if item`j' == `m' & tt == 0 + local nb_rn1_g0 = r(N) + qui count if item`j' == `m' & tt == 1 + local nb_rn1_g1 = r(N) + local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') + if `nb_rn' == 0 { + qui replace comz = 1 + local recoda_`j' = 1 + if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { + local stop = 1 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' + `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'+`kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { + local stop = 1 + forvalues kk = 1/`=`m'' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' - `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m' - `kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else { + if runiform()>0.5{ + local stop = 1 + forvalues kk = 1/`m' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' - `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'-`kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else { + local stop = 1 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' + `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ + qui replace item`j'=`=`m' + `kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + else { + if `stop' != 0 { + qui replace item`j'= `nbm_`j'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + } + } + } + } + qui levelsof item`j' + local val = r(levels) + local checker: word 1 of `val' + local checker2: word 2 of `val' + local checker3: word 3 of `val' + local nummoda=r(r) + if (`nummoda'==2) { + qui recode item`j' (`checker'=0) (`checker2'=1) + } + if (`nummoda'==3) { + if (`checker'!=0) { + qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) + } + else if (`checker2'!=1) { + qui recode item`j' (`checker2'=1) (`checker3'=2) + } + else if (`checker3'!=2) { + qui recode item`j' (`checker3'=2) + } + } + + } + +qui valuesof comz +local val = r(values) +local checker: word 1 of `val' + +* replications avec 3 mod +if (`checker' == 1) { + local difitems1=dif1 + local difitems2=dif2 + local difcheck = 0 + local difcheck2 = 0 +forvalues i=1/`nbitems' { + qui sum item`i' + local checker2 = r(max) + + if (`i'==`difitems1') { + if (`checker2'==3) { + local difcheck = 1 + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + else if (`checker2'==2) { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + } + else if (`checker2'==1) { + local difcheck = 2 + } + } + if (`i'==`difitems2') { + if (`checker2'==3) { + local difcheck2=1 + local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + else if (`checker2'==2) { + local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + } + else if (`checker2'==1) { + local difcheck2 = 2 + } + } +} +local mod "gsem " +forvalues i=1/`nbitems' { + qui sum item`i' + local checker2 = r(max) + if (`i'==`difitems1'| `i'==`difitems2') { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else if (`checker2'==2) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } + } + else { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } + else if (`checker2'==2) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } +} +} +if (`difcheck'==1) { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" + } + else if (`difcheck2'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" + } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" + } +} +else if (`difcheck'==0) { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" + } + else if (`difcheck2'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" + } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" + } +} +else if (`difcheck'==2) { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4)" + } + else if (`difcheck2'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2)" + } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + } +} + +if (`difcheck'!=2) { +qui `constrnt' +} +if (`difcheck2'!=2) { +qui `constrnt2' +} +if (`difcheck'==1) { + qui `constrnt3' +} +if (`difcheck2'==1) { + qui `constrnt4' +} +qui `mod' +mat V=r(table) +mat W=V[1..2,1...] +forvalues j=1/`nbitems' { + + mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif +} + mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta + mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta + mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif + mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif + restore +} + +* replications sans problèmes +else { + local difitems1=dif1 + local difitems2=dif2 +forvalues i=1/`nbitems' { + if (`i'==`difitems1') { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + if (`i'==`difitems2') { + local constrnt3 = "constraint 3 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } +} +local mod "gsem " +forvalues i=1/`nbitems' { + if (`i'==`difitems1' | `i'==`difitems2') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } +} +local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" +qui `constrnt' +qui `constrnt2' +qui `constrnt3' +qui `constrnt4' +qui `mod' +mat V=r(table) +mat W=V[1..2,1...] +forvalues j=1/`nbitems' { + mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif + } + mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta + mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta + mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif + mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif + restore +} +} +putexcel set "`path_res'/out/12`scen'_`Nn'.xls", sheet("outmat") replace +putexcel A1=matrix(outmat), colnames +} +} diff --git a/Scripts/Analysis/DIF/pcm_dif_50.do b/Scripts/Analysis/DIF/pcm_dif_50_J7.do similarity index 60% rename from Scripts/Analysis/DIF/pcm_dif_50.do rename to Scripts/Analysis/DIF/pcm_dif_50_J7.do index 8f04041..369bc8c 100644 --- a/Scripts/Analysis/DIF/pcm_dif_50.do +++ b/Scripts/Analysis/DIF/pcm_dif_50_J7.do @@ -11,1285 +11,6 @@ * *================================================================================================================================================ - * Load pcm.ado - adopath+"/home/corentin/Documents/These/Recherche/Simulations/Modules/" - - *========================== - * Scenarios with : J=4 - *========================== - - *************************************************** Scenarios with DIF on 1 item - - ************************************************** Scenario 5: J = 4 items / M = 2 modalities / DIF size 0.3 - local N = "50" - foreach Nnn in `N' { - local Nn = `Nnn' - local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" - local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" - local scenarios = "A B C D E F G" - foreach scen in `scenarios' { - clear - import delim "`path_data'/scenario_5`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear - rename TT tt - - * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif - local nbitems = 4 - local nbdif = 1 - local taillemat = `nbitems'+`nbdif'+3 - mat outmat = J(1000,`taillemat',.) - mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "beta" "se_beta" "dif_item_1" - di "Scenario 5`scen' / N=`Nnn'" - forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" - } - preserve - qui keep if replication==`k' - local difitems1=dif1 - local mod "gsem " - forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)" - } - } - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" - qui `mod' - mat V=r(table) - mat W=V[1..2,1...] - forvalues j=1/`nbitems' { - if (`j'<`difitems1') { - mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif - } - else { - mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif - } - } - mat outmat[`k',`nbitems'+1] = W[1,3*`difitems1'-1] // coef de dif - mat outmat[`k',`nbitems'+2] = W[1,3*`nbitems'+2] // beta - mat outmat[`k',`nbitems'+3] = W[2,3*`nbitems'+2] // se beta - mat outmat[`k',`nbitems'+4] = `difitems1' // numéro item de dif - restore - } - putexcel set "`path_res'/out/5`scen'_`Nn'.xls", sheet("outmat") replace - putexcel A1=matrix(outmat), colnames -} -} - - -************************************************** Scenario 6: J = 4 items / M = 4 modalities / DIF size 0.3 -local N = "50" -foreach Nnn in `N' { -local Nn = `Nnn' -local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" -local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" -foreach scen in `scenarios' { -clear -import delim "`path_data'/scenario_6`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear -rename TT tt - -* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 4 -local nbdif = 1 -local taillemat = 3*`nbitems'+`nbdif'+3+2 -mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "beta" "se_beta" "dif_item_1" -di "Scenario 6`scen' / N=`Nnn'" -forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" - } - preserve - qui keep if replication==`k' - local difitems1=dif1 - forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } - } - local mod "gsem " - forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" - } - } - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" - qui `constrnt' - qui `constrnt2' - qui `mod' - mat V=r(table) - mat W=V[1..2,1...] - forvalues j=1/`nbitems' { - if (`j'<`difitems1') { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+3] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+5] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+7] // items avant le premier dif - } - else if (`j'==`difitems1') { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+4] // items du le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+7] // items du le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items du le premier dif } - } - else { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+6] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+8] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items avant le premier dif - } - } - mat outmat[`k',3*`nbitems'+1] = W[1,7*(`difitems1'-1)+2] // coef de dif - mat outmat[`k',3*`nbitems'+2] = W[1,7*(`difitems1'-1)+5] // coef de dif - mat outmat[`k',3*`nbitems'+3] = W[1,7*(`difitems1'-1)+8] // coef de dif - mat outmat[`k',3*`nbitems'+4] = W[1,7*`nbitems'+4] // beta - mat outmat[`k',3*`nbitems'+5] = W[2,7*`nbitems'+4] // se beta - mat outmat[`k',3*`nbitems'+6] = `difitems1' // numéro item de dif - restore -} -putexcel set "`path_res'/out/6`scen'_`Nn'.xls", sheet("outmat") replace -putexcel A1=matrix(outmat), colnames -} -} - - -************************************************** Scenario 7: J = 4 items / M = 2 modalities / DIF size 0.5 - local N = "50" - foreach Nnn in `N' { - local Nn = `Nnn' - local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" - local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" - local scenarios = "A B C D E F G" - foreach scen in `scenarios' { - clear - import delim "`path_data'/scenario_7`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear - rename TT tt - - * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif - local nbitems = 4 - local nbdif = 1 - local taillemat = `nbitems'+`nbdif'+3 - mat outmat = J(1000,`taillemat',.) - mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "beta" "se_beta" "dif_item_1" - di "Scenario 7`scen' / N=`Nnn'" - forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" - } - preserve - qui keep if replication==`k' - local difitems1=dif1 - local mod "gsem " - forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)" - } - } - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" - qui `mod' - mat V=r(table) - mat W=V[1..2,1...] - forvalues j=1/`nbitems' { - if (`j'<`difitems1') { - mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif - } - else { - mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif - } - } - mat outmat[`k',`nbitems'+1] = W[1,3*`difitems1'-1] // coef de dif - mat outmat[`k',`nbitems'+2] = W[1,3*`nbitems'+2] // beta - mat outmat[`k',`nbitems'+3] = W[2,3*`nbitems'+2] // se beta - mat outmat[`k',`nbitems'+4] = `difitems1' // numéro item de dif - restore - } - putexcel set "`path_res'/out/7`scen'_`Nn'.xls", sheet("outmat") replace - putexcel A1=matrix(outmat), colnames -} -} - - - -************************************************** Scenario 8: J = 4 items / M = 4 modalities / DIF size 0.5 -local N = "50" -foreach Nnn in `N' { -local Nn = `Nnn' -local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" -local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" -foreach scen in `scenarios' { -clear -import delim "`path_data'/scenario_8`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear -rename TT tt - -* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 4 -local nbdif = 1 -local taillemat = 3*`nbitems'+`nbdif'+3+2 -mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "beta" "se_beta" "dif_item_1" -di "Scenario 8`scen' / N=`Nnn'" -forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" - } - preserve - qui keep if replication==`k' - -* MERGE des modalités si non représentées - local com_z = 0 - qui gen comz = 0 - forvalues j = 1 / `nbitems' { - local recoda_`j' = 0 - qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') - local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] - local minm`j'_t1_g0 = rect1_g0_`j'[1,1] - - qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') - local minm`j'_t1_g1 = rect1_g1_`j'[1,1] - local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] - - local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') - local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') - local nbm_`j' = `=`maxm_`j''-`minm_`j''' - - if `minm_`j'' != 0 & `com_z' == 0 { - local com_z = 1 - } - - qui count if item`j' == 3 & tt == 0 - local mod3plac = r(N) - qui count if item`j' == 3 & tt == 1 - local mod3tt = r(N) - local nb_rn3 = min(`mod3plac',`mod3tt') - if `nb_rn3'==0 { - qui replace comz = 1 - } - - forvalues m = 0/`=`nbm_`j''-1' { - qui count if item`j' == `m' & tt == 0 - local nb_rn1_g0 = r(N) - qui count if item`j' == `m' & tt == 1 - local nb_rn1_g1 = r(N) - local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') - if `nb_rn' == 0 { - qui replace comz = 1 - local recoda_`j' = 1 - if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'+`kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`m'' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m' - `kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else { - if runiform()>0.5{ - local stop = 1 - forvalues kk = 1/`m' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'-`kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else { - local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ - qui replace item`j'=`=`m' + `kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - else { - if `stop' != 0 { - qui replace item`j'= `nbm_`j'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - } - } - } - } - qui levelsof item`j' - local val = r(levels) - local checker: word 1 of `val' - local checker2: word 2 of `val' - local checker3: word 3 of `val' - if (`checker'!=0) { - qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) - } - else if (`checker2'!=1) { - qui recode item`j' (`checker2'=1) (`checker3'=2) - } - else if (`checker3'!=2) { - qui recode item`j' (`checker3'=2) - } - } - -qui valuesof comz -local val = r(values) -local checker: word 1 of `val' - -* replications avec 3 mod -if (`checker' == 1) { -local difitems1=dif1 -local difcheck = 0 -forvalues i=1/`nbitems' { - qui sum item`i' - local checker2 = r(max) - if (`i'==`difitems1') { - if (`checker2'==3) { - local difcheck = 1 - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } - else { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - } - } -} -local mod "gsem " -forvalues i=1/`nbitems' { - qui sum item`i' - local checker2 = r(max) - if (`i'==`difitems1') { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" - } -} - else { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" - } - } -} -if (`difcheck'==1) { -local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" -} -if (`difcheck'==0) { -local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" -} -qui `constrnt' -if (`difcheck'==1) { - qui `constrnt2' -} -qui `mod' -mat V=r(table) -mat W=V[1..2,1...] -forvalues j=1/`nbitems' { - - mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif -} - mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"THETA:tt")] // beta - mat outmat[`k',3*`nbitems'+5] = W[2,colnumb(W,"THETA:tt")] // se beta - mat outmat[`k',3*`nbitems'+6] = `difitems1' // numéro item de dif - restore -} - -* replications sans problèmes -else { -local difitems1=dif1 -forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } -} -local mod "gsem " -forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" - } -} -local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" -qui `constrnt' -qui `constrnt2' -qui `mod' -mat V=r(table) -mat W=V[1..2,1...] -forvalues j=1/`nbitems' { -if (`j'<`difitems1') { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+3] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+5] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+7] // items avant le premier dif - } -else if (`j'==`difitems1') { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+4] // items du le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+7] // items du le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items du le premier dif } - } -else { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+6] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+8] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items avant le premier dif - } -} - mat outmat[`k',3*`nbitems'+1] = W[1,7*(`difitems1'-1)+2] // coef de dif - mat outmat[`k',3*`nbitems'+2] = W[1,7*(`difitems1'-1)+5] // coef de dif - mat outmat[`k',3*`nbitems'+3] = W[1,7*(`difitems1'-1)+8] // coef de dif - mat outmat[`k',3*`nbitems'+4] = W[1,7*`nbitems'+4] // beta - mat outmat[`k',3*`nbitems'+5] = W[2,7*`nbitems'+4] // se beta - mat outmat[`k',3*`nbitems'+6] = `difitems1' // numéro item de dif - restore -} -} -putexcel set "`path_res'/out/8`scen'_`Nn'.xls", sheet("outmat") replace -putexcel A1=matrix(outmat), colnames -} -} - - - -*************************************************** Scenarios with DIF on 2 items - -* Load pcm.ado -adopath+"/home/corentin/Documents/These/Recherche/Simulations/Modules/" - -************************************************** Scenario 9: J = 4 items / M = 2 modalities / DIF size 0.3 x2 - local N = "50" - foreach Nnn in `N' { - local Nn = `Nnn' - local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" - local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" - local scenarios = "A B C D E F G" - foreach scen in `scenarios' { - clear - import delim "`path_data'/scenario_9`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear - rename TT tt - - * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif - local nbitems = 4 - local nbdif = 2 - local taillemat = `nbitems'+`nbdif'+4 - mat outmat = J(1000,`taillemat',.) - mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "dif2" "beta" "se_beta" "dif_item_1" "dif_item_2" - di "Scenario 9`scen' / N=`Nnn'" - forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" - } - preserve - qui keep if replication==`k' - local difitems1=dif1 - local difitems2=dif2 - if (`difitems1'<`difitems2') { - local difitemsmin `difitems1' - local difitemsmax `difitems2' - } - else { - local difitemsmin `difitems2' - local difitemsmax `difitems1' - } - local mod "gsem " - forvalues i=1/`nbitems' { - if (`i'==`difitemsmin' | `i'==`difitemsmax') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)" - } - } - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" - qui `mod' - mat V=r(table) - mat W=V[1..2,1...] - forvalues j=1/`nbitems' { - if (`j'<`difitemsmin') { - mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif - } - else if (`j'<`difitemsmax') { - mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif - } - else { - mat outmat[`k',`j'] = W[1,2+3*`j'] // items après le deuxieme dif - } - } - mat outmat[`k',`nbitems'+1] = W[1,3*`difitemsmin'-1] // coef de dif - mat outmat[`k',`nbitems'+2] = W[1,3*`difitemsmax'] // coef de dif - mat outmat[`k',`nbitems'+3] = W[1,3*`nbitems'+3] // beta - mat outmat[`k',`nbitems'+4] = W[2,3*`nbitems'+3] // se beta - mat outmat[`k',`nbitems'+5] = `difitemsmin' // numéro item de dif - mat outmat[`k',`nbitems'+6] = `difitemsmax' // numéro item de dif - restore - } - putexcel set "`path_res'/out/9`scen'_`Nn'.xls", sheet("outmat") replace - putexcel A1=matrix(outmat), colnames -} -} - - - -************************************************** Scenario 10: J = 4 items / M = 4 modalities / DIF size 0.3x2 -local N = "50" -foreach Nnn in `N' { -local Nn = `Nnn' -local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" -local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" -foreach scen in `scenarios' { -clear -import delim "`path_data'/scenario_10`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear -rename TT tt - -* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 4 -local nbdif = 2 -local taillemat = 3*`nbitems'+`nbdif'+6+2 -mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "dif2_1" "dif2_2" "dif2_3" "beta" "se_beta" "dif_item_1" "dif_item_2" -di "Scenario 10`scen' / N=`Nnn'" -forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" - } - preserve - qui keep if replication==`k' - - -* MERGE des modalités si non représentées - local com_z = 0 - qui gen comz = 0 - forvalues j = 1 / `nbitems' { - local recoda_`j' = 0 - qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') - local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] - local minm`j'_t1_g0 = rect1_g0_`j'[1,1] - - qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') - local minm`j'_t1_g1 = rect1_g1_`j'[1,1] - local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] - - local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') - local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') - local nbm_`j' = `=`maxm_`j''-`minm_`j''' - - if `minm_`j'' != 0 & `com_z' == 0 { - local com_z = 1 - } - - qui count if item`j' == 3 & tt == 0 - local mod3plac = r(N) - qui count if item`j' == 3 & tt == 1 - local mod3tt = r(N) - local nb_rn3 = min(`mod3plac',`mod3tt') - if `nb_rn3'==0 { - qui replace comz = 1 - } - - forvalues m = 0/`=`nbm_`j''-1' { - qui count if item`j' == `m' & tt == 0 - local nb_rn1_g0 = r(N) - qui count if item`j' == `m' & tt == 1 - local nb_rn1_g1 = r(N) - local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') - if `nb_rn' == 0 { - qui replace comz = 1 - local recoda_`j' = 1 - if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'+`kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`m'' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m' - `kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else { - if runiform()>0.5{ - local stop = 1 - forvalues kk = 1/`m' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'-`kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else { - local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ - qui replace item`j'=`=`m' + `kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - else { - if `stop' != 0 { - qui replace item`j'= `nbm_`j'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - } - } - } - } - qui levelsof item`j' - local val = r(levels) - local checker: word 1 of `val' - local checker2: word 2 of `val' - local checker3: word 3 of `val' - if (`checker'!=0) { - qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) - } - else if (`checker2'!=1) { - qui recode item`j' (`checker2'=1) (`checker3'=2) - } - else if (`checker3'!=2) { - qui recode item`j' (`checker3'=2) - } - } - -qui valuesof comz -local val = r(values) -local checker: word 1 of `val' - -* replications avec 3 mod -if (`checker' == 1) { - local difitems1=dif1 - local difitems2=dif2 - local difcheck = 0 - local difcheck2 = 0 -forvalues i=1/`nbitems' { - qui sum item`i' - local checker2 = r(max) - - if (`i'==`difitems1') { - if (`checker2'==3) { - local difcheck = 1 - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } - else { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - } - } - if (`i'==`difitems2') { - if (`checker2'==3) { - local difcheck2=1 - local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } - else { - local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - } - } -} -local mod "gsem " -forvalues i=1/`nbitems' { - qui sum item`i' - local checker2 = r(max) - if (`i'==`difitems1'| `i'==`difitems2') { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" - } - } - else { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" - } -} -} -if (`difcheck'==1) { - if (`difcheck2'==1) { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" - } - else { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" - } -} -else { - if (`difcheck2'==1) { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" - } - else { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" - } -} -qui `constrnt' -qui `constrnt2' -if (`difcheck'==1) { - qui `constrnt3' -} -if (`difcheck2'==1) { - qui `constrnt4' -} -qui `mod' -mat V=r(table) -mat W=V[1..2,1...] -forvalues j=1/`nbitems' { - - mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif -} - mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta - mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta - mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif - mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif - restore -} - -* replications sans problèmes -else { - local difitems1=dif1 - local difitems2=dif2 -forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } - if (`i'==`difitems2') { - local constrnt3 = "constraint 3 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } -} -local mod "gsem " -forvalues i=1/`nbitems' { - if (`i'==`difitems1' | `i'==`difitems2') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" - } -} -local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" -qui `constrnt' -qui `constrnt2' -qui `constrnt3' -qui `constrnt4' -qui `mod' -mat V=r(table) -mat W=V[1..2,1...] -forvalues j=1/`nbitems' { - mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif - } - mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta - mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta - mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif - mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif - restore -} -} -putexcel set "`path_res'/out/10`scen'_`Nn'.xls", sheet("outmat") replace -putexcel A1=matrix(outmat), colnames -} -} - - - - -************************************************** Scenario 11: J = 4 items / M = 2 modalities / DIF size 0.5 x2 - local N = "50" - foreach Nnn in `N' { - local Nn = `Nnn' - local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" - local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" - local scenarios = "A B C D E F G" - foreach scen in `scenarios' { - clear - import delim "`path_data'/scenario_11`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear - rename TT tt - - * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif - local nbitems = 4 - local nbdif = 2 - local taillemat = `nbitems'+`nbdif'+4 - mat outmat = J(1000,`taillemat',.) - mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "dif2" "beta" "se_beta" "dif_item_1" "dif_item_2" - di "Scenario 11`scen' / N=`Nnn'" - forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" - } - preserve - qui keep if replication==`k' - local difitems1=dif1 - local difitems2=dif2 - if (`difitems1'<`difitems2') { - local difitemsmin `difitems1' - local difitemsmax `difitems2' - } - else { - local difitemsmin `difitems2' - local difitemsmax `difitems1' - } - local mod "gsem " - forvalues i=1/`nbitems' { - if (`i'==`difitemsmin' | `i'==`difitemsmax') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)" - } - } - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" - qui `mod' - mat V=r(table) - mat W=V[1..2,1...] - forvalues j=1/`nbitems' { - if (`j'<`difitemsmin') { - mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif - } - else if (`j'<`difitemsmax') { - mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif - } - else { - mat outmat[`k',`j'] = W[1,2+3*`j'] // items après le deuxieme dif - } - } - mat outmat[`k',`nbitems'+1] = W[1,3*`difitemsmin'-1] // coef de dif - mat outmat[`k',`nbitems'+2] = W[1,3*`difitemsmax'] // coef de dif - mat outmat[`k',`nbitems'+3] = W[1,3*`nbitems'+3] // beta - mat outmat[`k',`nbitems'+4] = W[2,3*`nbitems'+3] // se beta - mat outmat[`k',`nbitems'+5] = `difitemsmin' // numéro item de dif - mat outmat[`k',`nbitems'+6] = `difitemsmax' // numéro item de dif - restore - } - putexcel set "`path_res'/out/11`scen'_`Nn'.xls", sheet("outmat") replace - putexcel A1=matrix(outmat), colnames -} -} - - - -************************************************** Scenario 12: J = 4 items / M = 4 modalities / DIF size 0.5 x2 -local N = "50" -foreach Nnn in `N' { -local Nn = `Nnn' -local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" -local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" -foreach scen in `scenarios' { -clear -import delim "`path_data'/scenario_12`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear -rename TT tt - -* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 4 -local nbdif = 2 -local taillemat = 3*`nbitems'+`nbdif'+6+2 -mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "dif2_1" "dif2_2" "dif2_3" "beta" "se_beta" "dif_item_1" "dif_item_2" -di "Scenario 12`scen' / N=`Nnn'" -forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" - } - preserve - qui keep if replication==`k' - - -* MERGE des modalités si non représentées - local com_z = 0 - qui gen comz = 0 - forvalues j = 1 / `nbitems' { - local recoda_`j' = 0 - qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') - local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] - local minm`j'_t1_g0 = rect1_g0_`j'[1,1] - - qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') - local minm`j'_t1_g1 = rect1_g1_`j'[1,1] - local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] - - local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') - local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') - local nbm_`j' = `=`maxm_`j''-`minm_`j''' - - if `minm_`j'' != 0 & `com_z' == 0 { - local com_z = 1 - } - - qui count if item`j' == 3 & tt == 0 - local mod3plac = r(N) - qui count if item`j' == 3 & tt == 1 - local mod3tt = r(N) - local nb_rn3 = min(`mod3plac',`mod3tt') - if `nb_rn3'==0 { - qui replace comz = 1 - } - - forvalues m = 0/`=`nbm_`j''-1' { - qui count if item`j' == `m' & tt == 0 - local nb_rn1_g0 = r(N) - qui count if item`j' == `m' & tt == 1 - local nb_rn1_g1 = r(N) - local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') - if `nb_rn' == 0 { - qui replace comz = 1 - local recoda_`j' = 1 - if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'+`kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`m'' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m' - `kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else { - if runiform()>0.5{ - local stop = 1 - forvalues kk = 1/`m' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'-`kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else { - local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ - qui replace item`j'=`=`m' + `kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - else { - if `stop' != 0 { - qui replace item`j'= `nbm_`j'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - } - } - } - } - qui levelsof item`j' - local val = r(levels) - local checker: word 1 of `val' - local checker2: word 2 of `val' - local checker3: word 3 of `val' - if (`checker'!=0) { - qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) - } - else if (`checker2'!=1) { - qui recode item`j' (`checker2'=1) (`checker3'=2) - } - else if (`checker3'!=2) { - qui recode item`j' (`checker3'=2) - } - } - -qui valuesof comz -local val = r(values) -local checker: word 1 of `val' - -* replications avec 3 mod -if (`checker' == 1) { - local difitems1=dif1 - local difitems2=dif2 - local difcheck = 0 - local difcheck2 = 0 -forvalues i=1/`nbitems' { - qui sum item`i' - local checker2 = r(max) - - if (`i'==`difitems1') { - if (`checker2'==3) { - local difcheck = 1 - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } - else { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - } - } - if (`i'==`difitems2') { - if (`checker2'==3) { - local difcheck2=1 - local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } - else { - local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - } - } -} -local mod "gsem " -forvalues i=1/`nbitems' { - qui sum item`i' - local checker2 = r(max) - if (`i'==`difitems1'| `i'==`difitems2') { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" - } - } - else { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" - } -} -} -if (`difcheck'==1) { - if (`difcheck2'==1) { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" - } - else { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" - } -} -else { - if (`difcheck2'==1) { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" - } - else { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" - } -} -qui `constrnt' -qui `constrnt2' -if (`difcheck'==1) { - qui `constrnt3' -} -if (`difcheck2'==1) { - qui `constrnt4' -} -qui `mod' -mat V=r(table) -mat W=V[1..2,1...] -forvalues j=1/`nbitems' { - - mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif -} - mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta - mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta - mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif - mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif - restore -} - -* replications sans problèmes -else { - local difitems1=dif1 - local difitems2=dif2 -forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } - if (`i'==`difitems2') { - local constrnt3 = "constraint 3 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } -} -local mod "gsem " -forvalues i=1/`nbitems' { - if (`i'==`difitems1' | `i'==`difitems2') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" - } -} -local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" -qui `constrnt' -qui `constrnt2' -qui `constrnt3' -qui `constrnt4' -qui `mod' -mat V=r(table) -mat W=V[1..2,1...] -forvalues j=1/`nbitems' { - mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif - } - mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif - mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta - mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta - mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif - mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif - restore -} -} -putexcel set "`path_res'/out/12`scen'_`Nn'.xls", sheet("outmat") replace -putexcel A1=matrix(outmat), colnames -} -} - - *========================== * Scenarios with : J=7 *========================== @@ -1372,8 +93,6 @@ adopath+"/home/corentin/Documents/These/Recherche/Simulations/Modules/" } } - - ************************************************** Scenario 14: J = 7 items / M = 4 modalities / DIF size 0.3 x2 local N = "50" foreach Nnn in `N' { @@ -1522,6 +241,11 @@ forvalues k=1/1000 { local checker: word 1 of `val' local checker2: word 2 of `val' local checker3: word 3 of `val' + local nummoda=r(r) + if (`nummoda'==2) { + qui recode item`j' (`checker'=0) (`checker2'=1) + } + if (`nummoda'==3) { if (`checker'!=0) { qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) } @@ -1532,6 +256,7 @@ forvalues k=1/1000 { qui recode item`j' (`checker3'=2) } } +} qui valuesof comz local val = r(values) @@ -1553,9 +278,12 @@ forvalues i=1/`nbitems' { local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck = 2 + } } if (`i'==`difitems2') { if (`checker2'==3) { @@ -1563,9 +291,12 @@ forvalues i=1/`nbitems' { local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck2 = 2 + } } } local mod "gsem " @@ -1576,37 +307,64 @@ forvalues i=1/`nbitems' { if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" } - else { + else if (`checker2'==2) { local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" } + else if (`checker2'==1) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } } else { if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" } - else { + else if (`checker2'==2) { local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" } + else if (`checker2'==1) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } } } if (`difcheck'==1) { if (`difcheck2'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" } - else { + else if (`difcheck2'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" + } } -else { +else if (`difcheck'==0) { if (`difcheck2'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" } - else { + else if (`difcheck2'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" + } +} +else if (`difcheck'==2) { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4)" + } + else if (`difcheck2'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2)" + } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + } } +if (`difcheck'!=2) { qui `constrnt' +} +if (`difcheck2'!=2) { qui `constrnt2' +} if (`difcheck'==1) { qui `constrnt3' } @@ -1691,6 +449,7 @@ putexcel A1=matrix(outmat), colnames + ************************************************** Scenario 15: J = 7 items / M = 2 modalities / DIF size 0.5 x2 local N = "50" foreach Nnn in `N' { @@ -1778,7 +537,7 @@ import delim "`path_data'/scenario_16`scen'_`Nn'.csv", encoding(ISO-8859-2) cas rename TT tt * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 7 +local nbitems = 4 local nbdif = 2 local taillemat = 3*`nbitems'+6+2+`nbdif' mat outmat = J(1000,`taillemat',.) @@ -1913,6 +672,11 @@ forvalues k=1/1000 { local checker: word 1 of `val' local checker2: word 2 of `val' local checker3: word 3 of `val' + local nummoda=r(r) + if (`nummoda'==2) { + qui recode item`j' (`checker'=0) (`checker2'=1) + } + if (`nummoda'==3) { if (`checker'!=0) { qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) } @@ -1923,7 +687,7 @@ forvalues k=1/1000 { qui recode item`j' (`checker3'=2) } } - +} qui valuesof comz local val = r(values) local checker: word 1 of `val' @@ -1944,9 +708,12 @@ forvalues i=1/`nbitems' { local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck = 2 + } } if (`i'==`difitems2') { if (`checker2'==3) { @@ -1954,9 +721,12 @@ forvalues i=1/`nbitems' { local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck2 = 2 + } } } local mod "gsem " @@ -1967,37 +737,64 @@ forvalues i=1/`nbitems' { if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" } - else { + else if (`checker2'==2) { local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" } + else if (`checker2'==1) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } } else { if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" } - else { + else if (`checker2'==2) { local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" } + else if (`checker2'==1) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } } } if (`difcheck'==1) { if (`difcheck2'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" } - else { + else if (`difcheck2'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" + } } -else { +else if (`difcheck'==0) { if (`difcheck2'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" } - else { + else if (`difcheck2'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" + } +} +else if (`difcheck'==2) { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4)" + } + else if (`difcheck2'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2)" + } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + } } +if (`difcheck'!=2) { qui `constrnt' +} +if (`difcheck2'!=2) { qui `constrnt2' +} if (`difcheck'==1) { qui `constrnt3' } @@ -2324,6 +1121,11 @@ forvalues k=1/1000 { local checker: word 1 of `val' local checker2: word 2 of `val' local checker3: word 3 of `val' + local nummoda=r(r) + if (`nummoda'==2) { + qui recode item`j' (`checker'=0) (`checker2'=1) + } + if (`nummoda'==3) { if (`checker'!=0) { qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) } @@ -2334,6 +1136,7 @@ forvalues k=1/1000 { qui recode item`j' (`checker3'=2) } } +} qui valuesof comz local val = r(values) local checker: word 1 of `val' @@ -2356,9 +1159,12 @@ forvalues i=1/`nbitems' { local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck = 2 + } } if (`i'==`difitems2') { if (`checker2'==3) { @@ -2366,9 +1172,12 @@ forvalues i=1/`nbitems' { local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck2 = 2 + } } if (`i'==`difitems3') { if (`checker2'==3) { @@ -2376,9 +1185,12 @@ forvalues i=1/`nbitems' { local constrnt5 = "constraint 5 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt6 = "constraint 6 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt5 = "constraint 5 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck3 = 2 + } } } @@ -2390,17 +1202,23 @@ forvalues i=1/`nbitems' { if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" } - else { + else if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } } else { if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" } - else { + else if (`checker2'==2) { local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } } } if (`difcheck'==1) { @@ -2408,40 +1226,115 @@ if (`difcheck'==1) { if (`difcheck3'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4 5 6)" } - else { + else if (`difcheck3'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4 5)" } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" + } } - else { + else if (`difcheck2'==0) { if (`difcheck3'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 5 6)" } - else { + else if (`difcheck3'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 5)" } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" + } + } + else if (`difcheck2'==2) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 3 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 3 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 3)" + } } } -else { +else if (`difcheck'==1) { if (`difcheck2'==1) { if (`difcheck3'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4 5 6)" } - else { + else if (`difcheck3'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4 5)" } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" + } } - else { + else if (`difcheck2'==0) { if (`difcheck3'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 5 6)" } - else { + else if (`difcheck3'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 5)" } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" + } + } + else if (`difcheck2'==2) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" + } } } -qui `constrnt' -qui `constrnt2' -qui `constrnt5' +else if (`difcheck'==2) { + if (`difcheck2'==1) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4)" + } + } + else if (`difcheck2'==0) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2)" + } + } + else if (`difcheck2'==2) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + } + } +} +if (`difcheck'!=2) { + qui `constrnt' +} +if (`difcheck2'!=2) { + qui `constrnt2' +} +if (`difcheck3'!=2) { + qui `constrnt5' +} if (`difcheck'==1) { qui `constrnt3' } @@ -2689,7 +1582,7 @@ putexcel A1=matrix(outmat), colnames -************************************************** Scenario 20: J = 7 items / M = 4 modalities / DIF size 0.5 x3 +************************************************** Scenario 20: J = 7 items / M = 4 modalities / DIF size 0.3 x3 local N = "50" foreach Nnn in `N' { local Nn = `Nnn' @@ -2836,6 +1729,11 @@ forvalues k=1/1000 { local checker: word 1 of `val' local checker2: word 2 of `val' local checker3: word 3 of `val' + local nummoda=r(r) + if (`nummoda'==2) { + qui recode item`j' (`checker'=0) (`checker2'=1) + } + if (`nummoda'==3) { if (`checker'!=0) { qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) } @@ -2846,6 +1744,7 @@ forvalues k=1/1000 { qui recode item`j' (`checker3'=2) } } +} qui valuesof comz local val = r(values) local checker: word 1 of `val' @@ -2868,9 +1767,12 @@ forvalues i=1/`nbitems' { local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck = 2 + } } if (`i'==`difitems2') { if (`checker2'==3) { @@ -2878,9 +1780,12 @@ forvalues i=1/`nbitems' { local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck2 = 2 + } } if (`i'==`difitems3') { if (`checker2'==3) { @@ -2888,9 +1793,12 @@ forvalues i=1/`nbitems' { local constrnt5 = "constraint 5 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" local constrnt6 = "constraint 6 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt5 = "constraint 5 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } + else if (`checker2'==1) { + local difcheck3 = 2 + } } } @@ -2902,17 +1810,23 @@ forvalues i=1/`nbitems' { if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" } - else { + else if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } } else { if (`checker2'==3) { local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" } - else { + else if (`checker2'==2) { local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } } } if (`difcheck'==1) { @@ -2920,40 +1834,115 @@ if (`difcheck'==1) { if (`difcheck3'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4 5 6)" } - else { + else if (`difcheck3'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4 5)" } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" + } } - else { + else if (`difcheck2'==0) { if (`difcheck3'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 5 6)" } - else { + else if (`difcheck3'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 5)" } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" + } + } + else if (`difcheck2'==2) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 3 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 3 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 3)" + } } } -else { +else if (`difcheck'==1) { if (`difcheck2'==1) { if (`difcheck3'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4 5 6)" } - else { + else if (`difcheck3'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4 5)" } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" + } } - else { + else if (`difcheck2'==0) { if (`difcheck3'==1) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 5 6)" } - else { + else if (`difcheck3'==0) { local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 5)" } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" + } + } + else if (`difcheck2'==2) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" + } } } -qui `constrnt' -qui `constrnt2' -qui `constrnt5' +else if (`difcheck'==2) { + if (`difcheck2'==1) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4)" + } + } + else if (`difcheck2'==0) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2)" + } + } + else if (`difcheck2'==2) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + } + } +} +if (`difcheck'!=2) { + qui `constrnt' +} +if (`difcheck2'!=2) { + qui `constrnt2' +} +if (`difcheck3'!=2) { + qui `constrnt5' +} if (`difcheck'==1) { qui `constrnt3' } diff --git a/Scripts/Analysis/DIF/temp/temp.do b/Scripts/Analysis/DIF/temp/temp.do index 79fa1ec..09ef0c6 100644 --- a/Scripts/Analysis/DIF/temp/temp.do +++ b/Scripts/Analysis/DIF/temp/temp.do @@ -1,379 +1,169 @@ -*================================================================================================================================= -* Date : 2024-01-23 -* Stata version : Stata 18 SE -* -* This program analyses simulated data accounting for DIF through a partial credit model -* -* ado-files needed : - pcm (version 5.5 October 25, 2023, available on gitea) -* -* outputs : for N=100 -* -* -*================================================================================================================================================ -* Load pcm.ado -adopath+"/home/corentin/Documents/These/Recherche/Simulations/Modules/" - -*========================== -* Scenarios with : J=4 -*========================== - -*************************************************** Scenarios with DIF on 1 item - -************************************************** Scenario 5: J = 4 items / M = 2 modalities / DIF size 0.3 +************************************************** Scenario 12: J = 4 items / M = 4 modalities / DIF size 0.5 x2 local N = "50" foreach Nnn in `N' { local Nn = `Nnn' local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" +local scenarios = "E" foreach scen in `scenarios' { clear -import delim "`path_data'/scenario_5`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear +import delim "`path_data'/scenario_12`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear rename TT tt * Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif local nbitems = 4 -local nbdif = 1 -local taillemat = `nbitems'+`nbdif'+3 +local nbdif = 2 +local taillemat = 3*`nbitems'+`nbdif'+6+2 mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "beta" "se_beta" "dif_item_1" -di "Scenario 5`scen' / N=`Nnn'" +mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "dif2_1" "dif2_2" "dif2_3" "beta" "se_beta" "dif_item_1" "dif_item_2" +di "Scenario 12`scen' / N=`Nnn'" forvalues k=1/1000 { if (mod(`k',100)==0) { di "`k'/1000" } preserve qui keep if replication==`k' - local difitems1=dif1 - local mod "gsem " - forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)" - } - } - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" - qui `mod' - mat V=r(table) - mat W=V[1..2,1...] - forvalues j=1/`nbitems' { - if (`j'<`difitems1') { - mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif - } - else { - mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif - } - } - mat outmat[`k',`nbitems'+1] = W[1,3*`difitems1'-1] // coef de dif - mat outmat[`k',`nbitems'+2] = W[1,3*`nbitems'+2] // beta - mat outmat[`k',`nbitems'+3] = W[2,3*`nbitems'+2] // se beta - mat outmat[`k',`nbitems'+4] = `difitems1' // numéro item de dif - restore -} -putexcel set "`path_res'/out/5`scen'_`Nn'.xls", sheet("outmat") replace -putexcel A1=matrix(outmat), colnames -} -} - -************************************************** Scenario 6: J = 4 items / M = 4 modalities / DIF size 0.3 -local N = "50" -foreach Nnn in `N' { -local Nn = `Nnn' -local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" -local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" -foreach scen in `scenarios' { -clear -import delim "`path_data'/scenario_6`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear -rename TT tt -* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 4 -local nbdif = 1 -local taillemat = 3*`nbitems'+`nbdif'+3+2 -mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "beta" "se_beta" "dif_item_1" -di "Scenario 6`scen' / N=`Nnn'" -forvalues k=1/1000 { -if (mod(`k',100)==0) { - di "`k'/1000" -} -preserve -qui keep if replication==`k' -local difitems1=dif1 -forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" - } -} -local mod "gsem " -forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" - } -} -local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" -qui `constrnt' -qui `constrnt2' -qui `mod' -mat V=r(table) -mat W=V[1..2,1...] -forvalues j=1/`nbitems' { -if (`j'<`difitems1') { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+3] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+5] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+7] // items avant le premier dif +* MERGE des modalités si non représentées + local com_z = 0 + qui gen comz = 0 + forvalues j = 1 / `nbitems' { + local recoda_`j' = 0 + qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') + local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] + local minm`j'_t1_g0 = rect1_g0_`j'[1,1] + + qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') + local minm`j'_t1_g1 = rect1_g1_`j'[1,1] + local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] + + local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') + local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') + local nbm_`j' = `=`maxm_`j''-`minm_`j''' + + if `minm_`j'' != 0 & `com_z' == 0 { + local com_z = 1 } -else if (`j'==`difitems1') { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+4] // items du le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+7] // items du le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items du le premier dif } - } -else { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+6] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+8] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items avant le premier dif - } -} - mat outmat[`k',3*`nbitems'+1] = W[1,7*(`difitems1'-1)+2] // coef de dif - mat outmat[`k',3*`nbitems'+2] = W[1,7*(`difitems1'-1)+5] // coef de dif - mat outmat[`k',3*`nbitems'+3] = W[1,7*(`difitems1'-1)+8] // coef de dif - mat outmat[`k',3*`nbitems'+4] = W[1,7*`nbitems'+4] // beta - mat outmat[`k',3*`nbitems'+5] = W[2,7*`nbitems'+4] // se beta - mat outmat[`k',3*`nbitems'+6] = `difitems1' // numéro item de dif - restore -} -putexcel set "`path_res'/out/6`scen'_`Nn'.xls", sheet("outmat") replace -putexcel A1=matrix(outmat), colnames -} -} - -************************************************** Scenario 7: J = 4 items / M = 2 modalities / DIF size 0.5 -local N = "50" -foreach Nnn in `N' { -local Nn = `Nnn' -local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" -local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" -foreach scen in `scenarios' { -clear -import delim "`path_data'/scenario_7`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear -rename TT tt - -* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 4 -local nbdif = 1 -local taillemat = `nbitems'+`nbdif'+3 -mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "beta" "se_beta" "dif_item_1" -di "Scenario 7`scen' / N=`Nnn'" -forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" - } - preserve - qui keep if replication==`k' - local difitems1=dif1 - local mod "gsem " - forvalues i=1/`nbitems' { - if (`i'==`difitems1') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" - } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)" - } - } - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" - qui `mod' - mat V=r(table) - mat W=V[1..2,1...] - forvalues j=1/`nbitems' { - if (`j'<`difitems1') { - mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif - } - else { - mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif + qui count if item`j' == 3 & tt == 0 + local mod3plac = r(N) + qui count if item`j' == 3 & tt == 1 + local mod3tt = r(N) + local nb_rn3 = min(`mod3plac',`mod3tt') + if `nb_rn3'==0 { + qui replace comz = 1 } - } - mat outmat[`k',`nbitems'+1] = W[1,3*`difitems1'-1] // coef de dif - mat outmat[`k',`nbitems'+2] = W[1,3*`nbitems'+2] // beta - mat outmat[`k',`nbitems'+3] = W[2,3*`nbitems'+2] // se beta - mat outmat[`k',`nbitems'+4] = `difitems1' // numéro item de dif - restore -} -putexcel set "`path_res'/out/7`scen'_`Nn'.xls", sheet("outmat") replace -putexcel A1=matrix(outmat), colnames -} -} - - - -************************************************** Scenario 8: J = 4 items / M = 4 modalities / DIF size 0.5 -local N = "50" -foreach Nnn in `N' { -local Nn = `Nnn' -local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" -local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" -foreach scen in `scenarios' { -clear -import delim "`path_data'/scenario_8`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear -rename TT tt - -* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 4 -local nbdif = 1 -local taillemat = 3*`nbitems'+`nbdif'+3+2 -mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "beta" "se_beta" "dif_item_1" -di "Scenario 8`scen' / N=`Nnn'" -forvalues k=1/1000 { -if (mod(`k',100)==0) { - di "`k'/1000" -} -preserve -qui keep if replication==`k' - -* MERGE des modalités si non représentées - local com_z = 0 - qui gen comz = 0 - forvalues j = 1 / `nbitems' { - local recoda_`j' = 0 - qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') - local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] - local minm`j'_t1_g0 = rect1_g0_`j'[1,1] - - qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') - local minm`j'_t1_g1 = rect1_g1_`j'[1,1] - local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] - - local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') - local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') - local nbm_`j' = `=`maxm_`j''-`minm_`j''' - - if `minm_`j'' != 0 & `com_z' == 0 { - local com_z = 1 - } - qui count if item`j' == 3 & tt == 0 - local mod3plac = r(N) - qui count if item`j' == 3 & tt == 1 - local mod3tt = r(N) - local nb_rn3 = min(`mod3plac',`mod3tt') - if `nb_rn3'==0 { - qui replace comz = 1 - } - - forvalues m = 0/`=`nbm_`j''-1' { - qui count if item`j' == `m' & tt == 0 - local nb_rn1_g0 = r(N) - qui count if item`j' == `m' & tt == 1 - local nb_rn1_g1 = r(N) - local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') - if `nb_rn' == 0 { - qui replace comz = 1 - local recoda_`j' = 1 - if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'+`kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`m'' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m' - `kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else { - if runiform()>0.5{ + forvalues m = 0/`=`nbm_`j''-1' { + qui count if item`j' == `m' & tt == 0 + local nb_rn1_g0 = r(N) + qui count if item`j' == `m' & tt == 1 + local nb_rn1_g1 = r(N) + local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') + if `nb_rn' == 0 { + qui replace comz = 1 + local recoda_`j' = 1 + if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { local stop = 1 - forvalues kk = 1/`m' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 + qui count if item`j' == `=`m' + `kk'' & tt == 1 local v`kk'1_1 = r(N) if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'-`kk'' if item`j'==`m' + qui replace item`j'= `=`m'+`kk'' if item`j'==`m' local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' + *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" local stop = 0 } } } - else { + else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 + forvalues kk = 1/`=`m'' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 + qui count if item`j' == `=`m' - `kk'' & tt == 1 local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ - qui replace item`j'=`=`m' + `kk'' if item`j'==`m' + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m' - `kk'' if item`j'==`m' local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' + *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" local stop = 0 } - else { - if `stop' != 0 { - qui replace item`j'= `nbm_`j'' if item`j'==`m' + } + } + else { + if runiform()>0.5{ + local stop = 1 + forvalues kk = 1/`m' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' - `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'-`kk'' if item`j'==`m' local zzz=`j'+`nbitems' - *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' + *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" local stop = 0 } } } + else { + local stop = 1 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' + `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ + qui replace item`j'=`=`m' + `kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + else { + if `stop' != 0 { + qui replace item`j'= `nbm_`j'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + } } } } + qui levelsof item`j' + local val = r(levels) + local checker: word 1 of `val' + local checker2: word 2 of `val' + local checker3: word 3 of `val' + local nummoda=r(r) + if (`nummoda'==2) { + qui recode item`j' (`checker'=0) (`checker2'=1) + } + if (`nummoda'==3) { + if (`checker'!=0) { + qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) + } + else if (`checker2'!=1) { + qui recode item`j' (`checker2'=1) (`checker3'=2) + } + else if (`checker3'!=2) { + qui recode item`j' (`checker3'=2) + } + } + } - qui levelsof item`j' - local val = r(levels) - local checker: word 1 of `val' - local checker2: word 2 of `val' - local checker3: word 3 of `val' - if (`checker'!=0) { - qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) - } - else if (`checker2'!=1) { - qui recode item`j' (`checker2'=1) (`checker3'=2) - } - else if (`checker3'!=2) { - qui recode item`j' (`checker3'=2) - } -} qui valuesof comz local val = r(values) @@ -381,490 +171,158 @@ local checker: word 1 of `val' * replications avec 3 mod if (`checker' == 1) { -local difitems1=dif1 -local difcheck = 0 + local difitems1=dif1 + local difitems2=dif2 + local difcheck = 0 + local difcheck2 = 0 forvalues i=1/`nbitems' { -qui sum item`i' -local checker2 = r(max) -if (`i'==`difitems1') { - if (`checker2'==3) { - local difcheck = 1 - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + qui sum item`i' + local checker2 = r(max) + + if (`i'==`difitems1') { + if (`checker2'==3) { + local difcheck = 1 + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } - else { + else if (`checker2'==2) { local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } -} -} -local mod "gsem " -forvalues i=1/`nbitems' { -qui sum item`i' -local checker2 = r(max) -if (`i'==`difitems1') { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" -} -else { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" -} -} -else { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + else if (`checker2'==1) { + local difcheck = 2 } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" } -} -} -if (`difcheck'==1) { -local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" -} -if (`difcheck'==0) { -local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" -} -qui `constrnt' -if (`difcheck'==1) { -qui `constrnt2' -} -qui `mod' -mat V=r(table) -mat W=V[1..2,1...] -forvalues j=1/`nbitems' { - - mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif -} -mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"THETA:tt")] // beta -mat outmat[`k',3*`nbitems'+5] = W[2,colnumb(W,"THETA:tt")] // se beta -mat outmat[`k',3*`nbitems'+6] = `difitems1' // numéro item de dif -restore -} - -* replications sans problèmes -else { -local difitems1=dif1 -forvalues i=1/`nbitems' { -if (`i'==`difitems1') { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" -} -} -local mod "gsem " -forvalues i=1/`nbitems' { -if (`i'==`difitems1') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" -} -else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" -} -} -local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" -qui `constrnt' -qui `constrnt2' -qui `mod' -mat V=r(table) -mat W=V[1..2,1...] -forvalues j=1/`nbitems' { -if (`j'<`difitems1') { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+3] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+5] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+7] // items avant le premier dif + if (`i'==`difitems2') { + if (`checker2'==3) { + local difcheck2=1 + local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" } -else if (`j'==`difitems1') { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+4] // items du le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+7] // items du le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items du le premier dif } -} -else { - mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+6] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+8] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items avant le premier dif -} -} -mat outmat[`k',3*`nbitems'+1] = W[1,7*(`difitems1'-1)+2] // coef de dif -mat outmat[`k',3*`nbitems'+2] = W[1,7*(`difitems1'-1)+5] // coef de dif -mat outmat[`k',3*`nbitems'+3] = W[1,7*(`difitems1'-1)+8] // coef de dif -mat outmat[`k',3*`nbitems'+4] = W[1,7*`nbitems'+4] // beta -mat outmat[`k',3*`nbitems'+5] = W[2,7*`nbitems'+4] // se beta -mat outmat[`k',3*`nbitems'+6] = `difitems1' // numéro item de dif -restore -} -} -putexcel set "`path_res'/out/8`scen'_`Nn'.xls", sheet("outmat") replace -putexcel A1=matrix(outmat), colnames -} -} - - - -*************************************************** Scenarios with DIF on 2 items - -* Load pcm.ado -adopath+"/home/corentin/Documents/These/Recherche/Simulations/Modules/" - -************************************************** Scenario 9: J = 4 items / M = 2 modalities / DIF size 0.3 x2 -local N = "50" -foreach Nnn in `N' { -local Nn = `Nnn' -local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" -local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" -foreach scen in `scenarios' { -clear -import delim "`path_data'/scenario_9`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear -rename TT tt - -* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 4 -local nbdif = 2 -local taillemat = `nbitems'+`nbdif'+4 -mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1" "item2" "item3" "item4" "dif1" "dif2" "beta" "se_beta" "dif_item_1" "dif_item_2" -di "Scenario 9`scen' / N=`Nnn'" -forvalues k=1/1000 { - if (mod(`k',100)==0) { - di "`k'/1000" + else if (`checker2'==2) { + local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" } - preserve - qui keep if replication==`k' - local difitems1=dif1 - local difitems2=dif2 - if (`difitems1'<`difitems2') { - local difitemsmin `difitems1' - local difitemsmax `difitems2' + else if (`checker2'==1) { + local difcheck2 = 2 } - else { - local difitemsmin `difitems2' - local difitemsmax `difitems1' } - local mod "gsem " - forvalues i=1/`nbitems' { - if (`i'==`difitemsmin' | `i'==`difitemsmax') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" +} +local mod "gsem " +forvalues i=1/`nbitems' { + qui sum item`i' + local checker2 = r(max) + if (`i'==`difitems1'| `i'==`difitems2') { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else if (`checker2'==2) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" } else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)" + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" } } - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" - qui `mod' - mat V=r(table) - mat W=V[1..2,1...] - forvalues j=1/`nbitems' { - if (`j'<`difitemsmin') { - mat outmat[`k',`j'] = W[1,3*`j'] // items avant le premier dif - } - else if (`j'<`difitemsmax') { - mat outmat[`k',`j'] = W[1,1+3*`j'] // items après le premier dif - } - else { - mat outmat[`k',`j'] = W[1,2+3*`j'] // items après le deuxieme dif - } + else { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } + else if (`checker2'==2) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" } - mat outmat[`k',`nbitems'+1] = W[1,3*`difitemsmin'-1] // coef de dif - mat outmat[`k',`nbitems'+2] = W[1,3*`difitemsmax'] // coef de dif - mat outmat[`k',`nbitems'+3] = W[1,3*`nbitems'+3] // beta - mat outmat[`k',`nbitems'+4] = W[2,3*`nbitems'+3] // se beta - mat outmat[`k',`nbitems'+5] = `difitemsmin' // numéro item de dif - mat outmat[`k',`nbitems'+6] = `difitemsmax' // numéro item de dif - restore -} -putexcel set "`path_res'/out/9`scen'_`Nn'.xls", sheet("outmat") replace -putexcel A1=matrix(outmat), colnames } } - - - -************************************************** Scenario 10: J = 4 items / M = 4 modalities / DIF size 0.3x2 -local N = "50" -foreach Nnn in `N' { -local Nn = `Nnn' -local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" -local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" -local scenarios = "A B C D E F G" -foreach scen in `scenarios' { -clear -import delim "`path_data'/scenario_10`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear -rename TT tt - -* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif -local nbitems = 4 -local nbdif = 2 -local taillemat = 3*`nbitems'+`nbdif'+6+2 -mat outmat = J(1000,`taillemat',.) -mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "dif1_1" "dif1_2" "dif1_3" "dif2_1" "dif2_2" "dif2_3" "beta" "se_beta" "dif_item_1" "dif_item_2" -di "Scenario 10`scen' / N=`Nnn'" -forvalues k=1/1000 { -if (mod(`k',100)==0) { - di "`k'/1000" -} -preserve -qui keep if replication==`k' - - -* MERGE des modalités si non représentées - local com_z = 0 - qui gen comz = 0 - forvalues j = 1 / `nbitems' { - local recoda_`j' = 0 - qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') - local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] - local minm`j'_t1_g0 = rect1_g0_`j'[1,1] - - qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') - local minm`j'_t1_g1 = rect1_g1_`j'[1,1] - local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] - - local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') - local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') - local nbm_`j' = `=`maxm_`j''-`minm_`j''' - - if `minm_`j'' != 0 & `com_z' == 0 { - local com_z = 1 +if (`difcheck'==1) { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" } - - qui count if item`j' == 3 & tt == 0 - local mod3plac = r(N) - qui count if item`j' == 3 & tt == 1 - local mod3tt = r(N) - local nb_rn3 = min(`mod3plac',`mod3tt') - if `nb_rn3'==0 { - qui replace comz = 1 + else if (`difcheck2'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" } - - forvalues m = 0/`=`nbm_`j''-1' { - qui count if item`j' == `m' & tt == 0 - local nb_rn1_g0 = r(N) - qui count if item`j' == `m' & tt == 1 - local nb_rn1_g1 = r(N) - local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') - if `nb_rn' == 0 { - qui replace comz = 1 - local recoda_`j' = 1 - if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'+`kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { - local stop = 1 - forvalues kk = 1/`=`m'' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m' - `kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else { - if runiform()>0.5{ - local stop = 1 - forvalues kk = 1/`m' { - qui count if item`j' == `=`m' - `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' - `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { - qui replace item`j'= `=`m'-`kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - else { - local stop = 1 - forvalues kk = 1/`=`nbm_`j''-`m'' { - qui count if item`j' == `=`m' + `kk'' & tt == 0 - local v`kk'1_0 = r(N) - qui count if item`j' == `=`m' + `kk'' & tt == 1 - local v`kk'1_1 = r(N) - if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ - qui replace item`j'=`=`m' + `kk'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - else { - if `stop' != 0 { - qui replace item`j'= `nbm_`j'' if item`j'==`m' - local zzz=`j'+`nbitems' - *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' - *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" - local stop = 0 - } - } - } - } - } - } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" } - qui levelsof item`j' - local val = r(levels) - local checker: word 1 of `val' - local checker2: word 2 of `val' - local checker3: word 3 of `val' - if (`checker'!=0) { - qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) +} +else if (`difcheck'==0) { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" } - else if (`checker2'!=1) { - qui recode item`j' (`checker2'=1) (`checker3'=2) + else if (`difcheck2'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" } - else if (`checker3'!=2) { - qui recode item`j' (`checker3'=2) + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" } } - -qui valuesof comz -local val = r(values) -local checker: word 1 of `val' - -* replications avec 3 mod -if (`checker' == 1) { -local difitems1=dif1 -local difitems2=dif2 -local difcheck = 0 -local difcheck2 = 0 -forvalues i=1/`nbitems' { -qui sum item`i' -local checker2 = r(max) - -if (`i'==`difitems1') { - if (`checker2'==3) { - local difcheck = 1 - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" -} -else { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" -} -} -if (`i'==`difitems2') { - if (`checker2'==3) { - local difcheck2=1 - local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" -} -else { - local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" -} -} -} -local mod "gsem " -forvalues i=1/`nbitems' { -qui sum item`i' -local checker2 = r(max) -if (`i'==`difitems1'| `i'==`difitems2') { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" -} - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" +else if (`difcheck'==2) { + if (`difcheck2'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4)" + } + else if (`difcheck2'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2)" + } + else if (`difcheck2'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" } } -else { - if (`checker2'==3) { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" -} -else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" -} -} -} -if (`difcheck'==1) { -if (`difcheck2'==1) { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" -} -else { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" -} -} -else { -if (`difcheck2'==1) { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" -} -else { - local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" -} -} + +if (`difcheck'!=2) { qui `constrnt' +} +if (`difcheck2'!=2) { qui `constrnt2' +} if (`difcheck'==1) { -qui `constrnt3' + qui `constrnt3' } if (`difcheck2'==1) { -qui `constrnt4' + qui `constrnt4' } qui `mod' mat V=r(table) mat W=V[1..2,1...] forvalues j=1/`nbitems' { - mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif } -mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta -mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta -mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif -mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif -restore + mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta + mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta + mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif + mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif + restore } * replications sans problèmes else { -local difitems1=dif1 -local difitems2=dif2 + local difitems1=dif1 + local difitems2=dif2 forvalues i=1/`nbitems' { -if (`i'==`difitems1') { - local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" -} -if (`i'==`difitems2') { - local constrnt3 = "constraint 3 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" - local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" -} + if (`i'==`difitems1') { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + if (`i'==`difitems2') { + local constrnt3 = "constraint 3 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } } local mod "gsem " forvalues i=1/`nbitems' { -if (`i'==`difitems1' | `i'==`difitems2') { - local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" -} -else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" -} + if (`i'==`difitems1' | `i'==`difitems2') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } } local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" qui `constrnt' @@ -875,24 +333,24 @@ qui `mod' mat V=r(table) mat W=V[1..2,1...] forvalues j=1/`nbitems' { - mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif - mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif -} -mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif -mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta -mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta -mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif -mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif -restore + mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif + } + mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"THETA:tt")] // beta + mat outmat[`k',3*`nbitems'+8] = W[2,colnumb(W,"THETA:tt")] // se beta + mat outmat[`k',3*`nbitems'+9] = `difitems1' // numéro item de dif + mat outmat[`k',3*`nbitems'+10] = `difitems2' // numéro item de dif + restore } } -putexcel set "`path_res'/out/10`scen'_`Nn'.xls", sheet("outmat") replace +putexcel set "`path_res'/out/12`scen'_`Nn'.xls", sheet("outmat") replace putexcel A1=matrix(outmat), colnames } } diff --git a/Scripts/Analysis/DIF/temp/temp_problem.do b/Scripts/Analysis/DIF/temp/temp_problem.do index e69de29..916037d 100644 --- a/Scripts/Analysis/DIF/temp/temp_problem.do +++ b/Scripts/Analysis/DIF/temp/temp_problem.do @@ -0,0 +1,516 @@ + +************************************************** Scenario 20: J = 7 items / M = 4 modalities / DIF size 0.3 x3 +local N = "50" +foreach Nnn in `N' { +local Nn = `Nnn' +local path_data = "/home/corentin/Documents/These/Recherche/Simulations/Data/DIF/N`Nn'" +local path_res = "/home/corentin/Documents/These/Recherche/Simulations/Analysis/DIF/N`Nn'" +local scenarios = "A B C D E F G" +foreach scen in `scenarios' { +clear +import delim "`path_data'/scenario_20`scen'_`Nn'.csv", encoding(ISO-8859-2) case(preserve) clear +rename TT tt + +* Matrice de taille 1000 * 4 items + 1 DIF + beta + std beta + 1 dif +local nbitems = 7 +local nbdif = 3 +local taillemat = 3*`nbitems'+9+2+`nbdif' +mat outmat = J(1000,`taillemat',.) +mat colnames outmat = "item1_1" "item1_2" "item1_3" "item2_1" "item2_2" "item2_3" "item3_1" "item3_2" "item3_3" "item4_1" "item4_2" "item4_3" "item5_1" "item5_2" "item5_3" "item6_1" "item6_2" "item6_3" "item7_1" "item7_2" "item7_3" "dif1_1" "dif1_2" "dif1_3" "dif2_1" "dif2_2" "dif2_3" "dif3_1" "dif3_2" "dif3_3" "beta" "se_beta" "dif_item_1" "dif_item_2" "dif_item_3" +di "Scenario 20`scen' / N=`Nnn'" +forvalues k=1/1000 { + if (mod(`k',100)==0) { + di "`k'/1000" + } + preserve + qui keep if replication==`k' + + +* MERGE des modalités si non représentées + local com_z = 0 + qui gen comz = 0 + forvalues j = 1 / `nbitems' { + local recoda_`j' = 0 + qui tab item`j' if tt == 0, matrow(rect1_g0_`j') matcell(nbrt1_g0_`j') + local maxm`j'_t1_g0 = rect1_g0_`j'[r(r),1] + local minm`j'_t1_g0 = rect1_g0_`j'[1,1] + + qui tab item`j' if tt == 1, matrow(rect1_g1_`j') matcell(nbrt1_g1_`j') + local minm`j'_t1_g1 = rect1_g1_`j'[1,1] + local maxm`j'_t1_g1 = rect1_g1_`j'[r(r),1] + + local minm_`j' = min(`minm`j'_t1_g0',`minm`j'_t1_g1') + local maxm_`j' = max(`maxm`j'_t1_g0',`maxm`j'_t1_g1') + local nbm_`j' = `=`maxm_`j''-`minm_`j''' + + if `minm_`j'' != 0 & `com_z' == 0 { + local com_z = 1 + } + qui count if item`j' == 3 & tt == 0 + local mod3plac = r(N) + qui count if item`j' == 3 & tt == 1 + local mod3tt = r(N) + local nb_rn3 = min(`mod3plac',`mod3tt') + if `nb_rn3'==0 { + qui replace comz = 1 + } + + forvalues m = 0/`=`nbm_`j''-1' { + qui count if item`j' == `m' & tt == 0 + local nb_rn1_g0 = r(N) + qui count if item`j' == `m' & tt == 1 + local nb_rn1_g1 = r(N) + local nb_rn = min(`nb_rn1_g0',`nb_rn1_g1') + if `nb_rn' == 0 { + qui replace comz = 1 + local recoda_`j' = 1 + if `m' == 0 | `m' < `minm`j'_t1_g0' | `m' < `minm`j'_t1_g1' { + local stop = 1 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' + `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'+`kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'+`kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { + local stop = 1 + forvalues kk = 1/`=`m'' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' - `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m' - `kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `=`m' - `kk'' if item`zzz'==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else { + if runiform()>0.5{ + local stop = 1 + forvalues kk = 1/`m' { + qui count if item`j' == `=`m' - `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' - `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'-`kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'-`kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + else { + local stop = 1 + forvalues kk = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `kk'' & tt == 0 + local v`kk'1_0 = r(N) + qui count if item`j' == `=`m' + `kk'' & tt == 1 + local v`kk'1_1 = r(N) + if (`v`kk'1_0' != 0 | `v`kk'1_1' != 0) & `stop' != 0{ + qui replace item`j'=`=`m' + `kk'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m' + `kk'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + else { + if `stop' != 0 { + qui replace item`j'= `nbm_`j'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `nbm_`j'' if item``=`j'+`nbitems'''==`m' + *di "WARNING SCENARIO `k': items `j': answers `m' and `=`m'+`kk'' merged" + local stop = 0 + } + } + } + } + } + } + } + qui levelsof item`j' + local val = r(levels) + local checker: word 1 of `val' + local checker2: word 2 of `val' + local checker3: word 3 of `val' + local nummoda=r(r) + if (`nummoda'==2) { + qui recode item`j' (`checker'=0) (`checker2'=1) + } + if (`nummoda'==3) { + if (`checker'!=0) { + qui recode item`j' (`checker'=0) (`checker2'=1) (`checker3'=2) + } + else if (`checker2'!=1) { + qui recode item`j' (`checker2'=1) (`checker3'=2) + } + else if (`checker3'!=2) { + qui recode item`j' (`checker3'=2) + } + } +} +qui valuesof comz +local val = r(values) +local checker: word 1 of `val' + +* replications avec 3 mod +if (`checker' == 1) { + local difitems1=dif1 + local difitems2=dif2 + local difitems3=dif3 + local difcheck = 0 + local difcheck2 = 0 + local difcheck3 = 0 +forvalues i=1/`nbitems' { + qui sum item`i' + local checker2 = r(max) + + if (`i'==`difitems1') { + if (`checker2'==3) { + local difcheck = 1 + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt3 = "constraint 3 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + else if (`checker2'==2) { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + } + else if (`checker2'==1) { + local difcheck = 2 + } + } + if (`i'==`difitems2') { + if (`checker2'==3) { + local difcheck2=1 + local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + else if (`checker2'==2) { + local constrnt2 = "constraint 2 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + } + else if (`checker2'==1) { + local difcheck2 = 2 + } + } + if (`i'==`difitems3') { + if (`checker2'==3) { + local difcheck3=1 + local constrnt5 = "constraint 5 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt6 = "constraint 6 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + else if (`checker2'==2) { + local constrnt5 = "constraint 5 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + } + else if (`checker2'==1) { + local difcheck3 = 2 + } + } +} + +local mod "gsem " +forvalues i=1/`nbitems' { + qui sum item`i' + local checker2 = r(max) + if (`i'==`difitems1'| `i'==`difitems2'| `i'==`difitems3') { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)" + } + } + else { + if (`checker2'==3) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } + else if (`checker2'==2) { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)" + } +} +} +if (`difcheck'==1) { + if (`difcheck2'==1) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4)" + } + } + else if (`difcheck2'==0) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3)" + } + } + else if (`difcheck2'==2) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 3 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 3 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 3)" + } + } +} +else if (`difcheck'==1) { + if (`difcheck2'==1) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 4)" + } + } + else if (`difcheck2'==0) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2)" + } + } + else if (`difcheck2'==2) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1)" + } + } +} +else if (`difcheck'==2) { + if (`difcheck2'==1) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 4)" + } + } + else if (`difcheck2'==0) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2 5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(2)" + } + } + else if (`difcheck2'==2) { + if (`difcheck3'==1) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(5 6)" + } + else if (`difcheck3'==0) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(5)" + } + else if (`difcheck3'==2) { + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent" + } + } +} +if (`difcheck'!=2) { + qui `constrnt' +} +if (`difcheck2'!=2) { + qui `constrnt2' +} +if (`difcheck3'!=2) { + qui `constrnt5' +} +if (`difcheck'==1) { + qui `constrnt3' +} +if (`difcheck2'==1) { + qui `constrnt4' +} +if (`difcheck3'==1) { + qui `constrnt6' +} +qui `mod' +mat V=r(table) +mat W=V[1..2,1...] +forvalues j=1/`nbitems' { + + mat outmat[`k',3*`j'-2] = W[1,colnumb(W,"1.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,colnumb(W,"2.item`j':_cons")] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,colnumb(W,"3.item`j':_cons")] // items avant le premier dif +} + mat outmat[`k',3*`nbitems'+1] = W[1,colnumb(W,"1.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,colnumb(W,"2.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,colnumb(W,"3.item`difitems1':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,colnumb(W,"1.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+5] = W[1,colnumb(W,"2.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+6] = W[1,colnumb(W,"3.item`difitems2':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+7] = W[1,colnumb(W,"1.item`difitems3':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+8] = W[1,colnumb(W,"2.item`difitems3':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+9] = W[1,colnumb(W,"3.item`difitems3':tt")] // coef de dif + mat outmat[`k',3*`nbitems'+10] = W[1,colnumb(W,"THETA:tt")] // beta + mat outmat[`k',3*`nbitems'+11] = W[2,colnumb(W,"THETA:tt")] // se beta + mat outmat[`k',3*`nbitems'+12] = `difitems1' // numéro item de dif + mat outmat[`k',3*`nbitems'+13] = `difitems2' // numéro item de dif + mat outmat[`k',3*`nbitems'+14] = `difitems3' // numéro item de dif + restore +} + +* replications sans problèmes +else { + local difitems1=dif1 + local difitems2=dif2 + local difitems3=dif3 + if (`difitems1' < `difitems2') { + local difitemsmin= `difitems1' + local difitemsmax= `difitems2' + } + else { + local difitemsmin= `difitems2' + local difitemsmax= `difitems1' + } + if (`difitems3' < `difitemsmin') { + local difitemsmid = `difitemsmin' + local difitemsmin= `difitems3' + } + else if (`difitems3' > `difitemsmax') { + local difitemsmid = `difitemsmax' + local difitemsmax= `difitems3' + } + else { + local difitemsmid = `difitems3' + } + forvalues i=1/`nbitems' { + if (`i'==`difitemsmin') { + local constrnt = "constraint 1 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt2 = "constraint 2 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + } + forvalues i=1/`nbitems' { + if (`i'==`difitemsmax') { + local constrn3 = "constraint 3 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt4 = "constraint 4 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + } + forvalues i=1/`nbitems' { + if (`i'==`difitemsmid') { + local constrn5 = "constraint 5 2*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([2.item`i']_cons-([2.item`i']_cons+[2.item`i'] tt))" + local constrnt6 = "constraint 6 3*([1.item`i']_cons-([1.item`i']_cons+[1.item`i'] tt))=([3.item`i']_cons-([3.item`i']_cons+[3.item`i'] tt))" + } + } + local mod "gsem " + forvalues i=1/`nbitems' { + if (`i'==`difitemsmin' | `i'==`difitemsmax' | `i'==`difitemsmid') { + local mod = "`mod'"+"(1.item`i'<-THETA@1 tt)(2.item`i'<-THETA@2 tt)(3.item`i'<-THETA@3 tt)" + } + else { + local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + } + } + local mod = "`mod'" + "(THETA<-tt), mlogit tol(0.01) iterate(500) latent(THETA) nocapslatent constraint(1 2 3 4 5 6)" + qui `constrnt' + qui `constrnt2' + qui `constrnt3' + qui `constrnt4' + qui `constrnt6' + qui `constrnt6' + qui `mod' + mat V=r(table) + mat W=V[1..2,1...] + forvalues j=1/`nbitems' { + if (`j'<`difitemsmin') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+3] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+5] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+7] // items avant le premier dif + } + else if (`j'==`difitemsmin') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+4] // items du le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+7] // items du le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items du le premier dif } + } + else if (`j'<`difitemsmid') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+6] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+8] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+10] // items avant le premier dif + } + else if (`j'==`difitemsmid') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+7] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+10] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+13] // items avant le premier dif + } + else if (`j'<`difitemsmax') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+9] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+11] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+13] // items avant le premier dif + } + else if (`j'==`difitemsmax') { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+10] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+13] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+16] // items avant le premier dif + } + else { + mat outmat[`k',3*`j'-2] = W[1,7*(`j'-1)+12] // items avant le premier dif + mat outmat[`k',3*`j'-1] = W[1,7*(`j'-1)+14] // items avant le premier dif + mat outmat[`k',3*`j'] = W[1,7*(`j'-1)+16] // items avant le premier dif + } + } + mat outmat[`k',3*`nbitems'+1] = W[1,7*(`difitemsmin'-1)+2] // coef de dif + mat outmat[`k',3*`nbitems'+2] = W[1,7*(`difitemsmin'-1)+5] // coef de dif + mat outmat[`k',3*`nbitems'+3] = W[1,7*(`difitemsmin'-1)+8] // coef de dif + mat outmat[`k',3*`nbitems'+4] = W[1,7*(`difitemsmid'-1)+5] // coef de dif + mat outmat[`k',3*`nbitems'+5] = W[1,7*(`difitemsmid'-1)+8] // coef de dif + mat outmat[`k',3*`nbitems'+6] = W[1,7*(`difitemsmid'-1)+11] // coef de dif + mat outmat[`k',3*`nbitems'+7] = W[1,7*(`difitemsmax'-1)+8] // coef de dif + mat outmat[`k',3*`nbitems'+8] = W[1,7*(`difitemsmax'-1)+11] // coef de dif + mat outmat[`k',3*`nbitems'+9] = W[1,7*(`difitemsmax'-1)+14] // coef de dif + mat outmat[`k',3*`nbitems'+10] = W[1,7*`nbitems'+10] // beta + mat outmat[`k',3*`nbitems'+11] = W[2,7*`nbitems'+10] // se beta + mat outmat[`k',3*`nbitems'+12] = `difitemsmin' // numéro item de dif + mat outmat[`k',3*`nbitems'+13] = `difitemsmid' // numéro item de dif + mat outmat[`k',3*`nbitems'+14] = `difitemsmax' // numéro item de dif + restore +} +} +putexcel set "`path_res'/out/20`scen'_`Nn'.xls", sheet("outmat") replace +putexcel A1=matrix(outmat), colnames +} +}