`as.data.frame.markov_msm()` stops with error (corner case)
Closed this issue · 1 comments
anddis commented
Thank you, Mark, for this great R package!
This bug bites when one tries to convert to data frame a markov_msm
object returned by the standardise()
function, but only if newdata
passed to standardise()
has 1 column.
Error in `[.default`(x$newdata, as.numeric(out$obs), , drop = FALSE) :
incorrect number of dimensions
The error seems to be due to the class of the object x$newdata <- x$newdata[0, ]
returned by standardise()
, which for a 1-column data frame is:
> df <- data.frame(rx = levels(survival::colon$rx))
> df[0, ] |> class()
[1] "character"
while for data frames with >1 column, is:
> df <- data.frame(rx = levels(survival::colon$rx),
+ age = c(65, 70, 75))
> df[0, ] |> class()
[1] "data.frame"
Reproducible example:
two_states <- function(model, ...) {
transmat = matrix(c(NA,1,NA,NA),2,2,byrow=TRUE)
rownames(transmat) <- colnames(transmat) <- c("Initial","Final")
rstpm2::markov_msm(list(model), ..., trans = transmat)
}
## ERROR
death = gsm(Surv(time,status)~factor(rx),
data=survival::colon, subset=(etype==2), df=3)
ts = two_states(death, newdata=data.frame(rx = levels(survival::colon$rx)),
t = seq(0,2500, length = 51))
sts <- standardise(ts)
as.data.frame(sts)
## OK
death = gsm(Surv(time,status)~factor(rx) + age,
data=survival::colon, subset=(etype==2), df=3)
ts = two_states(death, newdata=data.frame(rx="Obs",
age = c(65, 70, 75)),
t = seq(0,2500, length = 51))
sts <- standardise(ts)
as.data.frame(sts)
rstpm2
version 1.5.3, R version 4.2.0
mclements commented
Andrea: very late to this bug. This should now be fixed on GitHub (but not yet released to CRAN). Thank you for the bug report. Kindly, Mark.