python plug-in for stata - sociology...python plug-in for stata soc 561 programming for the social...

21
Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016

Upload: others

Post on 24-Mar-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

Python plug-in for Stata

SOC 561

Programming for the Social Sciences

Hyungjun Suh

Apr. 4. 2016

Page 2: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

2

1) What is it and what does it do?

Python plugin for Stata enables the user to use Python to interact with Stata data values, matrices, macros and numeric scalars.

The plugin requires Stata 12.1 or greater, and Python 3.x. The plugin will not work with Python 2.

Page 3: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

3

2) How to install – for windows

See James Fiedler (2013) “The Python plugin for Stata, version 0.1.0.”

Step1: install Stata, Python 3.3, and Visual Studio Express 2012 (this version will work as well and take less storage room than Visual Studio Express 2015).

Step2: get necessary files

- stplugin.h, stplugin.c (from http://www.stata.com/plugins/)

- python_plugin.c, python.ado, stata.py, stata_missing.py (from http://econpapers.repec.org/software/bocbocode/s457688.htm)

Page 4: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

4

2) How to install – for windowsStep3: make python_plugin.plugin

Step3.1: File > New Project

Page 5: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

5

2) How to install – for windowsStep3.2: Visual C++ > Win32 > Win32 Project. Click OK.

Page 6: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

6

2) How to install – for windows

Step3.4: Click on finish.

Page 7: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

7

2) How to install – for windows

Step3.5: under Application type, choose DLL. Below that, check the box for empty project. Click on Finish.

Page 8: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

8

2) How to install – for windowsStep3.6: find Resource Files. Right click, select Add > Existing Item. Add each of following: python_plugin.c,stplugin.h,stplugin.cpython33.lib (from c:\Python33\libs).Caution: stplugin.hshould be in the folder, c:\Python33\include.

Page 9: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

9

2) How to install – for windows

Step3.7: under Resource Files, click on python_plugin.c so that it’s highlighted. In the main menu bar, select VIEW > Property Pages.

Page 10: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

10

2) How to install – for windowsStep3.8: on a new pop-up, select C/C++ > General. On the right, click in the filed next to Additional Include Directories, and type in the directory for Python.h(C:\Python33\include).It is a typo. It should be

C:\Python33\include.

Page 11: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

11

2) How to install – for windowsStep3.9: at the top, find Debug below the main menu bar (not the DEBUG in the main menu bar), and change this to Release.

Page 12: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

12

2) How to install – for windowsStep3.10: if you have an x64 machine, change the field next to Debug from Win32 to x64.

Page 13: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

13

2) How to install – for windows

Step3.11: click on Configuration Manager. And a new window pops up. Under platform, select New…, then select x64. Click on OK, then Close.

Page 14: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

14

2) How to install – for windowsStep3.12: in the main menu bar, select BUILD > Build Solution. You should get a message in the Output window, below the main window, that says the project was successfully compiled.

Page 15: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

15

2) How to install – for windowsStep3.13: rename the compiled dll to python_plugin.plugin. The compiled dll is found in C:\Users\<my username>\My Documents\Visual Studio 2012\Projects\<project name>\x64\Release.

Page 16: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

16

2) How to install – for windows

Step4: Put python_plugin.plugin and python.ado in Stata’s ado path (in Stata use command adopath to see the ado path), and put stata.py and stata_missing.py in Python’s path (in Python use import systhen sys.path to see directories in the path. Normally, it is C:\Python33\Lib and C:\Python33\DLLs).

Step5: Open Stata and type python. If everything has worked, this should start an interactive session of Python within Stata.

Page 17: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

17

2) How to install – for mac os X

Step1: install Python 3.3 (www.python.org/getit)

Step2: get gcc with Xcode (https://developer.apple.com/xcode/), or see “Command Line Tools for Xcode” (http://www.mkyong.com/mac/how-to-install-gcc-compiler-on-mac-os-x/).

Step3: make sure python_plugin.c, stplugin.c, and stplugin.h reside in the same directory

Step4: get the compiler command from http://www.stata.com/plugins/, and modified for this plugin:

gcc – bundle – DSYSTEM=APPLEMAC stplugin.c

python_plugin.c –o python_plugin.plugin

Page 18: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

18

2) How to install – for mac os X

Step5: add to that compiler and linker flags for Python (see http://docs.python.org/3.3/extending/embedding.html#compiling-and-linking-under-unix-like-systems).

Step6: put python_plugin.plugin and python.ado in Stata’s ado path (in Stata use command adopath to see the ado path), and put stata.py and stata_missing.py in Python’s path (in Python use import systhen sys.path to see directories in the path).

Page 19: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

19

3) Basic Syntax

python [varlist] [if] [in] [,

file(some_file.py) args(some_args) ]

Alternatively, after declaring program python_plugin, plugin, Plugin call python_plugin [varlist] [,

file_name ]

The number of arguments in the args option is stored in Stata local _pynargs, arguments are stored in _pyarg0, _pyarg1, etc.

The number of variables in the varlist and their names are stored in Stata locals _pynvars, and _pyvar0, _pyvar1, etc.

Page 20: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

20

4) Options and Examples

There are many functions in Python plugin. For detailed explanation, see Fiedler (2013:7-18).

Often used functions are introduced with examples.

See the log-file for explanation of options and examples.

Page 21: Python plug-in for Stata - Sociology...Python plug-in for Stata SOC 561 Programming for the Social Sciences Hyungjun Suh Apr. 4. 2016. 2 1) What is it and what does it do? Python plugin

21