Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Partial Correlation


From   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Partial Correlation
Date   Sun, 26 Nov 2006 10:19:25 +0000 (GMT)

--- Raphael Fraser <raphael.fraser@gmail.com> wrote:
> What I was looking for is a command that could produce a 4x4 partial
> correlation MATRIX of y x1 x2 x3 controlling for x4 much in the same
> way as -corr y x1 x2 x3- would result. I was trying to avoid having
> to run pcorr several times to get my desired result.

The program below should do that.
HTH,
Maarten

*----------- begin example ---------------------
capture program drop pcorrmat
program define pcorrmat, rclass
	*!1.0.0 MLB 26 Nov 2006
	syntax varlist [if] [in] [aw fw], part(varlist)
	marksample touse
	markout `touse' `part'
	local weight "[`weight'`exp']"
	local k : word count `varlist'
	tokenize `varlist'
	tempname r2part r2big oneminus semip partial 
	matrix `partial' = J(`k',`k',1)

	forvalues i = 2/`k' {
		local end = `i' - 1
		forvalues j = 1/`end' {
			quietly regress ``i'' ``j'' `part' /* 
                        */ if `touse' `weight'
			local NmK = e(df_r)
			quietly test ``j''
	                local s cond(_b[``j'']>0 , 1 , -1)
			matrix `partial'[`i',`j'] = /*
                        */`s'*sqrt(r(F)/(r(F)+`NmK'))
			matrix `partial'[`j',`i'] =  /*
                        */`s'*sqrt(r(F)/(r(F)+`NmK'))
		}
	}
	matrix rownames `partial' = `varlist'
	matrix colnames `partial' = `varlist'
	matrix list `partial', noheader
	di as text "controlled for " "`part'"
	return matrix pcorr = `partial'
	return scalar N = e(N)
end

sysuse auto, clear
pcorrmat price mpg headroom, part(weight length rep78)
*--------------- end example --------------------

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


		
___________________________________________________________ 
Copy addresses and emails from any email account to Yahoo! Mail - quick, easy and free. http://uk.docs.yahoo.com/trueswitch2.html
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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