Message-ID: <C3FD87C3-12EC-4A0E-8486-4847A925BB6F@comcast.net>
Date: 2010-01-02T04:54:00Z
From: David Winsemius
Subject: Help with tryCatch
In-Reply-To: <4B3E76CE020000CB00058F58@medicine.umaryland.edu>
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
> }