# Getting Started In Data Analysis Using Stata

3y ago
41 Views
2.90 MB
63 Pages
Last View : 3d ago
Transcription

Getting Started in Data Analysisusing Stata(v. 6.0)Oscar Torres-Reynaotorres@princeton.eduDecember 2007http://dss.princeton.edu/training/

Stata Tutorial Topics What is Stata?Stata screen and general descriptionFirst steps: Setting the working directory (pwd and cd .) Log file (log using ) Memory allocation (set mem ) Do-files (doedit) Opening/saving a Stata datafile Quick way of finding variables Subsetting (using conditional “if”) Stata color coding systemFrom SPSS/SAS to StataExample of a dataset in ExcelFrom Excel to Stata (copy-and-paste, *.csv)Describe and summarizeRenameVariable labelsAdding value labelsCreating new variables (generate)Creating new variables from other variables (generate)Recoding variables (recode)Recoding variables using egenChanging values (replace)Indexing (using n and N) Creating ids and ids by categories Lags and forward values Countdown and specific valuesSorting (ascending and descending order)Deleting variables (drop)Dropping cases (drop if)Extracting characters from regular expressions MergeAppendMerging fuzzy text (reclink)Frequently used Stata commandsExploring data: Frequencies (tab, table) Crosstabulations (with test for associations) Descriptive statistics (tabstat)Examples of frequencies and crosstabulationsThree way crosstabsThree way crosstabs (with average of a fourth variable)Creating dummiesGraphs Scatterplot Histograms Catplot (for categorical data) Bars (graphing mean values)Data preparation/descriptive statistics(open a differentfile): inear Regression (open a different 101.pdfPanel data (fixed/random effects) (open a differentfile): ilevel Analysis (open a different 101.pdfTime Series (open a different seful sites (links only) Is my model OK? I can’t read the output of my model!!! Topics in Statistics Recommended booksPU/DSS/OTR

What is Stata? It is a multi-purpose statistical package to help you explore, summarize and analyze datasets. It is widely used in social science research.A dataset is a collection of several pieces of information called variables (usually arranged by columns). A variable can have one or several values (information forone or several cases).FeaturesLearningcurveSPSSSASStataJMP (SAS)RPython(Pandas)GradualPretty steepGradualGradualPretty steepSteepProgramming/Point-andProgramming point-andclickclickUser interfacePoint-andclickDatamanipulationStrongVery strongStrongStrongVery strongStrongData analysisVery strongVery strongVery strongStrongVery strongStrongGoodGoodVery goodVery goodExcellentGoodExpensive(perpetual,cost only withnew ual,cost only withnew amming ProgrammingOpen source Open source(free)(free)Free studentStudent disc.Student disc. version, 2014 Student disc.Released196819721985198919952008PU/DSS/OTR

Stata’s previous screensStata 10 and olderStata 11

Stata 12/13 screenVariables in dataset hereOutput hereHistory ofcommands, thiswindow?Files will besaved hereWrite commands hereProperty of eachvariable herePU/DSS/OTR

First steps: Working directoryTo see your working directory, typepwd. pwdh:\statadataTo change the working directory to avoid typing the whole path whencalling or saving files, type:cd c:\mydata. cd c:\mydatac:\mydataUse quotes if the new directory has blank spaces, for examplecd “h:\stata and data”. cd "h:\stata and data"h:\stata and dataPU/DSS/OTR

First steps: log fileCreate a log file, sort of Stata’s built-in tape recorder and where you can:1) retrieve the output of your work and 2) keep a record of your work.In the command line type:log using mylog.logThis will create the file ‘mylog.log’ in your working directory. You canread it using any word processor (notepad, word, etc.).To close a log file type:log closeTo add more output to an existing log file add the option append, type:log using mylog.log, appendTo replace a log file add the option replace, type:log using mylog.log, replaceNote that the option replace will delete the contents of the previousversion of the log.PU/DSS/OTR

First steps: memory allocationStata 12 will automatically allocate the necessary memory to open a file. It is recommended touse Stata 64-bit for files bigger than 1 g.If you get the error message “no room to add more observations ”, (usually in olderStata versions, 11 or older) then you need to manually set the memory higher. You can type, forexampleset mem 700mOr something higher.If the problem is in variable allocation (default is 5,000 variables), you increase it by typing, forexample:set maxvar 10000To check the initial parameters typequery memory

First steps: do-fileDo-files are ASCII files that contain of Stata commands to run specific procedures. It is highly recommended to usedo-files to store your commands so do you not have to type them again should you need to re-do your work.You can use any word processor and save the file in ASCII format, or you can use Stata’s ‘do-file editor’ with theadvantage that you can run the commands from there. Either , in the command window type:doeditOr, click on the icon here:You can write the commands, to run them select the line(s), and click on the last icon in the do-file windowCheck the following site for more info on do-files: http://www.princeton.edu/ otorres/Stata/

First steps: Opening/saving Stata files (*.dta)To open files already in Stata with extension *.dta, run Stata and you can either: Go to file- open in the menu, or Type use “c:\mydata\mydatafile.dta”If your working directory is already set to c:\mydata, just typeuse mydatafileTo save a data file from Stata go to file – save as or just type:save, replaceIf the dataset is new or just imported from other format go to file – save as orjust type:save mydatafile /*Pick a name for your file*/For ASCII data please see U/DSS/OTRPU/DSS/OTR

First steps: Quick way of finding variables (lookfor)You can use the command lookfor to find variables in a dataset, for example youwant to see which variables refer to education, type:lookfor educ. lookfor educvariable nameeducstorage displaytypeformatbyte%10.0gvaluelabelvariable labelEducation of R.lookfor will look for the keyword ‘educ’ in the variable name and labels. Youwill need to be creative with your keyword searches to find the variables youneed.It always recommended to use the codebook that comes with the dataset tohave a better idea of where things are.PU/DSS/OTRPU/DSS/OTR

First steps: Subsetting using conditional ‘if’Sometimes you may want to get frequencies, crosstabs or run a model just for aparticular group (lets say just for females or people younger than certain age).You can do this by using the conditional ‘if’, for example:/*Frequencies of var1 when gender 1*/tab var1 if gender 1, column row/*Frequencies of var1 when gender 1 and age 33*/tab var1 if gender 1 & age 33, column row/*Frequencies of var1 when gender 1 and marital status single*/tab var1 if gender 1 & marital 2 marital 3 marital 4, column row/*You can do the same with crosstabs: tab var1 var2 *//*Regression when gender 1 and age 33*/regress y x1 x2 if gender 1 & age 33, robust/*Scatterplots when gender 1 and age 33*/scater var1 var2 if gender 1 & age 33“if” goes at the end of the command BUT before the comma that separatesthe options from the command.PU/DSS/OTRPU/DSS/OTR

First steps: Stata color-coded systemAn important step is to make sure variables are in their expected format.Stata has a color-coded system for each type. Black is for numbers, red is for text or stringand blue is for labeled variables.Var2 is a string variable even though yousee numbers. You can’t do any statisticalprocedure with this variable other thansimple frequenciesFor var1 a value 2 has thelabel “Fairly well”. It is still anumeric variableVar3 is a numeric You can do any statisticalprocedure with this variableVar4 is clearly a string variable.You can do frequencies andcrosstabulations with this butnot statistical procedures.PU/DSS/OTRPU/DSS/OTR

First steps: starting the log file using the menuLog files help you to keep a record of your work, and lets you extract output. When usingextension *.log any word processor can open the file.Click on “Save as type:” right below ‘File name:” andselect Log (*.log). This will create the file *.log whichcan be read by any word processor or by Stata (go to File– Log – View). If you save it as *.smcl (Formatted Log)only Stata can read it. It is recommended to save the logfile as *.log

From SPSS/SAS to StataIf you have a file in SAS XPORT format you can use fduse (or go to file-import).If your data is already in SPSS format (*.sav) or SAS(*.sas7bcat). Two options:Option A) Use Stat/Transfer, see .pdfOption B) You can use the command usespss to read SPSS files in Stata or the command usesasto read SAS files.For SPSS and SAS, you may need to install it by typingssc install usespssssc install usesasOnce installed just typeusespss using “c:\mydata.sav”usesas using “c:\mydata.sas7bcat”Type help usespss or help usesas for more details.For ASCII data please see U/DSS/OTRPU/DSS/OTR

Example of a dataset in Excel.Variables are arranged by columns and cases by rows. Each variable has more than one valuePath to the file: http://www.princeton.edu/ otorres/Stata/Students.xlsPU/DSS/OTR

From Excel to Stata using copy-and-pasteIn Excel, select and copy the data you want. Then, in Stata type edit in the command line to open the data editor.Point the cursor to the first cell, then right-click, select ‘Paste’.

Saving data as Stata fileChange the working directorySaving as Stata datafileData will besaved in thisfolderNOTE: You can also use the menu, go toFile - Save AsSaving as Stata datafile

Excel to Stata (using insheet) step 1Another way to bring excel data into Stata is by saving the Excel file as *.csv (commaseparated values) and import it in Stata using the insheet command.In Excel go to File- Save as and save the Excel file as *.csv:You may get the following messages, click OK andYES Go to the next page PU/DSS/OTR

Excel to Stata (insheet using *.csv, - step 2)From *.csv using the menuFrom *.xls(x) using the menuimport delimited "H:\students.csv", clearinsheet using "H:\students.csv", clearimport excel "H:\Students.xlsx", sheet(“Sheet1") firstrow clear

Command: describeTo get a general description of the dataset and the format for each variable typedescribe. describeContains data from 30vars:1429 Sep 2009 17:12size:2,580 (99.9% of memory free)storagevariable enderstudent statusmajorcountryagesataveragescoreg eheightinnewspaperread eintbytebytebytevaluelabelvariable labelIDLast NameFirst NameCityStateGenderStudent StatusMajorCountryAgeSATAverage score (grade)Height (in)Newspaper readershipType help describe for more information PU/DSS/OTR

Command: summarizeType summarize to get some basic descriptive statistics. summarizeVariableObsMeanStd. genderstudentsta smajorcountryage00003025.2sataveragesco eheightinnewspaperr 3230996757Zeros indicate string variablesType help summarize for more information Use ‘min’ and ‘max’ values to check for avalid range in each variable. For example,‘age’ should have the expected values(‘don’t know’ or ‘no answer’ are usuallycoded as 99 or 999)PU/DSS/OTR

Exploring data: frequenciesFrequency refers to the number of times a value is repeated. Frequencies are used to analyzecategorical data. The tables below are frequency tables, values are in ascending order. In Stata usethe command tab varname.variable. tab ’ provides a raw count of each value. In this case 10students for each major.‘Percent’ gives the relative frequency for each value. Forexample, 33.33% of the students in this group are econmajors.‘Cum.’ is the cumulative frequency in ascending order ofthe values. For example, 66.67% of the students areecon or math majors.variable. tab .6790.00100.00Total30100.00‘Freq.’ Here 6 students read the newspaper 3 days aweek, 9 students read it 5 days a week.‘Percent’. Those who read the newspaper 3 days a weekrepresent 20% of the sample, 30% of the students in thesample read the newspaper 5 days a week.‘Cum.’ 66.67% of the students read the newspaper 3 to 5days a week.Type help tab for more details.PU/DSS/OTR

Exploring data: frequencies and descriptive statistics (using table)Command table produces frequencies and descriptive statistics per category. For more info and a list ofall statistics type help table. Here are some examples, typetable gender, contents(freq mean age mean score). table gender, contents(freq mean age mean 523.227.278.7333382The mean age of females is 23 years, for males is 27. The mean score is 78 for females and 82 formales. Here is another example:table major, contents(freq mean age mean sat mean score mean readnews). table major, contents(freq meanage mean sat meanscore e)mean(read .279.885.14.45.34.9PU/DSS/OTR

Exploring data: crosstabsAlso known as contingency tables, crosstabs help you to analyze the relationship between two ormore categorical variables. Below is a crosstab between the variable ‘ecostatu’ and ‘gender’. We usethe command tab var1 var2The first value in a cell tells you the number ofobservations for each xtab. In this case, 90respondents are ‘male’ and said that theeconomy is doing ‘very well’, 59 are ‘female’and believe the economy is doing ‘very well’Options ‘column’, ‘row’ gives you thecolumn and row percentages.var1var2. tab ecostatu gender, column rowKeyfrequencyrow percentagecolumn percentageStatus ofNat'l EcoGender of RespondentMaleFemaleTotalVery well9060.4014.335939.607.92149100.0010.85Fairly well33750.3053.6633349.7044.70670100.0048.80Fairly badly13939.9422.1320960.0628.05348100.0025.35Very badly5729.849.0813470.1617.99191100.0013.91Not 26100.001,373100.00100.00The second value in a cell gives you rowpercentages for the first variable in the xtab.Out of those who think the economy is doing‘very well’, 60.40% are males and 39.60% arefemales.The third value in a cell gives you columnpercentages for the second variable in the xtab.Among males, 14.33% think the economy isdoing ‘very well’ while 7.92% of females havethe same opinion.NOTE: You can use tab1 for multiple frequencies or tab2 torun all possible crosstabs combinations. Type help tab forfurther details.PU/DSS/OTR

Exploring data: crosstabs (a closer look)You can use crosstabs to compare responses among categories in relation to aggregateresponses. In the table below we can see how opinions for males and females divergefrom the national average. tab ecostatu gender, column rowKeyfrequencyrow percentagecolumn percentageStatus ofNat'l EcoGender of RespondentMaleFemaleTotalVery well9060.4014.335939.607.92149100.0010.85Fairly well33750.3053.6633349.7044.70670100.0048.80Fairly badly13939.9422.1320960.0628.05348100.0025.35Very badly5729.849.0813470.1617.99191100.0013.91Not .000.0062845.74100.0074554.26100.00RefusedTotalAs a rule-of-thumb, a margin of error of 4 percentage points can beused to indicate a significant difference (some use 3).For example, rounding up the percentages, 11% (10.85) answer ‘verywell’ at the national level. With the margin of error, this gives a rangeroughly between 7% and 15%, anything beyond this range could beconsidered significantly different (remember this is just anapproximation). It does not appear to be a significant bias betweenmales and females for this answer.In the ‘fairly well’ category we have 49%, with range between 45%and 53%. The response for males is 54% and for females 45%. Wecould say here that males tend to be a bit more optimistic on theeconomy and females tend to be a bit less optimistic.If we aggregate responses, we could get a better picture. In the tablebelow 68% of males believe the economy is doing well (comparing to60% at the national level, while 46% of females thing the economy isbad (comparing to 39% aggregate). Males seem to be more optimisticthan females.RECODE ofecostatu(Status ofNat'l Eco)Gender of 19636.3631.2134363.6446.04539100.0039.26Not 5.74100.0074554.26100.001,373100.00100.00recode ecostatu (1 2 1 "Well") (3 4 2 "Bad") (5 6 3 "Not sure/ref"), gen(ecostatu1) label(eco)PU/DSS/OTR

Exploring data: crosstabs (test for associations)To see whether there is a relationship between two variables you can choose a number oftests. Some apply to nominal variables some others to ordinal. I am running all of themhere for presentation purposes.tab ecostatu1 gender, column row nokey chi2 lrchi2 V exact gamma taubLikelihood-ratio χ2(chi-square)X2(chi-square)Goodman & Kruskal’s γ (gamma)Cramer’s VKendall’s τb (tau-b). tab ecostatu1 gender, column row nokey chi2 lrchi2 V exact gamma taubEnumerating sample-spacestage 3: enumerations stage 2: enumerations stage 1: enumerations RECODE ofecostatu(Status ofNat'l Eco)combinations:1160Gender of RespondentMaleFemaleFisher’s exact .65Bad19636.3631.2134363.6446.04539100.0039.26Not .81620.15630.30950.1553Pr 0.000Pr 0.000Pearson chi2(2)likelihood-ratio chi2(2)Cramér's VgammaKendall's tau-bFisher's exact ASE 0.050ASE 0.0260.000– For nominal data use chi2, lrchi2, V– For ordinal data use gamma and taub– Use exact instead of chi2 whenfrequencies are less than 5 across thetable.X2(chi-square) tests for relationships between variables. The nullhypothesis (Ho) is that there is no relationship. To reject this we need aPr 0.05 (at 95% confidence). Here both chi2 are significant. Thereforewe conclude that there is some relationship between perceptions of theeconomy and gender. lrchi2 reads the same way.Cramer’s V is a measure of association between two nominal variables. Itgoes from 0 to 1 where 1 indicates strong association (for rXc tables). In2x2 tables, the range is -1 to 1. Here the V is 0.15, which shows a smallassociation.Gamma and taub are measures of association between two ordinalvariables (both have to be in the same direction, i.e. negative to positive,low to high). Both go from -1 to 1. Negative shows inverse relationship,closer to 1 a strong relationship. Gamma is recommended when thereare lots of ties in the data. Taub is recommended for square tables.Fisher’s exact test is used when there are very few cases in the cells(usually less than 5). It tests the relationship between two variables. Thenull is that variables are independent. Here we reject the nul

First steps: starting the log file using the menu . Log files help you to keep a record of your work, and lets you extract output. When using extension *.log any word processor can open the file.

Related Documents:

Biacore T200 Getting Started 28-9840-98 Edition AB 5 Biacore T200 Getting Started Biacore T200 Getting Started Introduction This Getting Started handbook is designed as a self-study guide to introduce you to the basic operations of BiacoreTM T200, Biacore T200 Control Software and Biacore T200 Evaluation Software.

Categorical Data Analysis Getting Started Using Stata Scott Long and Shawna Rohrman cda12 StataGettingStarted 2012‐05‐11.docx Getting Started Using Stata – May 2012 – Page 2 Getting Started in Stata Opening Stata When you open Stata, the screen has seven key parts (This is Stata 12. Some of the later screen shots .

Getting Started with Oracle Data Integrator Getting Started 12c (12.2.1.3.0) E96509-02 March 2019 Oracle Data Integrator Getting Started This document provides instructions on how to

Getting Started applies to the "PCS 7 Engineering Toolset V 6.0". Preface Process Control System PCS 7, Getting Started - Part 1 iv A5E00164244-01 Guide to the Manual Getting Started explains the individual steps required to create the "color_gs" project. You will find the most important background information required to

Getting Started with SIMOTION SCOUT TIA Getting Started Valid as of Version 4.5 11/2016 Preface Fundamental safety instructions 1 Getting Started with SIMOTION SCOUT TIA 2 Prepare the configuration 3 Create a project 4 Create SIMOTION device and configure online communication 5 Start SIMOTION SCOUT TIA 6 Download the project to the target system 7

6 – ABSYNTH 5 – Getting Started 1.2 The ABSYNTH 5 Documentation 1.2.1 In this Manual What you are holding in your hands right now is the Getting Started Manual which will give you an overview of ABSYNTH 5’s main features and functions. This Getting Started Manual is divided into four parts:

Time Matters 10.0 - New User Guide 8 Starting the Application Getting Started Getting Started Getting Started Getting Started

Getting Started with ArcGIS Pro 12. The ArcGIS Pro Interface ArcGIS Pro uses a horizontal ribbon across the top of the application window to display and organize functionality into a series of tabs. Getting Started with ArcGIS Pro 13 A New Project Getting Started with ArcGIS Pro 14.