简体   繁体   English

处理功能错误,通知然后跳过

[英]Handling error in function, notify then skip

I am having trouble process a function that can both output regression summary to csv files, and process regression analysis. 我在处理一个函数时遇到麻烦,该函数既可以将回归摘要输出到csv文件,也可以进行回归分析。 So the code looks like this: 因此,代码如下所示:

I have three predicting variables: age1 (continuous), gender1 (categorical 0/1), FLUSHOT(categorical 0/1) 我有三个预测变量:age1(连续),gender1(类别0/1),FLUSHOT(类别0/1)

In the file, the first 100 columns are response variables (all categorical 0/1) I want to test. 在文件中,前100列是我要测试的响应变量(均为0/1类别)。

The goal is to do regression analysis with each of the response variables(1:100), and only output p-value, OR, and CI. 目标是使用每个响应变量(1:100)进行回归分析,并且仅输出p值,OR和CI。

So the code I have is something looks like this: 所以我拥有的代码如下所示:

fun1<-function(x){
  res<-c(paste(as.character(summary(x)$call),collapse = " "),
         summary(x)$coefficients[4,4],
         exp(coef(x))[4],
         exp(confint(x))[4,1:2],"\n")

  names(res)<-c("call","p-value","OR","LCI","UCI","")
  return(res)}


res2=NULL
lms=list()
for(i in 1:100)
{
  lms[[i]]=glm(A[,i]~age1+gender1+as.factor(FLUSHOT),family="binomial",data=A)               
  res2<-rbind(res2,fun1(lms[[i]]))
}
write.csv(res2,"A_attempt1.csv",row.names=F)

If for example, we have sufficient sample size in each categories, or if the marginal frequency looks like this: 例如,如果我们在每个类别中都有足够的样本量,或者边际频率如下所示:

table(variable1,FLUESHOT)
   0    1
0  15   3
1  11   19

This code works well, but if we have something like: 这段代码很好用,但是如果我们有类似的东西:

 table(variable15,FLUESHOT)
       0    1
    0  15   0
    1  11   19

The code run into a error, report, and stops. 代码遇到错误,报告并停止。

I tried multiple ways of using try() and tryCatch() , but didn't seems to work for me. 我尝试了使用try()tryCatch()多种方法,但似乎对我没有用。

What error message do you see? 您看到什么错误消息? You can try using lrm from the rms package to estimate the logistic regression model. 您可以尝试使用rms包中的lrm来估计逻辑回归模型。 And texreg to output it to csv. 和texreg将其输出到csv。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM