Working with Stata in Jupyter

You can work with a Jupyter notebook using Stata just as you would using Python. Text and math can be written in Markdown as we have done in the other notebooks. So, use your usual Stata code in code cells, produce figures etc., as if you were in the Stata console, but make it nice by using markdown to explain what you are doing.

A simple example

Let's run a simple Stata example using one of their help files.

In [1]:
sysuse auto.dta
(1978 Automobile Data)

Basic information

Let's summarize and describe the data.

In [3]:
summ
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
        make |          0
       price |         74    6165.257    2949.496       3291      15906
         mpg |         74     21.2973    5.785503         12         41
       rep78 |         69    3.405797    .9899323          1          5
    headroom |         74    2.993243    .8459948        1.5          5
-------------+---------------------------------------------------------
       trunk |         74    13.75676    4.277404          5         23
      weight |         74    3019.459    777.1936       1760       4840
      length |         74    187.9324    22.26634        142        233
        turn |         74    39.64865    4.399354         31         51
displacement |         74    197.2973    91.83722         79        425
-------------+---------------------------------------------------------
  gear_ratio |         74    3.014865    .4562871       2.19       3.89
     foreign |         74    .2972973    .4601885          0          1
In [4]:
desc
Contains data from /Applications/Stata/ado/base/a/auto.dta
  obs:            74                          1978 Automobile Data
 vars:            12                          13 Apr 2018 17:45
                                              (_dta has notes)
--------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
--------------------------------------------------------------------------------
make            str18   %-18s                 Make and Model
price           int     %8.0gc                Price
mpg             int     %8.0g                 Mileage (mpg)
rep78           int     %8.0g                 Repair Record 1978
headroom        float   %6.1f                 Headroom (in.)
trunk           int     %8.0g                 Trunk space (cu. ft.)
weight          int     %8.0gc                Weight (lbs.)
length          int     %8.0g                 Length (in.)
turn            int     %8.0g                 Turn Circle (ft.)
displacement    int     %8.0g                 Displacement (cu. in.)
gear_ratio      float   %6.2f                 Gear Ratio
foreign         byte    %8.0g      origin     Car type
--------------------------------------------------------------------------------
Sorted by: foreign

Regressions

Let's run a simple regression

In [8]:
reg price mpg rep78 headroom trunk weight length turn displacement gear_ratio foreign, r
Linear regression                               Number of obs     =         69
                                                F(10, 58)         =      11.47
                                                Prob > F          =     0.0000
                                                R-squared         =     0.5989
                                                Root MSE          =     1997.3

------------------------------------------------------------------------------
             |               Robust
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         mpg |  -21.80518   84.00518    -0.26   0.796    -189.9598    146.3495
       rep78 |   184.7935   337.0935     0.55   0.586    -489.9724    859.5594
    headroom |  -635.4921   251.3987    -2.53   0.014    -1138.721    -132.263
       trunk |   71.49929   70.18603     1.02   0.313     -68.9933    211.9919
      weight |   4.521161   1.963781     2.30   0.025      .590227    8.452096
      length |  -76.49101   51.40168    -1.49   0.142    -179.3826    26.40062
        turn |  -114.2777   122.2631    -0.93   0.354    -359.0139    130.4585
displacement |   11.54012   7.065004     1.63   0.108    -2.602017    25.68227
  gear_ratio |  -318.6479   1016.632    -0.31   0.755    -2353.658    1716.362
     foreign |   3334.848   988.7149     3.37   0.001     1355.721    5313.976
       _cons |   9789.494   8240.462     1.19   0.240    -6705.583    26284.57
------------------------------------------------------------------------------

Plots

In [9]:
scatter price mpg, mlabel(make)
In [ ]: