Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: How to return a Mata matrix in Stata
From
Tirthankar Chakravarty <[email protected]>
To
[email protected]
Subject
Re: st: How to return a Mata matrix in Stata
Date
Fri, 12 Aug 2011 03:10:24 -0700
Once you have -st_matrix()-ed your -r()- variables from Mata to within
the Stata -program-, you do not need to re-return- them from within
the -program-. You should simply -return add- like in the code below:
************************************************************
clear*
sysuse auto, clear
keep price length weight
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
capture program drop mytheta
program define mytheta, rclass
version 11
mata theta()
ret li
display as txt " theta = " as res r(theta)
return add
end
mytheta
return list
************************************************************
T
On Fri, Aug 12, 2011 at 2:51 AM, Alex Olssen <[email protected]> wrote:
> 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/
>
--
Tirthankar Chakravarty
[email protected]
[email protected]
*
* 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/