Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: Re: replacing a scalar from mat

From   "Steve Stillman" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: Re: replacing a scalar from mat
Date   Wed, 13 Jul 2005 14:18:18 +1200

Hi Rafa.  I ran into a similar problem just the other day.  I believe a solution is to use the following slightly awkward syntax.

mat A=e(b)
scalar s = A[1,colnumb(A,"x2")]

The colnumb function returns the column number of a matrix that has a label matching the name in quotes.


-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of R.E. De Hoyos
Sent: Wednesday, July 13, 2005 11:30 AM
To: [email protected]
Subject: st: Re: Re: replacing a scalar from mat


Thanks for your answer.

Your suggestion works fine. However I still face the row and column name 
problem when I try to replace a the scalar (or 1x1 matrix) into another 
vector. I should've explained myself better. The problem is the following. 

reg z1 x1 x2
mat A=e(b)

reg z2 x1 x2
mat B=e(b)

I want to know how much of the difference between z1 and z2 is explained by 
the difference in parameter x2 (a similar exercise like the Oaxaca 
decomposition). Therefore I get the parameter of x2 from mat A:

mat X2 = A["y1","x2"]

The second step is to replace this scalar (or 1x1 matrix) into mat B. To do 
so, I have to let Stata know the name of the variable--column--in mat B that 
has to be replaced with mat X2. My first though would be:

mat B["y1","x2"] = X2["y1","x2"]

However this syntax is not valid. The second thing I tried--following your 
advice--was to create a scalar from X2:

scalar x2 = X2[1,1]

Again I tried to replace mat B with scalar x2:
mat B["y1","x2"] = x2

And it didn't work either. To replace an element of a matrix you have to use 
numbers of rows and columns a not the names of them (mat B[1,2] = x2 works 
fine). Perhaps there is a strong reason for that but I cannot see it. In any 
case the problem was solved by assigning a number to each element of the 
independent variables:

local indv x1 x2
local i=1
foreach k in `indv' {
    local `k' = `i'
    local i=`i'+1
mat X`x1' = A[1,`x1']
mat B[1,`x1'] = X`x1'

I still wonder why Stata does not accept rows and columns names?

Best regards,


----- Original Message ----- 
From: "Michael Blasnik" <[email protected]>
To: <[email protected]>
Sent: Tuesday, July 12, 2005 9:33 PM
Subject: st: Re: replacing a scalar from mat

> If you know that you are only extract one row and column, why not
> mat B = e(b)
> mat B1 = B["y1","eq1:x1"]
> scalar s=B1[1,1]
> Michael Blasnik
> [email protected]
> ----- Original Message ----- 
> From: "R.E. De Hoyos" <[email protected]>
> To: <[email protected]>
> Sent: Tuesday, July 12, 2005 12:48 PM
> Subject: st: replacing a scalar from mat
>>I am trying to get a scalar from a matrix (vector). Within a multiple 
>>equations framework (-mlogit-) I want to get one of the parameters 
>>estimated. After running -mlogit- I tried the following:
>> mat B = e(b)
>> scalar s = B["y1","eq1:x1"]
>> The following error appears:
>> matrix operators that return matrices not allowed in this context
>> r(509);
>> However if I try one of the following, there is no error reported:
>> mat A = B["y1","eq1:x1"]
>> scalar s = B[1,1]
>> Using the rows and columns names of the matrix is important since these 
>> scalars will be replaced into a second vector of estimated parameters.
>> Thanks,
>> Rafa
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index