*Learning Mata by Example *Tutorial 1 *Highlight an example and then press the do icon and you will see the results in the results windows *Modify the code to see if you understand what is happening. *Highlight the help files under the example and press the do icon. Look at the online help for the commands and look *at the other options. ******************************************************************************************** Inputting Data * 2 ways of using Mata * between Mata and end *--------------Start Example 1 ----------------* mata printf("this is Mata \n") //mata command for printing to results window end *--------------End Example----------------------* help mf_printf **or between Stata code *--------------Start Example 2 ----------------* clear display "the next is a mata command" //Stata command mata: printf("this is Mata \n") //mata command for printing to results window display "that was a Mata command" //Stata command *--------------End Example----------------------* help mf_printf ********************************************************************************* manual input of matrices *column vector *--------------Start Example 3 ----------------* mata a=(1\2\3\4\5\6\7\8\9\10) a end *--------------End Example----------------------* help m1_first *row vector *--------------Start Example 4 ----------------* mata a=(1,2,3,4,5,6,7,8,9,10) a b=a' // the ' converts row vector to column vector b end *--------------End Example----------------------* help m1_first help m2_op_transpose *--------------Start Example 5 ----------------* mata a=(1,2) a b=(3,4) b c=(a\b) //combining the 2 matrices c end *--------------End Example----------------------* help m1_first *input strings *--------------Start Example 6 ----------------* clear mata //clears Mata matrices from memory mata a=("a","a","","b") a end *--------------End Example----------------------* ***************************************************************************Input from Stata - numbers only **(Note: the "make" variable in the auto dataset is a string and with the code below the resulting matrix **is coded as ".", a Mata matrix must be either a number or a string *--------------Start Example 7 ----------------* sysuse auto, clear set more off mata a=st_data(.,.) a end *--------------End Example----------------------* help mf_st_data *a selection of the dataset *--------------Start Example 8 ----------------* sysuse auto, clear set more off mata a=st_data(.,3) //3rd variable in dataset a b=st_data(.,"mpg") //variable "mpg" in dataset b c=st_data(.,("mpg","price")) //variable "mpg" and "price" in dataset c d=st_data((1::10),("mpg","price")) //variable "mpg" and "price" in dataset in obs 1 to 10 d end *--------------End Example----------------------* ** a view of the data, when you change the data in Stata the matrix changes and ** when you change the data in the matrix the value in Stata changes *--------------Start Example 9 ----------------* sysuse auto, clear set more off mata st_view(a, ., ("mpg", "displ", "weight")) a stata("replace mpg=1") //changes value in Stata a a[2,1]=100 //changes the value in row 2 and column 1 to 100 Remmember for matrix: matrix_name[row,column] end list mpg in 1/10 //to show that the value in Stata has changed *--------------End Example----------------------* help mf_st_view ***************************************************************************Input from Stata - strings only **If you want the "make" string variable in a Mata matrix *--------------Start Example 10 ----------------* sysuse auto, clear set more off mata a=st_sdata(., "make") a b=st_sdata(., 1) //notice the 1, this is the first variable in data set b end *--------------End Example----------------------* ****************************************************************************If you want to create a matrix *--------------Start Example 11 ----------------* mata a=J(3,3,100) a a=J(3,3,.) a a=J(3,3,"HI") //string matrix a end *--------------End Example----------------------* **other notes: ** // anything after // is ignored by mata ie. used for comments *--------------Start Example 12 ----------------* mata //matrix a=(1,1) //forming new matrix a //displaying new matrix end *--------------End Example----------------------* *******************end of Tutorial 1****************************