+1(505)531-9093

Common Stata Commands

Today I will teach you Common Stata Commands

  1. General Plotting Commands
    1. Plot a histogram of a variable:
      graph vn, bin(xx)
    2. Plot a histogram of a variable using frequencies:
      graph vn, bin(xx) freq
    3. Plot a histogram of a variable with a normal approximation:
      graph vn, bin(xx) norm
      where xx is the number of bins.
    4. Plot a boxplot of a variable:
      graph vn, box
    5. Plot side-by-side box plots for one variable (vone) by categories of another variable vtwo.
      (vtwo should be categorical)):
      sort vtwo
      graph vone, box by(vtwo)
    6. A scatter plot of two variables:
      graph vone vtwo
    7. A matrix of scatter plots for three variables:
      graph vone vtwo vthr, matrix
    8. A scatter plot of two variables with the values of a third variable used in place of points on the graph (vthr might contain numerical values or indicate categories, such as male (m) and female (f)):
      graph vone vtwo, symbol([vthr])
    9. Normal quantile plot:
      qnorm vn
  2. General commands
    1. To compute means and standard deviations of all variables:
      summarize
      or, using an abbreviation,
      summ
    2. To compute means and standard deviations of select variables:
      summarize vone vtwo vthr
      or, using an abbreviation,
      summ vone vtwo vthr
    3. To get more numerical summaries for one variable:
      summ vone, detail
    4. Correlation between two variables:
      correlate vone vtwo
    5. To see all values (all variables and all observations, not recommended for large data sets):
      list
    6. To list values for two variables:
      list vone vtwo
    7. To list the first 10 values for two variables:
      list vone vtwo in 1/10
    8. To list the last 10 values for two variables:
      list vone vtwo in -10/l
      (The end of this command is minus 10/letter l.)
  3. Tables
    1. Tabulate variable vn:
      tabulate vn
      or, using an abbreviation,
      tab vn
    2. Cross tabulate two variables:
      tab vone vtwo
    3. Cross tabulate two variables, include one or more of the options to produce column, row or cell percents and to suppress printing of frequencies:
      tab vone vtwo, column row cell
  4. Generating new variables
    1. General.
      1. Generate index of cases 1,2,ldots,n) (this may be useful if you sort the data, then want to restore the data to the original form without reloading the data):
        generate case= _n
        or, using an abbreviation,
        gen case=_n
      2. Multiply values in vx by b and add a, store results in vy:
        gen vy = a+ b * vx
      3. Generate a variable with all values 0:
        gen vone=0
      4. Generate a variable with values 0 unless vtwo is greater than c, then make the value 1:
        gen vone=0
        replace vone=1 if vtwo>c
    2. Random numbers.
      1. Set numbers of observations to n:
        set obs n
      2. Set random number seed to XXXX, default is 1000:
        set seed XXXX
      3. Generate n uniform random variables (equal chance of all outcomes between 0 and 1:
        gen vn=uniform()
      4. Generate n uniform random variables (equal chance of all outcomes between a and b:
        gen vn=a+(b-a)*uniform()
      5. Generate n discrete uniform random variables (equal chance of all outcomes between 1 and 6
        (These commands simulate rolling a six-sided die):
        gen vn=1+int(6**uniform())
      6. Normal data with mean 0 and standard deviation 1:
        gen vn= invnorm(uniform())
      7. Normal data with mean mu and standard deviation sigma:
        gen vn= mu + sigma * invnorm(uniform())
  5. Regression
    1. Compute simple regression line (vy is response, vx is predictor):
      regress vy vx
    2. Compute predictions, create new variable yhat:
      predict yhat
    3. Produce scatter plot with regression line added:
      graph vy yhat vx, connect(.s) symbol(oi)
    4. Compute residuals, create new variable tt residuals:
      predict residuals, resid
    5. Produce a residual plot with horizontal line at 0:
      graph residuals, yline(0)
    6. Identify points with largest and smallest residuals:
      sort residuals
      list in 1/5
      list in -5/l
      (The last command is minus 5/letter l.)
    7. Compute multiple regression equation (vy is response,
      vone, vtwo, and vthr are predictors):
      regress vy vone vtwo vthr

Important Notes on “stem” command

There is a glitch with Stata’s “stem” command for stem-and-leaf plots. The “stem” function seems to permanently reorder the data so that they are sorted according to the variable that the stem-and-leaf plot was plotted for. The best way to avoid this problem is to avoid doing any stem-and-leaf plots (do histograms instead). However, if you really want to do a stem-and-leaf plot you should always create a variable containing the original observation numbers (called “index”, for example). A command to do so is: generate index = _n
If you do this, then you can re-sort the data after the stem-and-leaf plot according to the index variable (Stata command: sort index ) so that the data is back in the original order.

Commands: Here are some other commands that you may find useful (this is by no means an exhaustive list of all Stata commands):

anovageneral ANOVA, ANCOVA, or regression
byrepeat operation for categories of a variable
ciconfidence intervals for means
clearclears previous dataset out of memory
correlatecorrelation between variables
describebriefly describes the data (# of obs, variable names, etc.)
diagplotdistribution diagnostic plots
dropeliminate variables from memory
editbetter alternative to input for Macs
exitleave Stata
generatecreates new variables (e.g. generate years = close – start)
graphgeneral graphing command (this command has many options)
helponline help
iflets you select a subset of observations (e.g. list if radius >= 3000)
infileread non-Stata-format dataset (ASCII or text file)
inputtype in raw data
listlists the whole dataset in memory (you can also list only certain variables)
logsave or print Stata ouput (except graphs)
lookupkeyword search of commands, often precursor to help
onewayoneway analysis of variance
pcorrpartial correlation coefficients
plottext-mode (crude) scatterplots
predictcalculated predicted values (y-hat), residuals (ordinary, standardized and studentized), leverages, Cook’s distance, standard error of predicted individual y, standard error of predicted mean y, standard error of residual from regression
regressregression
replacelets you change individual values of a variable
savesaves data and labels in a Stata-format dataset
sebarrstandard error-bar chart
sortsorts observations from smallest to largest
stemstem and leaf display
summarizeproduces summary statistics (# obs, mean, sd, min, max) (has a detail option)
testconducts various hypothesis tests (refers back to most recent model fit (e.g. regress or anova ) (see help function for info and examples))
ttestone and two-sample t-tests
useretrieve previously saved Stata dataset
Data Analytics Services
Need Our Services?
Econometrics & Statistics Modelling Services
Need Help, Whatsapp Us Now