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.

121 lines
2.8 KiB
Plaintext

7 months ago
program define compart2,rclass
version 9
syntax varlist [if] [in] [fweight iweight] [,part(numlist) type(string)]
preserve
unab varlist:`varlist'
di "`varlist'"
tokenize `varlist'
marksample touse
local nbvar:word count `varlist'
qui count
local nbind=r(N)
tempname p
qui gen `p'=1
qui su `p' [`weight'`exp'] if `touse'
local nbind=r(N)
if "`square'"=="" {
local quad=1
}
else {
local quad=2
}
if "`type'"=="" {
local type classical
}
if "`type'"!="classical"&"`type'"!="centroid"&"`type'"!="polychoric" {
di in red "The type of the matrix is not authorized. Please correct your {hi:type} option."
error 198
}
/* DEFINITION OF THE PARTITION OF THE VARIABLES*/
local newpart
foreach i in `part' {
if `i'!=0 {
local newpart `newpart' `i'
}
}
local part `newpart'
local meme=0
local diff=0
local nbpart:word count `part'
forvalues i=1/`nbpart' {
local iti:word `i' of `part'
local meme=`meme'+`iti'*(`iti'-1)/2
forvalues j=`=`i'+1'/`nbpart' {
local itj:word `j' of `part'
local diff=`diff'+`iti'*`itj'
}
}
local perc=`meme'/(`meme'+`diff')
di "meme: `meme' ; diff: `diff' ; perc: `perc'"
local test=0
local last0=0
forvalues i=1/`nbpart' {
local first`i'=`last`=`i'-1''+1
local size`i':word `i' of `part'
local last`i'=`first`i''+`size`i''-1
local test=`test'+`size`i''
local list`i'
forvalues j=`first`i''/`last`i'' {
local list`i' `list`i'' ``j''
}
}
if `test'!=`nbvar' {
di in red "{p}The described partition of the variables is composed of a number of variables different of the number of variables of varlist.{p_end}"
exit 198
}
forvalues g=1/`nbpart' {
tempname f1`g'
if `size`g''>1 {
if "`type'"=="classical" {
qui pca `list`g'',cov
qui predict `f1`g''
}
else if "`type'"=="polychoric" {
qui polychoricpca `list`g'',score(`f1`g'') nscore(1)
rename `f1`g''1 `f1`g''
}
else if "`type'"=="centroid" {
qui genscore `list`g'', score(`f1`g'') mean
}
}
else if `size`g''==1 {
qui gen `f1`g''=`list`g''
}
}
local minrho=2
local maxrho=-2
forvalue i=1/`nbvar' {
forvalues g=1/`nbpart' {
qui corr ``i'' `f1`g''
if `i'>=`first`g''&`i'<=`last`g'' {
if r(rho)<`minrho' {
local minrho=r(rho)
}
}
else if r(rho)>`maxrho' {
local maxrho=r(rho)
}
}
}
di "C=(`meme'*`minrho'-`diff'*`maxrho')/(`meme'+`diff')"
local C=(`meme'*`minrho'-`diff'*`maxrho')/(`meme'+`diff')
local C=(`meme'*`minrho'-`diff'*`maxrho')/(`meme'+`diff')
*local C=`minrho'
di "C= `C' min=`minrho' max=`maxrho'"
return local compart `C'
*return local mean `mean'
*return local list `varlist'
return local part `part'
restore
end