mclements / rstpm2

An R package for generalised survival models
28 stars 11 forks source link

Behaviour of predict(..type = "af") and plot(...type = "af") #18

Closed proshano closed 1 month ago

proshano commented 3 years ago

Hello,

Thank you for creating this excellent package.

I noticed something a bit odd that I was hoping you could help me with.

I am trying to calculate attributable fractions for an entire set of variables that represent harmful exposures as if they were removed from the population. The idea here is to see how much of an outcome can be prevented if these things were all corrected.

predicted.frame<-predict(flex.fit.tvc, type="af", newdata=transform(data.imputed), exposed=function(data) transform(data.imputed, var1=0, var2=0, var3=0, var4=0, var5=0), se.fit=TRUE)

If I plot the point estimates of the AF from the predicted.frame object above using ggplot, I see there is a tremendous amount of variability in the point estimate:

image

Some of the values are 0 or slightly negative when I examine the contents of predicted.frame.

When I plot with the plot() function in the package instead of using predict(), I get this:

image

How would I interpret that? What do the negative values on this plot mean?

Thank you for your time - I really want to make proper use of your package.

mclements commented 3 years ago

Apologies for this late reply.

You were correct that there was a bug in the predict function. The following code should now work:

library(rstpm2)
fit = gsm(Surv(rectime,censrec==1)~hormon,data=brcancer,df=3)
plot(fit,type="af",newdata=brcancer,
    exposed=function(data) transform(data,hormon=1))
pred2 <- predict(fit,type="af",newdata=brcancer, grid=TRUE,
    exposed=function(data) transform(data,hormon=1),
    se.fit=TRUE,full=TRUE)
with(pred2, matplot(rectime,cbind(Estimate,lower,upper),type="l",lty=c(1,2,2), col=1,
                    xlab="Time since treatment (days)", ylab="PAF", ylim=c(0,0.5)))
proshano commented 3 years ago

that is wonderful thank you - can't wait to try it out!

proshano commented 3 years ago

I get the following error:

pred2 <- predict(fit,type="af",newdata=brcancer, grid=TRUE, exposed=function(data) transform(data,hormon=1), se.fit=TRUE,full=TRUE)

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 205114, 299

mclements commented 3 years ago

Odd. Did you re-install from the GitHub source or use the version on CRAN? If the latter, then an updated version is currently under review by CRAN.

Sincerely, Mark.

mclements commented 3 years ago

@proshano, an update to rstpm2 is now available on CRAN. Does this address this issue?

Sincerely, Mark.