Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Question of factor analysis

From   [email protected]
To   [email protected]
Subject   Re: st: Question of factor analysis
Date   Fri, 26 Mar 2004 08:47:56 -0600

Herve Stolowy <[email protected]> asks:

> ... Is there a way to get the
> factor loadings in a factor analysis (pcf) sorted by size? (In SPSS,
> there is an option: Coefficient display format: Sorted by size). I read
> the manual and did not find the answer.

There is no option on -factor- to directly do this, but you can
grab a copy of the factor loading matrix and manipulate and then
display this matrix in whatever way you wish.


    . help matget

to learn how to grab a copy of the "Ld" loading matrix or the "L"
rotated loading matrix (if you have done a rotation).  (Also you
can grab "Ev" the eigenvalues etc.).

To automate all of this you could write a wrapper program that
calls -factor- and then does the work of sorting and displaying
the loadings.

I haven't looked at SPSS's factor command to know exactly what
kind of output you are talking about, but assuming that what is
wanted is to sort the rows of the loading matrix based on the
first eigenvector components, then the following program will do
that.  You could take this program and add to it or change it as

Anyone who wishes to take this code over and alter to taste may
feel free.  If this is of much use to people, then whoever takes
it over can write a hlp file.

This program does not do anything about or with rotated loadings
(if it exists).  That could be something someone might want to
enhance in the program.

------------------ begin factorsort.ado -------------------------
!* version 1.0.0  26mar2004
* Ken Higbee <[email protected]>
* Feel free to take this code and alter as you desire.  Change
* the date and version above and replace my name and email
* address with yours
program factorsort
        version 8.2

        syntax [varlist] [fw iw] [if] [in] [, * ]
        if "`weight'" != "" {
                local wtexp `"[`weight'`exp']"'

        // call to factor (if you don't want to see the standard
        // output, you could put "quietly" in front of the call)
        factor `varlist' `wtexp' `if' `in' , `options'

        // grab Loading matrix / give it reasonable name stripes
        tempname Ld
        mat `Ld' = get(Ld)
        mat rownames `Ld' = `varlist'
        forvalues i = 1/`= colsof(`Ld')' {
                local cnames `cnames' factor`i'
        mat colnames `Ld' = `cnames'

        // Resort the rows of Ld based on first column
        SortOnFirstCol `Ld'
        di "Loadings ordered based on first factor loadings"
        mat list `Ld', noheader noblank format(%8.5f)

program SortOnFirstCol
        // sorts column 1 from largest down to smallest
        // moving the rest of the columns in sync
        args mat

        tempname hold
        local r1 = rowsof(`mat') - 1
        local rnames : rownames `mat'
        tokenize `rnames'
        local sorted 0
        while !`sorted' {
                local sorted 1
                forvalues i = 1/`r1' {
                        local i1 = `i'+1
                        if `mat'[`i',1] < `mat'[`i1',1] {
                                // swap rows i and i+1
                                local sorted 0
                                mat `hold' = `mat'[`i',1...]
                                mat `mat'[`i',1]=`mat'[`i1',1...]
                                mat `mat'[`i1',1] = `hold'
                                local hname ``i''
                                local `i' ``i1''
                                local `i1' `hname'
                local r1 = `r1'-1
                if `r1' == 1 {
                        local sorted 0
        mat rownames `mat' = `*'
-------------------- end factorsort.ado -------------------------

Ken Higbee    [email protected]
StataCorp     1-800-STATAPC

*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index