Announcement

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

  • Matrix with phi correlation coefficients

    Hey there,

    I already made some matrices with the coefficients from spearman and parson correlations. Now I wanted to do the same with phi correlation coefficients (as both my variables are binary I cannot use Pearson or Spearman-right?) but it does not work as with the other both commands.
    I suspect that the problem is that phi does not store the results in the return list nor in the e-return list but I cannot find anywhere, where they are stored.
    This is my code for the matrix:
    mat define phi=(3,1)
    local line=1
    local row=1
    foreach var in A B C {
    replace `var'=. if `var'>=.
    phi `var' D if D<.
    mat phi [`line',`row' ]= r(rho)
    local line= `line'+1
    }
    frmttable, stat (phi) sdec (2)[/INDENT]The error I receive is that the matrix contains only missing values.

    Does anybody have an idea how to fix that?

    Thaank you!
    Lisa

  • #2
    What phi command are you using here? I found a phi command written by Rich Goldstein around 1991 and even a file on my machine that indicates we (he and I) fiddled with it around 2000 and again in 2011 but even the latest version doesn't save anything as r(rho) or indeed any other r-class result. That could be programmed but in the mean time please explain which you are using.

    See https://stats.stackexchange.com/ques...een-two-boolea for some opinions in this territory. I won't die on this hill, however.

    Comment


    • #3
      Thanks for your answer, Nick!

      I am using the one from Rich Goldstein.
      As it does not save anything, is it not possible to create a matrix (besides doing it manually)?
      I would appreciate any other solutions/ commands I could use. I read about "tetrachoric" but it assumes a bivariate normal distribution which my variables do not follow unfortunately.

      Thanks a lot

      Comment


      • #4
        You might as well use a script to calculate phi from tabulate results, which is what the phi command does.


        Code:
        webuse nlswork, clear
        
        * Stata Journal 
        findname, all(inlist(@, 0, 1, .)) 
        
        local vars `r(varlist)'
        
        local nvars : word count `vars'
        
        matrix phi = J(`nvars', `nvars', 1)
        
        tokenize "`vars'"
        
        forval i = 1/`nvars' {
            local j1 = `i' + 1
            forval j = `j1'/`nvars' { 
                tab ``i'' ``j'', chi 
                matrix phi[`i', `j'] = sqrt(r(chi2) / r(N))
                matrix phi[`j', `i'] = phi[`i', `j']
            }
        }
        
        matrix rownames phi = `vars'
        matrix colnames phi = `vars'
        
        mat li phi, format(%4.3f)
        
        symmetric phi[7,7]
                       msp   nev_mar  collgrad  not_smsa    c_city     south     union
             msp     1.000
         nev_mar     0.673     1.000
        collgrad     0.014     0.021     1.000
        not_smsa     0.086     0.081     0.076     1.000
          c_city     0.157     0.143     0.016     0.468     1.000
           south     0.035     0.056     0.047     0.175     0.003     1.000
           union     0.038     0.019     0.067     0.068     0.073     0.132     1.000

        Comment


        • #5
          Pearson's r and Phi are identical for the case of two binary variables, a memory conformed by note 4 in the Wikipedia entry on Phi. So, -correlate- should accomplish the desired end with built-in tools.

          Comment


          • #6
            #5 is a good reminder, but there is a qualifier. Pearson's r can be negative and phi cannot be, so the Pearson correlation can carry more information too.

            Comment


            • #7
              Now that Mike Lacy has reduced the problem to Pearson correlation, cpcorr from SSC becomes relevant for cases like #1 in which only a submatrix, even a vector, is needed.

              Code:
              . webuse nlswork, clear
              (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
              
              . cpcorr msp nev_mar collgrad \ c_city
              (obs=28510)
              
                         c_city
                   msp  -0.1573
               nev_mar   0.1429
              collgrad   0.0162
              
              . ssc desc cpcorr
              
              ------------------------------------------------------------------------------------------
              package cpcorr from http://fmwww.bc.edu/repec/bocode/c
              ------------------------------------------------------------------------------------------
              
              TITLE
                    'CPCORR': module for correlations for each row vs each column variable
              
              DESCRIPTION/AUTHOR(S)
                    
                    cpcorr produces a matrix of correlations for rowvarlist versus
                    colvarlist. cpspear does the same for Spearman correlations. This
                    matrix may thus be oblong, and need not be square. Both also
                    allow a single varlist.
                    
                    KW: correlate
                    KW: matrix
                    KW: oblong
                    
                    Requires: Stata version 10.0 (6.0 for cpcorr6, cpspear6)
                    
                    
                    Author: Nicholas J. Cox, Durham University
                    Support: email [email protected]
                    
                    Distribution-Date: 20200812
                    
              
              INSTALLATION FILES                               (type net install cpcorr)
                    cpcorr.ado
                    cpcorr.sthlp
                    cpspear.ado
                    cpspear.sthlp
                    cpcorr6.ado
                    cpcorr6.hlp
                    cpspear6.ado
                    cpspear6.hlp
              ------------------------------------------------------------------------------------------
              (type ssc install cpcorr to install)

              Comment

              Working...
              X