Announcement

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

  • Creating nxn matrix from scalars

    Hi,

    Let's say I have n variables, named a, b, c, ...,z (where z is the last variable in the list) and for each of these I calculate, e.g. variances and covariances. I store these as scalars, named as follows:

    cov_a_a (variance of a)
    cov_a_b (covariance between a and b)
    cov_b_c (covariance between b and c)

    and so on. Given that I have these stored as scalars, what is the easiest way of creating a variance-covariance matrix ? Suppose for argument's sake that I have to do it this way (i.e. I cannot just use correlate varlist, covariance)

  • #2
    I don't get the "for argument's sake". What circumstances oblige you to do this? Just go straight to where you want to be, e.g.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . corr mpg price weight
    (obs=74)
    
                 |      mpg    price   weight
    -------------+---------------------------
             mpg |   1.0000
           price |  -0.4686   1.0000
          weight |  -0.8072   0.5386   1.0000
    
    
    . matrix corr = r(C)
    
    . corr mpg price weight, cov
    (obs=74)
    
                 |      mpg    price   weight
    -------------+---------------------------
             mpg |   33.472
           price | -7996.28  8.7e+06
          weight | -3629.43  1.2e+06   604030
    
    , matrix cov = r(C)
    
    . mat li corr
    
    symmetric corr[3,3]
                   mpg       price      weight
       mpg           1
     price  -.46859669           1
    weight  -.80717486   .53861146           1
    
    . mat li cov
    
    symmetric cov[3,3]
                   mpg       price      weight
       mpg   33.472047
     price  -7996.2829     8699526
    weight  -3629.4261   1234674.8   604029.84

    That said, given your question you will need to tell us more about how you are holding your scalars and what exact naming structure you are following. Is your example literal or just schematic? Otherwise the answer is just create a matrix of missing values and loop over rows and columns populating it with non-missing scalar values.
    Last edited by Nick Cox; 13 Apr 2017, 04:21.

    Comment


    • #3
      Hi Nick,

      The reason I can't use corr var1 var2 ... , cov is that I am wanting pairwise variances and covariances, and corr uses listwise deletion. Neither will pwcorr help, since covar is not an option with that command (nor is the output of pwcorr stored).

      I've found a roundabout way of doing it. Here's my code so far:

      program pairwise, eclass
      version 13.1
      if !replay() {
      syntax varlist(numeric ts) [if] [in] [, Level(cilevel)]
      gettoken yvar xvars : varlist
      quietly{
      foreach x in `xvars'{
      correlate `yvar' `x', covariance
      scalar covar_`yvar'_`x' = r(cov_12)
      }
      local nvar : word count `xvars'
      forval i = 1/`nvar'{
      forval j = 1/`nvar'{
      local x1 : word `i' of `xvars'
      local x2 : word `j' of `xvars'
      correlate `x1' `x2', covariance
      scalar covar_`x1'_`x2' = r(cov_12)
      }
      }
      mat C = J(1,`nvar'+1,1)
      foreach x in `xvars'{
      local row_`x' 1
      forval i = 1/`nvar'{
      local x1 : word `i' of `xvars'
      local row_`x' "`row_`x'',covar_`x'_`x1'"
      mat row_`x' = (`row_`x'')
      }
      }
      foreach x in `xvars'{
      mat C =C\row_`x'
      }
      }
      }
      end

      Now at the moment, what this gives me is the matrix of pairwise variances and covariances, but augmented by a row and column of ones, which I need to remove (presumably this is straightforward). I wonder if there is a simpler way, however.

      p.s. I know that there is a user-written pwcov command out there that does largely the same thing, but I want to do this myself, as this is going to be a component of a command I am coding.

      Comment


      • #4
        This should help a bit. I've not tried to fix that code, but to start afresh.


        Code:
        program foobar, rclass  
            syntax varlist(numeric)  [if] [in] 
            tempname corr cov N 
            marksample touse, novarlist 
            
            local nv : word count `varlist' 
            mat `corr' = J(`nv', `nv', .) 
            mat `cov' = J(`nv', `nv', .) 
            mat `N' = J(`nv', `nv', .) 
            
            tokenize "`varlist'" 
            quietly forval i = 1/`nv' { 
                forval j = 1/`nv' { 
                    correlate ``i'' ``j'' if `touse' 
                    mat `corr'[`i', `j'] = r(rho)
                    mat `N'[`i', `j'] = r(N)
                    correlate ``i'' ``j'' if `touse', cov 
                    mat `cov'[`i', `j'] = r(cov_12) 
                }
            } 
            
            foreach p in corr cov N { 
                return matrix `p' = ``p''
            }
            
        end

        Comment

        Working...
        X