Statalist The Stata Listserver


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

RE: st: right shift elements in a matrix


From   "Lei Xuan" <leix@martingale-research.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: right shift elements in a matrix
Date   Thu, 18 May 2006 12:49:19 -0500

Thanks Nick! It is very helpful!

I tried your suggestion -- 4-line code for general case, where I changed
rowsof(b) to colsof(b) and get the result correctly.

Phil and Feiveson,
Thanks for contributing your solutions. I will try them all.

Lei


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: Thursday, May 18, 2006 12:07 PM
To: statalist@hsphsun2.harvard.edu
Subject: RE: st: right shift elements in a matrix

I think we can all agree that -svmat- 
is not especially helpful here. 

Alternatively, consider some retro-chic
or retro-not-so-chic solutions. 

For a row vector with 4 elements, 

mat b = e(b) 
mat b = b[1,4], b[1,1..3] 

More generally, 

mat b = e(b) 
local R = rowsof(b) 
local r = `R' - 1 
mat b = b[1,`R'], b[1,1..`r'] 

Yet more generally, check out the ancient
-matselrc- published in STB-56 in 2000. 

. search matselrc 

will point to downloadable code. 
-matselrc- fixes the column names as 
well, which the interactive code
above does not do. 

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

Phil Schumm
 
> On May 18, 2006, at 9:55 AM, Lei Xuan wrote:
> > After logistic regression (-logit-), I have a parameter matrix e(b).
> > Let
> >     matrix b=e(b)
> >     N=e(df_m)+1 (number of independent variables plus 1)
> >
> >  we have
> >     b[1,1] b[1,2] ... b[1,N]
> >
> > I want to generate a new matrix with the same elements but  
> > different order
> > (shift one element right), that is,
> >
> >    b[1,N], b[1,1], b[1,2], ..., b[1, N-1]
> 
> 
> Here is a Mata-based solution to your immediate question:
> 
> 
> cap mata: mata drop shift_columns()
> mata:
> void shift_columns(string scalar old_mat, string scalar new_mat) {
>      real matrix       m
>      real rowvector    v
> 
>      m = st_matrix(old_mat)
>      v = cols(m), (1..cols(m)-1)
> 
>      st_matrix(new_mat, m[.,v])
>      st_matrixrowstripe(new_mat, st_matrixrowstripe(old_mat))
>      st_matrixcolstripe(new_mat, st_matrixcolstripe(old_mat)[v,.])
> }
> end
> 
> mat b = e(b)
> mata: shift_columns("b", "foo")
> mat li foo
> 
> 
> Note that the first line is required if you put this into a do-file  
> and run it multiple times within the same Stata session; without it,  
> the second run would throw an error because the function 
> shift_columns 
> () has already been defined.
> 
> I presume that this step is just part of a larger problem you are  
> trying to tackle.  If so, depending upon what that problem is, this  
> might not be the optimal solution.

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



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