V krizi smisla tiči misel






         

22.08.2014

Stata rutina – bootstrapiranje elementov faktorske analize

Ena prvih resnejših lastnih Stata rutin, s katero dobite bootstrapirane parametre (uteži) in statistike (delež pojasnjene variance) faktorske analize (osnova za programček je bila tale spletna stran, kjer pa program ne dela).

Ker, kolikor vem, Stata ne dovoljuje bootstrapiranja vektorjev in matrik, vam spodnja rutina pride prav tudi kadarkoli potrebujete bootstrapirati posamezne koeficiente v matrikah ali vektorjih pri katerikoli statistični ali ekonometrični analizi. Ker še nisem prav hud ekspert za programiranje v Stati, so še mnogi elementi “very lame”, gre za faktorsko analizo 73 spremenljivk po metodi principal components factors, iz katerih izločimo tri osnovne faktorje in delež pojasnjene variance (slednjega Stata ne shranjuje direktno, zato je posebej izračunan kot povprečje komunalitet). Faktorji so bili rotirani po metodi varimax s Kaiserjevo normalizacijo.

Ker je šlo v osnovi za visokodimenzionalno podatkovno bazo, smo rezultate faktorske analize morali bootstrapirati, da smo dobili konsistentne ocene parametrov v analizi (glej npr. Song & Hart 2010).

// main program begins ——————————————-
// first, get the observed point estimates:
factor std_enav1 std_enav2 std_enav3 std_enav8 std_enav11 std_enav13 std_dvev2 std_triv1 std_triv2 ///
std_triv3 std_triv4 std_triv5 std_triv6 std_triv7 std_stiriv1 std_stiriv3 std_stiriv5 ///
std_stiriv7 std_stiriv9 std_stiriv11 std_petv2 std_sestv2 std_sestv5 std_sestv8 std_sestv11 ///
std_sestv17 std_sedemv3 std_sedemv5 std_sedemv7 std_devetv1 std_devetv4 std_devetv5 ///
std_devetv6 std_devetv7 std_desetv1 std_desetv2 std_desetv3 std_desetv4 std_desetv5 ///
std_desetv6 std_desetv7 std_desetv8 std_desetv9 std_desetv10 std_desetv11 std_desetv12 ///
std_desetv13 std_desetv14 std_desetv15 std_desetv16 std_desetv17 std_desetv18 std_enajstv3 ///
std_enajstv5 std_enajstv7 std_enajstv9 std_enajstv11 std_enajstv13 std_enajstv15 ///
std_enajstv17 std_enajstv19 std_enajstv21 std_enajstv23 std_enajstv25 std_enajstv27 ///
std_enajstv29 std_enajstv31 std_gengovpc std_centgovpc std_locgovpc std_gencultpc ///
std_centcultpc std_loccultpc, pcf factors(3) // here there are 3 factors and 73 variables
rotate, horst varimax
matrix obsload=e(r_L)
matrix obspsi=e(Psi)
forvalues i=1/3 {
matrix obsload`i’=obsload[1..73,`i'] // break the loadings matrix up
}
scalar obsexp=(obspsi[1,1]+obspsi[1,2]+obspsi[1,3]+obspsi[1,4]+obspsi[1,5]+obspsi[1,6]+obspsi[1,7] ///
+obspsi[1,8]+obspsi[1,9]+obspsi[1,10]+obspsi[1,11]+obspsi[1,12]+obspsi[1,13]+obspsi[1,14] ///
+obspsi[1,15]+obspsi[1,16]+obspsi[1,17]+obspsi[1,18]+obspsi[1,19]+obspsi[1,20]+obspsi[1,21] ///
+obspsi[1,22]+obspsi[1,23]+obspsi[1,24]+obspsi[1,25]+obspsi[1,26]+obspsi[1,27]+obspsi[1,28] ///
+obspsi[1,29]+obspsi[1,30]+obspsi[1,31]+obspsi[1,32]+obspsi[1,33]+obspsi[1,34]+obspsi[1,35] ///
+obspsi[1,36]+obspsi[1,37]+obspsi[1,38]+obspsi[1,39]+obspsi[1,40]+obspsi[1,41]+obspsi[1,42] ///
+obspsi[1,43]+obspsi[1,44]+obspsi[1,45]+obspsi[1,46]+obspsi[1,47]+obspsi[1,48]+obspsi[1,49] ///
+obspsi[1,50]+obspsi[1,51]+obspsi[1,52]+obspsi[1,53]+obspsi[1,54]+obspsi[1,55]+obspsi[1,56] ///
+obspsi[1,57]+obspsi[1,58]+obspsi[1,59]+obspsi[1,60]+obspsi[1,61]+obspsi[1,62]+obspsi[1,63] ///
+obspsi[1,64]+obspsi[1,65]+obspsi[1,66]+obspsi[1,67]+obspsi[1,68]+obspsi[1,69]+obspsi[1,70] ///
+obspsi[1,71]+obspsi[1,72]+obspsi[1,73])/73 // calculating the observed explained variance statistic
matrix obs=(obsload1 \ obsload2 \ obsload3 \ obsexp) // put it back together
matrix obst=obs’
// then carry on with the program…
// first part ends ——————————————–

// and here comes the program…
capture: program drop myboot
program define myboot, rclass
preserve
bsample
factor std_enav1 std_enav2 std_enav3 std_enav8 std_enav11 std_enav13 std_dvev2 std_triv1 std_triv2 ///
std_triv3 std_triv4 std_triv5 std_triv6 std_triv7 std_stiriv1 std_stiriv3 std_stiriv5 ///
std_stiriv7 std_stiriv9 std_stiriv11 std_petv2 std_sestv2 std_sestv5 std_sestv8 std_sestv11 ///
std_sestv17 std_sedemv3 std_sedemv5 std_sedemv7 std_devetv1 std_devetv4 std_devetv5 ///
std_devetv6 std_devetv7 std_desetv1 std_desetv2 std_desetv3 std_desetv4 std_desetv5 ///
std_desetv6 std_desetv7 std_desetv8 std_desetv9 std_desetv10 std_desetv11 std_desetv12 ///
std_desetv13 std_desetv14 std_desetv15 std_desetv16 std_desetv17 std_desetv18 std_enajstv3 ///
std_enajstv5 std_enajstv7 std_enajstv9 std_enajstv11 std_enajstv13 std_enajstv15 ///
std_enajstv17 std_enajstv19 std_enajstv21 std_enajstv23 std_enajstv25 std_enajstv27 ///
std_enajstv29 std_enajstv31 std_gengovpc std_centgovpc std_locgovpc std_gencultpc ///
std_centcultpc std_loccultpc, pcf factors(3)
rotate, horst varimax
matrix bootload=e(r_L)
matrix psi=e(Psi)
forvalues i=1/73 {
scalar bootloada`i’=bootload[`i',1]
scalar bootloadb`i’=bootload[`i',2]
scalar bootloadc`i’=bootload[`i',3]
}
scalar bootexp=(psi[1,1]+psi[1,2]+psi[1,3]+psi[1,4]+psi[1,5]+psi[1,6]+psi[1,7]+psi[1,8]+psi[1,9] ///
+psi[1,10]+psi[1,11]+psi[1,12]+psi[1,13]+psi[1,14]+psi[1,15]+psi[1,16]+psi[1,17] ///
+psi[1,18]+psi[1,19]+psi[1,20]+psi[1,21]+psi[1,22]+psi[1,23]+psi[1,24]+psi[1,25] ///
+psi[1,26]+psi[1,27]+psi[1,28]+psi[1,29]+psi[1,30]+psi[1,31]+psi[1,32]+psi[1,33] ///
+psi[1,34]+psi[1,35]+psi[1,36]+psi[1,37]+psi[1,38]+psi[1,39]+psi[1,40]+psi[1,41] ///
+psi[1,42]+psi[1,43]+psi[1,44]+psi[1,45]+psi[1,46]+psi[1,47]+psi[1,48]+psi[1,49] ///
+psi[1,50]+psi[1,51]+psi[1,52]+psi[1,53]+psi[1,54]+psi[1,55]+psi[1,56]+psi[1,57] ///
+psi[1,58]+psi[1,59]+psi[1,60]+psi[1,61]+psi[1,62]+psi[1,63]+psi[1,64]+psi[1,65] ///
+psi[1,66]+psi[1,67]+psi[1,68]+psi[1,69]+psi[1,70]+psi[1,71]+psi[1,72]+psi[1,73])/73
restore
end

// now you use -simulate- to run the -myboot- program, creating one resample each time.
simulate load1a=bootloada1 load2a=bootloada2 load3a=bootloada3 load4a=bootloada4 load5a=bootloada5 ///
load6a=bootloada6 load7a=bootloada7 load8a=bootloada8 load9a=bootloada9 load10a=bootloada10 ///
load11a=bootloada11 load12a=bootloada12 load13a=bootloada13 load14a=bootloada14 ///
load15a=bootloada15 load16a=bootloada16 load17a=bootloada17 load18a=bootloada18 ///
load19a=bootloada19 load20a=bootloada20 load21a=bootloada21 load22a=bootloada22 ///
load23a=bootloada23 load24a=bootloada24 load25a=bootloada25 load26a=bootloada26 ///
load27a=bootloada27 load28a=bootloada28 load29a=bootloada29 load30a=bootloada30 ///
load31a=bootloada31 load32a=bootloada32 load33a=bootloada33 load34a=bootloada34 ///
load35a=bootloada35 load36a=bootloada36 load37a=bootloada37 load38a=bootloada38 ///
load39a=bootloada39 load40a=bootloada40 load41a=bootloada41 load42a=bootloada42 ///
load43a=bootloada43 load44a=bootloada44 load45a=bootloada45 load46a=bootloada46 ///
load47a=bootloada47 load48a=bootloada48 load49a=bootloada49 load50a=bootloada50 ///
load51a=bootloada51 load52a=bootloada52 load53a=bootloada53 load54a=bootloada54 ///
load55a=bootloada55 load56a=bootloada56 load57a=bootloada57 load58a=bootloada58 ///
load59a=bootloada59 load60a=bootloada60 load61a=bootloada61 load62a=bootloada62 ///
load63a=bootloada63 load64a=bootloada64 load65a=bootloada65 load66a=bootloada66 ///
load67a=bootloada67 load68a=bootloada68 load69a=bootloada69 load70a=bootloada70 ///
load71a=bootloada71 load72a=bootloada72 load73a=bootloada73 ///
load1b=bootloadb1 load2b=bootloadb2 load3b=bootloadb3 load4b=bootloadb4 load5b=bootloadb5 ///
load6b=bootloadb6 load7b=bootloadb7 load8b=bootloadb8 load9b=bootloadb9 load10b=bootloadb10 ///
load11b=bootloadb11 load12b=bootloadb12 load13b=bootloadb13 load14b=bootloadb14 ///
load15b=bootloadb15 load16b=bootloadb16 load17b=bootloadb17 load18b=bootloadb18 ///
load19b=bootloadb19 load20b=bootloadb20 load21b=bootloadb21 load22b=bootloadb22 ///
load23b=bootloadb23 load24b=bootloadb24 load25b=bootloadb25 load26b=bootloadb26 ///
load27b=bootloadb27 load28b=bootloadb28 load29b=bootloadb29 load30b=bootloadb30 ///
load31b=bootloadb31 load32b=bootloadb32 load33b=bootloadb33 load34b=bootloadb34 ///
load35b=bootloadb35 load36b=bootloadb36 load37b=bootloadb37 load38b=bootloadb38 ///
load39b=bootloadb39 load40b=bootloadb40 load41b=bootloadb41 load42b=bootloadb42 ///
load43b=bootloadb43 load44b=bootloadb44 load45b=bootloadb45 load46b=bootloadb46 ///
load47b=bootloadb47 load48b=bootloadb48 load49b=bootloadb49 load50b=bootloadb50 ///
load51b=bootloadb51 load52b=bootloadb52 load53b=bootloadb53 load54b=bootloadb54 ///
load55b=bootloadb55 load56b=bootloadb56 load57b=bootloadb57 load58b=bootloadb58 ///
load59b=bootloadb59 load60b=bootloadb60 load61b=bootloadb61 load62b=bootloadb62 ///
load63b=bootloadb63 load64b=bootloadb64 load65b=bootloadb65 load66b=bootloadb66 ///
load67b=bootloadb67 load68b=bootloadb68 load69b=bootloadb69 load70b=bootloadb70 ///
load71b=bootloadb71 load72b=bootloadb72 load73b=bootloadb73 ///
load1c=bootloadc1 load2c=bootloadc2 load3c=bootloadc3 load4c=bootloadc4 load5c=bootloadc5 ///
load6c=bootloadc6 load7c=bootloadc7 load8c=bootloadc8 load9c=bootloadc9 load10c=bootloadc10 ///
load11c=bootloadc11 load12c=bootloadc12 load13c=bootloadc13 load14c=bootloadc14 ///
load15c=bootloadc15 load16c=bootloadc16 load17c=bootloadc17 load18c=bootloadc18 ///
load19c=bootloadc19 load20c=bootloadc20 load21c=bootloadc21 load22c=bootloadc22 ///
load23c=bootloadc23 load24c=bootloadc24 load25c=bootloadc25 load26c=bootloadc26 ///
load27c=bootloadc27 load28c=bootloadc28 load29c=bootloadc29 load30c=bootloadc30 ///
load31c=bootloadc31 load32c=bootloadc32 load33c=bootloadc33 load34c=bootloadc34 ///
load35c=bootloadc35 load36c=bootloadc36 load37c=bootloadc37 load38c=bootloadc38 ///
load39c=bootloadc39 load40c=bootloadc40 load41c=bootloadc41 load42c=bootloadc42 ///
load43c=bootloadc43 load44c=bootloadc44 load45c=bootloadc45 load46c=bootloadc46 ///
load47c=bootloadc47 load48c=bootloadc48 load49c=bootloadc49 load50c=bootloadc50 ///
load51c=bootloadc51 load52c=bootloadc52 load53c=bootloadc53 load54c=bootloadc54 ///
load55c=bootloadc55 load56c=bootloadc56 load57c=bootloadc57 load58c=bootloadc58 ///
load59c=bootloadc59 load60c=bootloadc60 load61c=bootloadc61 load62c=bootloadc62 ///
load63c=bootloadc63 load64c=bootloadc64 load65c=bootloadc65 load66c=bootloadc66 ///
load67c=bootloadc67 load68c=bootloadc68 load69c=bootloadc69 load70c=bootloadc70 ///
load71c=bootloadc71 load72c=bootloadc72 load73c=bootloadc73 ///
explained=bootexp, reps(1000) seed(1234) saving(myboot_loadings.dta, replace): myboot
bstat, stat(obst) n(33) // put the original number of observations into n()
estat bootstrap, all
// main program ends

  • Share/Bookmark


Brez komentarjev »

Še brez komentarjev.

RSS vir za komentarje na objavo. Trackback URI

Komentiraj

Komentiranje iz tujine je omogočeno zgolj prijavljenim uporabnikom !

Blog V krizi smisla tiči misel | Zagotavlja SiOL | O Sistemu |