Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: use mata loop to generate matrix

 From Matthew Baker To statalist@hsphsun2.harvard.edu Subject Re: st: use mata loop to generate matrix Date Sat, 21 May 2011 06:50:59 -0400

```Unless I've misunderstood the question, this seems to be a matter of
defining a matrix beforehand to fill the means into as the loop runs.
It also seems like XBM should really be XB in the example. For example
(with generated data beforehand so it runs and a phony V matrix):

**Begin example

clear all
set obs 667
forvalues i=1/4 {
gen xbm`i'=runiform()
}

mata
V=I(4)

N=1000
U=(invnormal(uniform(N,cols(V)))*cholesky(V)')
M_u=J(667,4,.)
for(k=1;k<=667;k++) {
XB=st_data(k,("xbm1", "xbm2", "xbm3","xbm4"))
Y_si=XB:+U
M_u[k,]=mean(Y_si)
}
end

**End

On Fri, May 20, 2011 at 4:02 PM, Zhi Su <su.zh@husky.neu.edu> wrote:
>
> I want to construct a simulated Y mean matrix that is a 667x4 matrix.
> XB is drawn from a dataset with 667 observations and four variables
> "xbm1", "xbm2", "xbm3","xbm4" . U is drawn from 4 normal distribution
> with a variance-covariance V.The draw is repeated 1000 times.  Here is
> what I do
>
> /*number of repetions*/
> N=1000
>
> /*Multiple draws from multivariate normal distribution with variance
> and covariance V */
> U=(invnormal(uniform(N,cols(V)))*cholesky(V)')
>
> for(k=1;k<=667;k++) {
>
> XB=st_data(k,("xbm1", "xbm2", "xbm3","xbm4"))
>
> Y_si=XBM:+U
>
> M_u=mean(Y_si)
> }
>
> I can get means of each simulated Y by looping the process from 1 to
> 667.  But I do not know how to put the mean row vectors into a 667X 4
> matrix.
> Anyone can give me some suggestions?
> Thanks!
> --
> Zhi Su
> 348 Holmes Hall
> Northeastern University
> 360 Huntington Avenue
> Boston, MA 02115
> Office:1-617-373-2316
> email:su.zh@husky.neu.edu
> *
> *   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/

--
Dr. Matthew J. Baker
Department of Economics
Hunter College and the Graduate Center, CUNY

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