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]

st: Re: Mata programming questions; STATA 12 (referencing to columns using stringnames)


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: Mata programming questions; STATA 12 (referencing to columns using stringnames)
Date   Thu, 26 Sep 2013 09:41:52 +0900

Wen Jun Jie wrote:

I have a few questions regarding Mata programming in Stata 12. The
questions are as follows:

- Is it possible to create a matrix that automatically increases in
size? (i.e. suppose I define a matrix of size 5 x 5, but after a while
I want a larger matrix, can I increase the size of my current matrix
without defining a new one or redefining the old one? Can I "just add"
something?)

- How should I update a value inside a matrix? Suppose the following code:
matrix define test = J(2,2,.)
matrix define test[1,1] = 5

This is how I update my matrix, but is this also the "correct" way to do this?

- Last, I would like to ask how the regress function can create
_beta["varname"] to refer to the coefficients. I would like that too!
How can I create that?

--------------------------------------------------------------------------------

1. You can append rows and columns to a matrix in Mata:

: A = 1, 2, 3

: A
       1   2   3
    +-------------+
  1 |  1   2   3  |
    +-------------+

: A = A \ 4, 5, 6

: A
       1   2   3
    +-------------+
  1 |  1   2   3  |
  2 |  4   5   6  |
    +-------------+

:

You can also do this noninteractively, say:

void function test() {
: void function test() {
> real matrix A
> A = J(2, 2, 1)
> A = A \ J(1, cols(A), 2)
> A
> }

: test()
       1   2
    +---------+
  1 |  1   1  |
  2 |  1   1  |
  3 |  2   2  |
    +---------+

:

2. You can refer to cells on the left-hand side of expressions in Mata:

: A = J(2, 2, .)

: A[1, 1] = 5

: A
[symmetric]
       1   2
    +---------+
  1 |  5      |
  2 |  .   .  |
    +---------+

:

All of this above is documented in Mata's help files and user manual entries.
Also, with an easily accessed interactive mode, you can try out various things
on your own to see whether they work as expected.

3.  Mata doesn't have a -regress()- function.  See
http://www.stata.com/help.cgi?m5_intro 
I assume that you mean the -regress- command in Stata.  Given that, I don't
really understand what you're asking to do.  You can refer to the regression
coefficients as either _b[] or _coef[], where the square brackets contain the
name of the regression coefficient, which is not necessarily the same as a
variable name.

. sysuse auto
(1978 Automobile Data)

. regress gear_ratio c.headroom i.foreign

[regression output omitted]

. display in smcl as text _b[headroom]
-.10046036

. display in smcl as text _coef[1.foreign]
.64646458

. display in smcl as text _b[_cons]
3.123375

Joseph Coveney

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index