For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
For within-chain threading using `reduce_sum()` or `map_rect()` Stan functions,
change `threads_per_chain` option:
rstan_options(threads_per_chain = 1)
library(arrow)
Attaching package: 'arrow'
The following object is masked from 'package:utils':
timestamp
library(survival)library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(ggplot2)library(eha)
Loading Data
# Load Data df <-read_parquet("age-stage-survival.parquet")# Prepare data for Stan df$status <-1# All events occurred, no censoring df$stage <-as.integer(df$grade) df <- df %>%mutate(stage1 =ifelse(stage ==1,1,0),stage2 =ifelse(stage ==2,1,0),stage3 =ifelse(stage ==3,1,0) ) stan_data <-list(N =nrow(df), # Number of observationsK =length(unique(df$grade)), # Number of unique stagestime = df$time, # Survival timesstatus = df$status, # Event indicators (1 = event, 0 = censored)age = df$age, # Age covariatestage = df$stage, # Stagestage1 = df$stage1,stage2 = df$stage2,stage3 = df$stage3 )
1. A single Weibull baseline hazard with a common age relationship across stage.
Let’s write the model. In stan for MLE, we need to define these 3 sections
writeLines("data { int<lower=1> N; // Number of observations vector[N] age; // Age variable array[N] int<lower=0, upper=1> stage1; array[N] int<lower=0, upper=1> stage2; array[N] int<lower=0, upper=1> stage3; vector[N] time; // Time until death variable}parameters { real alpha1; // Coefficients for stages 2, 3, and 4 (stage 1 is the baseline) real alpha2; real alpha3; real beta0; real beta1; // Coefficient for age real beta2; // Coefficient for squared age real<lower=0> shape; // Shape parameter of the Weibull distribution real<lower=0> scale; // Scale parameter of the Weibull distribution}model { for (i in 1:N) { // Loop through each observation // Calculate the linear predictor based on age and stage // Add the stage coefficient; stage 4 is the baseline, so we only adjust for stages 1-3 real linear_pred = beta0 + beta1 * age[i] + beta2 * square(age[i]) + alpha1 * stage1[i] + alpha2 * stage2[i] + alpha3 * stage3[i] ; target += weibull_lpdf(time[i] | shape, scale) +linear_pred + (exp(linear_pred)-1) * weibull_lccdf(time[i] | shape, scale); }}", con ="model1.stan")
Fitting the model
# Compile the Stan modelstan_model_1 <-stan_model(file ="model1.stan")
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘Apple clang version 14.0.3 (clang-1403.0.22.14.1)’
using SDK: ‘MacOSX13.3.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c foo.c -o foo.o
In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Core:19:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: 'cmath' file not found
#include <cmath>
^~~~~~~
1 error generated.
make: *** [foo.o] Error 1
# Perform MLE using the optimizing functionfit_mle_1 <-optimizing(stan_model_1, data = stan_data, hessian =TRUE)
Warning in .local(object, ...): non-zero return code in optimizing
# View the optimized parameter estimatesprint(fit_mle_1)
1b. Generate a partial effect plot of log relative hazard by age, stratified by stage. (Uncertainty interval not required for now.)
# Extract MLE estimates for parametersbeta0 <- fit_mle_1$par["beta0"]beta1 <- fit_mle_1$par["beta1"]beta2 <- fit_mle_1$par["beta2"]alpha1 <- fit_mle_1$par["alpha1"]alpha2 <- fit_mle_1$par["alpha2"]alpha3 <- fit_mle_1$par["alpha3"]shape <- fit_mle_1$par["shape"]scale <- fit_mle_1$par["scale"]# Define the age range for plottingage_range <-seq(20, 80, by =1) # Modify this as needed# Weibull baseline hazard for each age pointweibull_baseline <- (shape -1) *log(age_range / scale)# Calculate log relative hazards for each stagelog_relative_hazard_stage1 <- beta0 + beta1 * age_range + beta2 * age_range^2+ alpha1 + weibull_baselinelog_relative_hazard_stage2 <- beta0 + beta1 * age_range + beta2 * age_range^2+ alpha2 + weibull_baselinelog_relative_hazard_stage3 <- beta0 + beta1 * age_range + beta2 * age_range^2+ alpha3 + weibull_baselinelog_relative_hazard_stage4 <- beta0 + beta1 * age_range + beta2 * age_range^2+ weibull_baseline # baseline stage# Create the plotplot(age_range, log_relative_hazard_stage1, type ="l", col ="blue", lty =1,ylim =range(log_relative_hazard_stage1, log_relative_hazard_stage2, log_relative_hazard_stage3, log_relative_hazard_stage4),xlab ="Age", ylab ="Log Relative Hazard", main ="Log Relative Hazard vs Age by Stage")lines(age_range, log_relative_hazard_stage2, col ="red", lty =2)lines(age_range, log_relative_hazard_stage3, col ="green", lty =3)lines(age_range, log_relative_hazard_stage4, col ="purple", lty =4)# Add a legendlegend("topleft", legend =c("Stage 1", "Stage 2", "Stage 3", "Stage 4 (B)"),col =c("blue", "red", "green", "purple"), lty =1:4)
1c. Generate a partial effect plot of median survival by age, stratified by stage. (Uncertainty interval not required for now.) There is no reason to think the log relative hazard is linear in age.
# Extract MLE estimates for parametersbeta0 <- fit_mle_1$par["beta0"]beta1 <- fit_mle_1$par["beta1"]beta2 <- fit_mle_1$par["beta2"]alpha1 <- fit_mle_1$par["alpha1"]alpha2 <- fit_mle_1$par["alpha2"]alpha3 <- fit_mle_1$par["alpha3"]shape <- fit_mle_1$par["shape"]scale <- fit_mle_1$par["scale"]# Define age range and stagesages <-seq(20, 90, by =5) # Adjust age range as neededstages <-c(1, 2, 3, 4)# Function to calculate median survival using exponential_lccdfmedian_survival <-function(age, stage_indicator) {# Select the appropriate alpha for each stage stage_alpha <-c(0, alpha1, alpha2, alpha3)[stage_indicator +1] # +1 to align with R indexing# Calculate the log hazard rate for the given age and stage & convert log-hazard to median survival using the exponential lccdf log_hazard <- beta0 + beta1 * age + beta2 * age^2#+ stage_alpha-exponential_lccdf(0.5, log_hazard, shape, scale)}# Generate data for median survival by age and stagesurvival_data <-expand.grid(age = ages, stage = stages)survival_data$median_survival <-mapply(median_survival, survival_data$age, survival_data$stage)# Plotlibrary(ggplot2)ggplot(survival_data, aes(x = age, y = median_survival, color =as.factor(stage))) +geom_line() +labs(x ="Age",y ="Median Survival",color ="Stage",title ="Partial Effect of Age on Median Survival by Stage" ) +theme_minimal()
2. A single Weibull baseline hazard with a common age relationship across stage.
2a.Write a mathematical expression of the model. The hazard function is defined as:
writeLines("data { int<lower=1> N; // Number of observations vector[N] age; // Age variable array[N] int<lower=0, upper=1> stage2; array[N] int<lower=0, upper=1> stage3; array[N] int<lower=0, upper=1> stage1; vector[N] time; // Time until death variable } parameters { real beta0; // constant coefficient stage 4 real beta1; // Coefficient for age stage 4 real beta2; // Coefficient for squared age stage 4 real gamma0; //coefficient for stage 2 constant real gamma1; //cofefficient for stage 2 age real gamma2; // coefficient for stage 2 age squared real lambda0; //coefficient for stage 3 constant real lambda1; //cofefficient for stage 3 age real lambda2; // coefficient for stage 3 age squared real alpha0; //coefficient for stage 1 constant real alpha1; //cofefficient for stage 1 age real alpha2; // coefficient for stage 1 age squared real<lower=0> shape; // Shape parameter of the Weibull distribution real<lower=0> scale; // Scale parameter of the Weibull distribution } model { for (i in 1:N) { // Loop through each observation // Calculate the linear predictor based on age and stage // Add the stage coefficient; stage 4 is the baseline, so we only adjust for stages 1-3 real linear_pred = beta0 + beta1 * age[i] + beta2 * square(age[i]) + alpha0 * stage1[i] + alpha1 * stage1[i] * age[i] + alpha2 * stage1[i] * square(age[i]) + gamma0 * stage2[i] + gamma1 * stage2[i] * age[i] + gamma2 * stage2[i] * square(age[i]) + lambda0 *stage3[i] + lambda1 * stage3[i] * age[i] + lambda2 * stage3[i] * square(age[i]) ; target += weibull_lpdf(time[i] | shape, scale) +linear_pred + (exp(linear_pred)-1) * weibull_lccdf(time[i] | shape, scale); } } ", con ="model2.stan")
# Compile the Stan modelstan_model_2 <-stan_model(file ="model2.stan")
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘Apple clang version 14.0.3 (clang-1403.0.22.14.1)’
using SDK: ‘MacOSX13.3.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c foo.c -o foo.o
In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Core:19:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: 'cmath' file not found
#include <cmath>
^~~~~~~
1 error generated.
make: *** [foo.o] Error 1
# Perform MLE using the optimizing functionfit_mle_2 <-optimizing(stan_model_2, data = stan_data, hessian =TRUE)
Warning in .local(object, ...): non-zero return code in optimizing
# View the optimized parameter estimatesprint(fit_mle_2)
2b.Generate a partial effect plot of log relative hazard by age, stratified by stage. (Uncertainty interval not required for now.)
# Extract MLE estimates for parametersbeta0 <- fit_mle_2$par["beta0"]beta1 <- fit_mle_2$par["beta1"]beta2 <- fit_mle_2$par["beta2"]gamma0 <- fit_mle_2$par["gamma0"]gamma1 <- fit_mle_2$par["gamma1"]gamma2 <- fit_mle_2$par["gamma2"]lambda0 <- fit_mle_2$par["lambda0"]lambda1 <- fit_mle_2$par["lambda1"]lambda2 <- fit_mle_2$par["lambda2"]alpha0 <- fit_mle_2$par["alpha0"]alpha1 <- fit_mle_2$par["alpha1"]alpha2 <- fit_mle_2$par["alpha2"]shape <- fit_mle_2$par["shape"]scale <- fit_mle_2$par["scale"]# Define the age range for plottingage_range <-seq(20, 80, by =1) # Modify this as needed# Weibull baseline hazard for each age pointweibull_baseline <- (shape -1) *log(age_range / scale)# Calculate log relative hazards for each stagelog_relative_hazard_stage1 <- beta0 + beta1 * age_range + beta2 * age_range^2+ alpha0 + alpha1 * age_range + alpha2 * age_range^2+ weibull_baselinelog_relative_hazard_stage2 <- beta0 + beta1 * age_range + beta2 * age_range^2+ gamma0 + gamma1 * age_range + gamma2 * age_range^2+ weibull_baselinelog_relative_hazard_stage3 <- beta0 + beta1 * age_range + beta2 * age_range^2+ lambda0 + lambda1 * age_range + lambda2 * age_range^2+ weibull_baselinelog_relative_hazard_stage4 <- beta0 + beta1 * age_range + beta2 * age_range^2+ weibull_baseline # baseline stage#print(log_relative_hazard_stage1)#print(log_relative_hazard_stage2)#print(log_relative_hazard_stage3)#print(log_relative_hazard_stage4)# Create the plotplot(age_range, log_relative_hazard_stage1, type ="l", col ="blue", lty =1,ylim =range(log_relative_hazard_stage1, log_relative_hazard_stage2, log_relative_hazard_stage3, log_relative_hazard_stage4),xlab ="Age", ylab ="Log Relative Hazard", main ="Log Relative Hazard vs Age by Stage")lines(age_range, log_relative_hazard_stage2, col ="red", lty =2)lines(age_range, log_relative_hazard_stage3, col ="green", lty =3)lines(age_range, log_relative_hazard_stage4, col ="purple", lty =4)# Add a legendlegend("bottomleft", legend =c("Stage 1", "Stage 2", "Stage 3", "Stage 4 (Baseline)"),col =c("blue", "red", "green", "purple"), lty =1:4)
2c. Generate a partial effect plot of median survival by age, stratified by stage. (Uncertainty interval not required for now.) There is no reason to think the log relative hazard is linear in age.
3. Parametric baseline hazard stratified by stage, a common age relationship.
3a.Write a mathematical expression of the model. The hazard function is defined as:
writeLines("data { int<lower=1> N; // Number of observations vector[N] age; // Age variable array[N] int<lower=0, upper=1> stage2; array[N] int<lower=0, upper=1> stage3; array[N] int<lower=0, upper=1> stage1; array[N] int<lower=1, upper=4> stage; vector[N] time; // Time until death variable}parameters { real beta0; // Intercept real beta1; // Coefficient for age real beta2; // Coefficient for age^2 real<lower=0> shape1; // Shape parameter for stage 1 real<lower=0> scale1; // Scale parameter for stage 1 real<lower=0> shape2; // Shape parameter for stage 2 real<lower=0> scale2; // Scale parameter for stage 2 real<lower=0> shape3; // Shape parameter for stage 3 real<lower=0> scale3; // Scale parameter for stage 3 real<lower=0> shape4; // Shape parameter for stage 4 real<lower=0> scale4; // Scale parameter for stage 4}model { for (i in 1:N) { // real linear_pred = beta0+ beta1* age[i] + beta2 * square(age[i]); if (stage[i] == 1){ target += weibull_lpdf(time[i] | shape1, scale1) +linear_pred + (exp(linear_pred)-1) * weibull_lccdf(time[i] | shape1, scale1); } else if (stage[i] == 2){ target += weibull_lpdf(time[i] | shape2, scale2) +linear_pred + (exp(linear_pred)-1) * weibull_lccdf(time[i] | shape2, scale2); } else if (stage[i] == 3){ target += weibull_lpdf(time[i] | shape3, scale3) +linear_pred + (exp(linear_pred)-1) * weibull_lccdf(time[i] | shape3, scale3); } else{ target += weibull_lpdf(time[i] | shape4, scale4) +linear_pred + (exp(linear_pred)-1) * weibull_lccdf(time[i] | shape4, scale4); } }}", con ="model3.stan")
# Compile the Stan modelstan_model_3 <-stan_model(file ="model3.stan")
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘Apple clang version 14.0.3 (clang-1403.0.22.14.1)’
using SDK: ‘MacOSX13.3.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c foo.c -o foo.o
In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Core:19:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: 'cmath' file not found
#include <cmath>
^~~~~~~
1 error generated.
make: *** [foo.o] Error 1
# Perform MLE using the optimizing functionfit_mle_3 <-optimizing(stan_model_3, data = stan_data, hessian =TRUE)
Warning in .local(object, ...): non-zero return code in optimizing
# View the optimized parameter estimatesprint(fit_mle_3)
3b.Generate a partial effect plot of log relative hazard by age, stratified by stage. (Uncertainty interval not required for now.)
# Extract MLE estimates for parametersbeta0 <- fit_mle_3$par["beta0"]beta1 <- fit_mle_3$par["beta1"]beta2 <- fit_mle_3$par["beta2"]shape1 <- fit_mle_3$par["shape1"]scale1 <- fit_mle_3$par["scale1"]shape2 <- fit_mle_3$par["shape2"]scale2 <- fit_mle_3$par["scale2"]shape3 <- fit_mle_3$par["shape3"]scale3 <- fit_mle_3$par["scale3"]shape4 <- fit_mle_3$par["shape4"]scale4 <- fit_mle_3$par["scale4"]# Define the age range for plottingage_range <-seq(20, 80, by =1) # Modify this as needed# Weibull baseline hazard for each age pointweibull_baseline1 <- (shape1 -1) *log(age_range / scale1)weibull_baseline2 <- (shape2 -1) *log(age_range / scale2)weibull_baseline3 <- (shape3 -1) *log(age_range / scale3)weibull_baseline4 <- (shape4 -1) *log(age_range / scale4)# Calculate log relative hazards for each stagelog_relative_hazard_stage1 <- beta0 + beta1 * age_range + beta2 * age_range^2+ weibull_baseline1log_relative_hazard_stage2 <- beta0 + beta1 * age_range + beta2 * age_range^2+ weibull_baseline2log_relative_hazard_stage3 <- beta0 + beta1 * age_range + beta2 * age_range^2+ weibull_baseline3log_relative_hazard_stage4 <- beta0 + beta1 * age_range + beta2 * age_range^2+ weibull_baseline4 # baseline stage#print(log_relative_hazard_stage1)#print(log_relative_hazard_stage2)#print(log_relative_hazard_stage3)#print(log_relative_hazard_stage4)# Create the plotplot(age_range, log_relative_hazard_stage1, type ="l", col ="blue", lty =1,ylim =range(log_relative_hazard_stage1, log_relative_hazard_stage2, log_relative_hazard_stage3, log_relative_hazard_stage4),xlab ="Age", ylab ="Log Relative Hazard", main ="Log Relative Hazard vs Age by Stage")lines(age_range, log_relative_hazard_stage2, col ="red", lty =2)lines(age_range, log_relative_hazard_stage3, col ="green", lty =3)lines(age_range, log_relative_hazard_stage4, col ="purple", lty =4)# Add a legendlegend("topleft", legend =c("Stage 1", "Stage 2", "Stage 3", "Stage 4 (Baseline)"),col =c("blue", "red", "green", "purple"), lty =1:4)
3c. Generate a partial effect plot of median survival by age, stratified by stage. (Uncertainty interval not required for now.) There is no reason to think the log relative hazard is linear in age.
4.Parametric baseline hazard stratified by stage, a stage-specific age relationship.
writeLines("data { int<lower=1> N; // Number of observations vector[N] age; // Age variable array[N] int<lower=0, upper=1> stage2; array[N] int<lower=0, upper=1> stage3; array[N] int<lower=0, upper=1> stage1; array[N] int<lower=1, upper=4> stage; vector[N] time; // Time until death variable}parameters { // Common age-related coefficients real beta0; // intercept stage 4 real beta1; // age coefficient (linear term) stage 4 real beta2; // age coefficient (quadratic term) stage 4 // Stage-specific age relationship coefficients real alpha0; // stage 1 constant real alpha1; // stage 1 age coefficient (linear) real alpha2; // stage 1 age coefficient (quadratic) real gamma0; // stage 2 constant real gamma1; // stage 2 age coefficient (linear) real gamma2; // stage 2 age coefficient (quadratic) real lambda0; // stage 3 constant real lambda1; // stage 3 age coefficient (linear) real lambda2; // stage 3 age coefficient (quadratic) // Stage-specific baseline hazard parameters (Weibull) real<lower=0> shape1; // shape for stage 1 real<lower=0> scale1; // scale for stage 1 real<lower=0> shape2; // shape for stage 2 real<lower=0> scale2; // scale for stage 2 real<lower=0> shape3; // shape for stage 3 real<lower=0> scale3; // scale for stage 3 real<lower=0> shape4; // shape for stage 4 real<lower=0> scale4; // scale for stage 4}model { for (i in 1:N) { real log_baseline_hazard; // Common age-related effects real age_effect = beta0 + beta1 * age[i] + beta2 * square(age[i]); // Stage-specific effects if (stage[i] == 1) { age_effect += alpha0 + alpha1 * age[i] + alpha2 * square(age[i]); log_baseline_hazard = log(shape1) + (shape1 - 1) * log(time[i]) - time[i] / scale1; } else if (stage[i] == 2) { age_effect += gamma0 + gamma1 * age[i] + gamma2 * square(age[i]); log_baseline_hazard = log(shape2) + (shape2 - 1) * log(time[i]) - time[i] / scale2; } else if (stage[i] == 3) { age_effect += lambda0 + lambda1 * age[i] + lambda2 * square(age[i]); log_baseline_hazard = log(shape3) + (shape3 - 1) * log(time[i]) - time[i] / scale3; } else { log_baseline_hazard = log(shape4) + (shape4 - 1) * log(time[i]) - time[i] / scale4; } target += - exp(log_baseline_hazard + age_effect); }}", con ="model4.stan")
# Compile the Stan modelstan_model_4 <-stan_model(file ="model4.stan")
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘Apple clang version 14.0.3 (clang-1403.0.22.14.1)’
using SDK: ‘MacOSX13.3.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c foo.c -o foo.o
In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Core:19:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: 'cmath' file not found
#include <cmath>
^~~~~~~
1 error generated.
make: *** [foo.o] Error 1
# Perform MLE using the optimizing functionfit_mle_4 <-optimizing(stan_model_4, data = stan_data, hessian =TRUE)
Warning in .local(object, ...): non-zero return code in optimizing
# View the optimized parameter estimatesprint(fit_mle_4)
4b.Generate a partial effect plot of log relative hazard by age, stratified by stage. (Uncertainty interval not required for now.)
4c. Generate a partial effect plot of median survival by age, stratified by stage. (Uncertainty interval not required for now.) There is no reason to think the log relative hazard is linear in age.
5.Mean of log days with stage-specific age relationship.
Mean log(time) is directly related to the expected survival time on a logarithmic scale, rather than the instantaneous rate of event occurrence. The coefficients in this model represent direct adjustments to the expected log survival time based on age and stage, without needing a baseline hazard term to describe time-dependent risk.
writeLines("data { int<lower=1> N; // Number of observations vector[N] age; // Age variable array[N] int<lower=0, upper=1> stage2; array[N] int<lower=0, upper=1> stage3; array[N] int<lower=0, upper=1> stage1; vector[N] time; // Time until death variable } parameters { real beta0; // constant coefficient stage 4 real beta1; // Coefficient for age stage 4 real beta2; // Coefficient for squared age stage 4 real gamma0; //coefficient for stage 2 constant real gamma1; //cofefficient for stage 2 age real gamma2; // coefficient for stage 2 age squared real lambda0; //coefficient for stage 3 constant real lambda1; //cofefficient for stage 3 age real lambda2; // coefficient for stage 3 age squared real alpha0; //coefficient for stage 1 constant real alpha1; //cofefficient for stage 1 age real alpha2; // coefficient for stage 1 age squared } model { for (i in 1:N) { // Loop through each observation // Calculate the linear predictor based on age and stage real linear_pred = beta0 + beta1 * age[i] + beta2 * square(age[i]) + alpha0 * stage1[i] + alpha1 * stage1[i] * age[i] + alpha2 * stage1[i] * square(age[i]) + gamma0 * stage2[i] + gamma1 * stage2[i] * age[i] + gamma2 * stage2[i] * square(age[i]) + lambda0 *stage3[i] + lambda1 * stage3[i] * age[i] + lambda2 * stage3[i] * square(age[i]) ; target += linear_pred ; } } ", con ="model5.stan")
# Compile the Stan modelstan_model_5 <-stan_model(file ="model5.stan")
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘Apple clang version 14.0.3 (clang-1403.0.22.14.1)’
using SDK: ‘MacOSX13.3.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/include/" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c foo.c -o foo.o
In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/Core:19:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: 'cmath' file not found
#include <cmath>
^~~~~~~
1 error generated.
make: *** [foo.o] Error 1
# Perform MLE using the optimizing functionfit_mle_5 <-optimizing(stan_model_5, data = stan_data, hessian =TRUE)
Warning in .local(object, ...): non-zero return code in optimizing
# View the optimized parameter estimatesprint(fit_mle_5)
5b.Generate a partial effect plot of log relative hazard by age, stratified by stage. (Uncertainty interval not required for now.)
# Extract MLE estimates for parametersbeta0 <- fit_mle_5$par["beta0"]beta1 <- fit_mle_5$par["beta1"]beta2 <- fit_mle_5$par["beta2"]gamma0 <- fit_mle_5$par["gamma0"]gamma1 <- fit_mle_5$par["gamma1"]gamma2 <- fit_mle_5$par["gamma2"]lambda0 <- fit_mle_5$par["lambda0"]lambda1 <- fit_mle_5$par["lambda1"]lambda2 <- fit_mle_5$par["lambda2"]alpha0 <- fit_mle_5$par["alpha0"]alpha1 <- fit_mle_5$par["alpha1"]alpha2 <- fit_mle_5$par["alpha2"]# Define the age range for plottingage_range <-seq(20, 80, by =1) # Modify this as needed# Calculate log relative hazards for each stagelog_relative_hazard_stage1 <- beta0 + beta1 * age_range + beta2 * age_range^2+ alpha0 + alpha1 * age_range + alpha2 * age_range^2log_relative_hazard_stage2 <- beta0 + beta1 * age_range + beta2 * age_range^2+ gamma0 + gamma1 * age_range + gamma2 * age_range^2log_relative_hazard_stage3 <- beta0 + beta1 * age_range + beta2 * age_range^2+ lambda0 + lambda1 * age_range + lambda2 * age_range^2log_relative_hazard_stage4 <- beta0 + beta1 * age_range + beta2 * age_range^2# baseline stage#print(log_relative_hazard_stage1)#print(log_relative_hazard_stage2)#print(log_relative_hazard_stage3)#print(log_relative_hazard_stage4)# Create the plotplot(age_range, log_relative_hazard_stage1, type ="l", col ="blue", lty =1,ylim =range(log_relative_hazard_stage1, log_relative_hazard_stage2, log_relative_hazard_stage3, log_relative_hazard_stage4),xlab ="Age", ylab ="Log Relative Hazard", main ="Log Relative Hazard vs Age by Stage (Model 5)")lines(age_range, log_relative_hazard_stage2, col ="red", lty =2)lines(age_range, log_relative_hazard_stage3, col ="green", lty =3)lines(age_range, log_relative_hazard_stage4, col ="purple", lty =4)# Add a legendlegend("topleft", legend =c("Stage 1", "Stage 2", "Stage 3", "Stage 4 (Baseline)"),col =c("blue", "red", "green", "purple"), lty =1:4)
5c. Generate a partial effect plot of median survival by age, stratified by stage. (Uncertainty interval not required for now.) There is no reason to think the log relative hazard is linear in age.