日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python和stata_从Python运行Stata do文件

發布時間:2023/12/19 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python和stata_从Python运行Stata do文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我認為@ user229552指向正確的方向.可以使用Python的子進程模塊.下面是一個適用于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文件,具有以下內容:

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

資料來源:

可以在以下位置找到使用Python和Stata的不同路線

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python和stata_从Python运行Stata do文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。