从Python运行Stata do文件

时间:2014-01-21 16:24:47

标签: python stata

我有一个Python脚本,用于清理并对大型面板数据集(2,000,000+ observations)执行基本统计计算。

我发现其中一些任务更适合Stata,并使用必要的命令编写了一个do文件。因此,我想在我的Python代码中运行.do文件。如何从.do调用Python文件?

3 个答案:

答案 0 :(得分:11)

我认为@ user229552指向了正确的方向。可以使用Python的subprocess模块。下面是一个适用于Linux OS的示例。

假设您有一个名为pydo.py的Python文件,其中包含以下内容:

import subprocess

## Do some processing in Python

## Set do-file information
dofile = "/home/roberto/Desktop/pyexample3.do"
cmd = ["stata", "do", dofile, "mpg", "weight", "foreign"]

## Run do-file
subprocess.call(cmd) 

以及名为pyexample3.do的Stata do文件,其中包含以下内容:

clear all
set more off

local y `1'
local x1 `2'
local x2 `3'

display `"first parameter: `y'"'
display `"second parameter: `x1'"'
display `"third parameter: `x2'"'

sysuse auto
regress `y' `x1' `x2'

exit, STATA clear

然后在终端窗口中执行pydo.py按预期工作。

您还可以定义Python函数并使用它:

## Define a Python function to launch a do-file 
def dostata(dofile, *params):
    ## Launch a do-file, given the fullpath to the do-file
    ## and a list of parameters.
    import subprocess    
    cmd = ["stata", "do", dofile]
    for param in params:
        cmd.append(param)
    return subprocess.call(cmd) 

## Do some processing in Python

## Run a do-file
dostata("/home/roberto/Desktop/pyexample3.do", "mpg", "weight", "foreign")

来自终端的完整呼叫,结果为:

roberto@roberto-mint ~/Desktop
$ python pydo.py

  ___  ____  ____  ____  ____ (R)
 /__    /   ____/   /   ____/
___/   /   /___/   /   /___/   12.1   Copyright 1985-2011 StataCorp LP
  Statistics/Data Analysis            StataCorp
                                      4905 Lakeway Drive
                                      College Station, Texas 77845 USA
                                      800-STATA-PC        http://www.stata.com
                                      979-696-4600        stata@stata.com
                                      979-696-4601 (fax)


Notes:
      1.  Command line editing enabled

. do /home/roberto/Desktop/pyexample3.do mpg weight foreign 

. clear all

. set more off

. 
. local y `1'

. local x1 `2'

. local x2 `3'

. 
. display `"first parameter: `y'"'
first parameter: mpg

. display `"second parameter: `x1'"'
second parameter: weight

. display `"third parameter: `x2'"'
third parameter: foreign

. 
. sysuse auto
(1978 Automobile Data)

. regress `y' `x1' `x2'

      Source |       SS       df       MS              Number of obs =      74
-------------+------------------------------           F(  2,    71) =   69.75
       Model |   1619.2877     2  809.643849           Prob > F      =  0.0000
    Residual |  824.171761    71   11.608053           R-squared     =  0.6627
-------------+------------------------------           Adj R-squared =  0.6532
       Total |  2443.45946    73  33.4720474           Root MSE      =  3.4071

------------------------------------------------------------------------------
         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      weight |  -.0065879   .0006371   -10.34   0.000    -.0078583   -.0053175
     foreign |  -1.650029   1.075994    -1.53   0.130      -3.7955    .4954422
       _cons |    41.6797   2.165547    19.25   0.000     37.36172    45.99768
------------------------------------------------------------------------------

. 
. exit, STATA clear

<强>来源:

http://www.reddmetrics.com/2011/07/15/calling-stata-from-python.html

http://docs.python.org/2/library/subprocess.html

http://www.stata.com/support/faqs/unix/batch-mode/

可以在

找到使用Python和Stata的不同路线

http://ideas.repec.org/c/boc/bocode/s457688.html

http://www.stata.com/statalist/archive/2013-08/msg01304.html

答案 1 :(得分:1)

如果你在命令行设置中运行它,你应该可以从python的命令行调用Stata(我不知道如何从Python中调用shell命令,但它不应该太难了,请看这里:Calling an external command in Python)。要从命令行(也称为批处理模式)运行Stata,请参见此处:http://www.stata.com/support/faqs/unix/batch-mode/

答案 2 :(得分:1)

这个答案扩展了@Roberto Ferrer的答案,解决了我遇到的一些问题。

系统路径中的Stata

要使char A = 'A'; char B = A++; 运行代码,必须在系统路径中正确设置(至少在Windows上)。至少对我来说,安装Stata时没有自动设置,我发现最简单的修正是放入完整路径(对我来说是stata),即:

"C:\Program Files (x86)\Stata12\Stata-64

如何在后台安静地运行代码

通过添加命令cmd = ["C:\Program Files (x86)\Stata12\Stata-64","do", dofile]` 即可以使代码在后台安静地运行(即每次不打开Stata),即

/e

日志文件存储位置

最后,如果您在后台安静地运行,Stata将需要保存日志文件。它将在cmd = ["C:\Program Files (x86)\Stata12\Stata-64,"/e","do", dofile]的工作目录中执行此操作。这必须根据运行代码的位置而有所不同,但对我来说,因为我从Notepad ++执行Python,所以它希望将日志文件保存在cmd中,Stata没有写入访问权限。这可以通过在调用子进程时指定工作目录来更改。

C:\Program Files (x86)\Notepad++代码的这些修改导致:

Roberto Ferrer