Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

st: How to return a Mata matrix in Stata


From   Alex Olssen <alex.olssen@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: How to return a Mata matrix in Stata
Date   Fri, 12 Aug 2011 21:51:55 +1200

Dear Statalisters,

Can anyone help me return some matrices in a Stata program that calls
Mata?  My plan was to use -st_mata()- to names for the matrices and
then try a -return matrix- call in Stata.  Any help is appreciated.
Hopefully once I get my head around Mata I will be able to help some
people. :)  My code is below.  You can paste it straight into the
do-file editor and run it.  The problems are in the two lines that are
commented out.

************************************************************
sysuse auto, clear
keep price length weight

capture program drop mytheta
program define mytheta, rclass
	version 11
	mata theta()
	display as txt " theta = " as res r(theta)
	// return matrix eigval = r(eigval)
	// return matrix eigvec = r(eigvec)
	return scalar theta = r(theta)
end

capture mata mata drop theta()	
mata
void theta()
{
	st_view(test = ., ., .)
	test_cov = variance(test)
	st_matrix("r(v)", test_cov)
	test_eigen = symeigensystem(test_cov, X = ., L = .)
	st_matrix("r(eigval)", X)
	st_matrix("r(eigvec)", L)
	st_numscalar("r(theta)", L[1]/sum(L))
}
end

mytheta
************************************************************


Kind regards,
Alex Olssen
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index