Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Mata & Graphs


From   "Raphael Fraser" <[email protected]>
To   [email protected]
Subject   Re: st: Mata & Graphs
Date   Tue, 25 Jul 2006 18:44:27 -0500

Thanks Ben. I think I have solved the problem.

mata
st_matrix("tmpQ", Q) /*set the contents of tmpQ to be Q*/
stata("svmat tmpQ, names(x)") /*stores the columns of tmpQ as new
variables x1 and x2*/
st_matrix("tmpQ", J(0, 0, .)) /*deletes the matrix tmpQ*/
stata("twoway line x1 x2")
end

On 7/25/06, Ben  Jann <[email protected]> wrote:
Raphael wrote:
> I have the following matrix X, in Mata and would like to
> draw a twoway line graph using each column as a variable.
> How is this done?
>
>              1             2
>   +-----------------------------+
> 1 |  .8913041862             1  |
> 2 |  .8708690808             2  |
> 3 |  .8413558121             3  |
>   +-----------------------------+

Here is my solution:

mata:
void plot(
 real colvector y,
 real colvector x,
 | string scalar opts)
{
        real scalar n, N, Y, X

        n = rows(y)
        if (rows(x)!=n) _error(3200)
        N = st_nobs()
        if (N<n) st_addobs(n-N)
        st_store((1,n), Y=st_addvar("double", st_tempname()), y)
        st_store((1,n), X=st_addvar("double", st_tempname()), x)
        stata("twoway scatter " + st_varname(Y) + " " +
         st_varname(X) + ", " + opts)
        if (N<n) st_dropobsin((N+1,n))
        st_dropvar((Y,X))
}
end

mata: y = .8913041862 \ .8708690808 \ .8413558121
mata: x = 1 \ 2 \ 3
mata: plot(y, x)
mata: plot(y, x, "c(l)")

ben


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

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