[英]loop regression columns r
I have a dataframe like this one我有一个像这样的 dataframe
ID matching_var status code1 code2
1 1 0 1 0
2 1 1 1 0
3 2 0 0 1
I have several other columns like code1 and code2, up to code25 and I would like to do these regressions:我还有其他几个列,如 code1 和 code2,直到 code25,我想进行这些回归:
fit1<-clogit(status~code1+strata(matching_variable),data=df)
fit2<-clogit(status~code2+strata(matching_variable),data=df)
… ……
fit25<-clogit(status~code25+strata(matching_variable),data=df
My variables code 1 to code25 are in columns 4 to 29 of my df我的变量代码 1 到代码 25 在我的 df 的第 4 到 29 列中
I would like to find a way to automate this without having to type in each regression model, and I would like to have all the regression results in one table我想找到一种无需在每次回归中键入 model 即可自动执行此操作的方法,并且我希望将所有回归结果放在一个表中
I have tried this:我试过这个:
regression <- function(x){code<- x[,4:29]
f <- as.formula(
paste("status ~", code, "+ strata(matching_var)"))
clogit(f, data = x)
}
result_reg<-lapply(df,regression)
lapply(result_reg, summary)
But it doesn't work, several other posts deal with the same subject but I haven't managed to find a solution to my problem...但它不起作用,其他几个帖子处理同一主题,但我还没有设法找到解决我的问题的方法......
Thanks in advance for the help先谢谢您的帮助
Try this.尝试这个。 I used the mtcars
dataset since you did not post your data.我使用了mtcars
数据集,因为你没有发布你的数据。 Furthermore, explicitly call library
when you use non-common function like clogit
此外,当您使用像clogit
这样的非常见 function 时显式调用library
df <- mtcars
library(survival)
regression <- function(column){
f <- as.formula(
paste("vs ~", column, "+ strata(carb)"))
survival::clogit(f, data = df)
}
result_reg<-lapply(colnames(df)[9:11],regression)
lapply(result_reg, summary)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.