Announcement

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

  • Using bys on cpcorr

    Dear Statalistmembers,

    I am using Stata 18 for windows. I want to use the ssc available plugin cpcorr to measure correlation between variables. In addition, I would like to do so using 2 samples, which I created using a dummy variable called sample. However, in difference to the normal corr or pwcorr, it shows me that the command by is not allowed. Does anyone have a suggestion for this? As I am creating a handful of those correlation outputs, I hope to find a more elegant way than to run a a preserve/ restore query with keep if sample==1 or 0.

    Best,
    Moritz

  • #2
    cpcorr is ado-code, so as it were an add-on rather than a plugin, a term which in Stata usually means code in some other language (C, Java, whatever).

    As it's focused on production of a matrix there would be a real question about what you expect the command to do. I can't see any need for use of any or all of preserve restore keep as if can do the selection.

    Here is an example of something simple to imagine, but also quite simple to do:

    Code:
    . sysuse auto, clear 
    (1978 automobile data)
    
    . 
    . cpcorr mpg \ price weight if !foreign
    (obs=52)
    
           price   weight
    mpg  -0.5043  -0.8759
    
    . matrix corr0 = r(C)
    
    . 
    . cpcorr mpg \ price weight if foreign
    (obs=22)
    
           price   weight
    mpg  -0.6313  -0.6829
    
    . matrix corr = corr0 \ r(C)
    
    . 
    . matrix li corr 
    
    corr[2,2]
              price      weight
    mpg  -.50426286  -.87594266
    mpg  -.63130259  -.68285403
    
    . 
    . matrix rownames corr = Domestic Foreign 
    
    . 
    . matrix li corr, format(%4.3f)
    
    corr[2,2]
               price  weight
    Domestic  -0.504  -0.876
     Foreign  -0.631  -0.683

    Comment

    Working...
    X