Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Stata Commands Cheat Sheet, Cheat Sheet of Introduction to Econometrics

Cheat sheet on data processing, data transformation, data visualization, plotting, data analysis and programming with stata 15

Typology: Cheat Sheet

2020/2021

Uploaded on 04/27/2021

michaelporter
michaelporter 🇺🇸

4.4

(27)

287 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Processing
with Stata 15 Cheat Sheet
For more info see Stata's referenc.e manual (stata.com)
F2 -121-" Jttons
describe data
Ctrl + 8
Ctrl + 9
open a new .do file
Ctrl + D
open the data editor
clear
delete data in memory highlight text in .do file,
then ctrl + d executes it
in the command line
rll = t4A�0 D P
PgUp PgDn serali through previous commands
Tab autocompletes vanable name after typing part
cls clear the console (where results are displayed)
pwdl
print current (working) directory
cd "C\Program Files (x86)\Stata13"
change working directory
dir
display filenames in working directory
dir".dta
List all Stata data in working directory
.cauture log close---------t
close the log on any existing do files
log using "myDoFile.txr, replace
create a new log file to record your work and results
search mdesc 1 _ ,,
find the package mdesc to mstall ra e :I'--nands that
ssc i nstall mdesc �- �o
install the package mdesc; needs to be done once
lmp9rt:pat
sysuse auto, clear } fo ma exarrpes re
load system data (Auto data) u,e ttie auto dataset
use ·yourStataFile.dta ·, clear
load a dataset from the current directory . ed
import excel ·yourSpreadsheetxlsx·, r-·.
•; sheet("Shee,1") cellrange(A2:H11) firstrow
import an Excel spreadsheet
import delimited "yourFile.csv·, /*
"/ rowrange(Z:11) colrange(1:8) varnames(Z)
import a .csv file
web use set ·olll)S://githl,1><lC>ff'/G�ntortstmrr.,,nog1raw/ma:ter/OayZJO•,.,·
webuse "wb_indicators_lon
set web-based directory and load data from the web
Basic Syntax
Ali Stata commands have the same fermat (syntax):
[by varlist1:] commond [varlist2] [ =exp] [if exp] [in range) [weight] [using filename] Loptions]
._________, �--� ._________.
-do
--- -. '--
command
• e :nplE- Yt'E "'i1r.:: • on ed ,... __
vrrist1 bysort rep78. summarize price if foreign == O & price <= 9000, deta:I " wc _. � oh.. ,,..'":i a-d �a
To find out more about any command -like what options it takes -type help command
Arithmetic
add (numbers) &
+combine (strings) and
! or-not
or ! =] not
-Cli'= equa! < less than ual
'lhle
< = less than or equa! to
> greater than
-subtract
" multiply if fo<eign !� 1 & price : 10000 > = greater or equa I to
rf fore�n 1: 1 ( pnce : 10000
/ divide"
" raise to a power
Ex lare Data
_g_escribe make price
display variable type, fermat.
and any value/variable labels
count
count if price > 5000
number of rows (observations)
Can be combined wIth logie
ds, has(type string)
lookfor '1n.·
search far variable types.
vanable name. or variable label
isid mpg
check if mpg un1quely
identifies the data
codebook make price
overv1ew of vanable type, stats.
number of missing/unique values
summarize make price mpg
print summary statistics
(mean. stdev. min. max)
far variables
inspect mpg
show histogram of data.
number of missing or zero
observations
histogram mpg, freguency
plot a histogram of the
distribution of a variable
IN_THl [
browse or Ctrl + 8
open the data editor nge
�-----,,u bo/ -
!ist make pnce if price > 10000 & tmissing(pnce) gist .. 1co'T1pact formJ
list the make and price far observations with price > $10,000
.display price[4]
display the 4th observation in price; only works on single values
gsort price mpg (a:.cend r,g) gsort -price -mpg (de$Cendingl
sort in arder. first by price then miles per gallon
duplicates report assert price!=.
finds ali duplicate values in each variable verify trum of claim
levelsof rep78
display the uruque values far rep78
�� ·��r'.i%l•r:-� ._
Stata has 6 data types, and data can also be missing:
ru2..da!a tme/false WQ!ds .r:ll..l!Ilbill
m1ssmg byte string int long float double
To convert between numbers & strings:
gen foreign'String = string(foreigo)
tostring foreign, gen(foretgnStnng)
.d.ecode foreign. gen(forégnString) ·1·
·1·
"foreign'
gen or<? g-, ,L,..,e -= real(fore g�" "';, ì"
destrlng fo '? gnStr !'19. gen(fo '? gnNums" .. J ·1•
encode 'c"t ':i,: '::i gen(fc,,, gnNum,; ._) "foreign"
recast double mpg
generic way to convert between types
" 7nacie fo, rep7.:.
[ uC -• • b -n:,p,;; �rrl
1.abul ate rep 78, mi 1ge n(repairRecord)1
one-way table: number of rows with each value of rep78
tabulate rep78 foreign, mi
two-way table: cross-tabulate number of observations
tor each combination of rep78 and foreign
_bys_ort rep78: tabulate foreign
far each value of rep78, apply the command tabulate foreign
tabstat price weight mpg, by(foreign) stat(mean sd n)
create compact table of summary statistics -sia:s
i = ç,
���.--'---,
table foreign, contents(mean price sd price) f(%9 Zfc) row
create a flexible table of summary statistics
collapse (mean) price (max) mpg, by(foreign)-ep .::e'?. c.;;-�
calculate mean price & max mpg by car type (foreign)
generate mpgSo = mpg"Z gen byte lowPr = price < 4000
create a new variable. Useful also tor creating binary
variables based on a condition (generate byte)
generate id = _n bysort rep78: gen repairldx = _n
_n creates a running index of observations in a group
generate totRows = _N bysort rep78: gen repairTot = _N
_N aeates a running count of the total observations per group
pctile mpgQuartile = mpg, nq = 4
create quartiles of the mpg data
egen meanPrice = mean(price), by(foreign) -help egen
calculate mean price far each group in forergn rrore opoo-,s
updated June 2016
pf3
pf4
pf5

Partial preview of the text

Download Stata Commands Cheat Sheet and more Cheat Sheet Introduction to Econometrics in PDF only on Docsity!

Data Processing

with Stata 15 Cheat Sheet

For more info see Stata's referenc.e manual (stata.com)

F2 - 121 � -" � Jttons

describe data

Ctrl + 8

Ctrl + 9

open a new .do file

Ctrl + D

open the data editor

clear

delete data in memory

highlight text in .do file,

then ctrl + d executes it

in the command line

rll = � t4A�0 D P

PgUp PgDn serali through previous commands

Tab autocompletes vanable name after typing part

cls clear the console (where results are displayed)

pwdl

print current (working) directory

cd "C\Program Files (x86)\Stata13"

change working directory

dir

di splay filenames in working directory

dir".dta

List all Stata data in working directory

.cauture log close---------t

close the log on any existing do files

log using "myDoFile.txr, replace

create a new log file to record your work and results

search mdesc (^) 1 _ ,,

find the package mdesc to mstall ra e :I'--nands that

ssc i nstall mdesc �-^ �o

install the package mdesc; needs to be done once

lmp9rt:pat

sysuse auto, clear

load system data (Auto data)}^ fou,e ttie auto dataset^ ma^ exarrpes re

use ·yourStataFile.dta·, clear

load a dataset from the current directory. ed

import excel ·yourSpreadsheetxlsx·, r-·. •; sheet("Shee,1") cellrange(A2:H11) firstrow

import an Excel spreadsheet

import delimited "yourFile.csv·, /*

"/ rowrange(Z:11) colrange(1 :8) varnames(Z)

import a .csv file

web use set ·olll)S://githl,1>ff'/G�ntortstmrr.,,nog1raw/ma:ter/OayZJO•,.,·

webuse "wb_indicators_long·

set web-based directory and load data from the web

Basic Syntax

Ali Stata commands have the same fermat (syntax):

[by varlist1:] commond [varlist2] [=exp] [if exp] [in range) [weight] [using filename] Loptions]

._________, �--� ._________.

�-do

command

  • e :nplE- Yt'E "'i1r.:: • on ed ,...__

vrrist1 bysort rep78. s ummarize^ price^ if foreign == O & price <= 9000, deta:I^ "^ wc _.^ � oh.. ,,..

'":i a-d �a

To find out more about any command - like what options it takes - type hel (^) p command

Arithmetic

add (numbers) &

+ combine (strings)

and

! or- not

or

! (^) =] not

-Cli'= equa!

< less than

ual

'lhle

< = less than or equa! to

  • subtract > greater than

" multiply^

if fo<eign !� 1 & price : 10000

> = greater or equaI to

rf fore�n 1 : 1 ( pnce : 10000

/ divide "

" raise to a power

Ex lare Data

_g_escribe make price

display variable type, fermat.

and any value/variable labels

count

count if price > 5000

number of rows (observations)

Can be combined wIth logie

ds, has(type string)

lookfor '1n.·

search far variable types.

vanable name. or variable label

isid mpg

check if mpg un1quely

identifies the data

codebook make price

overv1ew of vanable type, stats.

number of missing/unique values

summarize make price mpg

print summary statistics

(mean. stdev. min. max)

far variables

inspect mpg

show histogram of data.

number of missing or zero

observations

histogram mpg, freguency

plot a histogram of the �

distribution of a variable

IN_THl [

browse or Ctrl + 8

open the data editor

ng e

�----^ - ,,u bo/ - €

!ist make pnce if price > 10000 & tmissing(pnce) gist .. 1co'T1pact formJ

list the make and price far observations with price > $10,

.display price[4]

display the 4th observation in price; only works on single values

gsort price mpg (a:.cend r,g) gsort -price -mpg (de$Cendingl

sort in arder. first by price then miles per gallon

duplicates report assert price!=.

finds ali duplicate values in each variable verify trum of claim

levelsof rep

display the uruque values far rep

�� ·��r'.i%l•r:-� ._

Stata has 6 data types, and data can also be missing:

ru2..da!a tme/false WQ!ds .r:ll..l!Ilbill

m1ssmg byte string int long float double

To convert between numbers & strings:

gen foreign'String = string(foreigo)

tostring foreign, gen(foretgnStnng)

.d.ecode foreign. gen(forégnString)

"foreign'

gen or<? g-, ,L,..,e - = real(fore g�" "';, ì"

destrlng fo '? gnStr !'19. gen(fo '? gnNums" ..J ·1•

encode 'c"t ':i,: '::i gen(fc,,, gnNum,; ._) "foreign"

recast double mpg

generic way to convert between types

� " 7nacie fo,�rep7.:.

[ uC - • • b - n:,p,;; �rrl

1.abulate rep78, mi

ge n(repairRecord)

1

one-way table: number of rows with each value of rep

tabulate rep78 foreign, mi

two-way table: cross-tabulate number of observations

tor each combination of rep78 and foreign

_bys_ort rep78: tabulate foreign

far each value of rep78, apply the command tabulate foreign

tabstat price weight mpg, by(foreign) stat(mean sd n)

create compact table of summary statistics -^ sia:s

i = � ç,

���.--'---,

table foreign, contents(mean price sd price) f(%9 Zfc) row

create a flexible table of summary statistics

collapse (mean) price (max) mpg, by(foreign)- ep .::e'?. c.;;-�

calculate mean price & max mpg by car type (foreign)

generate mpgSo = mpg"Z gen byte lowPr = price < 4000

create a new variable. Useful also tor creating binary

variables based on a condition (generate byte)

generate id = _n bysort rep78: gen repairldx = _n

_n creates a running index of observations in a group

generate totRows = _N bysort rep78: gen repairTot = _N

_N aeates a running count of the total observations per group

pctile mpgQuartile = mpg, nq = 4

create quartiles of the mpg data

egen meanPrice = mean(price), by(foreign) - help^ egen

calculate mean price far each group in forergn � rrore opoo-,s

updated June 2016

Data Transformation

with Stata 15 Cheat Sheet

For more info see Stata's reference manual (stata.com)

Select Parts of Data {Subsetting)

:;ELE-- '.:'E. C (OLUMNS

drop make

remove the 'make' variable

keep make price

apposite of drop; keep only variables 'make' and 'price'

r TE'"' SPE- F C. tl, • <;

drop if mpg < 20 drop in 1/

drop observations based on a conditìon 0eft)

or rows 1-4 (right)

keep i n 1/

apposite of drop; keep only rows 1-

keep if inrange(price, 5000, 10000)

keep values of price between $5.000 - $10.000 CTnclusive)

keep if inlist(make, "Honda Accord", "Honda Civic·. ·subaru")

keep the specified values of make

sample 25

sample 25% of the observations in the dataset

(use set seed # command tor reproducible sampling)

Cl!ANG[ (OLUMN NAM[S

.ren.ame (rep78 foreigin) (repairRecord carType)

rename one or multiple variables

CHt �E R V VAL lb

replace price = 5000 if price < 5000

replace alt values of price that are less than $5,000 with 5000

recode price (O/ 5000 = 5000)

change all prices less than 5000 to be $5,

recode foreign (O= 2 "US")(1 = 1 'Not US"), gen(

f

oreign2)

change the values and value labels then store in a new

variable. foreign

EP '-" E ,/'I .., G IALUt:,

mvdecode alt, mv(9999) " 1 .. , " "''

replace the number 9999 with missìng value in alt variables

mvencode _ali, mv(9999) ""., • e � _

replace missing values with the number 9999 for alt variables

Value labels map string descriptions to numbers. They allow the

underlying data to be numeric (making logica! tests simpler)

while also connecting the values to human-understandable text

.l.s!.bel define mylabel O ·us· 1 "Not US•

label .ltalues foreign mylabel

define a label and apply it the values in foreign

label !ist

list alt labels with1n the dataset

note: data note here

piace note m dataset

Reshape Data

webuse set https //g,thub.corn/GeoCerner/StataTra rt'lg/raw/mast.erfJay2/Data

webuse ·co�eeMaize.dta" load demo dataser

MELT DATA (WIDE - LONG)

re•hape var ables start ng un qtF. !1^ create: 'lE, var Jble wh eh captu�s

"! E' � "rat : •'ie r':; r •� (C -- -.,....,..é,

reshape long coffee@ maìze@, i(country) j(year)- new variable

convert a wide dataset to long

TIOY DATASETS W10E

m elt

cast

LoNG (Tiov) have each obser-

..,, ,.,.... -,•• -- vation in ìts own

row and each

varìable in ìts own

CAST DATA (LONG - WIDE) When datasets are

tìdy, they have a

c o n s i s t e nr.

standard fermat

that is easier iO

manipufate and

analyze.

what v. be create riew varaDite

create: 'll='o\ va iables n,:irned un que d wTth the year adae,

co"ee-01 mace2U'::. "var ab1€ (key to the co:urnn arn..

� r1r

reshape wide - ..,e,. ::.'\ 1(rnur·ry) j(year)

convert a long dataser to wide

L

xpose, clear vamame

transpose rows and columns of data, cfearing the data and saving

old column names as a new variable called ·_varname·

ADDING (APPENDING) NEW DATA

webuse coffeeMa ze2 dta, clear

save coffeeMa1ze2 dta, re piace load oemo data

webuse coffee!ia ze.dra, clear

fillpend using ·coffeeMaize2.dta", gen(filenum)

add observations from ·coffeeMaize2.dta" to

current data and create variable "filenum· to

tracie the orìgin of each observation

Mi:RGING Two DA A [ T GE -Ef<

MA-

webuse nd age.dta, clear

save 1nd_age.dta, replace

webuse ,-.a_ag dta, clear

merge 1:1 id using 'ind_age.dta'

one-to-one merge of "ind_age.dta·

into the loaded dataset and create

varìable •_merge· to track the orìgin

webuse hh2 dta, clear

save hh2 dta, replace

webuse '12.d'a clear

merge m:1 h1d using "hh2.dta·

many-to-one merge of "hh2.dta·

into the loaded dataset and create

variable • _merge· to track the orìgin

I Fuzz^ 1A~.'-li I.. -::c1•s I^ :; -�G D -A:,ET^ A -ye,u- A ·=-M^ •^ e

rmmi, match recOfds from different data sets using probabilistic matching mt r �

  • create distance measure for similarity between two strings

Manipulate Strings

GET STRING PROPERTIE.,

display length("This string has 29 characters")

retum the length of the string

charlist make ,_,." P^ Jr.

display the set of unique characters within a string

display strpos("Stata·, ·a·)

return the posit1on In Stata where a 1s first found

�,Ne I1ArH G .:=TRIN.:i�

display strmatch("123.89', "1??.?9")

retum true (1) or false (0) if string matches pattern

display substr("Stata·. 3. 5)

return string of 5 characters starting with position 3

list make if regexm(make, ·10-9)")

list observatìons where make matches the regular

expression (here, records that contain a number)

list if regexm(make, "(Cad.jChev.!Datsun)")

retum ali observations where make contains

·cad.". "Chev." or ·oatsun·

!ist if inlist(word(make. 1), ·cad.", "Chev.", ·oatslin")

retum ali observations where the first word of the

make variable contains the listed words

-KA, FoR� sT� r,,Gs

display regexr("My string·, "My", "Your")

repiace string1 ("My") with string2 ("Your")

replace make = subinstr(make, ·cad.". "Cadillac·. 1)

re piace first occurrence of ·cad." with Cadillac

in the make variable

display stritrim(" Too much Space")

replace consecutive spaces with a single space

display trim(' feadfng / traìling spaces ')

remove extra spaces before and after a string

display strlower("STATA should not be ALL-CAPS")

change stnng case; see also strupper. strp,roper

display strtoname("War name")

convert string to Stata-compatible variable name

display real("100")

convert string to a numenc or m1ssing value

compress

compress data m memory

save ·myData.dta·. replace

saveold ·myData.dta", replace verJion(12)

save data in Stata fermat. replacing rhe data if

a file with same name exists

export excef ·myData.xls", l"

•1 firstrow(variables) replace

export data as an Excel file (.xls) with the

variable names as the first row

export delimited "myData.cw·, delimiter(", ") replace

export data as a comma-delimited file (.csv)

updated June 2016

.l:l:Jn'..A.Q

ANATOMY OF A PLOT Plotting in Stata 15

Customizing Appearance

armota:.on (^) title oJ subbtle^

  • tte;

•or more ·nfo see Stata's referenc:e manual (statacom) (^) �I plots contain many features y-a, ---

I

�r-?:V""-gO" """' g,aon "'9 °" �-01cr.rw9·on

oiot"'9on

- ne (^) .-

  • •-- ma-1:er

r- ·,,,.....-..... :;""' ...,... er e-e .....-.

01..----r---,----,-----,-----,-- tid rnarls o � � w � m sc.atter or>ce mpg graphregion(kolor(i92'l921921ifcolor("2082lll 2001) specify the fi I of the background in RGB 0< with a Stata color scatter: l'Ce mpg plotregion(fcolor('224 224 224") ifcolor("240 240 240")) spectfy the fili of the pio! bac<ground in RGB or w,th a Stata color

SYMBOLS

·narker � marle

.... z opt10ns

arguments for the plot objects (In green) go in the options po rtionot these comma'lds (In orange) for e,ample SGltref pnce mpg, xbne(20, �ldth(vthick))

!fil_ofor(i45 168 208') mcofor(none) �edv the fili and stroke o': the maricer in RGB or with a Stata color 9 mfcolor('145168 208') mfcolor(ncme) 8 soecify the fili ef the maricer

rmize{medium) specify^ the marker si::e-:

V,V, ehuge • medlarge

z •^ medium

� (^) vhuge (^) • medsmall I-I: --... •^ small

huge l:l (^) l vi (^) • vlarge tiny

  • large^ vtiny

msymbol(Dh) specify the mance, 5Ymbol:

tl

  • o^ ♦^ D^ � T^ ■ s z (^) • o (^) ♦d A t (^) ■ s

o Oh^ ◊Dh^ 6, Th^ □ Sh

o oh^ ◊ dh I::. th^ □ sh

  • +^ X^ X^ p^ l'lOne i

z

jitter(#) (^) jitterwed(:} randomly dispiace the maricers set seed

LINES / 80RDERS

axe; xscafe{ �e()

�d; rnart: gnd ne; abe( ylabel(

!l;olor("145 168 208') !l;olor(none) specify the stroke color of the line a border ,.,., e^ mkolor(i45168 2081 tic,; 11"..ar, (^) tlcolor('145 168 208')

grid �es gkolor(i45168 208')

lwidth(medthick) specify the thicl:ness (:troke) o' a r.ne.

� wvthick

  • wthick
    • vthick
      • thick medthic.k medium

l"e a es .!pattem(dash) ';'cl ne; .glpattem(dash)

mlwidth(thin) tlwidth(th1n) gm,_idth(thin)

medthin thin vthin wthin vwthin none

speafythe fine patterr.

  • sotid - - longdash • , • longdash_dot •-• dash .... , shor tdash "■" shortdash_dot , ,. , dot - , • dash_dot blank

axes noline tic( mz • JlQ1kks

a, es off no axisllabels te IT'd� _ !!ength(2) gnd mes nogrid nogmin nogmax

tie 1w1 xfabel(#l0, !Qosition(crossing)) number cl bel: marks, pos-tion (outside I cross ng I inside)

x-axis title

ege•,d -1-•-��d va�,I

TEXT

marier label t�� ax;-; làbels m.::'ker trt',e (^) ) x1abel optons subtitle( abe annotaton xmle( )^ eçend ., vtte(. ) ��-^ .,

i;_ofor(i45 168 208") i;_ofor(none) specify tne color of lhe text ,.,.., er e-e mlab!.:ofor(i45168 208")

  • • re• fabcotor(i45 168 208") adjust transpare.ncy t7j adding %# .lill:ofor("1d5 168 208 %20")

siz.e{medsmafl) specify the size of the^ text ,.,,., e· .,be mlabsizfilmedsmam axis labes. fabsizg:(medsmall)

Text

Text Text Text Text Text

marler abel

vhuge

huge

vlarge la rge rrnedlarge medium

Tex^ medsmall Tt<t smatl r (^) vsmall Tt (^) tiny half_tiny third_tiny quarter_tiny minuscule

mlabet(foreign) label the points with the values o' the forcign variable nolabels no axìs labels = (^) labe. format(% 12.2f) change the format ol the a>ds labels egencJ off tum off legend lege<id label(;t "label") change legend abe rext

mar er aoel mlabposition(S) ,abe, locatìon relative to marker (dock posrbon: O - 12)

Apply Themes

Schemes are sets of graphical parameters. so you don't have to specify the look of the graphs every time. USING A SAVED THEME

twoway scatter mpg price, scheme(custom-heme)

help scherne entries , � see ali options for setting scheme properties adopath ++ '-//StataThemes• set path of the folder (StataThemes) where custom .scheme files are saved

set scheme customTheme, Qfilfilanently change the theme

netinst � from("https/,'wbuchanan.github.io/brewscheme/1 replace 1nstall William Buchanan·s package to generaie custom schemes and color patenes (inc.luding ColorBrewer) USING THE GRAPH EDITOR

twoway sc.atter mpg pnce, play(graphEdìtorTheme)

. •. .. 'F

! e

. (^). '

- �1- -.

,.,

e=;, -

Select the Graph Editor

Click Record

Double click on

  • .__ }

symbols and a�eas = on plot or reg 1ons -;- on sidebar to customize

Unclick Record

Save theme as a .gre< file

Save Plots

graph twoway scatter y x, saving("myPlot.gph") replace save the graph when drawing g_raph save "myPlot.gph", replace save current graph to disk g__raph combine plot1.gph plot2.gph... combine 2+ saved graphs into a single plot g__raph export •myPlot.pdf', as(.pdf) export the c.urrent graph as an image file

updateo June 2016

Data Analysis

with Stata 15 Cheat Sheet For more info see stata·s reference manual (stata.com) I' T Id· � ,�t Summarl·ze Data (^) unlessExamples useauto.dta (sysuseauto,dear) otherwlse noted

univar price mpg, boxplot (^) • ...: .•i... u calculate univariate summary. wrth box-and-whiskers plot stem mpg return stem-and-leaf display of mpg summarize price mpg, detail -1 , , catculate a variety of univariate summary statistics ci mean mpg price, level(99) - " 1r ....., r compute standard errors and confidence intervals correlate mpg price return correlation or covariance matrix pwcorr price mpg we1ght, gar(0 05) return all pairwise correlation coefficients wrth sig. levels

r

mean price mpg estimates of means. including standard errors proportion rep78 foreign estimates of proportions. including standard errors for r, catcgories idcntificd in varlist I ratio estimates of ratio, including standard errors

l total price estimates of totals. including standard errors

Statistica! Tests tabulate foreign rep78, chi2 .e_xact_gmected tabulate foreign and repair record and retum chi^2 and Fisher's exact statistic alongside the expected values ttest mpg, by(foreign) estimate t test on equality of means tor mpg by foreign

::: prtest foreign == O. 5 one-sample test of proportions ksmirnov mpg, by(foreign) g_xact Kolmogorov-Smimov equality-of-distributions test ranksum mpg, by(foreign) equality tests on unmatched data (independent samples)

[

anova systolic drug -t>u 1- ' analysis of variance and covariance pwmean mpg, over(rep78) pveffects mcompare(tukey) l estimate pairwise comparisons of means wrth equal variances include multiple compartson adjustment

Decla re Data By^ declarmg data type, you enable Stata to apply data mungcng and analys1s funct1ons specific to certam data types

TIME SERIES -+....,,. • tsset time, yearly Bm

PANEL / LONGITUDINAL wdluse^ rlswori:. cle.ar

declare sunspot data to be yearty time series HH

tsreport report time series aspects of a dataset 9.enerate lag_spot = L 1.spot c 1

_

reate a new variable ,

of annua! lags ��; ,J.,..

ts " ts ine spot · 1 • A • , • ftA e

plot time series of sunspots

arima spot, ar(1/2) I ••, �..,^ ·

estimate an auto-regressive model with 2 lags TtME SERIES 0PERATORS I

xtset id year dedare national longitudinal data to be a panel (^) t-t xtdescribe report pane! aspects of a dataset xtsum hours summarize hours worked. decomposing , � � standard deviation into between and within components xtline ln_wage if id <= 22, tlabel(#3} plot pane! data as a line plot

o-----

. - ---...,.,..

xtreg ln_w c.age##c.age ttl_exp, fe vce(robust) L llg x.- estimate a fixed-effects model with robust standard errors F. le.ad:x,.,

I.Z. 2-penoa rag ••, fZ. 2-i»riod lHd •..i SURVEY DATA O. �rencew,.-..,. S. SUSONI differeroce X ....,

02 -,_dd<Fe""'cel.-.:,.,il',.,,U S2. lag-2 (seasonal o.fferfflce) r,-", UsEFUL Aoo-lNS ---------------- 1:scollap 00frf>,1Ct tn>@ series "1!D ,.....,,.., wm, and end-cl-p@riod valuo, canry(orward CMT)I^ non-rrming values^ ID<watd from one om. ID the next tsspell idffltify ,pe1, cx runs in tòne sorin

SURVIVAL ANALYSIS stset studytime, failure(died) , declare sur vey design for a dataset stsum

lii --+-- I summarize survival-time data

e

stcox drug age j estimate a Cox proportional hazard model

o Estimate Models storesresult5asQ -dass

iggress price mpg weight vce(robust) estimate ordinary least squares (OLS) model on mpg weight and foreign, apply robust standard errors iggress price mpg we1ght if foreign == O. vce(cluster rep78) regress price only on domestic cars, cluster standard errors rreg price mpg weight genwt(reg_wt) estimate robust regression to eliminate outliers probit foreign turn price. vce(!:obust} estimate probrt regression with robust standard errors ,!Qgitforeign headroom mpg, or estimate logistic regression and report odds ratios bootstrap, reps(100):.rggress mpg /* */ we,ght gear foreign

AoomoNAL Moow pc.I - -•lblll � ...._..,. � l'KIOf ,,....,,....,^ ""'°'aNl,sk pob-•nllf"ll CIMI-

- -,d- "'"9"" IZl) lnltn,�-lablo,

m l!!J..c;,..,..,.....,z,<9'....,,,d� ........ìm.n^ ...,.,,....,._

-mm� .... .._

i§rèS❖·:i:dritl pr.,.a,1CCWe� l!lilll .,.-c:iomd ,....,.

svyset psuid (pweight = finalwgt), strata(stratid)

declare survey design for a dataset

svydescribe report survey data details r svy: mean age, over(sex) I estimate a population mean tor each subpopulation svy, subpop(rural): mean age

"

estimate a population mean for rural areas

I

svy: tabulate sex heartatk report two-way table with tests of independence svy: reg zinc c.age##c.age ternale we1ght rural estimate a regression using survey weights

tJ Diagnostics some ore tnappropriate w1th robust SEs

estat hettest test for heteroskedasticity ovtest test tor omitted variable bias vif report variance inffation factor dfbeta(length) MJOn pfoc calculate measure of inffuence -"' � ._.,u. � - rvfplot, yline(0) e. ,. e •, avplots plot residuals li�li�^ plot ali partial- against fitted � �t� regre�sion leverage values (^) ". .;.,..,., _ plots m one graph

I] Postestimation commands that use a fttted model

_rg_gress price headroom length " .....,...."'"'"'" eum.,i,,, .Qisplay _b[length] display _se[length] return coefficient estimate or standard errar for mpg from most recent regression model ma rgins, dydx(length) ,. " ,. •.,,- u .-. vs .JSed

estimate regression with bootstrapping jackknife r(mean), double: sum mpg

jaddmife standard errar of sample mean ED --<>--o.-"""'""°"' return the estimated marginai^ effect^ for mpg

Estimation with Categorica I & Factor Varia bles more deta1ls at http://www.rtato.com/manuals/u25.pdf

"' ma rgins, eyex(length) retum the estimated elasticity for price

(ONTJNUOUSVARIABL.ES predict yhat if e(sample) R mea1ure something CATEGORICA!. VAR!ABLES ■ • • identify an observabons belongs^ a group to^ whK:h

INDICATOR VARIABLES T F denote whethersomething is bue cr false

O�ERATOR

ib. Me c.

o. � ,nr

OESCRIPT10N specify ìndicalD,s specify bare indìcatll< a>mmand IO ct>ange ba!e treat variable as ccntiruous

orrit il varìilbfe or indic:ator specify -· ,pecify lactorial Ìn11!fiKIÌ<ms

ExAMPLE regress pnce Ln,p ,egress price ibC3).rep lvset baso flrl!quent rep 78 ,egress pnce Lfcre,gnlunpg 1Jcreign

,egreu price lo(Z)Jep ,eg,..n pria, mpg c.mpgltff1> ,egress pnce <Jll)Qtlc.mpg

specify rep78 variallle ID be an Oldlc.nDr ·;ariable set lhe third ca.:egory or rep78 ID be lhe ba<e categery <et the ba;e IO most m,quently cxwmng Còl!gOry ror '"1) treat mpg as a a>nbn1JOU$ vMlab!e and tpecrfy an vn:eract:ion between forelgn and mpg ,et "'1)78 a, an.,éoca10r cmitob<ervation1 with rep78 == 2 create a iqua!Wd mpg tffm a, be used In regl"HSXln cnoate all pos<ible lnteraction1 ""h mpg (rrc,g and mpg'J

create predictions tor sample on which model was fit predict double resid, residuals calculate residuals based on last fit model test headroom = O test linear hypotheses that headroom estimate equals zero lincom headroom - length test linear combination of estimates (headroom = length)

updated June 2016