如何通过 R 调用 Stata 和运行语法?
How to invoke Stata and run syntax via R?
我有一个奇怪的情况,请原谅我没有为这个问题提供一个可重现的例子。我有 1000 多行为 Stata 编写的语法来进行多次分析(我在开始使用 R 之前就写了)。此语法用于每 3 个月对季度数据集执行分析以创建报告。分析结果保存在 csv 文件中,通过 R 读取,并使用 ReporterS 包放入 Word 文档。
有没有什么方法可以通过 R 调用 Stata,以及 specify/pipe 运行它的语法? (我知道相反的情况可以在 Stata 中使用 rsource
(用户编写的命令)来完成)。我仍然可以手动启动 Stata 并在那里运行语法。但是有可能通过 R 来实现吗?所以,可以创建一个闪亮的 app/web 界面来完成这部分,而用户不需要手动完成?
正如@thelatemail 所建议的,这里最简单的事情就是 运行 Stata in batch mode 来自 system
调用。
这是一个示例文件(名为 "example.do"
):
log using out.log, replace
sysuse auto
regress mpg weight foreign
这是 运行 的 R 代码并检索输出(假设 Stata 在您的路径上并且您将 Stata-64
替换为计算机上适当的二进制文件):
> system("Stata-64 /e do example.do"); readLines("out.log")
[1] "-----------------------------------------------------------------------------------------------------------------------"
[2] " name: <unnamed>"
[3] " log: FilePathHere"
[4] " log type: text"
[5] " opened on: 9 Jan 2015, 13:34:18"
[6] ""
[7] ". sysuse auto"
[8] "(1978 Automobile Data)"
[9] ""
[10] ". regress mpg weight foreign"
[11] ""
[12] " Source | SS df MS Number of obs = 74"
[13] "-------------+------------------------------ F( 2, 71) = 69.75"
[14] " Model | 1619.2877 2 809.643849 Prob > F = 0.0000"
[15] " Residual | 824.171761 71 11.608053 R-squared = 0.6627"
[16] "-------------+------------------------------ Adj R-squared = 0.6532"
[17] " Total | 2443.45946 73 33.4720474 Root MSE = 3.4071"
[18] ""
[19] "------------------------------------------------------------------------------"
[20] " mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]"
[21] "-------------+----------------------------------------------------------------"
[22] " weight | -.0065879 .0006371 -10.34 0.000 -.0078583 -.0053175"
[23] " foreign | -1.650029 1.075994 -1.53 0.130 -3.7955 .4954422"
[24] " _cons | 41.6797 2.165547 19.25 0.000 37.36172 45.99768"
[25] "------------------------------------------------------------------------------"
[26] ""
[27] ". "
[28] "end of do-file"
[29] ""
[30] ". exit, clear"
如果您使用 Stata 标记控制语言 (SMCL) 登录,通过将 do 文件的第一行替换为 log using out.log, replace smcl
,可能更容易解析输出。那么输出将是:
[1] "{smcl}"
[2] "{com}{sf}{ul off}{txt}{.-}"
[3] " name: {res}<unnamed>"
[4] " {txt}log: {res}FilePathHere"
[5] " {txt}log type: {res}smcl"
[6] " {txt}opened on: {res} 9 Jan 2015, 13:41:53"
[7] "{txt}"
[8] "{com}. sysuse auto"
[9] "{txt}(1978 Automobile Data)"
[10] ""
[11] "{com}. regress mpg weight foreign"
[12] ""
[13] " {txt}Source {c |} SS df MS Number of obs ={res} 74"
[14] "{txt}{hline 13}{char +}{hline 30} F( 2, 71) ={res} 69.75"
[15] " {txt} Model {char |} {res} 1619.2877 2 809.643849 {txt}Prob > F = {res} 0.0000"
[16] " {txt}Residual {char |} {res} 824.171761 71 11.608053 {txt}R-squared = {res} 0.6627"
[17] "{txt}{hline 13}{char +}{hline 30} Adj R-squared = {res} 0.6532"
[18] " {txt} Total {char |} {res} 2443.45946 73 33.4720474 {txt}Root MSE = {res} 3.4071"
[19] ""
[20] "{txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"
[21] "{col 1} mpg{col 14}{c |} Coef.{col 26} Std. Err.{col 38} t{col 46} P>|t|{col 54} [95% Con{col 67}f. Interval]"
[22] "{hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"
[23] "{space 6}weight {c |}{col 14}{res}{space 2}-.0065879{col 26}{space 2} .0006371{col 37}{space 1} -10.34{col 46}{space 3}0.000{col 54}{space 4}-.0078583{col 67}{space 3}-.0053175"
[24] "{txt}{space 5}foreign {c |}{col 14}{res}{space 2}-1.650029{col 26}{space 2} 1.075994{col 37}{space 1} -1.53{col 46}{space 3}0.130{col 54}{space 4} -3.7955{col 67}{space 3} .4954422"
[25] "{txt}{space 7}_cons {c |}{col 14}{res}{space 2} 41.6797{col 26}{space 2} 2.165547{col 37}{space 1} 19.25{col 46}{space 3}0.000{col 54}{space 4} 37.36172{col 67}{space 3} 45.99768"
[26] "{txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"
[27] "{res}{txt}"
[28] "{com}. "
[29] "{txt}end of do-file"
我有一个奇怪的情况,请原谅我没有为这个问题提供一个可重现的例子。我有 1000 多行为 Stata 编写的语法来进行多次分析(我在开始使用 R 之前就写了)。此语法用于每 3 个月对季度数据集执行分析以创建报告。分析结果保存在 csv 文件中,通过 R 读取,并使用 ReporterS 包放入 Word 文档。
有没有什么方法可以通过 R 调用 Stata,以及 specify/pipe 运行它的语法? (我知道相反的情况可以在 Stata 中使用 rsource
(用户编写的命令)来完成)。我仍然可以手动启动 Stata 并在那里运行语法。但是有可能通过 R 来实现吗?所以,可以创建一个闪亮的 app/web 界面来完成这部分,而用户不需要手动完成?
正如@thelatemail 所建议的,这里最简单的事情就是 运行 Stata in batch mode 来自 system
调用。
这是一个示例文件(名为 "example.do"
):
log using out.log, replace
sysuse auto
regress mpg weight foreign
这是 运行 的 R 代码并检索输出(假设 Stata 在您的路径上并且您将 Stata-64
替换为计算机上适当的二进制文件):
> system("Stata-64 /e do example.do"); readLines("out.log")
[1] "-----------------------------------------------------------------------------------------------------------------------"
[2] " name: <unnamed>"
[3] " log: FilePathHere"
[4] " log type: text"
[5] " opened on: 9 Jan 2015, 13:34:18"
[6] ""
[7] ". sysuse auto"
[8] "(1978 Automobile Data)"
[9] ""
[10] ". regress mpg weight foreign"
[11] ""
[12] " Source | SS df MS Number of obs = 74"
[13] "-------------+------------------------------ F( 2, 71) = 69.75"
[14] " Model | 1619.2877 2 809.643849 Prob > F = 0.0000"
[15] " Residual | 824.171761 71 11.608053 R-squared = 0.6627"
[16] "-------------+------------------------------ Adj R-squared = 0.6532"
[17] " Total | 2443.45946 73 33.4720474 Root MSE = 3.4071"
[18] ""
[19] "------------------------------------------------------------------------------"
[20] " mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]"
[21] "-------------+----------------------------------------------------------------"
[22] " weight | -.0065879 .0006371 -10.34 0.000 -.0078583 -.0053175"
[23] " foreign | -1.650029 1.075994 -1.53 0.130 -3.7955 .4954422"
[24] " _cons | 41.6797 2.165547 19.25 0.000 37.36172 45.99768"
[25] "------------------------------------------------------------------------------"
[26] ""
[27] ". "
[28] "end of do-file"
[29] ""
[30] ". exit, clear"
如果您使用 Stata 标记控制语言 (SMCL) 登录,通过将 do 文件的第一行替换为 log using out.log, replace smcl
,可能更容易解析输出。那么输出将是:
[1] "{smcl}"
[2] "{com}{sf}{ul off}{txt}{.-}"
[3] " name: {res}<unnamed>"
[4] " {txt}log: {res}FilePathHere"
[5] " {txt}log type: {res}smcl"
[6] " {txt}opened on: {res} 9 Jan 2015, 13:41:53"
[7] "{txt}"
[8] "{com}. sysuse auto"
[9] "{txt}(1978 Automobile Data)"
[10] ""
[11] "{com}. regress mpg weight foreign"
[12] ""
[13] " {txt}Source {c |} SS df MS Number of obs ={res} 74"
[14] "{txt}{hline 13}{char +}{hline 30} F( 2, 71) ={res} 69.75"
[15] " {txt} Model {char |} {res} 1619.2877 2 809.643849 {txt}Prob > F = {res} 0.0000"
[16] " {txt}Residual {char |} {res} 824.171761 71 11.608053 {txt}R-squared = {res} 0.6627"
[17] "{txt}{hline 13}{char +}{hline 30} Adj R-squared = {res} 0.6532"
[18] " {txt} Total {char |} {res} 2443.45946 73 33.4720474 {txt}Root MSE = {res} 3.4071"
[19] ""
[20] "{txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"
[21] "{col 1} mpg{col 14}{c |} Coef.{col 26} Std. Err.{col 38} t{col 46} P>|t|{col 54} [95% Con{col 67}f. Interval]"
[22] "{hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"
[23] "{space 6}weight {c |}{col 14}{res}{space 2}-.0065879{col 26}{space 2} .0006371{col 37}{space 1} -10.34{col 46}{space 3}0.000{col 54}{space 4}-.0078583{col 67}{space 3}-.0053175"
[24] "{txt}{space 5}foreign {c |}{col 14}{res}{space 2}-1.650029{col 26}{space 2} 1.075994{col 37}{space 1} -1.53{col 46}{space 3}0.130{col 54}{space 4} -3.7955{col 67}{space 3} .4954422"
[25] "{txt}{space 7}_cons {c |}{col 14}{res}{space 2} 41.6797{col 26}{space 2} 2.165547{col 37}{space 1} 19.25{col 46}{space 3}0.000{col 54}{space 4} 37.36172{col 67}{space 3} 45.99768"
[26] "{txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"
[27] "{res}{txt}"
[28] "{com}. "
[29] "{txt}end of do-file"