-2

有没有办法在 R 中执行以下 Stata 代码?

我想创建一个交错模型,我想将 1994 年某些变量的值用于 1998 年 40 岁的人;并希望将 1996 年相同变量的值用于 2000 年年满 40 岁的人。

for any out temp emp edu married inc age \ var cesd1998 bitemp96 employ94 edu93 married94 inc94 age94 : gen XM2=Y if H0000200==1998

for any out temp emp edu married inc age \ var cesd2000 bitemp98 employ96 edu95 married96 inc96 age96 : replace XM2=Y if H0000200==2000
4

1 回答 1

1

这不是答案,但不会轻易放入评论中。我什至不尝试 R 代码。在我看来,流利的 R 编码人员,而不是我,可以合理地期望,作为绝对最低限度,你如何在 R 中保存数据的一些清晰度。

此处的 Stata 语法远非最新,但在 Stata 7for中已过时。从某种意义上说,此处甚至不再记录。

这不是一个最小的、完整的、可验证的例子:https ://stackoverflow.com/help/mcve

for any out temp emp edu married inc age \ var cesd1998 bitemp96 employ94 edu93 married94 inc94 age94 : gen XM2=Y if H0000200==1998

for any out temp emp edu married inc age \ var cesd2000 bitemp98 employ96 edu95 married96 inc96 age96 : replace XM2=Y if H0000200==2000

当前Stata的一种翻译是

 local x1list "out temp emp edu married inc age" 
 local x2list "out temp emp edu married inc age" 
 local y1list "cesd1998 bitemp96 employ94 edu93 married94 inc94 age94" 
 local y2list "cesd2000 bitemp98 employ96 edu95 married96 inc96 age96" 
 local nvars : word count `x1list' 

 forval j = 1/`nvars' { 
     local x : word `j' of `x1list' 
     local y : word `j' of `y1list' 
     replace `x'M2 = `y' if H0000200==1998
     local x : word `j' of `x2list' 
     local y : word `j' of `y2list'
     replace `x'M2 = `y' if H0000200==2000    
 } 

一点也不重要,但请注意,代码如此尴尬的一个原因是您对变量的命名约定不一致。

于 2016-03-18T08:59:28.817 回答