I would like to get output of anova.rms into a data.frame.
library("rms")
# generate data taken from ?cph
n <- 1000
set.seed(731)
age <- 50 + 12*rnorm(n)
label(age) <- "Age"
sex <- factor(sample(c('Male','Female'), n,rep=TRUE, prob=c(.6, .4)))
cens <- 15*runif(n)
h <- .02*exp(.04*(age-50)+.8*(sex=='Female'))
dt <- -log(runif(n))/h
label(dt) <- 'Follow-up Time'
e <- ifelse(dt <= cens,1,0)
dt <- pmin(dt, cens)
units(dt) <- "Year"
dd <- datadist(age, sex)
options(datadist='dd')
S <- Surv(dt,e)
f <- cph(S ~ rcs(age,4)*sex, x=TRUE, y=TRUE)
anovaTable <- anova(f,main.effect=T,indnl=F)
# the table I want
anovaTable
data.frame(anovaTable) does not work because of the duplicate row names. I searched str(anovaTable), but it was not clear how to obtain it. Thanks.
You can use the make.unique
function to make the row names unique.
as.data.frame(anovaTable, row.names = make.unique(rownames(anovaTable)))