Benchmarks Online

Skip Navigation Links


Page One

Campus Computing News

EIS Status Report

UNT Data Encryption Recommendations

Coming this Fall: GroupWise 7

Importing a GroupWise Address Book into Apple’s Address Book Program

Summer Hours

Today's Cartoon

RSS Matters

The Network Connection

Link of the Month

WWW@UNT.EDU

Short Courses

IRC News

Staff Activities

Subscribe to Benchmarks Online
    

 Research and Statistical Support - University of North Texas

RSS Matters

Link to the last RSS article here: A Video Introduction to Creating Web Surveys with QSurvey - An Open Source Zope Application - Ed.

Stata 9: Introduction to Mata

By Patrick McLeod, ACS Research Consultant

The main difference between Stata 9 and previous versions of Stata is the addition of Mata, a matrix programming language native to Stata 9. Mata can be used for matrix calculations and can be used as an alternative to add specific functionality to Stata outside of programming new .ado files.

In this short introduction, we’ll look at how to verify your Mata settings and a simple example of how to employ Mata.

What’s Mata?

Mata is not designed to be a replacement for programming in Stata, i.e. writing new .ado files to add functionality to Stata. According to the Mata manual, Mata is appropriate for use in creating subroutines to run within .ado files.

Before you use Mata for the first time, it’s a good idea to see what your Mata settings are since they will affect how it behaves. This can be accomplished by invoking Mata in Stata by typing mata on the Stata command line and then typing mata query at the Mata prompt:

Invoking Mata in Stata

Stata/Mata will return the settings for the following components of Mata (from Stata 9 help files): matastrict sets whether declarations can be omitted inside the body of a program.  The default is off.  If matastrict is switched on, compiling programs that omit the declarations will result in a compile-time error. The next component, matalnum turns program line-number tracing on or off.  The default setting is off.  This setting modifies how programs are compiled.  Programs compiled when matalnum is turned on include code so that, if an error occurs during execution of the program, the line number is also reported.  Turning matalnum on prevents Mata from being able to optimize programs, so they will run more slowly.  Except when debugging, the recommended setting for this is off.

Mataoptimize turns compile-time code optimization on or off. The default setting is on.  Programs compiled when mataoptimize is switched off will run more slowly and, in some cases, much more slowly.  The only reason to set mataoptimize off is if a bug in the optimizer is suspected. Matafavor specifies whether, when executing code, Mata should favor conserving memory (space) or running quickly (speed).  The default setting is space.  Switching to speed will make Mata, in a few instances, run a little quicker but consume more memory.

Matacache specifies the maximum amount of memory, in kilobytes, that may be consumed before Mata starts looking to drop autoloaded functions that are not currently being used.  The default value is 400, meaning 400 kilobytes.  This parameter affects the efficiency with which Stata runs.  Larger values cannot hurt, but once matacache is large enough, larger values will not improve performance.  Matalibs sets the names and order of the .mlib libraries to be searched. Matalibs usually is set to "lmatabase;lmataado".  However it is set, it is probably set correctly, because Mata automatically searches for libraries the first time it is invoked in a Stata session.  If, during a session, you erase or copy new libraries along the adopath, the best way to reset matalibs is with the mata index command.  The only reason to set matalibs by hand is to modify the order in which libraries are searched. Matamofirst states whether .mo files or .mlib libraries are searched first.  The default is off, meaning libraries are searched first.

Now that we’ve reviewed our settings, let’s progress to a simple example of employing Mata.

Mata: A First Example With An Enhanced Ado File

(from Mata Manual M-1)

Let’s pretend that Stata cannot produce sums and we want to write a new command for Stata that will report the sum of a single variable. Here is how this could be done with an .ado file:

 

Program varsum
  Version 9.0
  Syntax varname [if] [in]
  Marksample touse
  Mata: calcsum(“’varlist’”, “’touse’”)
  Display as txt “ sum = “ as res r(sum)
End
 
Version 9.0
Mata:
Void calcsum(string scalar varname, string scalar touse)
{
   real colvector   x
 
   st_view(x, ., varname, touse)
   st_numscalar(“r(sum)”, colsum(x))
}

end

Invoking this program from within Stata will produce the sum of a variable with the syntax of varsum and then the variable name. On a closing note, I will be covering elementary Mata operations in my Intermediate Stata short course in Fall 2005. Until then, happy computing!

Links:


 

Return to top