4

我有一个奇怪的情况,请原谅我没有为这个问题提供一个可重复的例子。我为 Stata 编写了 1000 多行语法来进行多重分析(我在开始使用 R 之前编写了它)。此语法用于每 3 个月在季度数据集中执行分析以创建报告。分析结果保存在 csv 文件中,并通过 R 读取,并使用 ReporterS 包放入 Word 文档。

有没有办法通过 R 调用 Stata,并指定/管道语法来运行它?rsource(我知道可以在Stata中使用(用户编写的命令)来完成相反的情况)。我仍然可以手动启动 Stata 并在那里运行语法。但是可以通过 R 来实现吗?那么,可以创建一个闪亮的应用程序/Web 界面来完成这部分工作,而用户不需要手动完成吗?

4

1 回答 1

3

正如@thelatemail 建议的那样,这里最简单的事情就是通过调用以批处理模式运行Stata。system

这是一个示例 do 文件(称为"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"
于 2015-01-09T12:44:03.010 回答