I'm running a lot of models and some throw errors. I want to save all outputs and all errors. However, my attemts with tryCatch
coming from here led to a code which either returns the warning or, if there is no warning, returns and creates the object. Here is the code:
DT <- iris
str(DT)
DT$binary <- as.numeric(DT$Petal.Width>1)
DT$dummy <- as.numeric(as.numeric(DT$Species)>2)
a model with a warning returns the warning but does not retain the logit model in the environment:
test <- tryCatch(
logit1 <- glm(binary~Sepal.Length+dummy,data = DT, family = binomial( link = 'logit')),
warning = function( w ) { conditionMessage( w ) } )
a model without a warning creates the logit object in the environment but when I save this tryCatch
as an object, it is the whole logit output.
test <- tryCatch(
logit2 <- glm(binary~Sepal.Length+Sepal.Width,data = DT, family = binomial( link = 'logit')),
warning = function( w ) { conditionMessage( w ) } )
as if tryCatch
is not even there (which I guess makes sense as the condition is not fulfilled). I'm looking for code which gives me nothing, a 0, or NA if there is no warning but the warning message if there is a warning and which creates the named logit model no matter if there is a warning or not. Somehow, tryCatch
does not seem to be very suitable for this, ideally, I'd just run the logit and save the warning message if that is possible?