Re: [R] using newdata in survfit with categorical variable

From: <Bill.Venables_at_csiro.au>
Date: Tue, 11 Nov 2008 12:09:16 +1100

This is a bit tricky, but you need to specify the full set of possible levels for the factor in newdata. Try this

temp <- data.frame(gender = factor("Male", levels = levels(wlwsn1$gender))) wlwsn1curve <- survfit(fit, newdata=temp)

(Warning: untested code.)

Bill Venables
http://www.cmis.csiro.au/bill.venables/

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Zhixin Liu Sent: Tuesday, 11 November 2008 10:58 AM To: r-help_at_r-project.org
Subject: [R] using newdata in survfit with categorical variable

Hi R-helpers,

I was trying to put gender='Male' in newdata to create a expected survival curve for a pseudo cohort by using survfit based on Cox regression. My codes are shown below:

fit<- coxph(Surv(end, status2)~gender, data=wlwsn1) Summary(fit)

                     coef exp(coef) se(coef)    z     p
genderMale 0.204      1.23   0.0912 2.23 0.025

temp<-data.frame(gender='Male)
wlwsn1curve<-survfit(fit, newdata=temp)
Then I got error message:
Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :   contrasts can be applied only to factors with 2 or more levels

I do not know what this error message indicates, do I have to recode gender to 0,1 to get it through?

Many thanks

Zhixin



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 11 Nov 2008 - 01:12:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 11 Nov 2008 - 01:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive