You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
3.3 KiB
Plaintext
147 lines
3.3 KiB
Plaintext
program define tri
|
|
syntax varlist [fweight iweight] [,global test(int 2) all]
|
|
|
|
preserve
|
|
local nbvar:word count `varlist'
|
|
tokenize `varlist'
|
|
/*forvalues i=1/`nbvar' {
|
|
qui su ``i'' [`weight'`exp']
|
|
qui replace ``i''=(``i''-r(mean))/r(sd)
|
|
} */
|
|
|
|
|
|
/*METHOD GLOBAL */
|
|
|
|
|
|
/* COVARIANCE OR CORRELATION MATRIX*/
|
|
|
|
if "`global'"!="" {
|
|
tempname Cov W
|
|
qui matrix accum `Cov'=`varlist' [`weight'`exp'],nocons dev
|
|
qui matrix `Cov'=`Cov'/(`nbind'-1)
|
|
matrix `W'=J(2,`nbvar',0)
|
|
|
|
local newlist
|
|
tempvar centroid
|
|
genscore `varlist',mean score(`centroid')
|
|
|
|
forvalues i=1/`nbvar' {
|
|
qui corr ``i'' `centroid' [`weight'`exp']
|
|
matrix `W'[1,`i']=`r(rho)'
|
|
}
|
|
|
|
forvalues c=1/`nbvar'{
|
|
local corrmin=2
|
|
local var=0
|
|
forvalues i=1/`nbvar' {
|
|
if `W'[1,`i']<`corrmin'&`W'[2,`i']==0 {
|
|
local corrmin=`W'[1,`i']
|
|
local var=`i'
|
|
}
|
|
}
|
|
matrix `W'[2,`var']=`c'
|
|
}
|
|
matrix list `W'
|
|
forvalues c=1/`nbvar' {
|
|
forvalues i=1/`nbvar' {
|
|
if `W'[2,`i']==`c' {
|
|
local newlist `newlist' ``i''
|
|
}
|
|
}
|
|
}
|
|
local varlist "`newlist'"
|
|
tokenize `varlist'
|
|
}
|
|
if "`all'"=="" {
|
|
local time=1
|
|
}
|
|
else {
|
|
local time=`nbvar'
|
|
}
|
|
|
|
local maxc=-99
|
|
local part
|
|
forvalues t=1/`time' {
|
|
tempname W
|
|
matrix `W'=J(1,`nbvar',0)
|
|
matrix `W'[1,`t']=1
|
|
forvalues iter=1/`nbvar' {
|
|
local dejaselect
|
|
forvalues i=1/`nbvar' {
|
|
if `W'[1,`i']!=0 {
|
|
local dejaselect `dejaselect' ``i''
|
|
}
|
|
}
|
|
tempvar centroid
|
|
qui genscore `dejaselect',score(`centroid') standardized
|
|
local maxr=-2
|
|
local maxi=0
|
|
forvalues i=1/`nbvar' {
|
|
if `W'[1,`i']==0 {
|
|
qui corr `centroid' ``i''
|
|
local r=r(rho)
|
|
if `r'>`maxr' {
|
|
local maxr=`r'
|
|
local maxi=`i'
|
|
}
|
|
}
|
|
}
|
|
if `maxi'!=0 {
|
|
matrix `W'[1,`maxi']=`iter'+1
|
|
}
|
|
}
|
|
local list
|
|
forvalues class=1/`nbvar' {
|
|
forvalues i=1/`nbvar' {
|
|
if `W'[1,`i']==`class' {
|
|
local list `list' ``i''
|
|
}
|
|
}
|
|
}
|
|
di "`list'"
|
|
forvalues i=0/`nbvar' {
|
|
local j=`nbvar'-`i'
|
|
if `test'>2 {
|
|
local jb `j'
|
|
}
|
|
else {
|
|
local jb 0
|
|
}
|
|
forvalues k=0/`jb' {
|
|
local l=`nbvar'-`i'-`k'
|
|
if `test'>3 {
|
|
local lb=`l'
|
|
}
|
|
else {
|
|
local lb=0
|
|
}
|
|
forvalues m=0/`lb' {
|
|
local n=`nbvar'-`i'-`k'-`m'
|
|
qui di "compart `list',part(`i' `k' `m' `n')"
|
|
di "`i' `k' `m' `n'"
|
|
if `i'>=0&`k'>=0&`m'>=0&`n'>=0 {
|
|
di " compart `list',part(`i' `k' `m' `n')"
|
|
qui compart2 `list',part(`i' `k' `m' `n')
|
|
local c=r(compart)
|
|
di "`c'"
|
|
if `c'>`maxc' {
|
|
local maxc=`c'
|
|
local part=r(part)
|
|
local savlist `list'
|
|
*di "Best : `c'"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
di "The best partition in two groups is `part'"
|
|
di "for `savlist'"
|
|
di "COMPART=`maxc'"
|
|
|
|
|
|
|
|
|
|
restore
|
|
end
|