Bookmark and Share

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: Use of matrix values in generate statements


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Use of matrix values in generate statements
Date   Sun, 27 Mar 2011 17:30:05 +0100

Excellent question. In fact, reviewing the thread, it is clear that at
least some of us were misled by the initial statement, which we should
have checked.  Consider a test such as

. clear

. set obs 18
obs was 0, now 18

. matrix foo = (1,2,3,4,5,6\7,8,9,10,11,12\13,14,15,16,17,18)

. mat li foo

foo[3,6]
    c1  c2  c3  c4  c5  c6
r1   1   2   3   4   5   6
r2   7   8   9  10  11  12
r3  13  14  15  16  17  18

. egen j = seq(), block(6)

. egen i = seq(), to(3)

. gen z = foo[i,j]

. gen z2 = foo[2*i - i,j]

. l

     +-----------------+
     | j   i    z   z2 |
     |-----------------|
  1. | 1   1    1    1 |
  2. | 1   2    7    7 |
  3. | 1   3   13   13 |
  4. | 1   1    1    1 |
  5. | 1   2    7    7 |
     |-----------------|
  6. | 1   3   13   13 |
  7. | 2   1    2    2 |
  8. | 2   2    8    8 |
  9. | 2   3   14   14 |
 10. | 2   1    2    2 |
     |-----------------|
 11. | 2   2    8    8 |
 12. | 2   3   14   14 |
 13. | 3   1    3    3 |
 14. | 3   2    9    9 |
 15. | 3   3   15   15 |
     |-----------------|
 16. | 3   1    3    3 |
 17. | 3   2    9    9 |
 18. | 3   3   15   15 |
     +-----------------+

As you say, the flexibility should -- and does -- extend to
expressions, so now the mystery is why Daniel made that statement.

Nick

On Sun, Mar 27, 2011 at 4:46 PM, Gordon Hughes <[email protected]> wrote:

> I am sorry for being a little pernickety about the implications of this
> discussion, but I would like to be clear about what is or is not possible in
> this respect.
>
> The original observation was that the code:
>
>        matrix input stdvalues (3700 6200...\3800 6350...\...
>        gen stded = stdvalues[year-1992,filestat]
>
> does not work.  Dan found that
>
>        gen int i = flpdyr-1992
>        gen int j = filestat
>        gen stded = stdvals[i,j]
>
> does work and Section 14.9 of my User Guide (Stata 10) says that
>
>        gen stded = stdvals[2,3]
>
> should also work.
>
> So one inference is that -generate- can access matrix sub-elements when
> either the sub-elements are either (a) constants, or (b) variables, but not
> if they are defined as (c) expressions.  This would be odd given the way
> most of Stata works.
>
> An alternative inference is that the problem lies in the variable type -
> i.e. if variables or variable expressions are used to specify sub-elements
> they must be of type int (or byte) but not float - and Dan's second piece of
> code is required to force the result of the expression to be of type int.
>
> Can anyone tells us what is the correct inference?
>
> Gordon Hughes
> [email protected]

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index