Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Tempfile in double loop for correlations
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Tempfile in double loop for correlations
Date
Thu, 4 Jul 2013 09:05:52 +0100
I can see five kinds of problems here
1. You do not give consistent instructions. You declare that
-postfile- will contain several new variables, but you only attempt to
post two. There should be a one-to-one correspondence, as many results
posted by a -post- statement as are declared in a -postfile-
statement.
2. One of the variables you want to post will need to be a string
variable. The longest variable name you use is -postmodernity-, so you
need a -str13- variable.
. di length("postmodernity")
13
You need to declare -str- variables explicitly. This problem is likely
to be what is biting first, and stopping the code.
3. You do not define any local called -rho-. Attempting to -display- a
non-existent local `rho' is perfectly legal, but -post-ing a
non-existent local to a file can't be what you want.
It is not an error as such to refer to a non-existent local, but doing
that may often be an error in terms of what you want.
4. What works, as you report, is picking up r(rho) from -correlate-.
But in your more ambitious code you try to use e(rho). That won't do
what you want -correlate- is r-class and does not produce e(rho).
5. -post- didn't work, you got an error message and your code stopped,
but you still expected to see a file with results afterwards. You
would need more than good luck for that.
This is likely to be closer to what you intend.
foreach k in statism modernity postmodernity student {
tempname memhold
tempfile results`k'
postfile `memhold' str13 which corr using `results`k'', replace
foreach var in histonly soc_oth civonly {
quietly corr `k' `var'
post `memhold' ("`k'") (`r(rho)')
}
postclose `memhold'
}
All that said, I wouldn't use this code at all. -corrci- (SJ) has an
option to save correlations to a file.
corrci statism modernity postmodernity student histonly soc_oth
civonly, saving(mycorr)
will do this. That will be more correlations than you want, but you
just -drop- what you don't want. (The correlations you don't ask for
could be more interesting than the ones you do ask for.)
For -corrci- see the 2008 paper but download the 2010 code.
SJ-8-3 pr0041 . Speaking Stata: Corr. with confidence, Fisher's z revisited
(help corrci, corrcii if installed) . . . . . . . . . . . . N. J. Cox
Q3/08 SJ 8(3):413--439
reviews Fisher's z transformation and its inverse, the
hyperbolic tangent, and reviews their use in inference
with correlations
pr0041_1 from http://www.stata-journal.com/software/sj10-4
SJ10-4 pr0041_1. Update: Correlation with confidence... / Update:
Correlation with confidence intervals / by Nicholas J. Cox, Durham
University, Durham City, UK / Support: [email protected] / After
installation, type help corrci and corrcii
Nick
[email protected]
On 4 July 2013 05:15, Peggy Fan <[email protected]> wrote:
> I wish to save the correlations between two sets of variables.
> Set 1: statism modernity postmodernity student
> Set 2: histonly soc_oth civonly
>
> I want to save a tempfile for the correlations between statism:set 2 variables, another tempfile for modernity:set2 variables, so forth. So I want to save and outsheet 4 tempfiles.
>
> I am running the following codes successfully-
>
> foreach k in statism modernity postmodernity student {
> foreach var of varlist histonly soc_oth civonly {
>
> quietly corr `k' `var'
> di as text "`: variable label `var'': " as result r(rho)
> }
> }
>
>
> But I'm having trouble saving the results in tempfiles. I played around with different parts of the code but kept getting error messages. This is an example of what I think should work-
>
> foreach k in statism modernity postmodernity student {
>
> tempname memhold
> tempfile results`k'
> postfile `memhold' statism modernity postmodernity student rho using `results`k'', replace
>
> foreach var of varlist histonly soc_oth civonly {
> quietly corr `k' `var'
> local `k'_`var' = e(rho)
> di `rho'
> post `memhold' ("`k'") (`rho')
> }
> postclose `memhold'
> }
>
>
> Error comes up:
> type mismatch
>
> When I try to outsheet the data
>
> use `results`k'', clear
>
> Error is: invalid specified file.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/