将Stata翻译为R:崩溃

时间:2015-06-22 19:13:23

标签: r data.table stata code-translation

我遇到了.do文件需要转换为R,因为我没有Stata许可证;我的Stata生锈了,那么有人可以确认代码是按我认为的那样做的吗?

为了重现性,我会将其翻译成我在网上找到的数据集,特别是Chatterjee,Hadi和Price的教科书Milk Production dataset (p004)

这是Stata代码:

collapse (min) min_protein = protein /// 
         (mean) avg_protein = protein /// 
         (median) median_protein = protein /// 
         (sd) sd_protein = protein /// 
         if protein > 2.8, by(lactatio)

以下是我认为用data.table语法进行的操作:

library(data.table)
library(foreign)
DT = read.dta("p004.dta")
setDT(DT)

DT[protein > 2.8,
   .(min_protein = min(protein),
     avg_protein = mean(protein),
     median_protein = median(protein),
     sd_protein = sd(protein)),
   keyby = lactatio]

#    lactatio min_protein avg_protein median_protein sd_protein
# 1:        1         2.9    3.162632           3.10  0.2180803
# 2:        2         2.9    3.304688           3.25  0.2858736
# 3:        3         2.9    3.371429           3.35  0.4547672
# 4:        4         2.9    3.231250           3.20  0.3419917
# 5:        5         2.9    3.855556           3.20  1.9086061
# 6:        6         3.0    3.200000           3.10  0.2645751
# 7:        7         3.3    3.650000           3.65  0.4949748
# 8:        8         3.2    3.300000           3.30  0.1414214

这是对的吗?

如果我在过去18个月内使用过Stata,或者我安装了副本,这很容易确认 - 希望我可以弯曲某人的耳朵。感谢。

3 个答案:

答案 0 :(得分:4)

以下是样本数据的Stata输出,与data.table输出相同:

collapse (min) min_protein = protein /// 
         (mean) avg_protein = protein /// 
         (median) median_protein = protein /// 
         (sd) sd_protein = protein /// 
         if protein > 2.8, by(lactatio)

   lactatio min_protein avg_protein median_protein  sd_protein
     1  2.9 3.162632    3.1 0.2180803
     2  2.9 3.304688    3.25 0.2858736
     3  2.9 3.371429    3.35 0.4547672
     4  2.9 3.23125     3.2 0.3419917
     5  2.9 3.855556    3.2 1.908606
     6  3   3.2         3.1 0.2645752
     7  3.3 3.65        3.65 0.4949748
     8  3.2 3.3         3.3 0.1414214

这里是data.table输出(只是为了确保我使用正确的数据)

    library(foreign) #reading Stata data
    data<-read.dta("p004.dta")
    setkey(setDT(data),lactatio)
    setDT(data)[protein>2.8,
                   .(min_protein=min(protein),
                     avg_protein=mean(protein),
                     median_protein=median(protein),
                     sd_protein=sd(protein)),
                   by=lactatio]

   lactatio min_protein avg_protein median_protein sd_protein
1:        1         2.9    3.162632           3.10  0.2180803
2:        2         2.9    3.304688           3.25  0.2858736
3:        3         2.9    3.371429           3.35  0.4547672
4:        4         2.9    3.231250           3.20  0.3419917
5:        5         2.9    3.855556           3.20  1.9086061
6:        6         3.0    3.200000           3.10  0.2645751
7:        7         3.3    3.650000           3.65  0.4949748
8:        8         3.2    3.300000           3.30  0.1414214
> 

答案 1 :(得分:1)

你的直觉是正确的。 collapse是R&#39; aggregate函数的Stata等价物,它通过将聚合函数(或多个聚合函数,每个变量一个)应用于每个变量,从输入数据集生成一个新数据集。数据集。

这里是示例数据集上该Stata命令的输出:

. list

     +------------------------------------------------------+
     | lactatio   min_pr~n   avg_pr~n   median~n   sd_pro~n |
     |------------------------------------------------------|
  1. |        1        2.9   3.162632        3.1   .2180803 |
  2. |        2        2.9   3.304688       3.25   .2858736 |
  3. |        3        2.9   3.371429       3.35   .4547672 |
  4. |        4        2.9    3.23125        3.2   .3419917 |
  5. |        5        2.9   3.855556        3.2   1.908606 |
     |------------------------------------------------------|
  6. |        6          3        3.2        3.1   .2645752 |
  7. |        7        3.3       3.65       3.65   .4949748 |
  8. |        8        3.2        3.3        3.3   .1414214 |
     +------------------------------------------------------+

答案 2 :(得分:0)

stata.collapse<-function(data,vars,newnames,stat,by) {
m=match(by,names(data))
data1=data[m]
x=length(by)
l=length(stat)

for (i in 1:l){
   nn=aggregate(data[vars[i]],by=data1,stat[i],na.rm=TRUE) 
   d=names(nn)
   d[ncol(data1)+1]<-newnames[i]
    names(nn)<-d
     xx1=nn[1:x]
     xx=nn[-(1:x)]
         if (i>1) {
         x2=cbind(x2,xx)
         }else {
        x2=nn
}
}

return(x2)
}

要运行,请像这样调用此函数

  h=stata.collapse(roster,c("idcode1","age","age") , 
    c("hhsize","meanage","maxage"),c("max","mean","max"),c("psu","hhno"))