Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

Re: st: Re: Defining a matrix in ml program

 From Maarten Buis To statalist@hsphsun2.harvard.edu Subject Re: st: Re: Defining a matrix in ml program Date Mon, 11 Mar 2013 10:00:55 +0100

> Cyrus Levy wrote:
>
> In the ml program below, I want to define a matrix based on parameter
> equations and use that matrix later in lnL. But "ml check" command
> returns an error in the line where I define the matrix MT. I am
> wondering if it is possible or not at all to create matrices in Stata
> based on ml parameters in this context. If yes, what is wrong with my
> code and how can I fix it?
>
> [snip]

On Mon, Mar 11, 2013 at 2:34 AM, Joseph Coveney wrote:
> It looks as if you're trying to assign a dataset variable to a matrix cell.  I
> believe that the variables created by -mleval- from a vector are
> constant-valued, so you can choose the first row (first observation) of data
> from the variable and plug that as a scalar into the matrix cell.  Try changing
> the line of code that defines the matrix from
>
>   matrix define `MT' = [`eta1', `eta2'\ `eta3', `eta4']
>
> to
>
>   matrix define `MT' = (`eta1'[1], `eta2'[1] \ `eta3'[1], `eta4'[1])

To give a bit more background: The mistake Cyrus made was in the use
of -mleval-. By default -mleval- create a new variable containing the
linear combination of the coefficients and the associated variables.
Even if the equation on parameter for a constant it will by default
still create variable that contains that constant for all
observations. Joseph's solution is to let -mleval- create such
variables, but use the first observation of each of these variables
when creating the matrix. Now you need to ask: what will happen when
someone accidentally specified a variable list for the 4th equation,
or when the first observation happens to be a missing values for one
of the variables included in the model? A more efficient and safer way
to do that is to tell -mleval- that these are scalars and thus not
create those variables in the first place:

tempvar b1x b0 sigma
tempname eta1 eta2 eta3 eta4
mleval `b1x' = `b', eq(1)
mleval `b0' = `b', eq(2)
mleval `sigma' = `b', eq(3)
mleval `eta1' = `b', eq(4) scalar
mleval `eta2' = `b', eq(5) scalar
mleval `eta3' = `b', eq(6) scalar
mleval `eta4' = `b', eq(7) scalar

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   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/