Example: air traffic controller

Data Analysis Declare data with Stata Cheat Sheet TIME ...

Data AnalysisCheat Sheetwith StataFor more info, see Stata s reference manual ( )Tim Essam Laura Hughes us @StataRGIS and @flaneuseksinspired by RStudio s awesome Cheat Sheets ( )updated May 2021CC BY : we are not affiliated with Stata . But we like rep78 variable to be an indicator price the third category of rep78 to be the base categoryregress price ib(3).rep78specify base indicatorfvsetcommand to change basefvset base frequent rep78set the base to most frequently occurring category for mpg as a continuous variable and specify an interaction between foreign and mpgregress price # variable as continuous#create a squared mpg term to be used in regressionregress price mpg # rep78 as an indicator; omit observations with rep78 == 2regress price io(2).rep78omit a variable or indicator##regress price ## all possible interactions with mpg (mpg and mpg2)specify factorial interactionsDESCRIPTIONCATEGORICAL VARIABLES identify a group to which an observation belongsINDICATOR VARIABLES denote whether something is true or falseTFCONTINUOUS VARIABLES measure somethingDeclare datatsline spotplot time series of sunspotsxtset id yeardeclare national longitudinal data to be a panelgenerate lag_spot = a new variable of annual lags of sunspotstsreport report time -series aspects of a datasetxtdescribereport pa

plot time series of sunspots xtset id year declare national longitudinal data to be a panel generate lag_spot = L1.spot create a new variable of annual lags of sunspots tsreport report time-series aspects of a dataset xtdescribe report panel aspects of a dataset xtsum hours summarize hours worked, decomposing standard deviation into between and

Tags:

  Time

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Data Analysis Declare data with Stata Cheat Sheet TIME ...

1 Data AnalysisCheat Sheetwith StataFor more info, see Stata s reference manual ( )Tim Essam Laura Hughes us @StataRGIS and @flaneuseksinspired by RStudio s awesome Cheat Sheets ( )updated May 2021CC BY : we are not affiliated with Stata . But we like rep78 variable to be an indicator price the third category of rep78 to be the base categoryregress price ib(3).rep78specify base indicatorfvsetcommand to change basefvset base frequent rep78set the base to most frequently occurring category for mpg as a continuous variable and specify an interaction between foreign and mpgregress price # variable as continuous#create a squared mpg term to be used in regressionregress price mpg # rep78 as an indicator; omit observations with rep78 == 2regress price io(2).rep78omit a variable or indicator##regress price ## all possible interactions with mpg (mpg and mpg2)specify factorial interactionsDESCRIPTIONCATEGORICAL VARIABLES identify a group to which an observation belongsINDICATOR VARIABLES denote whether something is true or falseTFCONTINUOUS VARIABLES measure somethingDeclare datatsline spotplot time series of sunspotsxtset id yeardeclare national longitudinal data to be a panelgenerate lag_spot = a new variable of annual lags of sunspotstsreport report time -series aspects of a datasetxtdescribereport panel aspects of a datasetxtsum hourssummarize hours worked, decomposingstandard deviation into between andwithin componentsarima spot, ar(1/2) fit an autoregressive model with 2 lags xtreg ln_w ## ttl_exp, fe vce(robust)fit a fixed-effects model with robust standard errorsxtline ln_wage if id <= 22, tlabel(#3)

2 Plot panel data as a line plotsvydescribereport survey-data detailssvy: mean age, over(sex)estimate a population mean for each subpopulationsvy: tabulate sex heartatkreport two-way table with tests of independencesvy, subpop(rural): mean ageestimate a population mean for rural areastsset time , yearlydeclare sunspot data to be yearly time seriesTIME SERIES webuse sunspot, clearPANEL / LONGITUDINAL webuse nlswork, clearSURVEY DATA webuse nhanes2b, clearsvyset psuid [pweight = finalwgt], strata(stratid) Declare survey design for a datasetsvy: reg zinc ## female weight ruralestimate a regression using survey weightsstset studytime, failure(died) Declare survey design for a datasetSURVIVAL Analysis webuse drugtr, clearstsumsummarize survival- time datastcox drug agefit a Cox proportional hazards modeltscollap carryforwardtsspellcompact time series into means, sums, and end-of-period valuescarry nonmissing values forward from one obs.

3 To the nextidentify spells or runs in time seriesUSEFUL ADD-INSpwmean mpg, over(rep78) pveffects mcompare(tukey)estimate pairwise comparisons of means with equal variances include multiple comparison adjustmentwebuse systolic, clearanova systolic druganalysis of variance and covariancettest mpg, by(foreign)estimate t test on equality of means for mpg by foreigntabulate foreign rep78, chi2 exact expectedtabulate foreign and repair record and return chi2 and Fisher s exact statistic alongside the expected valuesprtest foreign == test of proportions ksmirnov mpg, by(foreign) exact Kolmogorov Smirnov equality-of-distributions testranksum mpg, by(foreign)equality tests on unmatched data (independent samples)By declaring data type, you enable Stata to apply data munging and Analysis functions specific to certain data typesTIME-SERIES x t- 1L2. 2-period lag x t- x t+1F2. 2-period lead x t+ x t-x t- 1D2.

4 Difference of difference xt-xt 1-(xt 1-xt 2) difference x t-xt- 1S2. lag-2 (seasonal difference) xt xt 2logit foreign headroom mpg, orestimate logistic regression and report odds ratiosregress price mpg weight, vce(robust)fit ordinary least-squares (OLS) model on mpg, weight, and foreign, apply robust standard errorsprobit foreign turn price, vce(robust)estimate probit regression with robust standard errorsrreg price mpg weight, genwt(reg_wt)estimate robust regression to eliminate outliersregress price mpg weight if foreign == 0, vce(cluster rep78)regress price only on domestic cars, cluster standard errorsbootstrap, reps(100): regress mpg /* */ weight gear foreignestimate regression with bootstrappingjackknife r(mean): sum mpg jackknife standard error of sample meanExamples use (sysuse auto, clear) unless otherwise notedSummarize dataStatistical testsEstimation with categorical & factor variablesdisplay _b[length] display _se[length]return coefficient estimate or standard error for lengthfrom most recent regression modelmargins, dydx(length)return the estimated marginal effect for lengthmargins, eyex(length)return the estimated elasticity for lengthpredict yhat if e(sample)create predictions for sample on which model was fitpredict double resid, residualscalculate residuals based on last fitted modeltest headroom = 0test linear hypotheses that headroom estimate equals zerolincom headroom - lengthestimate linear combination (headroom - length)regress price headroom lengthUsed in all postestimation examplesmore details at price mpg weight, star( )

5 Return all pairwise correlation coefficients with sig. levelscorrelate mpg pricereturn correlation or covariance matrixmean price mpgestimates of means, including standard errorsproportion rep78 foreignestimates of proportions, including standard errors for categories identified in varlistratio price/mpgestimates of ratio, including standard errors total priceestimates of totals, including standard errorsci mean mpg price, level(99)compute standard errors and confidence intervalsstem mpgreturn stem-and-leaf display of mpgsummarize price mpg, detailcalculate a variety of univariate summary statisticsfrequently used commands are highlighted in yellowunivar price mpg, boxplotcalculate univariate summary with box-and-whiskers plotssc install univarreturns e-class information when post option is usedType help regress postestimation plotsfor additional diagnostic plotshettesttest for heteroskedasticityestat vifreport variance inflation factorovtesttest for omitted-variable biasdfbeta(length)calculate measure of influencervfplot, yline(0)

6 Plot residuals against fitted valuesplot all partial-regression leverageplots in one graphavplotsResidualsFitted valuespricempgpricerep78priceheadroompri ceweightsome are inappropriate with robust SEsDiagnostics2 Postestimation3 Fit models1commands that use a fitted modelstores results as -classrerereResults are stored as either -class or-class. See Programming Cheat Sheetrerrrrrreeee0100200 Number of sunspots195018501900420420197019801990id 1id 2id 3id 4420wage relative to inflationBlinder Oaxaca decompositionADDITIONAL MODELS xtline plottsline plotinstrumental variablesivregressivreg2principal components analysispcafactor analysisfactorcount outcomespoisson nbregcensored datatobitbuilt-in Stata commandregression discontinuityrddynamic panel estimatorxtabond xtdpdsys propensity score matchingteffects psmatch synthetic control analysissynthoaxacassc install ivreg2difference-in-differencedidregress user-writtenfor Stata 13: ci mpg price, level (99)ProgrammingCheat Sheetwith StataFor more info, see Stata s reference manual ( )Tim Essam Laura Hughes us @StataRGIS and @flaneuseksinspired by RStudio s awesome Cheat Sheets ( )updated May 2021CC BY : we are not affiliated with Stata .

7 But we like IT ALL TOGETHER sysuse auto, cleargenerate car_make = word(make, 1)levelsof car_make, local(cmake)local i = 1local cmake_len : word count `cmake'foreach x of local cmake { display in yellow "Make group `i' is `x'" if `i' == `cmake_len' { display "The total number of groups is `i'" } local i = `++i' }define the local i to be an iteratortests the position of the iterator, executes contents in brackets when the condition is trueincrement iterator by onestore the length of local cmake in local cmake_lencalculate unique groups of car_make and store in local cmakepull out the first word from the make variablesee also capture and scalar _rcStata has three options for repeating commands over lists or values: foreach, forvalues, and while. Though each has a different first line, the syntax is consistent:Loops: Automate repetitive tasksANATOMY OF A LOOPsee also whilei = 10(10)5010, 20, 30.

8 I = 10 20 to 5010, 20, 30, ..i = 10/5010, 11, 12, ..ITERATORSDEBUGGING CODEset trace on (off )trace the execution of programs for error checkingforeach x of varlist var1 var2 var3 { command `x', option}open brace must appear on first line temporary variable used only within the loopobjects to repeat overclose brace must appear on final line by itselfcommand(s) you want to repeatcan be one line or local macro notationFOREACH: REPEAT COMMANDS OVER STRINGS, LISTS, OR VARIABLESFORVALUES: REPEAT COMMANDS OVER LISTS OF NUMBERS display 10display length("Dr. Nick")display length("Dr. Hibbert")When calling a command that takes a string, surround the macro name with x in "Dr. Nick" "Dr. Hibbert" { display length ( "` x '" ) }LISTS sysuse " ", cleartab rep78, missingsysuse " ", cleartab rep78, missingsame repeat the same command over different arguments:foreach x in { sysuse "`x'", clear tab rep78, missing }STRINGS summarize mpgsummarize weight foreach in takes any list as an argument with elements separated by spaces foreach of requires you to state the list type, which makes it fasterforeach x in mpg weight { summarize `x' }foreach x of varlist mpg weight { summarize `x' }must define list typeVARIABLESUse display command to show the iterator value at each step in the loopforeach x in|of [ local, global, varlist, newlist, numlist ] { Stata commands referring to `x' }list types.

9 Objects over which the commands will be repeatedforvalues i = 10(10)50 { display `i' }numeric values over which loop will runiteratorAdditional programming resourcesinstall a package from a Github repositorynet install package, from ( ) download all examples from this Cheat Sheet in a Sublime text for Stata 11 15adolistList/copy user-written ado-filesado updateUpdate user-written ado-filesssc install adolistThe estout and outreg2 packages provide numerous flexible options for making tables after estimation commands. See also putexcel and putdocx RESULTS esttab using , replace plain seexport summary table to a text file, include standard errorsoutreg2 [est1 est2] using , see replaceexport summary table to a text file using outreg2 syntaxesttab est1 est2, se star(* ** ** ) label create summary table with standard errors and labelsAccess & save stored r- and e-class objects4mean pricereturns list of scalars, macros,matrices, and functionssummarize price, detailreturns a list of scalarsreturn listereturn listerMany Stata commands store results in types of lists.

10 To access these, use return or ereturn commands. Stored results can be scalars, macros, matrices, or a temporary copy of active dataframepreserverestore temporary copy to point last preservedrestorecreate a new variable equal toaverage of pricegenerate p_mean = r(mean)scalars:e(df_r) = 73e(N_over) = 1e(k_eq) = 1e(rank) = 1e(N) = 73scalars:..r(N) = 74r(sd) = (mean) = (Var) = a new variable equal toobs. in estimation commandgenerate meanN = e(N)Results are replaced each time an r-class / e-class command is calledset restore points to test code that changes datacreate local variable called myLocal with thestrings price, mpg, and lengthlocal myLocal price mpg lengthlevelsof rep78, local(levels)create a sorted list of distinct values of rep78, store results in a local macro called levelssummarize `myLocal'summarize contents of local myLocaladd a ` before and a ' after local macro name to callPRIVATE available only in programs, loops, or do-filesLOCALS local varLab.


Related search queries