scheike / timereg

timereg R package, survival analysis methods
30 stars 5 forks source link

Prediction with factors using aalen() #10

Closed mclements closed 4 years ago

mclements commented 4 years ago

Thomas, I hope this finds you well.

We recently ran into an issue with predictions from timereg::aalen for a factor:

library(timereg)
set.seed(12345)
d <- local({t <- rexp(100)
    c <- rexp(100)
    y <- pmin(t,c)
    e <- y<c
    data.frame(y,e,sex=1:2)
    })
m <- aalen(Surv(y,e)~factor(sex),d)
p <- predict(m, newdata=data.frame(sex=1), se=FALSE) # FAILS
p <- predict(m, newdata=data.frame(sex=factor(1)), se=FALSE) # FAILS
p <- predict(m, newdata=data.frame(sex=factor(1,1:2)), se=FALSE) # FAILS
m <- aalen(Surv(y,e)~factor(sex,1:2),d)
p <- predict(m, newdata=data.frame(sex=1), se=FALSE) # OK
p <- predict(m, newdata=data.frame(sex=factor(1)), se=FALSE) # OK
p <- predict(m, newdata=data.frame(sex=factor(1,1:2)), se=FALSE) # OK

This issue could be explained by the model frame being re-evaluated for new data, rather than model.matrix using the model frame used for the original fit. Calculation of predictions from stats::lm does not have this bug/feature.

I hope that this report is useful.

Sincerely, Mark Clements.

scheike commented 4 years ago

Dear Mark,

thanks for these insightful comments, the predict function could certainly need some updates to take of such issues.

best regards

Thomsa

mclements commented 4 years ago

Thomas,

Out of interest, why did you close an unresolved issue?

Sincerely, Mark.