Statalist


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

Re: st: A Question on Looping


From   n j cox <n.j.cox@durham.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: A Question on Looping
Date   Tue, 18 Sep 2007 17:26:23 +0100

I see problems on various levels.

1. You are confusing the syntax of -foreach- and -forval-. Look
again at the syntax diagrams for these commands.

2. You are using subscript notation for two purposes, (a) to
refer to individual observations and (b) to tag different matrices.
The first is fine; the second is not correct syntax at all.
Stata does not have any concept of an array of matrices.
Your -while- loop is falling over because of your syntax
errors under this heading.

3. -matrix list- will only list one matrix at a time.

4. -matrix symeigen- will only play if your matrices are
symmetric.

This code should be more nearly legal, but it is not tested:

forval i = 1/43 {
matrix K = (A[`i'], B[`i'] \ B[`i'], A[`i'])
matrix symeigen X L = K
di "`i'"
matrix list L
}

However, I get the impression that you think -matrix symeigen-
will oblige you if your matrices are asymmetric. It will not.
In Stata 8.2, however, you could write your own code to
extract your eigenvalues as the roots of each characteristic
equation, which in this case is always a quadratic. In general,
some eigenvalues may be complex.

Alternatively, Stata 9 will happily oblige, or there are
other programs that will do this.

Nick
n.j.cox@durham.ac.uk

Asgar Khademvatani [edited]

I have generated four variables A, B, C, and D that
each contain 43 observations, in Stata 8.2. I want to make 43 square
matrices (2 by 2) using each of the observations. Then I want to calculate the real eigenvalues of each matrix and list the 43 matrices
and their eigenvalues. I need to write a loop to do this job.

I have already written the following loop using -foreach- command but I am getting the following error as follows;

foreach i = 1/43 {
matrix K[`i'] = (A[`i'], B[`i'] \ C[`i'], D[`i'])
matrix symeigen X[`i'] V[`i'] = K[`i']
matrix list K[`i'] V[`i']
}
invalid syntax
r(198);

end of do-file
r(198);


Also, alternatively, I have written another alternative do file
with a -while- loop but it is also subject to error as follows;

1.local i 1
2. while `i' < 43 {
3. matrix K[`i'] = (lpl_m[`i'], lpm_m[`i'] \ le_m[`i'],lk_m[`i'])
4. matrix symeigen X[`i'] V[`i'] = K[`i']
5. matrix list V[`i']
6. local i = `i' + 1
7. }
too many ')' or ']'
r(132);

end of do-file
r(132);

Could you help me out on this matter and say what is wrong with any of
these alternative loops?

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