Department of Mathematics and Statistics, University of Vaasa, Finland Texas A&M University, May June, 2013 As of May 17, 2013
Part I Stata Introduction
1 Stata Introduction Interface Commands Command syntax Immediate commands Stata help Loading data Stata working tips: directories Stata working tips: log file Stata working tips: do-file Stata resources
Interface Review window: Past comments Results window: Displays results Variables window: Displays variables and their labels Properties window: Displays data properties Command window: Commands are typed here
Commands Commands typed in the command windows are exercised immediately after hitting the return key: E.g. typing: display 5*2 produces in the Results window. display 2*5 10 Note: Stata commands are case sensitive!. Display 2 + 2 unrecognized command: r(199); Display
Commands scatter r1 rm, by(event) lfit r1 rm estimation period event period.05 0.05.1.02 0.02.04.02 0.02.04 Market return Merril Lynch & Co Inc Fitted values Graphs by Event/Estimation period
Command syntax Generally the commands are of the form [prefix:] command [if] [in] [weight] [, options] Brackets indicate optional parts. summarize r1 r2 if event == 1. summarize r1 r2 if event == 1 Variable Obs Mean Std. Dev. Min Max ---------+----------------------------------------------------- r1 62 -.0012754.0222433 -.0541516.0658915 r2 62.0016534.0291768 -.0677966.0754717
Command syntax. summarize r1 if event == 1, detail Merrill Lynch & Co Inc ------------------------------------------------------------- Percentiles Smallest 1% -.0541516 -.0541516 5% -.0371747 -.0524345 10% -.0319149 -.0413534 Obs 62 25% -.0110294 -.0371747 Sum of Wgt. 62 50% 0 Mean -.0012754 Largest Std. Dev..0222433 75%.0113636.0297398 90%.023166.0339623 Variance.0004948 95%.0297398.0472441 Skewness.0692 99%.0658915.0658915 Kurtosis 3.789862
Immediate commands Immediate commands turn Stata into a useful calculator. They do not affect data in memory. Usually have also a nonimmediate (computed from data) counterparts. End in the letter i (the converse is not true). Confidence interval of Merrill Lynch & Co Inc daily returns.. ci r1 Variable Obs Mean Std. Err. [95% Conf. Interval] ---------+-------------------------------------------------------- r1 127 -.0000973.0021306 -.0043136.0041191
Immediate commands Suppose that in a simulation study the empirical rejection rate at 0.05 level of the null hypothesis by a new test statistic was 0.071 in 1, 000 independent replications. Size problems with the new test? Immediate command for variable distributed as binomial cii #obs #succ [, ciib_options] cii 1000 71 -- Binomial Exact -- Variable Obs Mean Std. Err. [95% Conf. Interval] ---------+------------------------------------------------ 1000.071.0081215.0558646.0887164
Immediate commands Alternatively, using binomial test: Immediate form of binomial probability test bitesti #N #succ #p [, detail]. bitesti 1000 71.05 N Observed k Expected k Assumed p Observed p --------------------------------------------------------- 1000 71 50 0.05000 0.07100 Pr(k >= 71) = 0.002330 (one-sided test) Pr(k <= 71) = 0.998458 (one-sided test) Pr(k <= 30 or k >= 71) = 0.003607 (two-sided test)
Immediate commands For further information, see Stata help help immediate commands
Stata help To get help type: help command name For example, help ttest opens a new window, called viewer window See also the search command. Type: help search to learn more. You can also use the help menu.
Loading data Data can be loaded as a Stata file (.dta), various forms of text files and from various applications like Excel (Stata version 12). See, Menu: File > Import Files can be also loaded from the internet. Stata web-site has several example data sets. For example use http://www.stata-press.com/data/r12/ibm opens the file with comments. use http://www.stata-press.com/data/r12/ibm (Source: Yahoo! Finance)
Loading data. describe Contains data from http://www.stata-press.com/data/r12/ibm.dta obs: 494 Source: Yahoo! Finance vars: 5 12 Jan 2011 09:32 size: 9,880 -------------------------------------------------------------------- storage display value variable name type format label variable label -------------------------------------------------------------------- date float %td Date irx float %9.0g Annual return / 365 (percent) ibm float %9.0g Daily return (percent) spx float %9.0g Daily return (percent) t float %9.0g -------------------------------------------------------------------- Sorted by: date
Loading data Listing 5 first observations. list in 1/5 +--------------------------------------------------+ date irx ibm spx t -------------------------------------------------- 1. 02jan2003.0032603.. 1 2. 03jan2003.0032603 1.340452 -.0484035 2 3. 06jan2003.0032329 2.375989 2.247436 3 4. 07jan2003.0032055 2.883125 -.654462 4 5. 08jan2003.0032055-2.104648-1.408558 5 +--------------------------------------------------+
Stata working tips: directories It is a good idea to organize the project to directories. For example: Documents, Data, Results, Stata, etc. Directories and files can be managed from Stata with command: pwd to see the current directory mkdir to create a new directory dir or ls to list the names of the files cd to change a directory type to list file contents erase or rm to delete a file
Stata working tips: log file Output goes to the Results window. To keep a permanent record you can open a log file: log using filename, text replace The options text makes the file as a text file and replace specifies that the file will be overwritten if it exists.
Stata working tips: do-file Stata commands can be written into a text file, called do file. Stata has a build-in do-file Editor. New do-file can be opened by command doedit or Ctrl-9 (version 12, Ctrl-8 in earlier versions) or by clicking New Do-file Editor in the task bar or by selecting menu Window > Do-file Editor > New Do-file Editor. Crtl-d to run the whole do-file or mark by paint specific lines to run (Menu: Tools > Execute).
Stata working tips: do-file In a do-file everything followed // to the end of file is a comment (Note the exception, url http://) Everything between /* */ is comment. * in the first column indicates a comment line /// indicates line continuation. Text after /// is ignored, i.e., is comment
Stata working tips: do-file Sample do-file
Stata resources Menu: Home page: http://www.stata.com Net course: http://www.stata.com/netcourse/ Faq: http://www.stata.com/support/faqs/ Tutorials http://data.princeton.edu/stata/default.html http://www.ats.ucla.edu/stat/stata/ Manuals (version 11 and above): Help > PDF Documentation User written additions to Stata can be found by Stata prompt net from http://stata.com