From c59b0e75e4abaec8231b31ed5c3a31e46b8c7343 Mon Sep 17 00:00:00 2001 From: corentinchoisy Date: Mon, 26 Feb 2024 16:08:40 +0100 Subject: [PATCH] Improved attempt at solving non-exhaustive scenarios for n=50 --- Scripts/Analysis/DIF/temp_problem.do | 206 +++++++++++++++++++-------- 1 file changed, 148 insertions(+), 58 deletions(-) diff --git a/Scripts/Analysis/DIF/temp_problem.do b/Scripts/Analysis/DIF/temp_problem.do index d8906f1..23b60a5 100644 --- a/Scripts/Analysis/DIF/temp_problem.do +++ b/Scripts/Analysis/DIF/temp_problem.do @@ -25,63 +25,152 @@ forvalues k=1/1000 { preserve qui keep if replication==`k' - // replications sans problèmes - if (1==1-inlist(`k',scenar_prob)) { - 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))" +* 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 } - } - 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)" + + 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 k = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `k'' & tt == 0 + local v`k'1_0 = r(N) + qui count if item`j' == `=`m' + `k'' & tt == 1 + local v`k'1_1 = r(N) + if (`v`k'1_0' != 0 | `v`k'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'+`k'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'+`k'' if item``=`j'+`nbitems'''==`m' + di "WARNING: items `j': answers `m' and `=`m'+`k'' merged" + local stop = 0 + } + } + } + else if `m' == `=`nbm_`j''-1' | `m' >= `maxm`j'_t1_g1' { + local stop = 1 + forvalues k = 1/`=`m'' { + qui count if item`j' == `=`m' - `k'' & tt == 0 + local v`k'1_0 = r(N) + qui count if item`j' == `=`m' - `k'' & tt == 1 + local v`k'1_1 = r(N) + if (`v`k'1_0' != 0 | `v`k'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m' - `k'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'= `=`m' - `k'' if item`zzz'==`m' + di "WARNING: items `j': answers `m' and `=`m'+`k'' merged" + local stop = 0 + } + } + } + else { + if runiform()>0.5{ + local stop = 1 + forvalues k = 1/`m' { + qui count if item`j' == `=`m' - `k'' & tt == 0 + local v`k'1_0 = r(N) + qui count if item`j' == `=`m' - `k'' & tt == 1 + local v`k'1_1 = r(N) + if (`v`k'1_0' != 0 | `v`k'1_1' != 0) & `stop' != 0 { + qui replace item`j'= `=`m'-`k'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m'-`k'' if item``=`j'+`nbitems'''==`m' + di "WARNING: items `j': answers `m' and `=`m'+`k'' merged" + local stop = 0 + } + } + } + else { + local stop = 1 + forvalues k = 1/`=`nbm_`j''-`m'' { + qui count if item`j' == `=`m' + `k'' & tt == 0 + local v`k'1_0 = r(N) + qui count if item`j' == `=`m' + `k'' & tt == 1 + local v`k'1_1 = r(N) + if (`v`k'1_0' != 0 | `v`k'1_1' != 0) & `stop' != 0{ + qui replace item`j'=`=`m' + `k'' if item`j'==`m' + local zzz=`j'+`nbitems' + *qui replace item`zzz'=`=`m' + `k'' if item``=`j'+`nbitems'''==`m' + di "WARNING: items `j': answers `m' and `=`m'+`k'' 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: items `j': answers `m' and `=`m'+`k'' merged" + local stop = 0 + } + } + } + } + } + } } - else { - local mod = "`mod'"+"(1.item`i'<-THETA@1)(2.item`i'<-THETA@2)(3.item`i'<-THETA@3)" + 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) } - } - 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 if (`checker2'!=1) { + qui recode item`j' (`checker2'=1) (`checker3'=2) } - 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 + else if (`checker3'!=2) { + qui recode item`j' (`checker3'=2) } } - 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 -} -// replications avec 3 mod -if (1==inlist(`k',scenar_prob_3)) { + + + + + + + + + + + + + +qui valuesof comz +local val = r(values) +local checker: word 1 of `val' +* replications avec 3 mod +if (`checker' == 1) { local difitems1=dif1 forvalues i=1/`nbitems' { if (`i'==`difitems1') { @@ -128,23 +217,27 @@ else { restore } - - -// replications avec 2 mod -if (1==inlist(`k',scenar_prob_2)) { - - +* 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)" + 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)" + 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" +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...] @@ -173,14 +266,11 @@ else { 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 } } -}