Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: WG: Star Levels in Correlation Tables


From   Roy Wada <[email protected]>
To   [email protected]
Subject   Re: st: WG: Star Levels in Correlation Tables
Date   Mon, 25 Jan 2010 03:04:06 -0800

>> Alternatively, is there any other command that will include the stars
>> automatically? The command "mkcorr" does not appear to have this option but
>> I'm wondering if you might know of any other similar command for correlation
>> tables that does..
>>
>> Nish

Here is another one. A gift that keeps on giving.

Should work with most table-making programs. Specify -pval- option to
take advantage of inverse-normal distribution of p-value. You'll need
to fix the diagonal ones by hand.


** syntax that should work with most programs
corr2 price length turn displace foreign
- est store, or whatever of choice here -
corr2 length turn displace foreign
- est store, or whatever of choice here -
corr2 turn displace foreign
- est store, or whatever of choice here -
corr2 displace foreign
- est store, or whatever of choice here -
corr2 foreign
- est store, or whatever of choice here -

** one syntax with outreg2
cap erase myfile.txt
corr2 price length turn displace foreign: outreg2 using myfile, pval noobs excel

** another syntax with original outreg
corr2 price length turn displace foreign
outreg using myfile, pval replace nonobs nolab
corr2 length turn displace foreign: outreg using myfile, pval append
nonobs nolab
outreg using myfile, pval append nonobs nolab


** the program
*! corr2 1.0.0 25jan2010 by [email protected]
*! Convert pairwise correlation to estimates matrix
*! based on pwcorr version 3.0.11 09feb2006

prog define corr2
version 8
 *set trace on
 local _0 `0'
 cap _on_colon_parse `0'

 if `"`s(after)'"'~="" & _rc==0 {
  local after `"`s(after)'"'

  local 0 `s(before)'
  syntax [varlist(min=1 ts)] [if] [in] [aw fw] [, * ]

  local leftover `varlist'

  while "`leftover'"~="" & "`leftover'"~=" " & "`leftover'"~="  " {

   qui _corr2 `leftover' `if' `in' `weight'`exp', `options'
   `after'

   gettoken first leftover : leftover
  }

  local _0 `s(before)'
 }
 else {
  cap _corr2 `0'
 }
end



prog define _corr2, rclass
version 8

syntax [varlist(min=1 ts)] [if] [in] [aw fw] [, * ]

marksample touse

local count: word count `varlist'
local depvar: word 1 of `varlist'

tempname rho se
 forval num=1/1 {
  local word: word `num' of `varlist'
  _pwcorr `depvar' `word' if `touse' `weight'`exp', `options'
  if r(N)~=0 {
   mat `rho' = nullmat(`rho') \ `c2_1'
   local temp =`c2_1'/invnormal(0.00000001/2)
   mat `se' = nullmat(`se') \ `temp'
   local matname "`matname' `word'"
  }
 }
 forval num=2/`count' {
  local word: word `num' of `varlist'
  _pwcorr `depvar' `word' if `touse' `weight'`exp', `options'
  if r(N)~=0 {
   mat `rho' = nullmat(`rho') \ `c2_1'
   local temp =`c2_1'/invnormal(`p2_1'/2)
   mat `se' = nullmat(`se') \ `temp'
   local matname "`matname' `word'"
  }
 }

mkest, replace bmat(`rho') vmat(`se') depvar(`depvar') /*
 */ cmd(correlation) noobs noesample matname(`matname')

end


* mkest 1.0.1 14jan2010 by [email protected]
* Convert variables to estimates matrix

program def mkest, eclass
version 8

syntax [if] [in], replace [b(str asis) v(str asis) bmat(str asis) /*
 */ vmat(str asis) depvar(str asis) cmd(str asis) obs(numlist) /*
 */ NOESAMPLE NOOBS matname(str asis)]

tempname ebmat eVmat
marksample touse

if "`depvar'"=="" {
 local depvar `b'
}
if "`cmd'"=="" {
 local cmd `depvar'
}

if "`bmat'"=="" {
 * get from variables
 mkmat `b' if `touse', matrix(`ebmat') nomiss
 mat `ebmat'=`ebmat''

 mkmat `v' if `touse', matrix(`eVmat') nomiss
 mat `eVmat'=`eVmat''
 mat `eVmat'=(`eVmat''*`eVmat')
}
else {
 * get from matrices
 mat `ebmat'=`bmat''

 mat `eVmat'=`vmat''
 mat `eVmat'=(`eVmat''*`eVmat')
}

if "`obs'"=="" {
 local obs=colsof(`ebmat')
}
if "`noobs'"~="" {
 local obs .
}
if "`matname'"~="" {
 matname `ebmat' `matname', col(.)
 matname `eVmat' `matname', row(.)
 matname `eVmat' `matname', col(.)
}


tempvar sample
cap gen `sample'=1 if `b'~=. & `touse'
cap gen `sample'=1 if `b'~="" & `touse'
cap qui replace `sample'=0 if `sample'==.

 if "`replace'"=="replace" {
  eret clear
  if "`noesample'"=="" {
   eret post `ebmat' `eVmat', esample(`sample')
  }
  else {
   eret post `ebmat' `eVmat'
  }
  eret local depvar `"`depvar'"'
  eret local cmd "`cmd'"
  eret scalar N = `obs'
 }

end


* pwcorr version 3.0.11 09feb2006
program define _pwcorr, byable(recall)
        version 6, missing
        syntax [varlist(min=2 ts)] [if] [in] [aw fw] [, /*
       */ Bonferroni Obs Print(real -1) SIDak SIG STar(real -1) ]
        tempvar touse
        mark `touse' `if' `in' /* but do not markout varlist */
        tokenize `varlist'

        local i 1
        while "``i''" != "" {
       capture confirm str var ``i''
       if _rc==0 {
      di in gr "(``i'' ignored because string variable)"
      local `i' " "
       }
       local i = `i' + 1
        }
        local varlist `*'
        tokenize `varlist'
        local nvar : word count `varlist'
        if `nvar' < 2 { error 102 }

        local weight "[`weight'`exp']"
        local nvar : word count `varlist'
        local adj 1
        if "`bonferr'"!="" | "`sidak'"!="" {
       if "`bonferr'"!="" & "`sidak'"!="" { error 198 }
       local nrho=(`nvar'*(`nvar'-1))/2
       if "`bonferr'"!="" { local adj `nrho' }
        }
        if (`star'>=1) {
       local star = `star'/100
       if `star'>=1 {
      di in red "star() out of range"
      exit 198
       }
        }
        if (`print'>=1) {
       local print = `print'/100
       if `print'>=1 {
      di in red "print() out of range"
      exit 198
       }

        }

        local j0 1
        while (`j0'<=`nvar') {
       di
       local j1=min(`j0'+6,`nvar')
       local j `j0'
       di in smcl in gr _skip(13) "{c |}" _c
       while (`j'<=`j1') {
      di in gr %9s abbrev("``j''",8) _c
      local j=`j'+1
       }
       local l=9*(`j1'-`j0'+1)
       di in smcl in gr _n "{hline 13}{c +}{hline `l'}"

       local i `j0'
       while `i'<=`nvar' {
      di in smcl in gr %12s abbrev("``i''",12) " {c |} " _c
      local j `j0'
      while (`j'<=min(`j1',`i')) {
     cap corr ``i'' ``j'' if `touse' `weight'
     if _rc == 2000 {
    local c`j' = .
     }
     else {
    local c`j'=r(rho)
     }
* added:
c_local c`i'_`j'=`c`j''
     local n`j'=r(N)
     local p`j'=min(`adj'*tprob(r(N)-2,/*
     */ r(rho)*sqrt(r(N)-2)/ /*
     */ sqrt(1-r(rho)^2)),1)
     if (r(rho)>=1) {
    local p`j'=0
     }
     if "`sidak'"!="" {
    local p`j'=min(1,1-(1-`p`j'')^`nrho')
     }
* added:
c_local p`i'_`j'=`p`j''
     local j=`j'+1
      }
      local j `j0'
      while (`j'<=min(`j1',`i')) {
     if `p`j''<=`star' & `i'!=`j' {
    local ast "*"
     }
     else local ast " "
     if `p`j''<=`print' | `print'==-1 |`i'==`j' {
    di " " %7.4f `c`j'' "`ast'" _c
     }
     else    di _skip(9) _c
     local j=`j'+1
      }
      di
      if "`sig'"!="" {
     di in smcl in gr _skip(13) "{c |}" _c
     local j `j0'
     while (`j'<=min(`j1',`i'-1)) {
    if `p`j''<=`print' | `print'==-1 {
            di "  " %7.4f `p`j'' _c
    }
    else    di _skip(9) _c
    local j=`j'+1
     }
     di
      }
      if "`obs'"!="" {
     di in smcl in gr _skip(13) "{c |}" _c
     local j `j0'
     while (`j'<=min(`j1',`i')) {
    if `p`j''<=`print' | `print'==-1 /*
    */ |`i'==`j' {
            di "  " %7.0g `n`j'' _c
    }
    else    di _skip(9) _c
    local j=`j'+1
     }
     di
      }
      if "`obs'"!="" | "`sig'"!="" {
     di in smcl in gr _skip(13) "{c |}"
      }
      local i=`i'+1
       }
       local j0=`j0'+7
        }
end
exit
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index