简体   繁体   中英

Tobit regression in R, while looping regressions my data is not censored using censReg

hive mind!

I am looping regression analysis for multiple data sets (for each antibiotic and concentration). Some of the data is censored at the lower limit of 3.912023. So I need to use censReg() instead of lm() in those cases.

Even though multiple subsets are including the lower limit the model output is the lm() for each regression.

Any thoughts on what I am doing wrong here?

model.summary<- tibble()

for(j in unique(myData$antibiotic)){
        abx<- filter(myData, antibiotic == j)
        for(i in unique(abx$concentration)){
            if (min(abx$datapoints) == 3.912023){
                conc<-filter(abx, concentration == i)
                model<-censReg(x ~ y, data = conc, left = 3) 
                model.coef<-coef(model)[2] 
                model.list<-c(j,i ,model.coef)
                model.summary<-bind_rows(model.summary, model.list)
} else {
                conc<-filter(abx, concentration == i)
                model<-lm(x ~ y, data = conc)
                model.coef<-coef(model)[2] 
                model.list<-c(j,i, model.coef)
                model.summary<-bind_rows(model.summary, model.list)
    }   

     }
}
antibiotic (chr) conc (fct) x (dbl) y (dbl)
B 2 0 14.1
B 2 0.167 12.0
B 2 0.5 9.58
B 2 1 8.40
B 2 2 6.29
B 2 5 3.91
B 4 0 14.1
B 4 0.167 4.61
B 4 0.5 3.91
B 4 1 3.91
B 4 2 3.91
B 4 5 3.91

I suggest that you figure out in which round of the loop the problem occurs. Then, you can investigate the regression analysis in this selected round of the loop and provide a minimal example that illustrates the problem. (Sorry that I cannot post my comment as a "comment" because I have not yet "50 reputation".)

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