[R] Obtaining partial output from a function that does not run to completion.

David Winsemius dwinsemius at comcast.net
Thu Dec 31 16:21:25 CET 2009


On Dec 31, 2009, at 8:29 AM, John Sorkin wrote:

> I have written a function that contains runs
> lm()
> vif()  and
> glm()
>
> When the glm() blows up with an error message, I don't get the  
> output from either the lm() or vf() even thought neither lm() nor  
> vif() have any problems . How can I force the function to print  
> sequential results rather than wait for the entire function to  
> complete before listing the functhion's output?
> Thanks,
> John

I thought the usual method for graceful error recovery was to use the  
try or tryCatch functions.
>
>
minBMI<-function(SS,SimData)
{
SampleData<-sample(1:SS,size=SS,replace=TRUE)
fitBMIEpiRevlm<-lm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI  
+jMEDICAID+H_AGE+jMARSTAT +factor(jEDUCATION) +factor(jsmokercat) 
+factor(jrace)+log(INCOME_C+1),data=SimData[SampleData,],x=TRUE)
print(summary(fitBMIEpiRevlm))
print(vif(fitBMIEpiRevlm))
#Perhaps (untested):
try( {
fitBMIEpiRev<- 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"))

print(summary(fitBMIEpiRev))
       } )
}
minBMI(SS,SimData)

-- 
David.


> John David Sorkin M.D., Ph.D.
> Chief, Biostatistics and Informatics




More information about the R-help mailing list