Announcement

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

  • -cpcorr- updated on SSC: module for rectangular blocks of correlations

    Thanks to Kit Baum as always, the module cpcorr on SSC has been updated. This module was first posted on SSC (and indeed last revised) in 2001, so long ago that the original post has disappeared into the oblivion of history.

    The module requires Stata 10, but so as not to disqualify those who could have made use of the previous version, the original programs remain, but with suffix *6 to indicate that the programs work in Stata 6 through 9.

    The changes include:

    1. Modernised presentation of the help.

    2. Cross-references in the help to other related commands.

    3. Saved results.

    The module includes an eponymous command cpcorr to give selected rectangular blocks of correlations that need not be entire correlation matrices. An easy example is a vector restricted to the response variable and possible predictors. A more esoteric example is a matrix with rows the original variables and columns their principal components: I find such correlations easier to think about, often, than loadings.

    Also included, but not illustrated here, is a cpspear command for Spearman correlation.

    Code:
    . clear
    
    .  sysuse auto
    (1978 Automobile Data)
    
    . cpcorr headroom trunk weight length displacement \ mpg
    (obs=74)
    
                      mpg
        headroom  -0.4138
           trunk  -0.5816
          weight  -0.8072
          length  -0.7958
    displacement  -0.7056
    
    . cpcorr headroom trunk weight length displacement \ mpg, format(%4.3f)
    (obs=74)
    
                     mpg
        headroom  -0.414
           trunk  -0.582
          weight  -0.807
          length  -0.796
    displacement  -0.706
    
    . mat C = r(C)
    
    . mat li C
    
    C[5,1]
                         mpg
        headroom  -.41380299
           trunk  -.58158503
          weight  -.80717486
          length  -.79577944
    displacement  -.70564259
    
    . pca headroom trunk weight length displacement
    
    Principal components/correlation                 Number of obs    =         74
                                                     Number of comp.  =          5
                                                     Trace            =          5
        Rotation: (unrotated = principal)            Rho              =     1.0000
    
        --------------------------------------------------------------------------
           Component |   Eigenvalue   Difference         Proportion   Cumulative
        -------------+------------------------------------------------------------
               Comp1 |      3.76201        3.026             0.7524       0.7524
               Comp2 |      .736006      .427915             0.1472       0.8996
               Comp3 |      .308091      .155465             0.0616       0.9612
               Comp4 |      .152627      .111357             0.0305       0.9917
               Comp5 |     .0412693            .             0.0083       1.0000
        --------------------------------------------------------------------------
    
    Principal components (eigenvectors)
    
        ----------------------------------------------------------------
            Variable |    Comp1     Comp2     Comp3     Comp4     Comp5
        -------------+--------------------------------------------------
            headroom |   0.3587    0.7640    0.5224   -0.1209    0.0130
               trunk |   0.4334    0.3665   -0.7676    0.2914    0.0612
              weight |   0.4842   -0.3329    0.0737   -0.2669    0.7603
              length |   0.4863   -0.2372   -0.1050   -0.5745   -0.6051
        displacement |   0.4610   -0.3390    0.3484    0.7065   -0.2279
        ----------------------------------------------------------------
    
        ---------------------------
            Variable | Unexplained
        -------------+-------------
            headroom |           0
               trunk |           0
              weight |           0
              length |           0
        displacement |           0
        ---------------------------
    
    . predict PC1-PC5
    (score assumed)
    
    Scoring coefficients
        sum of squares(column-loading) = 1
    
        ----------------------------------------------------------------
            Variable |    Comp1     Comp2     Comp3     Comp4     Comp5
        -------------+--------------------------------------------------
            headroom |   0.3587    0.7640    0.5224   -0.1209    0.0130
               trunk |   0.4334    0.3665   -0.7676    0.2914    0.0612
              weight |   0.4842   -0.3329    0.0737   -0.2669    0.7603
              length |   0.4863   -0.2372   -0.1050   -0.5745   -0.6051
        displacement |   0.4610   -0.3390    0.3484    0.7065   -0.2279
        ----------------------------------------------------------------
    
    . cpcorr headroom trunk weight length displacement \ PC?
    (obs=74)
    
                      PC1      PC2      PC3      PC4      PC5
        headroom   0.6958   0.6554   0.2900  -0.0472   0.0026
           trunk   0.8405   0.3144  -0.4261   0.1138   0.0124
          weight   0.9392  -0.2856   0.0409  -0.1043   0.1545
          length   0.9432  -0.2035  -0.0583  -0.2245  -0.1229
    displacement   0.8942  -0.2909   0.1934   0.2760  -0.0463
    
    . cpcorr headroom trunk weight length displacement \ PC?, format(%3.2f)
    (obs=74)
    
                    PC1    PC2    PC3    PC4    PC5
        headroom   0.70   0.66   0.29  -0.05   0.00
           trunk   0.84   0.31  -0.43   0.11   0.01
          weight   0.94  -0.29   0.04  -0.10   0.15
          length   0.94  -0.20  -0.06  -0.22  -0.12
    displacement   0.89  -0.29   0.19   0.28  -0.05
    Somewhat deeper than these commands, which are just convenience commands, is the minor machinery of how to do it. Let's illustrate with Kendall's tau_a to compensate for not going all the way and providing a command for that measure. In essence, you set up a matrix of the right dimensions and then loop over row and column variables to populate it with results.


    Code:
    sysuse auto, clear
    mat ktau = J(5, 1, .)
    local Y mpg
    local nY : word count `Y'
    local X headroom trunk weight length displacement
    local nX : word count `X'
    
    forval i = 1/`nX' {
        local x : word `i' of `X'
        forval j = 1/`nY' {
            local y : word `j' of `Y'  
            quietly ktau `x' `y'
            matrix ktau[`i', `j'] = r(tau_a)
        }
    }
    
    matrix colnames ktau = `Y'
    matrix rownames ktau = `X'
    
    mat li ktau, format(%4.3f)  
    
    ktau[5,1]
                     mpg
        headroom  -0.305
           trunk  -0.451
          weight  -0.686
          length  -0.633
    displacement  -0.594
Working...
X