用Julia跑Stata

1、读取Stata数据文件

``````ReadStat.jl不仅可以直接用来读dta文件，而且还能打开SPSS和SAS的数据文件，命令也很简单
``````

2、运行Stata命令

srand(1)

#打开Stata

ENV[“STATA_BIN”] = “C:\Program Files (x86)\Stata13\StataMP-64.exe”

#生成个数据表

df = DataFrame(myint = Int64.(floor.(100.*rand(Float64, 10))), myfloat = rand(Float64, 10))

#跑Stata命令

instructions = [“gen newvar1 = myint + myfloat”;

“gen newvar2 = floor(_n/2)”;
“bysort newvar2: egen newvar3 = mean(newvar1)”
]
dfOut = StataCall.stataCall(instructions, df)

3、还有更简洁的

``````GitHub里的大神jmboehm用StataCall时感觉不爽，大概三个月前，他自己写了个更简洁的包，名为Douglass.jl，以纪念1993年诺奖得主Douglass North。示例代码：
``````

using Douglass, RDatasets

以经典的鸢尾花数据为例

df = dataset(“datasets”, “iris”)
Douglass.set_active_df(:df)
d"replace :z = _n if _n <= 10"
d"drop :z"
d"bysort :Species : egen :z = mean(:SepalLength) if :SepalWidth .> 3.0"

· generate – Creates a new variable and assigns the output from an expression to it.

· replace – Recplaces the content of a variable, but does not change the type.

· egenerate (or egen for short) – Creates a new variable. Operates on vectors.

· ereplace (or erep for short) – Analogous to egen, replaces values of existing variables.

· drop – Drops the specified observations (if used in conjunction with if) or variables (without if)

· rename – Rename a variable

· sort – Sort the rows activate DataFrame by the specified columns

· reshape – Reshape the activate DataFrame between wide and long format (reshape_long, reshape_wide)

· merge – Merge the active DataFrame with another one in the local scope (merge_m1, merge_1m, merge_11)

· duplicates_drop – Delete duplicate rows, also by subset of columns

2赞

deepin linux 测试失败

``````julia> using StataCall
[ Info: Precompiling StataCall [149ba50c-99c4-11e9-1e32-e960083e84dc]
ERROR: LoadError: UndefVarError: locate_executable not defined
Stacktrace:
[1] top-level scope at /home/jerrywang/.julia/packages/StataCall/VVxuj/src/StataCall.jl:53
[2] include(::Module, ::String) at ./Base.jl:377
[3] top-level scope at none:2
[4] eval at ./boot.jl:331 [inlined]
[5] eval(::Expr) at ./client.jl:449
[6] top-level scope at ./none:3
in expression starting at /home/jerrywang/.julia/packages/StataCall/VVxuj/src/StataCall.jl:53
ERROR: Failed to precompile StataCall [149ba50c-99c4-11e9-1e32-e960083e84dc] to /home/jerrywang/.julia/compiled/v1.4/StataCall/JL1l9_x4wXy.ji.
Stacktrace:
[1] error(::String) at ./error.jl:33
``````

Linux 的还没有实现 还是 TODO 状态

1赞