Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: AW: RE: AW: Big set of 1x1 matrices into scalar - how to do fast?


From   "Schnepf S.V." <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: AW: RE: AW: Big set of 1x1 matrices into scalar - how to do fast?
Date   Mon, 22 Jun 2009 16:13:34 +0100

Many thanks, Austin - very helpful and clear. Also good to see how to program that in Mata.

The command line
qui replace result=`=el(c`i',1,1)' in `i'
is very elegant and solves the problem in less than a second!

Sylke

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Austin Nichols
Sent: 22 June 2009 14:27
To: [email protected]
Subject: Re: st: AW: RE: AW: Big set of 1x1 matrices into scalar - how to do fast?

Sylke V. Schnepf:
You are generating and summarizing unnecessarily, so fixing that will
speed things up considerably.  Doing the calculations with one big
matrix in Mata will not, probably, unless you can put the calculations
needed to make the thousands of individual matrices in Mata as well:

clear all
sysuse auto
forv c=1/10 {
 mat b=nullmat(b), `=uniform()'
}
mat li b
forv i=1/74 {
 forv c=1/10 {
  mat Xi`i'=nullmat(Xi`i'), `=uniform()'
 }
}
g result=.
set rmsg on
forvalues i = 1(1)74 {
mat c`i'= b*Xi`i''
capture drop c
quietly gen c= el(c`i',1,1)
quietly sum c
quietly replace result=r(mean) in `i'
drop c
}
su result
replace result=.
forv i = 1/74 {
 mat c`i'= b*Xi`i''
 qui replace result=`=el(c`i',1,1)' in `i'
}
su result
replace result=.
mata
r=(74,0,.)
st_view(r, ., ("result"))
b=st_matrix("b")
X=J(0,10,.)
for (i=1;i<=74;i++) X=X\st_matrix(sprintf("Xi%1.0f",i))
r[.,.]=X*b'
end
su result
set rmsg off


On Mon, Jun 22, 2009 at 5:46 AM, Martin Weiss <[email protected]> wrote:
>
> <>
>
>
> I did this in the old matrix language, but you should seriously look into
> Mata:
> http://www.stata.com/meeting/fnasug08/baum_StataMata.beamer.FNASUG08.pdf
>
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected]
> [mailto:[email protected]] Im Auftrag von Schnepf S.V.
> Gesendet: Montag, 22. Juni 2009 11:43
> An: [email protected]
> Betreff: st: RE: AW: Big set of 1x1 matrices into scalar - how to do fast?
>
> Many thanks Martin. Stata matsize limitation does not let me run your
> proposal since I have too many observations.
> If svmat could be used in the way, that the value of the 1x1 matrix could be
> written into row `I' of an existing variable instead of creating for each
> matrix a new variable, then the problem could be overcome. But I do not find
> a code for doing so, that does not use egen or sum - and both of those are
> just too time consuming.
>
> Many thanks again for your help!
> Sylke
>
>
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Martin Weiss
> Sent: 19 June 2009 17:55
> To: [email protected]
> Subject: st: AW: Big set of 1x1 matrices into scalar - how to do fast?
>
>
> <>
>
> Why no tdo this in one fell swoop?
>
>
> *************
> clear*
>
> /*fake matrices */
> matrix input b = /*
>  */ (1,8,7,6,8,1,32,4,3,2)
> matrix input Xi1 = /*
>  */ (3\3\3\3\3\3\3\3\3\3)
> matrix input Xi2 = /*
>  */ (9\3\1\3\3\2\3\3\3\4)
> matrix input Xi3 = /*
>  */ (9\3\4\3\10\2\3\3\3\0)
>
> /*construct one big
>  matrix */
> forv i=1/3{
>        mat Xi=(nullmat(Xi),Xi`i')
> }
>
> /*multiply in one fell swoop */
> mat c=b*Xi
> matrix d=c'
> svmat d
>
> list, noo
> *************
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected]
> [mailto:[email protected]] Im Auftrag von Schnepf S.V.
> Gesendet: Freitag, 19. Juni 2009 18:32
> An: [email protected]
> Betreff: st: Big set of 1x1 matrices into scalar - how to do fast?
>
> Dear Statalist users,
>
>        forvalues i = 1(1)3641 {
>        mat c`i'= b*Xi`i''
>        capture drop c
>        quietly gen c= el(c`i',1,1)
>        quietly sum c
>        quietly replace result=r(mean) in `i'
>        drop c
>        }
>
> For a large set of individuals (here 3641), I have to multiply two vectors,
> with b and Xi`I' being a 1x10 vector. As a result I receive for each
> individual a 1x1 matrix, just containing 1 value.
> I now would like to produce one single scalar variable, that for each `I'
> row contains the number of c`I'.
>
> I have found a way how to do that, given above. But my simple solution is
> incredibly slow.
>
> Could you help?
> Many thanks
> Sylke V. Schnepf
>
> Ps. Many thanks for the help on the program for programming a weighted
> regression, very helpful.

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



*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index