This is a code I had written for factor congruence analysis based on the formula that I was provided. It calculates the congruence for multiple datasets of results of factor analysis (again – not original data but results). Each db being an excel file with two sheets – factor analysis results of even numbed patients and odd numbered patients.
foreach db in db1.xls db2.xls db3.xls db4.xls {
clear
import excel "`db'", sheet("FA_even") firstrow case(lower)
count
rename itemname name
save FA_even_patients, replace
clear
import excel "`db'", sheet("FA_odd") firstrow case(lower)
rename itemname name
save FA_odd_patients, replace
duplicates report item
clear
use FA_even_patients
duplicates report item
drop if item ==.
count
merge 1:1 item using FA_odd_patients
drop _merge
drop if item ==.
mvdecode odd_* even_*, mv(0=.)
mvencode _all, mv(0.0)
save "congruence_dataset`db'.dta", replace
// FACTOR CONGRUENCE
save "congruence_dataset2`db'.dta", replace
capture log close
log using "congruence_analysis`db'.smcl", replace
foreach fa in 2 3 4 5 6 7 8 9 10 { // FOR EACH NUMBER OF FACTORS
matrix mat`fa' = J(`fa',`fa',.) // CREATE EMPTY MATRIX
forvalues nx = 1(1)`fa' { // Schedule loop to run till nx FACTOR Numbers for Even patients
forvalues ny = 1(1)`fa' { // Schedule loop to run till nx FACTOR Numbers for Odd patients
foreach x in `fa'fac`nx' { // Pick Factor for even patients
foreach y in `fa'fac`ny' { // Pick Factor for Odd patients
capture drop e`x'_x_o`y'
gen e`x'_x_o`y' = even_`x' * odd_`y' // A*B
capture drop o_`y'_sq
gen o_`y'_sq = odd_`y' * odd_`y' // B-square
capture drop e_`x'_sq
gen e_`x'_sq = even_`x' * even_`x' // A-Square
capture drop e`x'_x_o`y'_s
egen e`x'_x_o`y'_s = total(e`x'_x_o`y') // Sigma (A*B)
capture drop o_`y'_sqs
egen o_`y'_sqs = total(o_`y'_sq) // Sigma (B-sq)
capture drop e_`x'_sqs
egen e_`x'_sqs = total(e_`x'_sq) // Sigma (A-sq)
// CONGRUENCE FACTOR CALCULATION
capture drop e`x'_x_o`y'_c
gen e`x'_x_o`y'_c = e`x'_x_o`y'_s / (o_`y'_sqs *e_`x'_sqs)^0.5
di " Even `x' Vs. Odd `y' Congruence IS "
list e`x'_x_o`y'_c in 1
sum e`x'_x_o`y'_c
di r(mean)
matrix mat`fa'[`nx', `ny']=r(mean)
capture drop e_`x'_x_o_`y'
capture drop e`x'_x_o`y'
capture drop o_`y'_sq
capture drop e_`x'_sq
capture drop e`x'_x_o`y'_s
capture drop o_`y'_sqs
capture drop e_`x'_sqs
}
}
}
}
mat rownames mat`fa'= even1_`fa'
mat colnames mat`fa'= odd1_`fa'
}
preserve
// SAVING MATRICES
foreach fa in 2 3 4 5 6 7 8 9 10 {
mat list mat`fa'
clear
svmat mat`fa', names(odd_`fa'_)
gen even = "even_`fa'_"
gen rownum = _n
egen col1 = concat (even rownum)
order col1
drop even
drop rownum
outsheet using "mat`fa'`db'.csv", comma replace
export excel using "`db'results_congruence.xlsx", sheet("`fa'_factors") firstrow(variables) sheetreplace
}
restore
save "congruence_analysis3`db'.dta", replace
capture log close
view "congruence_analysis`db'.smcl"
}
Code language: PHP (php)
This example show shows the following techniques, some of which may help:
- Creating Empty Matrix
- Replacing contents of cell of a matrix using indexation
- Matrix rownames and column names
- Saving matrices as data