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

st: saving and loading matrices


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: saving and loading matrices
Date   Mon, 16 Aug 2004 10:04:34 -0400

At 17:21 15/08/2004, Oleksandr Shepotylo. wrote:
>Hi ,
>
>Is there a way to save and load matrices in text format?
>Suppose I run Stata and want to use matrix A in another program X.
>So, I want to save A in csv or txt form. Then I do some manipulations
>with A and want to load A back in Stata while keeping all my data unchanged.

and Roger replied...

To save a matrix to a text file, se -svmat- to copy a matrix to a dataset
in memory, and -outsheet- to write that dataset to a disk file. To load a
matrix from a text file, use -insheet- to read from the text file into a
dataset in memory, and -mkmat- to copy the dataset into a matrix. To find
more about -insheet- and -outsheet-, see -[R] insheet- and -[R] outsheet-.
To find more about -mkmat- and -svmat-, see the Programming manual under
-[P] matrix mkmat

But this does not really address Oleksandr's desire to load a matrix WHIILE KEEPING ALL MY DATA UNCHANGED.

Thankfully Bill Gould produced some sample code to do just this some time ago, when the -file- command was added to Stata 7. Way back when I lifted that code from -help file- and put it up on my Unix server's Stata, but never shared it via SSC. I have put the code up in SSC package matin4-matout4. They have some rough edges, but the example below shows precisely how
they may be used in the middle of a Stata session without disturbing the contents of the current dataset. Note that the matrix name is not preserved, so that one can read it in under another name (which might be very useful, if you're trying to compare two matrices which originally had the same name).


. ssc install matin4-matout4

(output suppressed)

. webuse auto
(1978 Automobile Data)

. reg price mpg rep78 headroom

Source | SS df MS Number of obs = 69
-------------+------------------------------ F( 3, 65) = 7.51
Model | 148497605 3 49499201.8 Prob > F = 0.0002
Residual | 428299354 65 6589220.82 R-squared = 0.2575
-------------+------------------------------ Adj R-squared = 0.2232
Total | 576796959 68 8482308.22 Root MSE = 2566.9

------------------------------------------------------------------------ ------
price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
------------- +----------------------------------------------------------------
mpg | -289.3462 62.53921 -4.63 0.000 -414.2456 -164.4467
rep78 | 670.8971 343.5213 1.95 0.055 -15.16241 1356.957
headroom | -300.0293 398.0516 -0.75 0.454 -1094.993 494.9346
_cons | 10921.33 2153.003 5.07 0.000 6621.487 15221.17
------------------------------------------------------------------------ ------

. mat vauto = e(V)

. matout4 vauto using vauto.mat,replace

. clear

. webuse grunfeld

. matin4 vvv using vauto.mat

. mat list vvv

symmetric vvv[4,4]
mpg rep78 headroom _cons
mpg 3911.1533
rep78 -8132.8048 118006.91
headroom 9349.2526 -2080.8639 158445.09
_cons -83616.961 -222518.75 -667292.49 4635420.1


Kit

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