我使用rsource命令在Stata中调用R,以便创建一个随机向量并将其返回给stata,这是我的do文件的简单工作示例:
clear
rsource, terminator(END_OF_R) rpath(C:\Program Files\R\R-3.1.2\bin\R.exe) roptions("--vanilla")
set.seed(1234);
library(mvtnorm);
library(tmvtnorm);
library(foreign);
xmean<- rep(0,100);
xSigma<- diag(100);
a<- rep(0,100);
b<- rep(+Inf,100);
X<- rtmvnorm(n=1,
mean=xmean, sigma=xSigma,
lower=a, upper=b,
algorithm="gibbs");
X<- t(X);
write.dta(data.frame(X), "C:/Users/.../Desktop/all/newx.dta");
END_OF_R
use newx.dta, replace是否可以将一些值作为参数传递给rsource,我可以在stata中定义这些参数(例如rseed数或N个obs数)。例如:
cap prog drop callr
program callr
version 13
syntax , seed(int) n(int)
.... to call rsource with `seed' and `n' as arguments
use newx.dta, replace
end我在这方面是很新的,无法在资源帮助或实习生中找到和回答,所以任何评论都是非常感谢的!
发布于 2014-12-05 04:03:05
可以使用roptions选项将Stata宏值传递给R。传递两个本地宏的示例:
clear
local mynumber 999
local secondone 1000
rsource, terminator(END_OF_R) rpath("/usr/bin/R") roptions(`" --vanilla --args "`mynumber'" "`secondone'" "')
trailargs <- commandArgs(trailingOnly=TRUE);
trailargs;
trailargs[1];
trailargs[2];
END_OF_R这在help rsource中有记录。如果这个概念不清楚,请参见help macro。
https://stackoverflow.com/questions/27308187
复制相似问题