Function Predict() does not accept exponential regression in R

Asked

Viewed 309 times

2

Please try to perform a parametric modeling of survival analysis and function predict() is returning an error that does not recognize a variable, but it is there.

Following example:

base=NULL

base$DIAS= c(7,6,8,6,5,5,5,6,6,11,6,4,5,5,5,5,6,4,5,6)

base$DELTA= c(0,1,0,1,0,1,1,0,1,1,0,0,0,1,1,1,1,0,0,0)

base$Protocolo= c(1,0,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1)

base$TIPAC= c(1,0,1,0,0,0,0,1,1,1,0,1,1,1,1,1,0,0,0,0)

base=data.frame(base)

modelo <- survreg(Surv(DIAS,DELTA) ~ Protocolo+TIPAC,data= base, dist = "exponential")

ptempo <- predict(modelo , type = "quantile", newdata= data.frame(1), p=1:99/100,se=TRUE)

Error in Eval(expr, envir, Enclos) : 'Protocol' object not found

1 answer

1


In your code, you use base as an argument data, but I imagine you meant modelo. It is not a good practice to use the same variable name for completely different objects as well (data frame and regression). (Modified after AP editing)

Getting back to your problem, the error occurs because you pass a data frame that does not have a column called Protocolo when he calls the predict. You must pass a new data set of the same format as the data used in the regression (with the same variables used in the formula), or leave blank, to perform the prediction with the initial data. The code below works:

base <- data.frame(DIAS = c(7,6,8,6,5,5,5,6,6,11,6,4,5,5,5,5,6,4,5,6),
                     DELTA = c(0,1,0,1,0,1,1,0,1,1,0,0,0,1,1,1,1,0,0,0),
                     Protocolo = c(1,0,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1),
                     TIPAC = c(1,0,1,0,0,0,0,1,1,1,0,1,1,1,1,1,0,0,0,0))

library(survival)
modelo <- survreg(Surv(DIAS, DELTA) ~ Protocolo + TIPAC, data = base, dist = "exponential")

ptempo <- predict(modelo , type = "quantile", p = 1:99/100, se = TRUE)
str(ptempo)
# List of 2
#  $ fit   : num [1:20, 1:99] 0.1285 0.0929 0.1285 0.1228 0.0929 ...
#  $ se.fit: num [1:20, 1:99] 0.0587 0.0552 0.0587 0.0701 0.0552 ...
  • Good evening Molx, thanks for the help, I created a base to use example (which I erroneously called a template) and copied the log of the R execution, whose data frame name was base. Your command worked perfectly, thank you very much.

  • When you remove the argument "newdata= data.frame(1)" the ptempo$fit gets a disproportionate size (from 99 to 14530 in the original program) and I can’t plot a graph. Please, what would that argument be? and why does its presence prevent the program from recognizing the variable Procolo?

  • 1

    @Henriquepizarro The argument p is, according to the help of the function, "a percentile vector. Used only for quantis prediction". The result is a multi-column matrix because it makes a prediction for each quantile, in your case, it’s 99 quantiles. If you do not intend to use the quantis, you can remove the argument, and then the result will return two columns, both in the fit how much in the se.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.