Bookmark and Share

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: Errors "out of range" for a simple Mata function


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Errors "out of range" for a simple Mata function
Date   Tue, 20 Nov 2012 10:26:50 +0000

Thanks for the correction. The word "wrong" was itself wrong. I was
elevating my personal preference only to use views for data Ithat I
won't change to an implication that you can't do it, which was
incorrect.

Given

local macro generate
local macro varlist
local macro touse

as discussed to date in the thread

. mata : st_store(., "`generate'", "``touse''", rowsum(st_data(.,
tokens("`varlist'"), "``touse''")))

is I think a complete solution. However, I'd sympathise with anyone
who thought that overvalues conciseness. Also, using Mata as a
calculator in this way undermines Ly Tran's major purpose of using the
row sum problem as a vehicle for learning Mata.

Nick

On Tue, Nov 20, 2012 at 2:01 AM, Christopher Baum <[email protected]> wrote:

> Ly Tran:
>
> Nick Cox is correct in pointing out that you need to pass `generate' to your Mata function. But it is not necessary to use st_store() as he suggests. Your Mata code works with one small change:
>
> capture program drop testsum
> program testsum, rclass
> syntax varlist(numeric) [if] [in], GENerate(string) [FLOAT]
> qui generate `float' `generate' = .
> mata: tsum( "`varlist'", "`generate'")
> end
>
> mata
> void function tsum (string scalar varlist, string scalar newvar)
> {
> real matrix X, Z
> st_view(X = .,., tokens(varlist))
> st_view(Z =., ., newvar)
> Z[.,.] = rowsum(X)
> }
> end
>
> When an entire view matrix is the LHS of a statement, you must use the syntax shown, rather than the name of the view matrix, for the statement to update the view matrix (and the Stata variable(s)) and not create a new Mata object. In the help for st_view(), read the section "Cautions when using views 2:  Assignment" to see this point illustrated. It is also mentioned in the Mata chapter of ITSP below.
>
> . sysuse auto,clear
> (1978 Automobile Data)
>
> . testsum price mpg turn, gen(junk)
>
> . egen junk2=rowtotal(price mpg turn)
>
> . su junk*
>
>     Variable |       Obs        Mean    Std. Dev.       Min        Max
> -------------+--------------------------------------------------------
>         junk |        74    6226.203    2948.149       3354      15972
>        junk2 |        74    6226.203    2948.149       3354      15972
>
>
> One other thing: if you allow [if] and [in] in your ado-file, you should -marksample touse- and pass `touse' to Mata so that it can limit the observations being used in the Mata routine. Again, example code in ITSP.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index