Announcement

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

  • Performing PCA by group

    Dear all,

    I an trying to perform a PCA analysis by company on panel data.

    The goal is to create an index out of multiple variables for each company which should use the same variable name (aware_pca_test) for all companies.

    Currently my code looks like this


    Code:
        levelsof gvkey, local(gvkeys)
        gen aware_pca_final =.
        
        foreach i of local gvkeys{
            pca Awareness_score womexposure_score if gvkey == `i'
            rotate
            predict aware_pca_test
            replace aware_pca_final = aware_pca_test if gvkey == `i' 
            drop aware_pca_test
        }
    The loop seems to work, but the pca always creates the same value. Am i not looping correctly through the values for í'?

    Any advice would be most appreciated.

    Thanks and best,
    Micha
    Last edited by Micha; 13 Apr 2019, 07:29.

  • #2
    You didn't provide example data, but I tested your code using one of the built-in Stata data sets, renaming a few variables:

    Code:
    clear*
    webuse grunfeld
    
    rename company gvkey
    rename invest Awareness_score
    rename mvalue womexposure_score
    
    levelsof gvkey, local(gvkeys)
    gen aware_pca_final =.
    
    foreach i of local gvkeys{
        pca Awareness_score womexposure_score if gvkey == `i'
        rotate
        predict aware_pca_test
        replace aware_pca_final = aware_pca_test if gvkey == `i' 
        drop aware_pca_test
    }
    It seems to work just fine. It does not create the same value over and over.

    So, if this truly is exactly the code you were running, I am unable to reproduce your problem and have no explanation.

    My best gueses are that either:

    1. What you have shown is somehow subtly different from the actual code you ran. Devil is in the details and it isn't possible to troubleshoot code working with anything but the exact real code.

    2. There is something wrong with your data.

    Comment


    • #3
      Dear clyde,

      thanks for your support, much appreciated! Tried again with a clean-run of the code and this time it also worked for me.

      Thanks a lot!

      Best,
      Micha

      Comment

      Working...
      X