// Stan functions from nfidd version 1.3.0
array[] real renewal(real I0, array[] real R, array[] real gen_time) {
int n = num_elements(R);
int max_gen_time = num_elements(gen_time); // gen_time st ...
Write functions to a temporary file for demonstration:
Stan functions written to: /tmp/RtmpmAsn27/my_functions.stan
[1] "functions {\n// Stan functions from nfidd version 1.3.0\narray[] real renewal(real I0, array[] real R, array[] real gen_time) {\n int n = num_elements(R);\n int max_gen_time = num_elements(gen_time); // gen_time starts at day 1\n array[n + 1] real I; // infections, with I0 prepended at index 1\n I[1] = I0;\n for (i in 1:n) {\n // window of past infections contributing to the current time point\n int first_index = max(1, i - max_gen_time + 1);\n int len = i - first_index + 1;\n array[len] real segment = I[first_index:i];\n // reverse the (possibly truncated) generation time to align with the segment\n array[len] real gen_pmf = reverse(gen_time[1:len]);\n // renewal equation: convolve past infections with the generation time, scale by R\n I[i + 1] = dot_product(segment, gen_pmf) * R[i];\n }\n return I[2:(n + 1)]; // drop the prepended I0\n}\n}"
Verify file was created:
cat("File created at:", temp_file)
File created at: /tmp/RtmpmAsn27/my_functions.stan