Bianca Barth
Bianca Barth

Reputation: 1

How to get my cox.zph(cpfit.sd) output as a csv file instead of just in the console

Here is the script for my cox model:

# Fit a Cox model (Use data=int.data if you want to include the intensities from a measurement channel in the cox model)
cpfit.sd <- coxph(Surv(Time,Event)~Sci_SampleID,data=prim.data)
cph.pval <- summary(cpfit.sd)$coef
cph.hr <- summary(cpfit.sd)$coefficients
summary(cpfit.sd)
cox.zph(cpfit.sd)

lrtest <- survdiff(Surv(Time, as.logical(Event)) ~ Sci_SampleID,data=prim.data,rho=0) lrtest

I would like to get my output data from the above exported as a csv file.

I was able to do the following: write.csv(cph.hr,paste(root.dir,output.file,sep=""))

however this does not give me the full data set, and it won't allow me to do a similar function for the cox.zph file. I always receive this error:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ‘"cox.zph"’ to a data.frame

Thank you for your help!

Upvotes: 0

Views: 697

Answers (1)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

I feel broom::tidy could be easier than summary.

cph.hr <- broom::tidy(fit, conf.int = TRUE, exponetiante = TRUE)
write.csv(cph.hr, "cph_hr.csv")

For a cox.zph object, you could try:

zph<- cox.zph(cpfit.sd)$table
write.csv(zph, "zph.csv")

Upvotes: 0

Related Questions