使用lapply在R中批量运行clogit

时间:2015-07-23 05:49:20

标签: r apply lapply

我正在尝试使用R中的survival {clogit}来运行条件逻辑回归。因为我在我的数据集中有大约300个要回归的特征,我试图在循环中运行clogit。但是当我通过在每次迭代中改变特征来使用它时,似乎clogit不会运行。一个例子如下:

intervention<-c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1)
match_id<-c(10019,10019,10026,10026,10051,10051,10155,10155,10156,10156,10191,10191,10276,10276,10352,10352,10398,10398,10448,10448)
AAA<-c(0.561882338,0.590773332,0.280532865,0.28826806,0.33906137,0.242355092,0.340010932,0.352662714,0.324693609,0.322127852,0.338397267,0.697613063,0.363248497,0.391860659,0.374711683,0.366319323,0.335986428,0.390359798,0.492056527,0.652158866)
BBB<-c(0.069485923,0.067290724,0.030931896,0.032030524,0.047487704,0.041788871,0.04246649,0.034830486,0.02499338,0.03231839,0.033805974,0.074300373,0.051343724,0.04842683,0.040587732,0.041552691,0.026273574,0.034315159,0.063913729,0.065222581)

df_clogit<-data.frame(intervention,match_id,AAA,BBB)

IV<-colnames(df_clogit)[3:dim(df_clogit)[2]] 
DV<-colnames(df_clogit)[1]

my.clogit.formula <- lapply(IV, function(var) {paste(DV,"~",var," +   strata(match_id)")})
listcLogitOut<-lapply(my.clogit.formula, function(f) clogit(f,df_clogit))
  

错误:$运算符对原子矢量无效

my.clogit.formula <- lapply(IV, function(var) {paste(DV,"~",var," + strata(match_id),data=df_clogit")})
listcLogitOut<-lapply(my.clogit.formula, function(f) clogit(f))
  

解析时出错(text = x,keep.source = FALSE):   :1:39:意想不到的&#39;,&#39;

但是,如果我使用my.clogit.formula的一个元素并将其运行,那么它的工作方式就可以了!

clogit(intervention ~ BBB  + strata(match_id),data=df_clogit)

我做错了什么?是否有替代从lapply内部运行clogit或使用任何apply函数?

1 个答案:

答案 0 :(得分:1)

你必须使用公式作为clogit的参数

   listcLogitOut<-lapply(my.clogit.formula, function(f) clogit(as.formula(f),data=df_clogit))