Hi,
I simulated a dataset to test a survival model with both right censoring and interval censoring. The simulation is based on a dataset of cat adoptions used by Richard McElreath. The original dataset only has right censoring, so I added interval censoring.
days_to_event
- time until the cat was adopted or the data collection ended
y1
- number of days until the cat was last seen at the shelter
y2
- number of days until the cat was adopted
color_id
- color of the cat
censored
- β1β if right censored, β2β if interval censoring
This is the Stan model Iβm testing:
data {
int<lower=1> N;
vector[N] y1;
vector[N] y2;
int color_id[N];
int<lower=1,upper=2> adopted[N];
}
parameters{
vector[2] a;
}
transformed parameters{
vector[N] mu = exp(a[color_id]);
vector[N] lambda;
for(i in 1:N) lambda[i] = 1/mu[i];
}
model {
for (i in 1:N)
if ( adopted[i] == 1 ) target += exponential_lccdf(y1[i] | lambda[i]);
for (i in 1:N)
if ( adopted[i] == 2 ) target += log_diff_exp(exponential_lcdf(y2[i] | lambda[i]),exponential_lcdf(y1[i] | lambda[i]));
a ~ normal( 0 , 1 );
}
I run the model with the following code:
library(rstan)
dat<-list(N=nrow(d),
adopted=d$adopted,
y1=d$y1,
y2=d$y2,
color_id=d$color_id)
library(rstan)
fit<-stan("interval.stan",chains=4,cores=4,data=dat)
but Iβm getting a bunch errors.
Chain 4: Stan can't start sampling from this initial value.
Chain 4: Rejecting initial value:
Chain 4: Log probability evaluates to log(0), i.e. negative infinity.
Chain 4: Stan can't start sampling from this initial value.
Chain 4:
Chain 4: Initialization between (-2, 2) failed after 100 attempts.
Chain 4: Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
I tried running the model with only right censoring and it runs ok.
Thanks for the help.
Example data
d<-structure(list(days_to_event = c(191, 140, 135, 106, 208, 195,
145, 158, 137, 108, 140, 107, 184, 145, 276, 123, 107, 216, 117,
160, 101, 124, 206, 144, 107, 132, 135, 164, 139, 126, 141, 234,
186, 106, 201, 114, 111, 176, 111, 117, 267, 240, 143, 144, 107,
132, 143, 120, 151, 133, 211, 109, 107, 110, 221, 131, 107, 102,
180, 109, 131, 257, 168, 130, 116, 110, 161, 108, 116, 143, 119,
101, 140, 148, 153, 150, 163, 107, 144, 129, 127, 135, 286, 106,
139, 134, 116, 110, 132, 110, 161, 149, 384, 157, 205, 147, 167,
169, 150, 159, 110, 122, 193, 141, 255, 101, 121, 139, 165, 249,
158, 104, 106, 119, 111, 174, 121, 147, 109, 142, 194, 113, 133,
165, 154, 123, 211, 123, 108, 138, 119, 127, 126, 127, 123, 119,
123, 109, 115, 157, 159, 140, 106, 148, 203, 130, 114, 216, 115,
126, 157, 135, 117, 117, 137, 111, 109, 129, 118, 104, 112, 102,
129, 208, 151, 150, 138, 136, 183, 129, 162, 150, 104, 130, 107
), color_id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), adopted = c(2, 2, 2,
2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 1, 2,
2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2,
2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2,
2, 2, 2, 2, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2), y1 = c(161, 110, 105, 76, 178, 165, 115, 128, 107,
78, 110, 77, 154, 115, 246, 93, 77, 186, 87, 130, 71, 94, 176,
114, 77, 102, 105, 134, 109, 96, 111, 204, 156, 76, 171, 84,
81, 146, 81, 87, 237, 210, 113, 114, 77, 102, 113, 90, 121, 103,
181, 79, 77, 80, 191, 101, 77, 72, 150, 79, 101, 227, 138, 100,
86, 80, 131, 78, 86, 113, 89, 71, 110, 118, 123, 120, 133, 77,
114, 99, 97, 105, 256, 76, 109, 104, 86, 80, 102, 80, 131, 119,
354, 127, 175, 117, 137, 139, 120, 129, 80, 92, 163, 111, 225,
71, 91, 109, 135, 219, 128, 74, 76, 89, 81, 144, 91, 117, 79,
112, 164, 83, 103, 135, 124, 93, 181, 93, 78, 108, 89, 97, 96,
97, 93, 89, 93, 79, 85, 127, 129, 110, 76, 118, 173, 100, 84,
186, 85, 96, 127, 105, 87, 87, 107, 81, 79, 99, 88, 74, 82, 72,
99, 178, 121, 120, 108, 106, 153, 99, 132, 120, 74, 100, 77),
y2 = c(221, 170, 165, 136, 0, 225, 175, 188, 167, 138, 170,
137, 214, 175, 0, 153, 137, 0, 147, 190, 131, 154, 0, 174,
137, 162, 165, 194, 169, 156, 171, 0, 216, 136, 0, 144, 141,
206, 141, 147, 0, 0, 173, 174, 137, 162, 173, 150, 181, 163,
0, 139, 137, 140, 0, 161, 137, 132, 210, 139, 161, 0, 198,
160, 146, 140, 191, 138, 146, 173, 149, 131, 170, 178, 183,
180, 193, 137, 174, 159, 157, 165, 0, 136, 169, 164, 146,
140, 162, 140, 191, 179, 0, 187, 0, 177, 197, 199, 180, 189,
140, 152, 223, 171, 0, 131, 151, 169, 195, 0, 188, 134, 136,
149, 141, 204, 151, 177, 139, 172, 224, 143, 163, 195, 184,
153, 0, 153, 138, 168, 149, 157, 156, 157, 153, 149, 153,
139, 145, 187, 189, 170, 136, 178, 0, 160, 144, 0, 145, 156,
187, 165, 147, 147, 167, 141, 139, 159, 148, 134, 142, 132,
159, 0, 181, 180, 168, 166, 213, 159, 192, 180, 134, 160,
137)), class = "data.frame", row.names = c(NA, -175L))