简体   繁体   中英

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. So the code looks like this:

I have three predicting variables: age1 (continuous), gender1 (categorical 0/1), FLUSHOT(categorical 0/1)

In the file, the first 100 columns are response variables (all categorical 0/1) I want to test.

The goal is to do regression analysis with each of the response variables(1:100), and only output p-value, OR, and 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.

What error message do you see? You can try using lrm from the rms package to estimate the logistic regression model. And texreg to output it to csv.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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