Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Making index for a number of countries.

    I am working on the panel data of a large number of countries.

    I have to use PCA on three variable namely inf to bdg by country. And have to make an index by combining these three variables.
    Like the results for country 3 are given below.
    -> country = 3

    Principal components/correlation Number of obs = 21
    Number of comp. = 3
    Trace = 3
    Rotation: (unrotated = principal) Rho = 1.0000

    --------------------------------------------------------------------------
    Component | Eigenvalue Difference Proportion Cumulative
    -------------+------------------------------------------------------------
    Comp1 | 1.49128 .514823 0.4971 0.4971
    Comp2 | .976458 .444197 0.3255 0.8226
    Comp3 | .532261 . 0.1774 1.0000
    --------------------------------------------------------------------------

    Principal components (eigenvectors)

    ----------------------------------------------------------
    Variable | Comp1 Comp2 Comp3 | Unexplained
    -------------+------------------------------+-------------
    to | -0.6853 0.1973 0.7010 | 0
    inf | 0.2206 0.9736 -0.0584 | 0
    bdg | 0.6940 -0.1146 0.7108 | 0
    ----------------------------------------------------------

    . the formula for the index will be
    P=-0.6853*to +0.2206 * inf +| 0.6940 *bdg

    For country 2
    -> country = 2

    Principal components/correlation Number of obs = 21
    Number of comp. = 3
    Trace = 3
    Rotation: (unrotated = principal) Rho = 1.0000

    --------------------------------------------------------------------------
    Component | Eigenvalue Difference Proportion Cumulative
    -------------+------------------------------------------------------------
    Comp1 | 2.03907 1.48791 0.6797 0.6797
    Comp2 | .55116 .141388 0.1837 0.8634
    Comp3 | .409772 . 0.1366 1.0000
    --------------------------------------------------------------------------

    Principal components (eigenvectors)

    ----------------------------------------------------------
    Variable | Comp1 Comp2 Comp3 | Unexplained
    -------------+------------------------------+-------------
    to | 0.5499 0.8304 -0.0894 | 0
    inf | 0.5947 -0.3141 0.7400 | 0
    bdg | -0.5864 0.4601 0.6666 | 0
    ----------------------------------------------------------

    . the formula for the index for country 2 will be
    P=-0. 5499 *to +0.5947 * inf - 0.5864*bdg
    And so on for other countries.

    How I can do this in stata through do file.
    The data for the three countries is attached with the email.

  • #2
    So, first you need a loop over the levels of the country variable. And you don't need to manually score this: -predict- will do that for you.

    So:

    [code]
    levelsof country, local(countries)

    foreach c of local countries {

    Comment


    • #3
      how i can loop over the levels of the country variable.

      Comment


      • #4
        So, first you need a loop over the levels of the country variable. And you don't need to manually score this: -predict- will do that for you.

        So:

        Code:
        levelsof country, local(countries)
        
        gen score1 = .
        gen score2 = .
        gen score3 = .
        
        foreach c of local countries {
            pca to inf bdg if country == `c'
            predict sc* if country == `c'
            forvalues j = 1/3 {
                replace score`j' = sc`j' if country == `c'
                drop sc`j'
            }
        }
        To be clear: the result you are looking for will be in score1. (score2 and score3 will be the scores on the other two principal components. You can drop them if they are of no use to you.)

        The coding issues aside, this seems an odd thing to do. Most people would say that doing 3 principal components on 21 observations at a time is really pushing your luck. And it's unclear why you want to do a separate PCA for each country--I don't know what your research questions are, but I've never seen anything that would entail this approach before. So just raising some questions, for what they're worth.
        Last edited by Clyde Schechter; 11 Jan 2015, 21:52.

        Comment


        • #5
          The code is giving error

          Comment


          • #6
            Show us, by copying and pasting from Stata's results window into a code block, exactly what error you are getting. To open a code block, click on the underlined A button, which opens the advanced editor. Then click on the # button. Two code block delimiters will appear. Paste between them.

            Comment

            Working...
            X