[R] Defining reference category for a cph model summary inside of a "for" loop

Frank E Harrell Jr f.harrell at vanderbilt.edu
Sun Mar 30 15:14:20 CEST 2008


Wells, Brian wrote:
> Dr. Harrell, 
> Thanks for you help. 
> 
> I tried:
> 
>> print(summary(f,parse(text=paste(i,'="1st Quartile"', sep=''))))
> 
> Same result. No error, the reference category simply doesn't change. 

That's good, because the default in summary is to compare the outer 
quartiles for a continuous variable.  And as I said before the string 
'1st Quartile' has no special meaning for R or Design.

Get what you are trying to do to work without parse (and you'll need 
eval() with parse) first.  When you want total control over a setting, 
say getting a hazard ratio for the .2 to the .8 quantile, do something like

summary(f, age=quantile(age,c(.2,.8),na.rm=TRUE))

Frank

> 
> Brian 
> 
> -----Original Message-----
> From: Frank E Harrell Jr [mailto:f.harrell at vanderbilt.edu] 
> Sent: Friday, March 28, 2008 8:34 PM
> To: Wells, Brian
> Cc: r-help at r-project.org
> Subject: Re: [R] Defining reference category for a cph model summary
> inside of a "for" loop
> 
> Wells, Brian wrote:
>> I have the following code. 
>>
>>  
>>
>>  
>>
>>> f <- cph(formula = Surv(TimeToDeath, Dead == "Yes")
>> ~1,data=single.dat, x=T, y=T, surv=T)
>>
>>> for(i in c('A', 'B', 'C', 'D', 'E', 'F')){
>>> f <-update(f,as.formula(paste('Surv(TimeToDeath, Dead ==
>> "Yes")~',i,sep='')))
>>
>>> print(summary(f, paste(i,"=1st Quartile", sep='')))
>>  
>>
>>  
>>
>> There is no error message generated in R, but R ignores the reference
>> category defined with paste in the summary function for the cph model.
> 
>>  
>>
>> The output uses the "1st Quartile" as the reference category to
>> calculate hazards for some of the variables defined by i, but not all
> of
>> them. 
> 
> 
> Your code is confusing.  What is to the right of ~ in a formula is a 
> predictor variable name, not a value.  If your variables are named A, B,
> 
> C, ... you are OK.
> 
> '1st Quartile' has no special meaning to R or Design, and you can't pass
> 
> a character string as a second argument to summary and expect it to
> work.
> 
> You will need parse(text=paste(...)) to create an appropriate
> expression.
> 
> But Design gives you inter-quartile range hazard ratios by default
> anyway.
> 
> Beware of getting hazard ratios that are not adjusted for other 
> variables needed in the model.
> 
> Frank Harrell
> 
>>  
>>
>>  
>>
>> Any help would be greatly appreciated. 
>>
>>  
>>
>> thanks
>>
>>  
>>
>> Brian J. Wells, MD, MS
>>
>> Research Associate
>>
>> Quantitative Health Sciences
>>
>> Cleveland Clinic
>>
> 


-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University



More information about the R-help mailing list