Help with tryCatch
Dr Sorkin; You are a smart person. Why are you unable to create a working example that would allow testing of the code structure?
On Jan 1, 2010, at 10:27 PM, John Sorkin wrote:
coeffs <- matrix(nrow=10, ncol=3)
dimnames(coeffs) < -list(NULL,c("BMI","BMIsq","min"))
# set row counter
n<-0
# Function to run
testone<-function()
{
SampleData<-sample(1:SS,size=SS,replace=TRUE)
# a simple dataset
result<-summary(glm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI +jMEDICAID+H_AGE+jMARSTAT+factor(jEDUCATION)+factor(jsmokercat) +factor(jrace)+log(INCOME_C +1),data=SimData[SampleData,],family=Gamma(link="log"))) }
# and a simple model would be more appropriate
# if function ran correctly, increment row counter by one and store results # Do this 10 times
# Try this untested (for lack of a working example) code:
# Do this 10 times
while (n<10) {
resulttry<-try(testone(),final=n<-n+1)
# Do the function ran without an error
if (class(resulttry) == "summary.glm" ){ #test for something that
will distinguish
#the error (of type=="list") from a valid fit (also of type == "list")
coeffs[n,1] <- result$coefficients[2,1]
coeffs[n,2] <- result$coefficients[3,1]
coeffs[n,3] <- -(coeffs[n,1])/(2*coeffs[n,2])
print(n)
print(coeffs)} else { }
}
David Winsemius, MD. MPH
> while (n<10) {
> result<-tryCatch(testone(),final=n<-n+1)
>
> # Do the function ran without an error
> result
> coeffs[n,1] <- result$coefficients[2,1]
> coeffs[n,2] <- result$coefficients[3,1]
> coeffs[n,3] <- -(coeffs[n,1])/(2*coeffs[n,2])
> print(n)
> print(coeffs)
>
> # if function returned with either a warning or error do not
> increment row counter, do not store results
> # I have no idea how to get to this code!
> # I need to know if an error code has been raised
> }