Stata notebooks


Stata notebooks are available for those researchers who are more comfortable using Stata and its ecosystem. These are built off the same base image as python notebooks, but include the official pystata library to allow for the execution of Stata in a notebook environment.

Working with Stata in a notebook environment is slightly different than the Stata desktop application, in that we need to utilize python to pass data into Stata. This step is quite simple, and doesn't require any expertise in python – see working with tabular data below.

While Stata is fully supported on Redivis, certain Redivis concepts, such as unstructured data files, don't have a corollary in Stata. Moreover, Stata doesn't support the sorts of parallelized stream processing available in Python and R.

Enabling Stata notebooks

Because Stata is proprietary software, you will need to provide a license for Stata 16 or later in order to enable Stata notebooks on Redivis. Organizations can specify license information in their settings, which will make Stata notebooks available to all members of their organization. Alternatively, you can provide your own stata license in your workspace.

In the Jupyter-Stata documentation, you may see references to configuring stata via the stata_setup command. There is no need to run this command in Stata notebooks on Redivis, as everything has been pre-configured.

Working with tabular data

In order to load data into Stata, we first pull it into a data frame in python, and then pass that variable into Stata. If you're unfamiliar with python, you can just copy+paste the below into the first cell of your notebook to load the data in python.

# We first load the table via python, and then pass the dataframe into stata
df = redivis.table("_source_").to_pandas_dataframe(dtype_backend="numpy")

View the Table.to_pandas_dataframe() python documentation ->

Next, in a separate cell, we use the %%stata "magic" at the start of our cell to specify that this is stata code. We include the -d df argument to pass in the df variable from python into Stata, and include the -force flag to tell Stata to overwrite any current dataset that we have.

%%stata -d df -force
/* Run stata code! All stata cells must be prefixed with %%stata */

Any subsequent cells that execute stata code should be prefixed by %%stata if they are more than one line, or by %stata if the code to be executed is all on one line:

%stata scatter mpg price

View full documentation for the %%stata magic, including other helpful flags for moving data between python and Stata, here >

You can also use the %%mata command to execute Mata code:

 Create the matrix X in Mata and then obtain its inverse, Xi. 
 Then, multiply Xi by the original matrix, X 

X = (76, 53, 48 \ 53, 88, 46 \ 48, 46, 63)
Xi = invsym(X)

Working with geospatial data

Through various packages, Stata offers some support for geospatial datatypes. However, we can't pass geospatial data from python natively, and instead need to first create a shapefile that can then be loaded into Stata.

# This python code loads a geospatiale table and then writes it to a shapefile
geopandas_df = redivis.table("_source_").to_geopandas_dataframe(dtype_backend="numpy")

View the Table.to_geopandas_dataframe() python documentation ->

spshape2dta out.shp

Working with larger tables

If your data is too big to fit into memory, you may need to first save the data as a delimited file, and then read that file into Stata:

# This python code downloads a table and then rewrites it to a CSV

from pyarrow.dataset import write_dataset

output_directory = "/scratch/df" # directory where file should be located
arrow_dataset = redivis.table("_source_").to_arrow_dataset()
    basename_template="out_{i}.csv" # The name of the file

View the Table.to_arrow_dataset() python documentation ->

import delimited "/scratch/df/out_0.csv"

Creating output tables

Redivis notebooks offer the ability to materialize notebook outputs as a new table node in your project. This table can then be processed by transforms, read into other notebooks, exported, or even re-imported into a dataset.

To create an output table, we first need to pass our Stata data back to python, using the -dout flag. We can then use the redivis.current_notebook().create_output_table() method in python to output our data.

If an output table for the notebook already exists, by default it will be overwritten. You can pass append=True to append, rather than overwrite, the table. In order for the append to succeed, all variables in the appended table, which are also present in the existing table, must have the same type.

%%stata -doutd df2
  Once this cell executes, the current dataset will be pushed 
  to the python variable df2
rename v* newv*
# Via python, pass this dataframe to the output table
# If append=True, subsequent calls will add to the existing table, 
#   rather than replacing it
redivis.current_notebook().create_output_table(df2, append=False)

Storing files

As you perform your analysis, you may generate files that are stored on the notebook's hard disk. There are two locations that you should write files to: /out for persistent storage, and /scratch for temporary storage. By default, the output location is set to /scratch.

Any files written to persistent storage will be available when the notebook is stopped, and will be restored to the same state when the notebook is run again. Alternatively, any files written to temporary storage will only exist for the duration of the current notebook session.

save "/out/my_dataset.dta"
outreg2 using /out/table.xls, replace

Last updated