Transcript Slide 1
STATA Third group training course in application of information and communication technology to production and dissemination of official statistics 10 May – 11July 2007 Gereltuya Altankhuyag, Lecturer/Statistician, UNSIAP [email protected] 7/21/2015 1 Basic commands of graphics The commands that draw graphs are: command description --------------------------------------------------------------------graph twoway scatterplots, line plots, etc. graph matrix scatterplot matrices graph bar bar charts graph dot dot charts graph box box-and-whisker plots graph pie pie charts other more commands to draw statistical graphs --------------------------------------------------------------------- 7/21/2015 2 Basic commands of graphics The commands that save a previously drawn graph, redisplay previously saved graphs, and combine graphs are: command description ---------------------------------------------------------------------graph save save graph to disk graph use redisplay graph stored on disk graph display redisplay graph stored in memory graph combine combine graphs into one ---------------------------------------------------------------------- 7/21/2015 3 Basic commands of graphics The commands for printing a graph are: command description ---------------------------------------------------------------------graph print print currently displayed graph set printcolor set how colors are printed graph export export .gph file to PostScript, etc. ---------------------------------------------------------------------- All graphs have extension “graph” 7/21/2015 4 1.5e-04 1.0e-04 0 5.0e-05 Density We can generate plots, e.g. a histogram, with STATA: hist pcexp which shows per capita expenditure distn as skewed 2.0e-04 Basic commands of graphics 0 7/21/2015 20000 40000 Per capita expenditure per year 60000 5 .8 .6 .4 0 .2 We can induce normality by transforming consumption on a log scale: gen logpcexp= ln(pcexp) hist logpce Density 1 Basic commands of graphics 7 7/21/2015 8 9 logpcexp 10 11 6 Basic commands of graphics graph bar (mean) pcexp indsave, over(region) title(Per capita expenditure and individuals' savings by region) 0 2,000 4,000 6,000 8,000 Per capita expenditure and individuals' savings by region Dhaka Chittagon mean of pcexp 7/21/2015 Khulna Ragfhahi mean of indsave 7 10 Household size To create a box plot of the household size vaiable in the hh.dat dataset: 15 20 Basic commands of graphics 0 5 graph box famsize 7/21/2015 8 Creation of “Do-files” and use of “Do-file Editor” Up until now we have used Stata interactively. We can do batch processing using Do Files. Do files – a text file containing commands and instruct Stata to execute the commands stored in that file. 7/21/2015 9 Creation of “Do-files” and use of “Do-file Editor” We can create Do Files using “New Do-file Editor” 7/21/2015 10 Creation of “Do-files” and use of “Do-file Editor” Do-file Editor has menu bar Do-file Editor has tool bar 7/21/2015 11 Creation of “Do-files” and use of “Do-file Editor” The do-file editor lets you submit several commands to Stata at once General steps of using the do-file: Click on the Do-file Editor to open do-file editor 7/21/2015 12 Creation of “Do-files” and use of “Do-file Editor” 7/21/2015 Then type the commands that you wish to submit to Stata 13 Creation of “Do-files” and use of “Do-file Editor” 7/21/2015 Then click on “Do current file” 14 Creation of “Do-files” and use of “Do-file Editor” If you wish to execute a subset of the lines in your do-files, highlight those lines and click “Do current file” button. 7/21/2015 15 Creation of “Do-files” and use of “Do-file Editor” Tool bar button “Run current file” is the same as “Do current file”, but Run suppressess output. It is unlikely that you will ever need to use “Run current file” button. 7/21/2015 16 Creation of “Do-files” and use of “Do-file Editor” You can put comments in your Do-file There are ways to include comments in a Dofile: 7/21/2015 Begin with asterisk * Begin with asterisk and forward slash */ Place the comment in /*…..*/ delimiters Place the comment after 2 forward slashes //….// Place the comment after 3 forward slashes ///…/// 17 Creation of “Do-files” and use of “Do-file Editor” 7/21/2015 18 Creation of “Do-files” and use of “Do-file Editor” Advantage of using /* */ as a comment indicator The /* */ comment can be put anywhere – at the end of a line, even at the middle of a line. Advantage of using * as a comment indicator The ‘*’ comment indicator can be used interactively. The /* */ can be used only in do-files. 7/21/2015 19 Creation of “Do-files” and use of “Do-file Editor” By default Stata assumes that each command is ended by the carriage return (cr) (ENTER key press). If a Syntax with commands is too long to fit in one line, we can handle it in 2 ways: Using #delimit Using comment signs 7/21/2015 20 Creation of “Do-files” and use of “Do-file Editor” using #delimit – each commandmust end with semicolon (;) 7/21/2015 21 Creation of “Do-files” and use of “Do-file Editor” using comment signs (*; */; /*..*/; //..//; ///…///) 7/21/2015 22 Creation of “Do-files” and use of “Do-file Editor” A do-file stops executing when: The end of the file is reached An exit is executed An error occurs If an error occurs, the remaining commands in the do-file are not executed 7/21/2015 23 Creation of “Do-files” and use of “Do-file Editor” Retrieve saved do-file: Save do-file: Click do-file editor File Open Type in your commands in do-file editor File Save or Save as Saved file takes “.do” extension 7/21/2015 24 Creating “log-files” Stata can record your session into a file called a “log file” Log file does not start automatically You must tell Stata to record your session By default, log files are created in a format called Stata Markup and Control Language (SMCL) 7/21/2015 25 Creating “log-files” To create log files using do-files: Type the followings: clear capture log close Specify the location of your log-file in your dofile: log using C:\intropov\logfiles\day2, replace Specify the start date and time of running do-file: di "Job started at $S_TIME on $S_DATE" 7/21/2015 26 Creating “log-files” Write all commands and comments accordingly Specify the finishing date and time of running do-file: di "Job ended at $S_TIME on $S_DATE“ Tell Stata that log is finished: log close Saved file takes “.log” or”smcl” extension Note: you can make changes in your do-file!!! 7/21/2015 27 Creating “log-files” To run do-file which contains log: Click on Edit of the do-file menu bar Click Select All: all commands and comments will be highlighted Click “Do current file” button on tool bar 7/21/2015 28 Creating “log-files” 7/21/2015 29 Creating “log-files” 7/21/2015 30 Creating “log-files” To open/retrieve log file click “begin log” button on toolbar 7/21/2015 31 Creating “log-files” Find you log-file name in relevant folder 7/21/2015 32 Creating “log-files” Log files have two extensions: 7/21/2015 filename.smcl – Stata produces this file when you open log filename.log – You can load into your word processor. Do-file uses this extension!!! 33 Creating “log-files” Choose the extension of your log-file by scrolling down Save as type: 7/21/2015 34 Creating “log-files” Then Stata log options window appears and choose “View existing file (read only)” The click OK button. 7/21/2015 35 Creating “log-files” Then you will see all your command records and outputs. 7/21/2015 36 Creating “log-files” Meaning Syntax To start a log (in smcl) log using filename To stop temporarily logging log off To resume log log on To stop logging and close the file log close To create filename.log translate filename.smcl filename.log 7/21/2015 37 Creating “log-files” Append to an existing log-file: log using filename, append Replace an existing log-file: log using filename, replace 7/21/2015 38 Use of “ado-files” Stata has two types of commands in terms of use: Commands built into Stata Ado-file commands An ado-file is an ASCII text file that contains a Stata program. Ado-files usually come with help files. 7/21/2015 39 Use of “ado-files” To determine your command: which command name which ci . which ci \\Unitednations\Stata9\ado\base\c\ci.ado *! version 3.3.15 17mar2005 Which summarize . which summarize built-in command: summarize 7/21/2015 40 Use of “ado-files” You can view ado-files: . viewsource ci.ado 7/21/2015 41 Use of “ado-files” To know where are ado-files on your PC: sysdir sysdir STATA: UPDATES: BASE: SITE: PLUS: PERSONAL: OLDPLACE: 7/21/2015 \\Unitednations\Stata9\ \\Unitednations\Stata9\ado\updates\ \\Unitednations\Stata9\ado\base\ \\Unitednations\Stata9\ado\site\ c:\ado\plus\ c:\ado\personal\ c:\ado\ 42 Use of “ado-files” To run ado-files: ado-filename varname Examples: ci pcexp gini pcexp lorenz pcexp 7/21/2015 43 Use of “ado-files” Examples: . ci pcexp Variable | Obs Mean Std. Err. [95% Conf. Interval] -------------+--------------------------------------------------------------pcexp | 519 6901.953 192.7895 6523.207 7280.698 . gini pcexp Gini coefficient .26204428 7/21/2015 44 Use of “ado-files” . lorenz pcexp 7/21/2015 45 Inputting data To input data into Stata, you can use: 7/21/2015 46 Inputting data edit and input Enter data from keyboad insheet Reads text (ASCII) files created by a spreadsheet infile (free format) Read unformatted ASCII (text) data infile (fixed format) or Read ASCII (text) data in fixed format with a infix (fixed format) dictionary xmlsave 7/21/2015 Allows datasets to be saved or used in XML and EXCEL format 47 Inputting data odbc (open darabase connectivity) Read data from ODBC source sdasave Read datasets in SAS SPORT format haver Read data in Haver Analytic fromat Stata-Transfer program 7/21/2015 48 Inputting data Suppose we have an ASCII text dataset as shown below: 7/21/2015 49 Inputting data Suppose we have a dataset countries.txt in c:\intropov\data\ folder Use the infile command with syntax infile variable-list using filename.raw In particular, enter cd c:\intropov\data infile y_a y_b y_c using countries.txt 7/21/2015 50 Inputting data When using the infile command for character (string) variables (eg.hhcode), we need to identify these variables. For instance infile y_a y_b y_c str30 domain using countries.txt For more details regarding infile, enter help infile1 7/21/2015 51 Inputting data Suppose we have a dataset try2.txt in c:\intropov\data folder with the data in specific fields Assumes last line is blank line 7/21/2015 52 Inputting data Use the infix command to input into Stata infix domain 1 hhcode 2 age 3-4 using try2.txt, clear Infix accepts an “if” statement, e.g., infix … if domain ==1 7/21/2015 53 Inputting data IMPORTING A WORKSHEET Suppose we have an Excel worksheet called countries2.xls also in the c:\intropov\data folder. Read the file with Excel, then SAVE AS a comma delimited CSV file. Enter insheet using countries2.csv, clear Note: Stata cannot read/import data directly from Excel!! 7/21/2015 54 Inputting data Thus, Stata can read text files with Infile (if the data in text is separated by spaces and does not have strings, or if strings are just one word, or if all strings are enclosed in quotes) Infix (fixed format text) Insheet (if text file was created by a spreadsheet or db program) 7/21/2015 55 Exporting data The command outfile save the data in ASCII; outsheet in a comma delimited format. File ► Export ►Comma or tabseparated data 7/21/2015 56 Inputting and exporting datasets There are third party programs, esp. Stat/Transfer and DBMS/COPY, that perform translations from one data format (e.g., Excel, SAS, SPSS, Stata) to another. 7/21/2015 57 Inputting and exporting datasets 7/21/2015 58 Inputting data There are 2 ways of creating a new dataset in Stata: 1. Use of Edit: 1.1 Open the STATA spreadsheet editor 7/21/2015 59 Inputting data 1.2 Enter data into the editor, when done close the editor. 1.3 Save dataset 7/21/2015 60 Inputting data 2. Use of input command 2.1 input hhcode pid age 2.2 enter values of variables 2.3 end 2.4 Save dataset 7/21/2015 61 Inputting data NOTE We can copy and paste values from a spreadsheet Data can be entered variable-by- variable or observation-by-observation When entering data variable-by-variable, use TAB; for observation-by-observation use ENTER key Missing data code is a period “.” 7/21/2015 62 Inputting data NOTE Numerical values SHOULD NOT include commas; else these are considered string. String variable values can be almost any combination of letters, numbers, symbols up to 80 characters long in Intercooled/ Small and up to 244 characters in STATA/SE. 7/21/2015 63 Summary of STATA session 7/21/2015 64 Summary Stata Files .log .do records commands and outputs contains a sequence of Stata commands for batch execution .dta data files .graph graphics files .ado for writing new commands 7/21/2015 65 Summary Basic Commands Interrupt Stata: Ctrl Break or Stop button Exit from Stata: type exit or from File menu, choose exit or clear Help Search 7/21/2015 66 Summary To inspect datasets: codebook: display contents of variables count: counts cases describe: display properties of variables summarize: descriptive stats list: show values tabulate: one or two way freq tables table: higher dim freq tables tabstat: tables with statistics Note: weigh is important!!!! 7/21/2015 67 Summary To create and change varaibles: generate/egen: create new variable replace: replace old values recode: change values by conditions label: label var (up to 80 chars) 7/21/2015 68 Summary To reorganize datasets: rename: change the name of a variable drop: eliminate variables or observations keep: keep variables or observations clone: copy of an existing variable sort: arrange into ascending order gsort: arrange in ascending or descending order 7/21/2015 69 Summary To reorganize datasets: append: append two datasets merge: merges two datasets reshape: converts dataset from wide to long and vs 7/21/2015 70 Summary Survey Data Analysis svy commands svyset svymean svytotal svytab syyprop svyreg 7/21/2015 71 Summary Importing Datasets Files must be in text delimited form (.txt) Use infile function to import: infile var1 var2 etc using filename.txt, clear Or the infix or insheet functions Or use StataTransfer software 7/21/2015 72 END OF STATA SESSION Please perform EXERCISE 4 7/21/2015 73