[R] Error survreg: Density function returned an invalid matrix

Israel Ortiz isra4884 at gmail.com
Mon Nov 2 17:27:21 CET 2015

Hi, I want to perform a survival analysis using survreg procedure from
survival library in R for a pareto distribution for a time variable, so I
set the new distribution using the following sintax:


    mypareto <- list(name='Pareto',
                 init= function(x, weights,parms){
                   alpha <-
length(x)/(sum(log(x))-length(x)*log(min(x)))#this is a MLE for alpha
                   c(media <-(alpha*min(x)/(alpha-1)),varianza <-
                 density= function(x,weights) {
                   alpha <- length(x)/(sum(log(x))-length(x)*log(min(x)))
                   cdf1 <- function(x, alpha) ifelse(x > min(x) , 1 -
(min(x)/x)**alpha, 0 )
                         cdf2 <- function(x, alpha) ifelse(x > min(x),
(min(x)/x)**alpha ,0)
                         distribution <- function(x, alpha) ifelse(x >
min(x) , alpha*min(x)**alpha/(x**(alpha+1)), 0)
                         firstdev <- function(x, alpha) ifelse(x > min(x),
-(alpha+x)/x, 0)
                         seconddev <- function(x, alpha) ifelse(x > min(x),
                   cbind(cdf1(x,alpha),cdf2(x, alpha),
                 deviance=function(x) {stop('deviance residuals not
                 quantile= function(p, alpha) ifelse(p < 0 | p > 1, NaN,

I tested new distribution using survregDtest and it was successful:

    survregDtest(mypareto, TRUE)

But I get the following error when I use it:

    a <- rpareto(100, 1, 6)
    b <- rnorm(100,5,1)
    c <- rep(1,100)
    base <- cbind.data.frame(a,b,c)

    mod1<-survreg(Surv(a, c) ~ b, base, dist = mypareto)

    Error in survreg.fit(X, Y, weights, offset, init = init,
    controlvals =   control,  :  Density function returned an invalid matrix

Why this happened even when the test was successful? and how can I solve

	[[alternative HTML version deleted]]

More information about the R-help mailing list