Reputation: 799
I need to export a matrix of regression results from Stata to MATLAB. I have tried using the Stata command matwrite
without success (I get an unrecognized command
error). Here is that attempt:
...
*Regression 1
reg invlrevcrp_CAm071 lacres_CAm071 lrainm07 lrainm07sq ///
lannxt lannxtsq lrlanxtsq pkgamz if invlrevcrp_CAm071~=.
reg lrevcrp_CAm071 lacres_CAm071 lrainm07 lrainm07sq ///
lannxt lannxtsq lrlanxtsq lpkgamz
* Store results
mat coef=get(_b)
*Export to matlab
matwrite using "Z:\Thesis\data needed for 2007\matlabfile", ///
mat(coef) replace
...
I have had more success using the Stata xml_tab
which exports the matrix to Excel, which I can then import into MATLAB. However, xml_tab
gives me too much information. The matrix I want to export is simply the estimated coefficients from two regressions, without labels. xml_tab
exports everything related to the regression--the t-statistics, p-values, 95% conf. intervals, etc., including labels. Here is my code using this approach:
*===============================
* Regressions
*===============================
*Regression 1
reg invlrevcrp_CAm071 lacres_CAm071 lrainm07 lrainm07sq ///
lannxt lannxtsq lrlanxtsq pkgamz if invlrevcrp_CAm071~=.
reg lrevcrp_CAm071 lacres_CAm071 lrainm07 lrainm07sq ///
lannxt lannxtsq lrlanxtsq lpkgamz
* Store results
estimates store revCA1
*Regression 2
reg lcostcrp_CAm071 lacres_CAm071 lrainm07 lrainm07sq ///
lannxt lannit lannxtsq lannitsq lpkgf3 lwage if costcrp_CAm071>0
*Store results
estimates store cosCA1
*Export to excel
xml_tab revCA1 cosCA1, ///
save("Z:\Thesis\data needed for 2007\RegCoefs") replace
I am going through the xml_tab
help file to see if I can get what I want, but can anyone help with matwrite
or xml_tab
?
Upvotes: 1
Views: 17064
Reputation: 31
To write a matrix to a preexisting Excel file, look at putexcel
.
putexcel
will not export the matrix row and column names unless you tell it to.
help putexcel
Briefly, to export a Stata matrix:
change directory if necessary (example)
cd ""Z:\Thesis\data needed for 2007\"
set putexcel
to call your Excel file: putexcel
lets you specify the sheet in your Excel file that you would like to modify, which comes in handy if you have programmed an Excel sheet to transform results. If you do this, be sure to call the modify
option, rather than replace
: replace
will overwrite any preexisting work in your Excel file
putexcel set RegCoefs.xlsx, modify sheet(sheetname)
next call putexcel
, specifying at which cell ou would like to place your matrix
putexcel A1 = matrix(revCA1)
if successful, Stata should return the following message:
file RegCoefs.xlsx saved
Upvotes: 3
Reputation: 1037
I wrote an .ado program to do this, named mat2txt2. You can find it here: http://code.google.com/p/kk-adofiles/source/browse/#hg%2Fm I wrote this program to expand the capabilities of mat2txt.ado by Ben Jann and M Blasnik.
The program will export a matrix to a delimited text file (e.g., a comma-separated .csv file or tab-separated file). From there, you can easily pull the data into Excel or Matlab.
Upvotes: 5