R/sm4.R

Defines functions sm4

Documented in sm4

sm4 <- function(t1, t2, sequence, weights=NULL, dist, cuts.12=NULL,cuts.13=NULL,cuts.14=NULL,cuts.23=NULL,cuts.24=NULL,ini.base.12=NULL, ini.base.13=NULL, ini.base.14=NULL, ini.base.23=NULL, ini.base.24=NULL, cov.12=NULL, init.cov.12=NULL, names.12=NULL, cov.13=NULL, init.cov.13=NULL, names.13=NULL, cov.14=NULL, init.cov.14=NULL, names.14=NULL, cov.23=NULL, init.cov.23=NULL, names.23=NULL, cov.24=NULL, init.cov.24=NULL, names.24=NULL, conf.int=TRUE, silent=TRUE, precision=10^(-6))
{

cat("\n ******  4-state semi-Markov Model ****** \n")

#check conditions
if (missing(t1)) 
        stop("Argument 't1' is missing with no default")
if (missing(t2)) 
        stop("Argument 't2' is missing with no default")
if (missing(sequence)) 
        stop("Argument 'sequence' is missing with no default")
if (missing(dist)) 
        stop("Argument 'dist' is missing with no default")
        
if (!is.vector(t1) | !is.numeric(t1))
        stop("Argument 't1' must be a numeric vector")
if (min(t1,na.rm=T)<0)
		stop("Negative values for 't1'")
if (is.na(min(t1)))
		cat("Warning: individuals with missing values for 't1' will be removed \n")
if (!is.vector(t2) | !is.numeric(t2))
        stop("Argument 't2' must be a numeric vector")
if (min(t2,na.rm=T)<0)
		stop("Negative values for 't2'")
		
if (!is.vector(sequence) | !is.numeric(sequence) | (min(names(table(sequence)) %in% c(1,12,13,14,123,124))==0) )
        stop("Argument 'sequence' must be a numeric vector with values 1, 12, 13, 14, 123, or 124")
if (min( c(1,12,13,14,123,124) %in% names(table(sequence))) ==0)
        cat("Warning: all sequences (1, 12, 13, 14, 123, 124) are not present \n ")
        
if (min(length(t1),length(t2),length(sequence)) != max(length(t1),length(t2),length(sequence)))
        stop("Arguments 't1', 't2', and 'sequence' need to have the same number of rows")
if (!all(is.na(t2[which(sequence==1 | sequence==13 | sequence==14)])))		
		stop("Arguments 't2' should be NA for individuals right-censored in X=1 or individuals who directly transited from X=1 to X=3 or to X=1 to X=4")	
if (min(t2-t1,na.rm=T)<=0)
		stop("Some values for 't2' are lower or equal to 't1'")
                
if(!is.null(weights))
{		
	if (!is.vector(weights) | !is.numeric(weights))
        stop("Argument 'weights' must be a numeric vector")
	if (min(weights,na.rm=T)<0)
		stop("Negative values for 'weights'")
	if (is.na(min(weights)))
		cat("Warning: individual with missing values for 'weights' will be removed \n")
}

if(length(dist)!=5)
 {stop("Argument 'dist' have to contain 5 values")} 
 
if(!(dist[1] %in% c("PE","E","W","WG")))
 {stop("Argument 'dist': incorrect distribution for transition 12")} 
if(!(dist[2] %in% c("PE","E","W","WG")))
 {stop("Argument 'dist': incorrect distribution for transition 13")} 
if(!(dist[3] %in% c("PE","E","W","WG")))
 {stop("Argument 'dist': incorrect distribution for transition 14")} 
if(!(dist[4] %in% c("PE","E","W","WG")))
 {stop("Argument 'dist': incorrect distribution for transition 23")} 
if(!(dist[5] %in% c("PE","E","W","WG")))
 {stop("Argument 'dist': incorrect distribution for transition 24")} 
 
if(dist[1]!="PE" & (!is.null(cuts.12)))
 {stop("Arguments 'cuts.12' is only allowed with exponential distribution for transition 12 (piecewise model)")} 
if(dist[2]!="PE" & (!is.null(cuts.13)))
 {stop("Arguments 'cuts.13' is only allowed with exponential distribution for transition 13 (piecewise model)")}
if(dist[3]!="PE" & (!is.null(cuts.14)))
 {stop("Arguments 'cuts.14' is only allowed with exponential distribution for transition 14 (piecewise model)")}  
if(dist[4]!="PE" & (!is.null(cuts.23)))
 {stop("Arguments 'cuts.23' is only allowed with exponential distribution for transition 23 (piecewise model)")}
if(dist[5]!="PE" & (!is.null(cuts.24)))
 {stop("Arguments 'cuts.24' is only allowed with exponential distribution for transition 24 (piecewise model)")}  
 
if(dist[1]=="PE" & !is.null(cuts.12))
 {
 if (!all(is.numeric(cuts.12)) | !all(!is.na(cuts.12)) | !all(cuts.12>0) | !all(is.finite(cuts.12)) | is.unsorted(cuts.12)) 
 {stop("Arguments 'cuts.12' must be a sorted vector with only positive and finite numeric values (internal timepoints)")}
 }
if(dist[1]=="PE" & !is.null(cuts.12))
{
 if (max(cuts.12)>=max(t1,na.rm=T)) 
 {stop("Arguments 'cuts.12': check internal timepoints or time units (last internal timepoint is greater or equal to the maximum value for t1)")}
}
if(dist[2]=="PE" & !is.null(cuts.13))
 {
 if (!all(is.numeric(cuts.13)) | !all(!is.na(cuts.13)) | !all(cuts.13>0) | !all(is.finite(cuts.13)) | is.unsorted(cuts.13)) 
 {stop("Arguments 'cuts.13' must be a sorted vector with only positive and finite numeric values (internal timepoints)")}
 }
if(dist[2]=="PE" & !is.null(cuts.13))
{
 if (max(cuts.13)>=max(t1,na.rm=T)) 
 {stop("Arguments 'cuts.13': check internal timepoints or time units (last internal timepoint is greater or equal to the maximum value for t1)")}
}
if(dist[3]=="PE" & !is.null(cuts.14))
 {
 if (!all(is.numeric(cuts.14)) | !all(!is.na(cuts.14)) | !all(cuts.14>0) | !all(is.finite(cuts.14)) | is.unsorted(cuts.14)) 
 {stop("Arguments 'cuts.14' must be a sorted vector with only positive and finite numeric values (internal timepoints)")}
 }
if(dist[3]=="PE" & !is.null(cuts.14))
{
 if (max(cuts.14)>=max(t1,na.rm=T)) 
 {stop("Arguments 'cuts.14': check internal timepoints or time units (last internal timepoint is greater or equal to the maximum value for t1)")}
}
if(dist[4]=="PE" & !is.null(cuts.23))
 {
 if (!all(is.numeric(cuts.23)) | !all(!is.na(cuts.23)) | !all(cuts.23>0) | !all(is.finite(cuts.23)) | is.unsorted(cuts.23)) 
 {stop("Arguments 'cuts.23' must be a sorted vector with only positive and finite numeric values (internal timepoints)")}
 }
if(dist[4]=="PE" & !is.null(cuts.23))
{
 if (max(cuts.23)>=max(t1,na.rm=T)) 
 {stop("Arguments 'cuts.23': check internal timepoints or time units (last internal timepoint is greater or equal to the maximum value for t1)")}
}
if(dist[5]=="PE" & !is.null(cuts.24))
 {
 if (!all(is.numeric(cuts.24)) | !all(!is.na(cuts.24)) | !all(cuts.24>0) | !all(is.finite(cuts.24)) | is.unsorted(cuts.24)) 
 {stop("Arguments 'cuts.24' must be a sorted vector with only positive and finite numeric values (internal timepoints)")}
 }
if(dist[5]=="PE" & !is.null(cuts.24))
{
 if (max(cuts.24)>=max(t1,na.rm=T)) 
 {stop("Arguments 'cuts.24': check internal timepoints or time units (last internal timepoint is greater or equal to the maximum value for t1)")}
}
 
if(!is.null(ini.base.12) & !is.numeric(ini.base.12))
 {stop("Argument 'ini.base.12' must be a numeric vector (default is NULL)")} 
if(!is.null(ini.base.13) & !is.numeric(ini.base.13))
 {stop("Argument 'ini.base.13' must be a numeric vector (default is NULL)")} 
if(!is.null(ini.base.14) & !is.numeric(ini.base.13))
 {stop("Argument 'ini.base.14' must be a numeric vector (default is NULL)")} 
if(!is.null(ini.base.23) & !is.numeric(ini.base.23))
 {stop("Argument 'ini.base.23' must be a numeric vector (default is NULL)")}  
if(!is.null(ini.base.24) & !is.numeric(ini.base.23))
 {stop("Argument 'ini.base.24' must be a numeric vector (default is NULL)")}  

if(dist[1]=="PE" & !is.null(ini.base.12) & length(ini.base.12)!=(length(cuts.12)+1))
 {stop("Incorrect number of parameters initialized for transition 12 (piecewise model)")} 
if(dist[2]=="PE" & !is.null(ini.base.13) & length(ini.base.13)!=(length(cuts.13)+1))
 {stop("Incorrect number of parameters initialized for transition 13 (piecewise model)")}
if(dist[3]=="PE" & !is.null(ini.base.14) & length(ini.base.14)!=(length(cuts.14)+1))
 {stop("Incorrect number of parameters initialized for transition 14 (piecewise model)")}
if(dist[4]=="PE" & !is.null(ini.base.23) & length(ini.base.23)!=(length(cuts.23)+1))
 {stop("Incorrect number of parameters initialized for transition 23 (piecewise model)")}
if(dist[5]=="PE" & !is.null(ini.base.24) & length(ini.base.24)!=(length(cuts.24)+1))
 {stop("Incorrect number of parameters initialized for transition 24 (piecewise model)")} 

if( (dist[1]=="E" & is.null(cuts.12) & !is.null(ini.base.12) & length(ini.base.12)!=1) )
 {stop("Exponential distribution (transition 12) needs initialization of one parameter")} 
if( (dist[1]=="W" & is.null(cuts.12) & !is.null(ini.base.12) & length(ini.base.12)!=2) )
 {stop("Weibull distribution (transition 12) needs initialization of two parameters")} 
if( (dist[1]=="WG" & is.null(cuts.12) & !is.null(ini.base.12) & length(ini.base.12)!=3) )
 {stop("Generalized Weibull distribution (transition 12) needs initialization of three parameters")}  

if( (dist[2]=="E" & is.null(cuts.13) & !is.null(ini.base.13) & length(ini.base.13)!=1) )
 {stop("Exponential distribution (transition 13) needs initialization of one parameter")} 
if( (dist[2]=="W" & is.null(cuts.13) & !is.null(ini.base.13) & length(ini.base.13)!=2) )
 {stop("Weibull distribution (transition 13) needs initialization of two parameters")} 
if( (dist[2]=="WG" & is.null(cuts.13) & !is.null(ini.base.13) & length(ini.base.13)!=3) )
 {stop("Generalized Weibull distribution (transition 13) needs initialization of three parameters")}
 
 if( (dist[3]=="E" & is.null(cuts.14) & !is.null(ini.base.14) & length(ini.base.14)!=1) )
 {stop("Exponential distribution (transition 14) needs initialization of one parameter")} 
if( (dist[3]=="W" & is.null(cuts.14) & !is.null(ini.base.14) & length(ini.base.14)!=2) )
 {stop("Weibull distribution (transition 14) needs initialization of two parameters")} 
if( (dist[3]=="WG" & is.null(cuts.14) & !is.null(ini.base.14) & length(ini.base.14)!=3) )
 {stop("Generalized Weibull distribution (transition 14) needs initialization of three parameters")}
 
 if( (dist[4]=="E" & is.null(cuts.23) & !is.null(ini.base.23) & length(ini.base.23)!=1) )
 {stop("Exponential distribution (transition 23) needs initialization of one parameter")} 
if( (dist[4]=="W" & is.null(cuts.23) & !is.null(ini.base.23) & length(ini.base.23)!=2) )
 {stop("Weibull distribution (transition 23) needs initialization of two parameters")} 
if( (dist[4]=="WG" & is.null(cuts.23) & !is.null(ini.base.23) & length(ini.base.23)!=3) )
 {stop("Generalized Weibull distribution (transition 23) needs initialization of three parameters")}

 if( (dist[5]=="E" & is.null(cuts.24) & !is.null(ini.base.24) & length(ini.base.24)!=1) )
 {stop("Exponential distribution (transition 24) needs initialization of one parameter")} 
if( (dist[5]=="W" & is.null(cuts.24) & !is.null(ini.base.24) & length(ini.base.24)!=2) )
 {stop("Weibull distribution (transition 24) needs initialization of two parameters")} 
if( (dist[5]=="WG" & is.null(cuts.24) & !is.null(ini.base.24) & length(ini.base.24)!=3) )
 {stop("Generalized Weibull distribution (transition 24) needs initialization of three parameters")}
 
if(!is.null(cov.12))
{
if ((!is.vector(cov.12) & !is.data.frame(cov.12) & !is.matrix(cov.12)) | !all(sapply(cov.12,is.numeric)))
 {stop("Argument 'cov.12' must be a numeric matrix or data.frame (default is NULL)")} 
if (nrow(data.frame(cov.12))!=length(t1))
 {stop("Argument 'cov.12' needs to have the same number of rows than 't1'")}
if (sum(apply(sapply(data.frame(cov.12),is.na),1,sum))>0)
		cat("Warning:",sum(apply(sapply(data.frame(cov.12),is.na),1,sum)),"individuals with missing values on 'cov.12' will be removed \n")
if(!is.null(init.cov.12))
	{
	if (!is.numeric(init.cov.12))
	{stop("Argument 'init.cov.12' must be a numeric vector (default is NULL)")} 
	if (ncol(data.frame(cov.12))!=length(init.cov.12))
	{stop("Argument 'init.cov.12' needs to have the same length than number of columns of 'cov.12'")}
	}
if (!is.null(names.12))
	{
	if (!is.character(names.12))
	{stop("Argument 'names.12' must be a character vector (default is NULL)")} 
	if (ncol(data.frame(cov.12))!=length(names.12))
	{stop("Argument 'names.12' needs to have the same length than number of columns of 'cov.12'")}
	}
}

if(!is.null(cov.13))
{
if ((!is.vector(cov.13) & !is.data.frame(cov.13) & !is.matrix(cov.13)) | !all(sapply(cov.13,is.numeric)))
 {stop("Argument 'cov.13' must be a numeric matrix or data.frame (default is NULL)")} 
if (nrow(data.frame(cov.13))!=length(t1))
 {stop("Argument 'cov.13' needs to have the same number of rows than 't1'")}
if (sum(apply(sapply(data.frame(cov.13),is.na),1,sum))>0)
		cat("Warning:",sum(apply(sapply(data.frame(cov.13),is.na),1,sum)),"individuals with missing values on 'cov.13' will be removed \n")
if(!is.null(init.cov.13))
	{
	if (!is.numeric(init.cov.13))
	{stop("Argument 'init.cov.13' must be a numeric vector (default is NULL)")} 
	if (ncol(data.frame(cov.13))!=length(init.cov.13))
	{stop("Argument 'init.cov.13' needs to have the same length than number of columns of 'cov.13'")}
	}
if (!is.null(names.13))
	{
	if (!is.character(names.13))
	{stop("Argument 'names.13' must be a character vector (default is NULL)")} 
	if (ncol(data.frame(cov.13))!=length(names.13))
	{stop("Argument 'names.13' needs to have the same length than number of columns of 'cov.13'")}
	}
}

if(!is.null(cov.14))
{
if ((!is.vector(cov.14) & !is.data.frame(cov.14) & !is.matrix(cov.14)) | !all(sapply(cov.14,is.numeric)))
 {stop("Argument 'cov.14' must be a numeric matrix or data.frame (default is NULL)")} 
if (nrow(data.frame(cov.14))!=length(t1))
 {stop("Argument 'cov.14' needs to have the same number of rows than 't1'")}
if (sum(apply(sapply(data.frame(cov.14),is.na),1,sum))>0)
		cat("Warning:",sum(apply(sapply(data.frame(cov.14),is.na),1,sum)),"individuals with missing values on 'cov.14' will be removed \n")
if(!is.null(init.cov.14))
	{
	if (!is.numeric(init.cov.14))
	{stop("Argument 'init.cov.14' must be a numeric vector (default is NULL)")} 
	if (ncol(data.frame(cov.14))!=length(init.cov.14))
	{stop("Argument 'init.cov.14' needs to have the same length than number of columns of 'cov.14'")}
	}
if (!is.null(names.14))
	{
	if (!is.character(names.14))
	{stop("Argument 'names.14' must be a character vector (default is NULL)")} 
	if (ncol(data.frame(cov.14))!=length(names.14))
	{stop("Argument 'names.14' needs to have the same length than number of columns of 'cov.14'")}
	}
}

if(!is.null(cov.23))
{
if ((!is.vector(cov.23) & !is.data.frame(cov.23) & !is.matrix(cov.23)) | !all(sapply(cov.23,is.numeric)))
 {stop("Argument 'cov.23' must be a numeric matrix or data.frame (default is NULL)")} 
if (nrow(data.frame(cov.23))!=length(t2))
 {stop("Argument 'cov.23' needs to have the same number of rows than 't2'")}
if (sum(apply(sapply(data.frame(cov.23),is.na),1,sum))>0)
		cat("Warning:",sum(apply(sapply(data.frame(cov.23),is.na),1,sum)),"individuals with missing values on 'cov.23' will be removed \n")
if(!is.null(init.cov.23))
	{
	if (!is.numeric(init.cov.23))
	{stop("Argument 'init.cov.23' must be a numeric vector (default is NULL)")} 
	if (ncol(data.frame(cov.23))!=length(init.cov.23))
	{stop("Argument 'init.cov.23' needs to have the same length than number of columns of 'cov.23'")}
	}
if (!is.null(names.23))
	{
	if (!is.character(names.23))
	{stop("Argument 'names.23' must be a character vector (default is NULL)")} 
	if (ncol(data.frame(cov.23))!=length(names.23))
	{stop("Argument 'names.23' needs to have the same length than number of columns of 'cov.23'")}
	}
} 
 
if(!is.null(cov.24))
{
if ((!is.vector(cov.24) & !is.data.frame(cov.24) & !is.matrix(cov.24)) | !all(sapply(cov.24,is.numeric)))
 {stop("Argument 'cov.24' must be a numeric matrix or data.frame (default is NULL)")} 
if (nrow(data.frame(cov.24))!=length(t2))
 {stop("Argument 'cov.24' needs to have the same number of rows than 't1'")}
if (sum(apply(sapply(data.frame(cov.24),is.na),1,sum))>0)
		cat("Warning:",sum(apply(sapply(data.frame(cov.24),is.na),1,sum)),"individuals with missing values on 'cov.24' will be removed \n")
if(!is.null(init.cov.24))
	{
	if (!is.numeric(init.cov.24))
	{stop("Argument 'init.cov.24' must be a numeric vector (default is NULL)")} 
	if (ncol(data.frame(cov.24))!=length(init.cov.24))
	{stop("Argument 'init.cov.24' needs to have the same length than number of columns of 'cov.24'")}
	}
if (!is.null(names.24))
	{
	if (!is.character(names.24))
	{stop("Argument 'names.24' must be a character vector (default is NULL)")} 
	if (ncol(data.frame(cov.24))!=length(names.24))
	{stop("Argument 'names.24' needs to have the same length than number of columns of 'cov.24'")}
	}
}  
 
 
 if(!(conf.int %in% c("TRUE","FALSE")))
 {stop("Argument 'conf.int' must be TRUE or FALSE (default is TRUE)")} 

 if(!is.null(precision))
 {
 if(!is.numeric(precision))
 {stop("Argument 'precision' must be numeric (default is 0)")} 
 if(precision<0)
 {stop("Argument 'precision' must be greater or equal to 0 (default is 0)")}
 }
 
 if(!(silent %in% c("TRUE","FALSE")))
 {stop("Argument 'silent' must be TRUE or FALSE (default is TRUE)")} 
 
coef12<-NULL
sigma12<-NULL
nu12<-NULL
theta12<-NULL

coef13<-NULL
sigma13<-NULL
nu13<-NULL
theta13<-NULL

coef23<-NULL
nu23<-NULL
theta23<-NULL
sigma23<-NULL

coef14<-NULL
sigma14<-NULL
nu14<-NULL
theta14<-NULL

coef24<-NULL
nu24<-NULL
theta24<-NULL
sigma24<-NULL

#sojourn time distributions
if(dist[1]=="WG" | dist[1]=="W" | (dist[1]=="E" & is.null(cuts.12)))
 {
 H12<-function(t,z,cuts) { exp(as.matrix(z) %*% coef12) * ((((1+(t/sigma12)^nu12))^(1/theta12))-1) }
 log.h12<-function(t,z,cuts) { (as.matrix(z) %*% coef12) - log(theta12) + ((1/theta12)-1) * log1p((t/sigma12)^nu12) + log(nu12) + (nu12-1)*log(t) - nu12*log(sigma12) }
 }

if(dist[1]=="PE" & !is.null(cuts.12))
 {
cuts.12 <- sort(cuts.12)
if ((cuts.12[1] <= 0) || (cuts.12[length(cuts.12)] == Inf)) 
   stop("'cuts.12' must be positive and finite.")
cuts.12 <- c(0, cuts.12, Inf)

H12<-function(t,z,cuts) {
 H<-rep(0,length(t))
for (i in (1:(length(cuts)-1)))
  {
  H<-H+(1*(t>=cuts[i]))*exp(as.matrix(z) %*% coef12)*((pmin(cuts[i+1],t)-cuts[i])/sigma12[i])
  }
return(H)
rm(H)
 }
log.h12<-function(t,z,cuts) {
 log.h<-rep(0,length(t))
 for (i in (1:(length(cuts)-1)))
  {
  log.h<-log.h+(1*(t>=cuts[i])*(t<cuts[i+1]))*(as.matrix(z) %*% coef12-log(sigma12[i]))
  }
 return(log.h)
 rm(log.h)
}
}


if(dist[2]=="WG" | dist[2]=="W" | (dist[2]=="E" & is.null(cuts.13)))
 {
 H13<-function(t,z,cuts) { exp(as.matrix(z) %*% coef13) * ((((1+(t/sigma13)^nu13))^(1/theta13))-1) }
 log.h13<-function(t,z,cuts) { (as.matrix(z) %*% coef13) - log(theta13) + ((1/theta13)-1) * log1p((t/sigma13)^nu13) + log(nu13) + (nu13-1)*log(t) - nu13*log(sigma13) }
 }

if(dist[2]=="PE" & !is.null(cuts.13))
 {
cuts.13 <- sort(cuts.13)
if ((cuts.13[1] <= 0) || (cuts.13[length(cuts.13)] == Inf)) 
   stop("'cuts.13' must be positive and finite.")
cuts.13 <- c(0, cuts.13, Inf)

H13<-function(t,z,cuts) {
 H<-rep(0,length(t))
for (i in (1:(length(cuts)-1)))
  {
  H<-H+(1*(t>=cuts[i]))*exp(as.matrix(z) %*% coef13)*((pmin(cuts[i+1],t)-cuts[i])/sigma13[i])
  }
return(H)
rm(H)
 }
log.h13<-function(t,z,cuts) {
 log.h<-rep(0,length(t))
 for (i in (1:(length(cuts)-1)))
  {
  log.h<-log.h+(1*(t>=cuts[i])*(t<cuts[i+1]))*(as.matrix(z) %*% coef13-log(sigma13[i]))
  }
 return(log.h)
 rm(log.h)
}
}


if(dist[3]=="WG" | dist[3]=="W" | (dist[3]=="E" & is.null(cuts.14)))
 {
H14<-function(t,z,cuts) { exp(as.matrix(z) %*% coef14) * ((((1+(t/sigma14)^nu14))^(1/theta14))-1) }
 log.h14<-function(t,z,cuts) { (as.matrix(z) %*% coef14) - log(theta14) + ((1/theta14)-1) * log1p((t/sigma14)^nu14) + log(nu14) + (nu14-1)*log(t) - nu14*log(sigma14) }
 }

if(dist[3]=="PE" & !is.null(cuts.14))
 {
cuts.14 <- sort(cuts.14)
if ((cuts.14[1] <= 0) || (cuts.14[length(cuts.14)] == Inf)) 
   stop("'cuts.14' must be positive and finite.")
cuts.14 <- c(0, cuts.14, Inf)

H14<-function(t,z,cuts) {
 H<-rep(0,length(t))
for (i in (1:(length(cuts)-1)))
  {
  H<-H+(1*(t>=cuts[i]))*exp(as.matrix(z) %*% coef14)*((pmin(cuts[i+1],t)-cuts[i])/sigma14[i])
  }
return(H)
rm(H)
 }
log.h14<-function(t,z,cuts) {
 log.h<-rep(0,length(t))
 for (i in (1:(length(cuts)-1)))
  {
  log.h<-log.h+(1*(t>=cuts[i])*(t<cuts[i+1]))*(as.matrix(z) %*% coef14-log(sigma14[i]))
  }
 return(log.h)
 rm(log.h)
}
}


if(dist[4]=="WG" | dist[4]=="W" | (dist[4]=="E" & is.null(cuts.23)))
 {
 H23<-function(t,z,cuts) { exp(as.matrix(z) %*% coef23) * ((((1+(t/sigma23)^nu23))^(1/theta23))-1) }
 log.h23<-function(t,z,cuts) { (as.matrix(z) %*% coef23) - log(theta23) + ((1/theta23)-1) * log1p((t/sigma23)^nu23) + log(nu23) + (nu23-1)*log(t) - nu23*log(sigma23) }
 }

if(dist[4]=="PE" & !is.null(cuts.23))
 {
cuts.23 <- sort(cuts.23)
if ((cuts.23[1] <= 0) || (cuts.23[length(cuts.23)] == Inf)) 
   stop("'cuts.23' must be positive and finite.")
cuts.23 <- c(0, cuts.23, Inf)

H23<-function(t,z,cuts) {
 H<-rep(0,length(t))
for (i in (1:(length(cuts)-1)))
  {
  H<-H+(1*(t>=cuts[i]))*exp(as.matrix(z) %*% coef23)*((pmin(cuts[i+1],t)-cuts[i])/sigma23[i])
  }
return(H)
rm(H)
 }
log.h23<-function(t,z,cuts) {
 log.h<-rep(0,length(t))
 for (i in (1:(length(cuts)-1)))
  {
  log.h<-log.h+(1*(t>=cuts[i])*(t<cuts[i+1]))*(as.matrix(z) %*% coef23-log(sigma23[i]))
  }
 return(log.h)
 rm(log.h)
}
}


if(dist[5]=="WG" | dist[5]=="W" | (dist[5]=="E" & is.null(cuts.24)))
 {
H24<-function(t,z,cuts) { exp(as.matrix(z) %*% coef24) * ((((1+(t/sigma24)^nu24))^(1/theta24))-1) }
 log.h24<-function(t,z,cuts) { (as.matrix(z) %*% coef24) - log(theta24) + ((1/theta24)-1) * log1p((t/sigma24)^nu24) + log(nu24) + (nu24-1)*log(t) - nu24*log(sigma24) }
 }

if(dist[5]=="PE" & !is.null(cuts.24))
 {
cuts.24 <- sort(cuts.24)
if ((cuts.24[1] <= 0) || (cuts.24[length(cuts.24)] == Inf)) 
   stop("'cuts.24' must be positive and finite.")
cuts.24 <- c(0, cuts.24, Inf)

H24<-function(t,z,cuts) {
 H<-rep(0,length(t))
for (i in (1:(length(cuts)-1)))
  {
  H<-H+(1*(t>=cuts[i]))*exp(as.matrix(z) %*% coef24)*((pmin(cuts[i+1],t)-cuts[i])/sigma24[i])
  }
return(H)
rm(H)
 }
log.h24<-function(t,z,cuts) {
 log.h<-rep(0,length(t))
 for (i in (1:(length(cuts)-1)))
  {
  log.h<-log.h+(1*(t>=cuts[i])*(t<cuts[i+1]))*(as.matrix(z) %*% coef24-log(sigma24[i]))
  }
 return(log.h)
 rm(log.h)
}
}

#contributions to the log-likelihood
c1<-function(t1, z12, z13, z14, cut12, cut13, cut14)
 {return( -H12(t1, z12, cut12) - H13(t1, z13, cut13)- H14(t1, z14, cut14))}

c12<-function(t1, t2, z12, z13, z14, z23, z24, cut12, cut13, cut14, cut23, cut24)
 {return(log.h12(t1, z12, cut12) - H12(t1, z12, cut12) - H13(t1, z13, cut13) - H14(t1, z14, cut14) - H23(t2, z23, cut23) - H24(t2, z24, cut24))}

c13<-function(t1, z12, z13, z14, cut12, cut13, cut14)
 {return(log.h13(t1, z13, cut13) - H12(t1, z12, cut12) - H13(t1, z13, cut13) - H14(t1, z14, cut14))}

c14<-function(t1, z12, z13, z14, cut12, cut13, cut14)
 {return(log.h14(t1, z14, cut14) - H12(t1, z12, cut12) - H13(t1, z13, cut13) - H14(t1, z14, cut14))}

c123<-function(t1, t2, z12, z13, z14, z23, z24, cut12, cut13, cut14, cut23, cut24)
 {return(log.h12(t1, z12, cut12) - H12(t1, z12, cut12) - H13(t1, z13, cut13) - H14(t1, z14, cut14) + log.h23(t2, z23, cut23) - H23(t2, z23, cut23) - H24(t2, z24, cut24))}

c124<-function(t1, t2, z12, z13, z14, z23, z24, cut12, cut13, cut14, cut23, cut24)
 {return(log.h12(t1, z12, cut12) - H12(t1, z12, cut12) - H13(t1, z13, cut13) - H14(t1, z14, cut14) + log.h24(t2, z24, cut24) - H23(t2, z23, cut23) - H24(t2, z24, cut24))}

#sojourn time
d1<-t1
d2<-t2-t1

#missing data
.D <- cbind(d1, cov.12, cov.13, cov.14, cov.23, cov.24)
.na <- (!is.na(apply(.D, FUN="sum", MARGIN=1)))

#initialization of the parameters
if (is.null(cov.12)) {cov.12.mat <- cbind(rep(0, length(d1))); n.12 <- NULL} else { cov.12.mat <- cbind(cov.12); n.12 <- paste("covariate(s) on trans. 12:  num", 1:ncol(data.frame(cov.12))); if(!is.null(names.12)) {n.12 <- names.12} }

if (is.null(cov.13)) {cov.13.mat <- cbind(rep(0, length(d1))); n.13 <- NULL} else { cov.13.mat <- cbind(cov.13); n.13 <- paste("covariate(s) on trans. 13:  num", 1:ncol(data.frame(cov.13))); if(!is.null(names.13)) {n.13 <- names.13} }

if (is.null(cov.14)) {cov.14.mat <- cbind(rep(0, length(d1))); n.14 <- NULL} else { cov.14.mat <- cbind(cov.14); n.14 <- paste("covariate(s) on trans. 14:  num", 1:ncol(data.frame(cov.14))); if(!is.null(names.14)) {n.14 <- names.14} }

if (is.null(cov.23)) {cov.23.mat <- cbind(rep(0, length(d1))); n.23 <- NULL} else { cov.23.mat <- cbind(cov.23); n.23 <- paste("covariate(s) on trans. 23:  num", 1:ncol(data.frame(cov.23))); if(!is.null(names.23)) {n.23 <- names.23} }

if (is.null(cov.24)) {cov.24.mat <- cbind(rep(0, length(d1))); n.24 <- NULL} else { cov.24.mat <- cbind(cov.24); n.24 <- paste("covariate(s) on trans. 24:  num", 1:ncol(data.frame(cov.24))); if(!is.null(names.24)) {n.24 <- names.24} }


if (is.null(ini.base.12)) {i.12.dist<-rep(0, 1*(dist[1]=="E" & is.null(cuts.12)) + 2*(dist[1]=="W") + 3*(dist[1]=="WG") + 1*(dist[1]=="PE" & !is.null(cuts.12))*(length(cuts.12)-1))}
 else {i.12.dist<-ini.base.12}

if (is.null(ini.base.13)) {i.13.dist<-rep(0, 1*(dist[2]=="E" & is.null(cuts.13)) + 2*(dist[2]=="W") + 3*(dist[2]=="WG") + 1*(dist[2]=="PE" & !is.null(cuts.13))*(length(cuts.13)-1))}
 else {i.13.dist<-ini.base.13}

 if (is.null(ini.base.14)) {i.14.dist<-rep(0, 1*(dist[3]=="E" & is.null(cuts.14)) + 2*(dist[3]=="W") + 3*(dist[3]=="WG") + 1*(dist[3]=="PE" & !is.null(cuts.14))*(length(cuts.14)-1))}
 else {i.14.dist<-ini.base.14}

if (is.null(ini.base.23)) {i.23.dist<-rep(0, 1*(dist[4]=="E" & is.null(cuts.23)) + 2*(dist[4]=="W") + 3*(dist[4]=="WG") + 1*(dist[4]=="PE" & !is.null(cuts.23))*(length(cuts.23)-1))}
 else {i.23.dist<-ini.base.23}

 if (is.null(ini.base.24)) {i.24.dist<-rep(0, 1*(dist[5]=="E" & is.null(cuts.24)) + 2*(dist[5]=="W") + 3*(dist[5]=="WG") + 1*(dist[5]=="PE" & !is.null(cuts.24))*(length(cuts.24)-1))}
 else {i.24.dist<-ini.base.24}


if (!is.null(init.cov.12)) {i.12<-init.cov.12}
if (is.null(init.cov.12) & is.null(cov.12)) {i.12<-NULL}
if (is.null(init.cov.12) & !is.null(cov.12)) {i.12<-rep(0, ncol(data.frame(cov.12)))}

if (!is.null(init.cov.13)) {i.13<-init.cov.13}
if (is.null(init.cov.13) & is.null(cov.13)) {i.13<-NULL}
if (is.null(init.cov.13) & !is.null(cov.13)) {i.13<-rep(0, ncol(data.frame(cov.13)))}

if (!is.null(init.cov.14)) {i.14<-init.cov.14}
if (is.null(init.cov.14) & is.null(cov.14)) {i.14<-NULL}
if (is.null(init.cov.14) & !is.null(cov.14)) {i.14<-rep(0, ncol(data.frame(cov.14)))}

if (!is.null(init.cov.23)) {i.23<-init.cov.23}
if (is.null(init.cov.23) & is.null(cov.23)) {i.23<-NULL}
if (is.null(init.cov.23) & !is.null(cov.23)) {i.23<-rep(0, ncol(data.frame(cov.23)))}

if (!is.null(init.cov.24)) {i.24<-init.cov.24}
if (is.null(init.cov.24) & is.null(cov.24)) {i.24<-NULL}
if (is.null(init.cov.24) & !is.null(cov.24)) {i.24<-rep(0, ncol(data.frame(cov.24)))}

ini <- c(i.12.dist, i.13.dist, i.14.dist, i.23.dist, i.24.dist, i.12, i.13, i.14, i.23, i.24)

if (is.null(weights)) {w <- rep(1, length(d1))} else {w <- weights }

#parameters for contributions associated to each transition
.w1 <- w[(sequence==1 & .na)]
.d1.1 <- d1[(sequence==1 & .na)]
.c1.12 <- cov.12.mat[(sequence==1 & .na),]
.c1.13 <- cov.13.mat[(sequence==1 & .na),]
.c1.14 <- cov.14.mat[(sequence==1 & .na),]

.w12 <- w[(sequence==12 & .na)]
.d12.1 <- d1[(sequence==12 & .na)]
.d12.2 <- d2[(sequence==12 & .na)]
.c12.12 <- cov.12.mat[(sequence==12 & .na),]
.c12.13 <- cov.13.mat[(sequence==12 & .na),]
.c12.14 <- cov.14.mat[(sequence==12 & .na),]
.c12.23 <- cov.23.mat[(sequence==12 & .na),]
.c12.24 <- cov.24.mat[(sequence==12 & .na),]

.w13 <- w[(sequence==13 & .na)]
.d13.1 <- d1[(sequence==13 & .na)]
.c13.12 <- cov.12.mat[(sequence==13 & .na),]
.c13.13 <- cov.13.mat[(sequence==13 & .na),]
.c13.14 <- cov.14.mat[(sequence==13 & .na),]

.w14 <- w[(sequence==14 & .na)]
.d14.1 <- d1[(sequence==14 & .na)]
.c14.12 <- cov.12.mat[(sequence==14 & .na),]
.c14.13 <- cov.13.mat[(sequence==14 & .na),]
.c14.14 <- cov.14.mat[(sequence==14 & .na),]

.w123 <- w[(sequence==123 & .na)]
.d123.1 <- d1[(sequence==123 & .na)]
.d123.2 <- d2[(sequence==123 & .na)]
.c123.12 <- cov.12.mat[(sequence==123 & .na),]
.c123.13 <- cov.13.mat[(sequence==123 & .na),]
.c123.14 <- cov.14.mat[(sequence==123 & .na),]
.c123.23 <- cov.23.mat[(sequence==123 & .na),]
.c123.24 <- cov.24.mat[(sequence==123 & .na),]

.w124 <- w[(sequence==124 & .na)]
.d124.1 <- d1[(sequence==124 & .na)]
.d124.2 <- d2[(sequence==124 & .na)]
.c124.12 <- cov.12.mat[(sequence==124 & .na),]
.c124.13 <- cov.13.mat[(sequence==124 & .na),]
.c124.14 <- cov.14.mat[(sequence==124 & .na),]
.c124.23 <- cov.23.mat[(sequence==124 & .na),]
.c124.24 <- cov.24.mat[(sequence==124 & .na),]

#log-likelihood
logV<-function(x)
{
if (dist[1]=="E" & is.null(cuts.12)) {assign("sigma12",exp(x[1]), inherits = TRUE); assign("nu12",1, inherits = TRUE); assign("theta12",1, inherits = TRUE); i<-1}
if (dist[1]=="W") { assign("sigma12",exp(x[1]), inherits = TRUE); assign("nu12",exp(x[2]), inherits = TRUE); assign("theta12",1, inherits = TRUE); i<-2}
if (dist[1]=="WG") {assign("sigma12",exp(x[1]), inherits = TRUE); assign("nu12",exp(x[2]), inherits = TRUE); assign("theta12",exp(x[3]), inherits = TRUE); i<-3}
if (dist[1]=="PE" & !is.null(cuts.12)) {assign("sigma12",exp(x[1:(length(cuts.12)-1)]), inherits = TRUE); i<-(length(cuts.12)-1)}

if (dist[2]=="E" & is.null(cuts.13)) {assign("sigma13",exp(x[i+1]), inherits = TRUE); assign("nu13",1, inherits = TRUE); assign("theta13",1, inherits = TRUE); i<-i+1}
if (dist[2]=="W") { assign("sigma13",exp(x[i+1]), inherits = TRUE); assign("nu13",exp(x[i+2]), inherits = TRUE); assign("theta13",1, inherits = TRUE); i<-i+2}
if (dist[2]=="WG") {assign("sigma13",exp(x[i+1]), inherits = TRUE); assign("nu13",exp(x[i+2]), inherits = TRUE); assign("theta13",exp(x[i+3]), inherits = TRUE); i<-i+3}
if (dist[2]=="PE" & !is.null(cuts.13)) {assign("sigma13",exp(x[(i+1):(i+length(cuts.13)-1)]), inherits = TRUE); i<-(i+length(cuts.13)-1)}

if (dist[3]=="E" & is.null(cuts.14)) {assign("sigma14",exp(x[i+1]), inherits = TRUE); assign("nu14",1, inherits = TRUE); assign("theta14",1, inherits = TRUE); i<-i+1}
if (dist[3]=="W") { assign("sigma14",exp(x[i+1]), inherits = TRUE); assign("nu14",exp(x[i+2]), inherits = TRUE); assign("theta14",1, inherits = TRUE); i<-i+2}
if (dist[3]=="WG") {assign("sigma14",exp(x[i+1]), inherits = TRUE); assign("nu14",exp(x[i+2]), inherits = TRUE); assign("theta14",exp(x[i+3]), inherits = TRUE); i<-i+3}
if (dist[3]=="PE" & !is.null(cuts.14)) {assign("sigma14",exp(x[(i+1):(i+length(cuts.14)-1)]), inherits = TRUE); i<-(i+length(cuts.14)-1)}

if (dist[4]=="E" & is.null(cuts.23)) {assign("sigma23",exp(x[i+1]), inherits = TRUE); assign("nu23",1, inherits = TRUE); assign("theta23",1, inherits = TRUE); i<-i+1}
if (dist[4]=="W") { assign("sigma23",exp(x[i+1]), inherits = TRUE); assign("nu23",exp(x[i+2]), inherits = TRUE); assign("theta23",1, inherits = TRUE); i<-i+2}
if (dist[4]=="WG") {assign("sigma23",exp(x[i+1]), inherits = TRUE); assign("nu23",exp(x[i+2]), inherits = TRUE); assign("theta23",exp(x[i+3]), inherits = TRUE); i<-i+3}
if (dist[4]=="PE" & !is.null(cuts.23)) {assign("sigma23",exp(x[(i+1):(i+length(cuts.23)-1)]), inherits = TRUE); i<-(i+length(cuts.23)-1)}

if (dist[5]=="E" & is.null(cuts.24)) {assign("sigma24",exp(x[i+1]), inherits = TRUE); assign("nu24",1, inherits = TRUE); assign("theta24",1, inherits = TRUE); i<-i+1}
if (dist[5]=="W") { assign("sigma24",exp(x[i+1]), inherits = TRUE); assign("nu24",exp(x[i+2]), inherits = TRUE); assign("theta24",1, inherits = TRUE); i<-i+2}
if (dist[5]=="WG") {assign("sigma24",exp(x[i+1]), inherits = TRUE); assign("nu24",exp(x[i+2]), inherits = TRUE); assign("theta24",exp(x[i+3]), inherits = TRUE); i<-i+3}
if (dist[5]=="PE" & !is.null(cuts.24)) {assign("sigma24",exp(x[(i+1):(i+length(cuts.24)-1)]), inherits = TRUE); i<-(i+length(cuts.24)-1)}

if (is.null(cov.12)) {assign("coef12",0, inherits = TRUE)}
 else {assign("coef12",x[(i+1):(i+ncol(data.frame(cov.12)))], inherits = TRUE); i <-i+ncol(data.frame(cov.12))}
if (is.null(cov.13)) {assign("coef13",0, inherits = TRUE)}
 else {assign("coef13",x[(i+1):(i+ncol(data.frame(cov.13)))], inherits = TRUE); i <-i+ncol(data.frame(cov.13))}
if (is.null(cov.14)) {assign("coef14",0, inherits = TRUE)}
 else {assign("coef14",x[(i+1):(i+ncol(data.frame(cov.14)))], inherits = TRUE); i <-i+ncol(data.frame(cov.14))}
if (is.null(cov.23)) {assign("coef23",0, inherits = TRUE)}
 else {assign("coef23",x[(i+1):(i+ncol(data.frame(cov.23)))], inherits = TRUE); i <-i+ncol(data.frame(cov.23))}
if (is.null(cov.24)) {assign("coef24",0, inherits = TRUE)}
 else {assign("coef24",x[(i+1):(i+ncol(data.frame(cov.24)))], inherits = TRUE); i <-i+ncol(data.frame(cov.24))}

return( -1*(
 sum( .w1 * c1(.d1.1, .c1.12, .c1.13, .c1.14, cuts.12, cuts.13, cuts.14) ) +
 sum( .w12 * c12(.d12.1, .d12.2, .c12.12, .c12.13, .c12.14, .c12.23, .c12.24, cuts.12, cuts.13, cuts.14, cuts.23, cuts.24) ) +
 sum( .w13 * c13(.d13.1, .c13.12, .c13.13, .c13.14, cuts.12, cuts.13, cuts.14) ) +
 sum( .w14 * c14(.d14.1, .c14.12, .c14.13, .c14.14, cuts.12, cuts.13, cuts.14) ) +
 sum( .w123 * c123(.d123.1, .d123.2, .c123.12, .c123.13, .c123.14, .c123.23, .c123.24, cuts.12, cuts.13, cuts.14, cuts.23, cuts.24) ) +
 sum( .w124 * c124(.d124.1, .d124.2, .c124.12, .c124.13, .c124.14, .c124.23, .c124.24, cuts.12, cuts.13, cuts.14, cuts.23, cuts.24) ) ) )

}

#first maximum likelihood optimization
n<-1
res<-tryCatch(optim(ini, logV, hessian=conf.int, control=list(maxit=100000)))

if(inherits(res, "error"))  {
cat("Maximum likelihood optimization fails to converge", "\n")
  } else  {
if(silent==FALSE) {cat(-1*res$value, "\n")}  

#further maximum likelihood optimizations
if(is.null(precision)) {delta <- 10^(-6)} else {delta <-precision}


while (n<=2 & !(inherits(res, "error"))) {
temp.value<-res$value
res<-tryCatch(optim(res$par, logV, hessian=conf.int, control=list(maxit=100000)))

if (!(inherits(res, "error"))) {
   n<-1*((temp.value-res$value)>delta) + (n+1)*((temp.value-res$value)<=delta)
   if(silent==FALSE) {cat(-1*res$value, "\n")} }
   }
if(inherits(res, "error")) {
cat("Maximum likelihood optimization fails to converge", "\n")
  } else {

#output
if (conf.int==TRUE) {
  if (max(!is.na(tryCatch(solve(res$hessian), error=function(e) NA)),na.rm=F)==1){
  table.res <- data.frame(Estimate = round(res$par, 4),
  SE = round(sqrt(diag(solve(res$hessian))), 4),
  Wald = round(res$par/sqrt(diag(solve(res$hessian))), 4),
  Pvalue = round(2*(1-pnorm(abs(res$par/sqrt(diag(solve(res$hessian)))), 0, 1)) , 4) )
  names(table.res)<-c("Estimate","Std.Error","t.value","Pr(>|t|)")
  table.covariance<-res$hessian
  }
  else {
  table.res <- data.frame(Estimate = round(res$par, 4) )
  table.covariance<-NULL
  cat("Hessian matrix not defined", "\n")
  } #end else for hessian matrix condition
}

if (conf.int==FALSE) {
table.res <- data.frame(Estimate = round(res$par, 4) )
table.covariance<-NULL
}

if (dist[1]=="E" & is.null(cuts.12))  { lab12<-c("log(sigma) on trans. 12")}
if (dist[1]=="W" & is.null(cuts.12))  { lab12<-c("log(sigma) on trans. 12", "log(nu) on trans. 12")}
if (dist[1]=="WG" & is.null(cuts.12)) { lab12<-c("log(sigma) on trans. 12", "log(nu) on trans. 12", "log(theta) on trans. 12")}
if (dist[1]=="PE" & !is.null(cuts.12)) {
 lab12<-rep("",length(cuts.12)-1)
 for (i in (1:(length(cuts.12)-1)))
  {
  lab12[i]<-paste("log(sigma) on trans. 12, interval [",cuts.12[i],";",cuts.12[i+1],"[",sep="")
  }
 }

if (dist[2]=="E" & is.null(cuts.13))  { lab13<-c("log(sigma) on trans. 13")}
if (dist[2]=="W" & is.null(cuts.13))  { lab13<-c("log(sigma) on trans. 13", "log(nu) on trans. 13")}
if (dist[2]=="WG" & is.null(cuts.13)) { lab13<-c("log(sigma) on trans. 13", "log(nu) on trans. 13", "log(theta) on trans. 13")}
if (dist[2]=="PE" & !is.null(cuts.13)) {
 lab13<-rep("",length(cuts.13)-1)
 for (i in (1:(length(cuts.13)-1)))
  {
  lab13[i]<-paste("log(sigma) on trans. 13, interval [",cuts.13[i],";",cuts.13[i+1],"[",sep="")
  }
 }

if (dist[3]=="E" & is.null(cuts.14))  { lab14<-c("log(sigma) on trans. 14")}
if (dist[3]=="W" & is.null(cuts.14))  { lab14<-c("log(sigma) on trans. 14", "log(nu) on trans. 14")}
if (dist[3]=="WG" & is.null(cuts.14)) { lab14<-c("log(sigma) on trans. 14", "log(nu) on trans. 14", "log(theta) on trans. 14")}
if (dist[3]=="PE" & !is.null(cuts.14)) {
 lab14<-rep("",length(cuts.14)-1)
 for (i in (1:(length(cuts.14)-1)))
  {
  lab14[i]<-paste("log(sigma) on trans. 14, interval [",cuts.14[i],";",cuts.14[i+1],"[",sep="")
  }
 }

if (dist[4]=="E" & is.null(cuts.23))  { lab23<-c("log(sigma) on trans. 23")}
if (dist[4]=="W" & is.null(cuts.23))  { lab23<-c("log(sigma) on trans. 23", "log(nu) on trans. 23")}
if (dist[4]=="WG" & is.null(cuts.23)) { lab23<-c("log(sigma) on trans. 23", "log(nu) on trans. 23", "log(theta) on trans. 23")}
if (dist[4]=="PE" & !is.null(cuts.23)) {
 lab23<-rep("",length(cuts.23)-1)
 for (i in (1:(length(cuts.23)-1)))
  {
  lab23[i]<-paste("log(sigma) on trans. 23, interval [",cuts.23[i],";",cuts.23[i+1],"[",sep="")
  }
 }

if (dist[5]=="E" & is.null(cuts.24))  { lab24<-c("log(sigma) on trans. 24")}
if (dist[5]=="W" & is.null(cuts.24))  { lab24<-c("log(sigma) on trans. 24", "log(nu) on trans. 24")}
if (dist[5]=="WG" & is.null(cuts.24)) { lab24<-c("log(sigma) on trans. 24", "log(nu) on trans. 24", "log(theta) on trans. 24")}
if (dist[5]=="PE" & !is.null(cuts.24)) {
 lab24<-rep("",length(cuts.24)-1)
 for (i in (1:(length(cuts.24)-1)))
  {
  lab24[i]<-paste("log(sigma) on trans. 24, interval [",cuts.24[i],";",cuts.24[i+1],"[",sep="")
  }
 }


lab<-c(lab12, lab13, lab14, lab23,lab24, n.12, n.13, n.14, n.23, n.24)

rownames(table.res) <- paste(1:length(lab), lab)

cat("Number of data rows:",nrow(.D))
cat("Number of data rows with missing values (deleted):",nrow(.D)-sum(.na),"\n")

return(list(
object="sm4 (4-state semi-Markov model)",
dist=dist,
cuts.12=cuts.12,
cuts.13=cuts.13,
cuts.14=cuts.14,
cuts.23=cuts.23,
cuts.24=cuts.24,
covariates=c( max(0, length(n.12)), max(0, length(n.13)), max(0, length(n.14)), max(0, length(n.23)), max(0, length(n.24)) ),
table=table.res,
cov.matrix=table.covariance,
LogLik=(-1*res$value),
AIC=2*length(res$par)-2*(-1*res$value)))
  } 
  } 
}

Try the multistate package in your browser

Any scripts or data that you put into this service are public.

multistate documentation built on May 2, 2019, 5:16 a.m.