[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:
library(foreign)
library(survival)
library(VGAM)
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 <-
((min(x)/alpha)^2)*(alpha/(alpha-2)))},
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),
(alpha+1)*(alpha+2)/x^2,0)
cbind(cdf1(x,alpha),cdf2(x, alpha),
distribution(x,alpha),firstdev(x,alpha),seconddev(x,alpha))},
deviance=function(x) {stop('deviance residuals not
defined')},
quantile= function(p, alpha) ifelse(p < 0 | p > 1, NaN,
min(x)*(1-p)**(-1/alpha)))
I tested new distribution using survregDtest and it was successful:
survregDtest(mypareto, TRUE)
#TRUE
But I get the following error when I use it:
set.seed(1)
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
that?
[[alternative HTML version deleted]]
More information about the R-help
mailing list