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 01:51:46 -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

Actually, there are several. Here is one. Glenn can have them.


*** the syntax here:
sysuse auto, clear
mkcorr2 pri mpg disp tr rep78, log(newstuff) replace aster sig
dataout using newstuff.log, save(output) excel replace


*** the program here:
* touched by [email protected]
prog drop _all
program mkcorr2, byable(recall)
*! Author: Glenn Hoetker
syntax varlist [if] [in],Log(string) [Replace] [CDec(integer 2)]  ///
[MDec(integer 3)] [Means] [Sig] [Num] [Label] ///
[NOCorr] [CASEwise] [ASTER]

version 8.0

/* Macros */
local width=`cdec'+2
local cformat "%`width'.`cdec'f"
local mformat "%9.`mdec'f"
tempname output
tempvar touse
local n_rows:list sizeof varlist
local n_cols: list sizeof varlist

//Limit the sample to those with observations for all
//vars if CASEwise selected
if "`casewise'"~="" {
        marksample touse
        if "`if'"~="" {
                local if="`if'" + " & \`touse'"
        }
        else {
                local if "if \`touse'"
                }
        }
                //Open and write to the file.

if index(`"`log'"',".")>0 {
        local logname `log'
        }
else {
        local logname `log'.log
        }

file open `output' using `"`logname'"', write text `replace'

//The labels across the top
        file write `output' _tab
        if "`num'"~="" file write `output' _tab
        if "`means'"~="" {
                file write `output' "Mean" _tab "S.D." _tab "Min" _tab
"Max" _tab
                }

                //Put either variable names or numbers
                if "`nocorr'"=="" {
                        if "`num'"=="" {
                                foreach var of local varlist{
                                        local lab: variable label `var'
                                        if "`label'"=="" | `"`lab'"'=="" {
                                                local lab `var'
                                                }
                                        file write `output' "`lab'" _tab
                                        }
                                }
                                else {
                                        forvalues x=1/`n_cols' {
                                                file write `output'
"(" (`x') ")" _tab
                                        }
                                }
                        }
        file write `output' _n


//Output rows of the matrix one at a time, starting with the variable name
//and then the values

forvalues row=1/`n_rows' {
        //The number of the variable, if requested by NUM
        if "`num'"~="" {
                file write `output' "(" (`row') ")" _tab
                }

        //The variable name
        local var: word `row' of `varlist'
        local lab: variable label `var'
        if "`label'"=="" | `"`lab'"'==""{
                local lab `var'
                }
        file write `output' "`lab'" _tab

        //The values
                //If we are putting in means
                if "`means'"~="" {
                quietly summarize `var' `if' `in'
                file write `output' `mformat' (`r(mean)') _tab ///
                `mformat' (`r(sd)') _tab `mformat' (`r(min)') _tab
`mformat' (`r(max)') _tab
                }
        //Correlations
        if "`nocorr'"=="" {
                forvalues col=1/`row' {     //Notice that we are only
doing the bottom half
                        local var1:word `row' of `varlist'
                        local var2:word `col' of `varlist'
                        qui corr(`var1' `var2') `if' `in'
                        local val=r(rho)

* touched by [email protected]
local star
if "`aster'"=="aster" {
	local p=min(tprob(r(N)-2,r(rho)*sqrt(r(N)-2)/sqrt(1-r(rho)^2)),1)
	if `p'<.10 {
		local star "*"
	}
	if `p'<.05 {
		local star "**"
	}
	if `p'<.01 {
		local star "***"
	}
}

                        file write `output' `cformat' (`val') "`star'" _tab
                }
                file write `output' _n

                //Put in the significance in the next row, if requested
                if "`sig'"~="" {
                        file write `output' _tab
                        if "`means'"~="" {
                                file write `output' _tab(4)
                                }
                        if "`num'"~="" {
                                file write `output' _tab
                                }
                        forvalues col=1/`row' {
                                local var1:word `row' of `varlist'
                                local var2:word `col' of `varlist'
                                if "`row'"=="`col'" {
                                        file write `output' "" _tab
                                        }
                                else {
                                qui correlate `var1' `var2' `if' `in'
                                local rho=`r(rho)'
                                local n=`r(N)'
                                local
p=min(tprob(r(N)-2,r(rho)*sqrt(r(N)-2)/sqrt(1-r(rho)^2)),1)
                                file write `output' "(" `cformat' (`p') ")" _tab
                                }
                        }
                        file write `output' _n
                        }
                }
        else file write `output' _n
        }

file close `output'

end
*
*   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