### Initialize riskRegression Package Source: https://github.com/tagteam/riskregression/blob/master/simulation/k-fold-CV.org Load the riskRegression package and verify the installed version. ```R library(riskRegression) packageVersion("riskRegression") ``` -------------------------------- ### Install riskRegression Package Source: https://github.com/tagteam/riskregression/blob/master/README.md Install the riskRegression package from GitHub using the devtools library. Ensure devtools is installed first. ```r library(devtools) install_github("tagteam/riskRegression") ``` -------------------------------- ### Setup Simulation Data and Models Source: https://github.com/tagteam/riskregression/blob/master/simulation/k-fold-CV.org Simulate active surveillance data and fit logistic regression models for performance comparison. ```R set.seed(18) astrain <- simActiveSurveillance(278) astest <- simActiveSurveillance(208) astrain[,Y1:=1*(event==1 & time<=1)] astest[,Y1:=1*(event==1 & time<=1)] lrfit.ex <- glm(Y1~age+lpsaden+ppb5+lmax+ct1+diaggs,data=astrain,family="binomial") lrfit.inc <- glm(Y1~age+lpsaden+ppb5+lmax+ct1+diaggs+erg.status,data=astrain,family="binomial") ## Score(list("Exclusive ERG"=lrfit.ex,"Inclusive ERG"=lrfit.inc),data=astest,formula=Y1~1,se.fit=0L,metrics="brier",contrasts=FALSE) ``` -------------------------------- ### Simulate Example Data for Binary, Survival, and Competing Risks Outcomes Source: https://context7.com/tagteam/riskregression/llms.txt Generates simulated datasets for testing and examples with binary, survival, or competing risks outcomes. Creates 10 covariates (5 binary, 5 continuous) and response variables. Requires the 'riskRegression' library. ```r library(riskRegression) # Binary outcome set.seed(10) binary_data <- sampleData(n = 100, outcome = "binary") head(binary_data) # Contains: Y (binary outcome), X1-X5 (binary), X6-X10 (continuous) ``` ```r # Survival outcome survival_data <- sampleData(n = 100, outcome = "survival") head(survival_data) # Contains: time, event, X1-X5 (binary), X6-X10 (continuous) ``` ```r # Competing risks outcome cr_data <- sampleData(n = 100, outcome = "competing.risks") head(cr_data) # Contains: time, event (0=censored, 1=cause1, 2=cause2), X1-X10 ``` ```r # Custom formula for regression coefficients custom_data <- sampleData(n = 100, outcome = "binary", formula = ~ f(X1, 3) + f(X6, 0.5) + f(X8, -0.5), intercept = -1) ``` -------------------------------- ### Setup and Fit Logistic Regression Models Source: https://github.com/tagteam/riskregression/blob/master/simulation/k-fold-CV.html Initializes training and testing datasets and fits two logistic regression models for comparison. ```R set.seed(18) astrain <- simActiveSurveillance(278) astest <- simActiveSurveillance(208) astrain[,Y1:=1*(event==1 & time<=1)] astest[,Y1:=1*(event==1 & time<=1)] lrfit.ex <- glm(Y1~age+lpsaden+ppb5+lmax+ct1+diaggs,data=astrain,family="binomial") lrfit.inc <- glm(Y1~age+lpsaden+ppb5+lmax+ct1+diaggs+erg.status,data=astrain,family="binomial") ``` -------------------------------- ### Check Package Versions Source: https://github.com/tagteam/riskregression/blob/master/vignettes/IPA.org Prints the installed versions of the 'data.table', 'survival', 'riskRegression', and 'Publish' packages. This is useful for ensuring compatibility and reproducibility. ```R library(data.table) library(survival) library(riskRegression) library(Publish) cat("data.table:") packageVersion("data.table") cat("\nsurvival:") packageVersion("survival") cat("\nriskRegression:") packageVersion("riskRegression") cat("\nPublish:") packageVersion("Publish") ``` -------------------------------- ### Load and Prepare Data for Risk Regression Source: https://github.com/tagteam/riskregression/blob/master/vignettes/IPA.org Simulates learning and validation datasets for a prostate cancer study and defines a binary outcome variable for 1-year progression. Ensure the 'riskRegression' package is installed. ```R set.seed(18) astrain <- simActiveSurveillance(278) astest <- simActiveSurveillance(208) astrain[,Y1:=1*(event==1 & time<=1)] astest[,Y1:=1*(event==1 & time<=1)] ``` -------------------------------- ### Define and Synthesize Lava Model for Simulation Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Defines a latent variable model (lvm) using the `lava` package, specifies distributions and regression relationships, synthesizes the model, and then simulates data. This setup is for survival analysis with censored data. ```R library(lava) u <- lvm() # specify covariate distribution(u,~sex) <- binomial.lvm() #specify binary covariate distribution(u,~age) <- normal.lvm() #specify continuous covariate distribution(u,~logthick) <- normal.lvm() #note: remove paranthesis around log categorical(u,K=3) <- "invasion" #specify categorical covariate #include event time, so it knows it is the survival case u <-eventTime(u,time~min(time.cens=0,time.death=1,time.other=2), "status") #specify regression relationships lava::regression(u,logthick~sex+age+invasion) <- 1 lava::regression(u,age~invasion) <- 1 #specify relationships for censored variables (could also change them) lava::regression(u,time.other~logthick+sex+age+invasion) <- 1 lava::regression(u,time.death~logthick+sex+age+invasion) <- 1 lava::regression(u,time.cens~logthick+sex+age+invasion) <- 1 #finally synthesize object u_synt <- synthesize(u,data=Melanoma) #do a simulation study d <- sim(u_synt,10000) fit.rec.alt <- lm(log(thick)~sex+age+invasion,data=Melanoma) fit.rec.alt.s <- lm(logthick ~ sex + age + invasion, data= d) res <- as.data.frame(cbind(coef(fit.rec.alt),coef(fit.rec.alt.s))) colnames(res) <- c("Parameters of the model from original data", "Parameters of the model from simulated data") knitr::kable(res) ``` -------------------------------- ### Simulate and Fit CoxPH Model Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Simulates data from a model and fits a Cox proportional hazards model to both original and simulated data to compare parameter estimates. Ensure the 'survival' and 'knitr' packages are installed. ```R set.seed(15) d <- sim(ms,10000) fit <- coxph(Surv(time,eventBin)~log(thick)+sex+age+invasion,data=Melanoma) fit.s <- coxph(Surv(time,eventBin)~log(thick)+sex+age+invasion,data=d) res <- as.data.frame(cbind(coef(fit),coef(fit.s))) colnames(res) <- c("Parameters of the model from original data", "Parameters of the model from simulated data") knitr::kable(res) ``` -------------------------------- ### Load riskRegression package Source: https://github.com/tagteam/riskregression/blob/master/examples/demo.org Initializes the environment by loading necessary libraries and checking the package version. ```R library(riskRegression) library(survival) packageVersion("riskRegression") ``` -------------------------------- ### Load and Prepare Melanoma Data Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Initializes the environment and subsets the Melanoma dataset for survival analysis. ```R library(riskRegression) #> riskRegression version 2021.08.27 library(survival) library(lava) data("Melanoma") Melanoma <- subset(Melanoma, select = c("time","status","thick","sex","age","invasion")) knitr::kable(head(Melanoma)) ``` -------------------------------- ### Load riskRegression Package and Session Info Source: https://github.com/tagteam/riskregression/blob/master/simulation/k-fold-CV.html Loads the riskRegression package and displays session information, including attached packages and R version. This is useful for ensuring the correct environment for analysis. ```R library(riskRegression) sessionInfo() ``` -------------------------------- ### Simulate and Fit Alternative Recursive Model Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Simulates data from a recursive model and fits an alternative linear model to compare parameters with the original data. Requires the `lava` package. ```R set.seed(15) d <- sim(ms.rec,10000) fit.rec.alt <- lm(log(thick)~sex+age+invasion,data=Melanoma) fit.rec.alt.s <- lm(log(thick) ~ sex + age + invasion, data= d) res <- as.data.frame(cbind(coef(fit.rec.alt),coef(fit.rec.alt.s))) colnames(res) <- c("Parameters of the model (with formula: logthick ~ sex + age + invasion) from original data", "Parameters of the model (with formula: logthick ~ sex + age + invasion) from simulated data") knitr::kable(res) ``` -------------------------------- ### Simulate Data from Linear Model Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Simulate a dataset based on a fitted linear model. Use set.seed for reproducibility. The simulated data can then be used to fit a new model. ```R set.seed(15) d <- sim(m.cars,10000) fit <- lm(dist ~ speed, data = cars) fit.s <- lm(dist ~ speed, data = d) res <- as.data.frame(cbind(coef(fit),coef(fit.s))) colnames(res) <- c("Parameters of the model from original data", "Parameters of the model from simulated data") knitr::kable(res) ``` -------------------------------- ### Prepare Binary Event Variable Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Converts the status variable into a binary event indicator for survival modeling. ```R Melanoma$eventBin <- 1*(Melanoma$status!=0) ``` -------------------------------- ### Fit GLMnet and Predict Survival Risks Source: https://context7.com/tagteam/riskregression/llms.txt Demonstrates fitting a GLMnet model with unpenalized terms and predicting survival risks. ```r fit_mixed <- GLMnet(Surv(time, status == 1) ~ unpenalized(age) + invasion + epicel + ulcer + logthick, data = Melanoma) # Predict survival risks surv_risks <- predictRisk(fit_surv, newdata = Melanoma[1:10,], times = c(1000, 2000)) ``` -------------------------------- ### Linear Regression with synthesize Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Fit a simple linear regression model using the synthesize function. Ensure the 'cars' dataset is loaded. The output provides model summary statistics. ```R data(cars) m.cars <- synthesize(dist ~ speed, data=cars) summary(m.cars) ``` -------------------------------- ### Generate Random Numbers and Predict Risk Source: https://github.com/tagteam/riskregression/blob/master/examples/useScore.org Demonstrates generating sample data and comparing risk predictions between two random forest models. ```R set.seed(437) rnorm(10) library(randomForestSRC) library(riskRegression) d1 <- sampleData(400,outcome="binary") d2 <- sampleData(8,outcome="binary") d1[,Y1:=factor(Y)] d2[,Y1:=factor(Y)] f1 <- rfsrc(Y~X6+X8,data=d1,ntree=20,seed=8) f2 <- rfsrc(Y1~X6+X8,data=d1,ntree=20,seed=8) cbind(predictRisk(f1,newdata=d2), predictRisk(f2,newdata=d2)) ``` -------------------------------- ### Simulate and Visualize Risk Regression Models Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html This snippet simulates data for recursive and non-recursive models, fits Cox proportional hazards models, and generates a boxplot to compare the coefficients. ```R colnames(df) <- column.names #simulate for (sample.size in sample.sizes){ for (i in 1:n.sim){ d <- sim(object, sample.size) coeff <- coef(coxph(Surv(time,eventBin)~log(thick)+sex+age+invasion,data=d)) df[nrow(df)+1,] <- c(coeff,sample.size,"non recursive") d.rec <- sim(object.rec, sample.size) coeff.rec <- coef(coxph(Surv(time,eventBin)~log(thick)+sex+age+invasion,data=d.rec)) df[nrow(df)+1,] <- c(coeff.rec,sample.size,"recursive") } } #make variables of the correct type; otherwise ggplot doesn't understand for (c in names(df)[0:length(coef.compare)]){ df[[c]] <- as.numeric(df[[c]]) } df$sample.size <- as.factor(df$sample.size) df$recursive <- as.factor(df$recursive) ggplot(df, aes(x=sample.size, y=age, fill=recursive)) +geom_boxplot() + geom_hline(yintercept =coef.compare[["age"]]) #plots <- c() #for (c in names(df)[0:length(coef.compare)]){ # plots <- c(plots,ggplot(df, aes(x=sample.size, y=c, fill=recursive)) +geom_boxplot() + geom_hline(yintercept =coef.compare[[c]])) #} #do.call("grid.arrange", c(plots, ncol=length(coef.compare))) } make.boxplot.simulation(ms,ms.rec, Melanoma, c(100,200,500,1000),1000) ``` -------------------------------- ### Methodological References in BibTeX Source: https://github.com/tagteam/riskregression/blob/master/README.org A collection of BibTeX entries documenting the statistical methods and research papers underlying the riskRegression package. ```LaTeX @article{gerds2006consistent, title = {Consistent Estimation of the Expected {B}rier Score in General Survival Models with Right-Censored Event Times}, author = {Gerds, T.A. and Schumacher, M.}, journal = {Biometrical Journal}, volume = 48, number = 6, pages = {1029--1040}, year = 2006, publisher = {Wiley Online Library} } @article{gerds2007efron, title = {Efron-Type Measures of Prediction Error for Survival Analysis}, author = {Gerds, T.A. and Schumacher, M.}, journal = {Biometrics}, volume = 63, number = 4, pages = {1283--1287}, year = 2007, publisher = {Wiley Online Library} } @article{gerds2008performance, title = {The performance of risk prediction models}, author = {Gerds, T.A. and Cai, T. and Schumacher, M.}, journal = {Biometrical Journal}, volume = 50, number = 4, pages = {457--479}, year = 2008, publisher = {Wiley Online Library} } @Article{mogensen2012pec, title = {Evaluating random forests for survival analysis using prediction error curves}, author = {Mogensen, U B and Ishwaran, H. and Gerds, T A}, journal = {Journal of Statistical Software}, year = 2012, volume = 50, number = 11 } @article{Blanche2013statmed, title = "{Estimating and comparing time-dependent areas under receiver operating characteristic curves for censored event times with competing risks}", author = {Blanche, P. and Dartigues, J-F and Jacqmin-Gadda, H.}, journal = {Statistics in Medicine}, volume = 32, number = 30, pages = {5381--5397}, year = 2013 } @article{blanche2015, title = {Quantifying and comparing dynamic predictive accuracy of joint models for longitudinal marker and time-to-event in presence of censoring and competing risks}, author = {Blanche, Paul and Proust-Lima, C{'e}cile and Loub{'e}re, Lucie and Berr, Claudine and Dartigues, Jean-Fran{'c}ois and Jacqmin-Gadda, H{'e}l{'e}ne}, journal = {Biometrics}, volume = 71, number = 1, pages = {102--113}, year = 2015, publisher = {Wiley Online Library} } @article{ozenne2017, title = {riskRegression: Predicting the Risk of an Event using Cox Regression Modelss}, author = {Ozenne, Brice and Sørensen, Anne Lyngholm and Scheike, Thomas and Torp-Pedersen, Christian and Gerds, Thomas Alexander}, journal = {The R Journal}, volume = 9, number = 2, pages = {440--460}, year = 2017 } ``` -------------------------------- ### Publish Head of Test Data (Org Mode) Source: https://github.com/tagteam/riskregression/blob/master/vignettes/IPA.html Publishes the first few rows of the 'astest' data frame, excluding specific columns, in Org mode format with specified digit precision. Useful for displaying sample predictions. ```R publish(head(astest[,-c(8,9)]),digits=1,org=TRUE) ``` -------------------------------- ### Evaluate Models with Bootstrap Cross-Validation Source: https://github.com/tagteam/riskregression/blob/master/simulation/k-fold-CV.org Perform bootstrap cross-validation to evaluate model performance using Brier score and IPA. ```R X1 <- Score(list("Exclusive ERG"=lrfit.ex,"Inclusive ERG"=lrfit.inc),data=astest, formula=Y1~1,summary="ipa",se.fit=0L,metrics="brier",contrasts=FALSE, split.method = "bootcv", B=100) ``` ```R X1 ``` -------------------------------- ### Simulate and Fit Competing Risk Model Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Simulates data using a competing risk model and fits a Cox proportional hazards model to both original and simulated data. Requires the `survival` package. ```R set.seed(15) d <- sim(ms.comp,10000) fit.comp <- coxph(Surv(time,status==2)~log(thick)+sex+age+invasion,data=Melanoma) fit.comp.s <- coxph(Surv(time,status==2)~log(thick)+sex+age+invasion,data=d) ``` -------------------------------- ### Fit Penalized Regression Models (LASSO, Ridge, Elastic Net) with Formula Interface Source: https://context7.com/tagteam/riskregression/llms.txt Fits elastic net (LASSO/ridge) regression models using a formula interface, with support for survival outcomes, penalty weights, and restricted cubic splines. Requires 'riskRegression', 'glmnet', and 'survival' libraries. ```r library(riskRegression) library(glmnet) library(survival) # Binary outcome with penalization set.seed(42) bin_data <- sampleData(200, outcome = "binary") bin_test <- sampleData(50, outcome = "binary") fit_lasso <- GLMnet(Y ~ X1 + X2 + X3 + X6 + X7 + X8 + X9 + X10, data = bin_data, alpha = 1) # LASSO # Predict risks <- predictRisk(fit_lasso, newdata = bin_test) head(risks) ``` ```r # Survival outcome with mixed penalties data(Melanoma, package = "riskRegression") fit_surv <- GLMnet(Surv(time, status == 1) ~ pen(invasion, 4) + epicel + ulcer + logthick + pen(sex, 0) + age, data = Melanoma, alpha = 0.5) # Elastic net ``` -------------------------------- ### Plot Survival Calibration Curves with Quantile Groups Source: https://context7.com/tagteam/riskregression/llms.txt Visualize survival calibration curves using quantile-based groups. This method divides the data into quantiles to assess calibration. Requires 'riskRegression', 'survival', and 'prodlim' libraries. ```r library(riskRegression) library(survival) library(prodlim) # Survival outcome calibration surv_data <- sampleData(200, outcome = "survival") cox1 <- coxph(Surv(time, event) ~ X1 + X5 + X7, data = surv_data, x = TRUE) cox2 <- coxph(Surv(time, event) ~ X1 + X3 + X6 + X7, data = surv_data, x = TRUE) xs <- Score(list(Cox1 = cox1, Cox2 = cox2), Surv(time, event) ~ 1, data = surv_data, times = 10, plots = "cal") # With quantile-based groups plotCalibration(xs, method = "quantile", q = 10) ``` -------------------------------- ### Fit Fine-Gray Regression Model with riskRegression Source: https://context7.com/tagteam/riskregression/llms.txt Illustrates fitting Fine-Gray subdistribution hazard models for competing risks analysis using the FGR function. This approach directly models the cumulative incidence function for a specific cause of interest. Includes model summary and prediction. ```R library(riskRegression) library(prodlim) # Generate competing risks data set.seed(123) train <- sampleData(300, outcome = "competing.risks") test <- sampleData(50, outcome = "competing.risks") # Fit Fine-Gray model for cause 1 fgr_fit <- FGR(Hist(time, event) ~ X1 + X3 + X6 + X8, data = train, cause = 1) # View model summary summary(fgr_fit) # Predict cumulative incidence at specific times pred <- predict(fgr_fit, newdata = test, times = c(5, 10, 15)) head(pred) ``` -------------------------------- ### Simulate and Compare Model Coefficients Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Simulates data from a recursive model and compares the resulting Cox proportional hazards model coefficients with those from the original data. ```R set.seed(15) d <- sim(ms.rec,10000) fit.rec.s <- coxph(Surv(time,eventBin)~log(thick)+sex+age+invasion,data=d) res <- as.data.frame(cbind(coef(fit),coef(fit.rec.s),coef(fit.s))) colnames(res) <- c("Parameters of the model from original data", "Parameters of the model from simulated data of the recursive model","Parameters of the model from simulated of the non-recursive model") knitr::kable(res) ``` -------------------------------- ### Score Logistic Regression Models for IPA and Brier Source: https://github.com/tagteam/riskregression/blob/master/vignettes/IPA.org Calculates IPA and Brier metrics for logistic regression models. Ensure the 'Score' function and necessary models (lrfit.ex, lrfit.inc) are available. Results are presented in percentages. ```R X1 <- Score(list("Exclusive ERG"=lrfit.ex,"Inclusive ERG"=lrfit.inc),data=astest, formula=Y1~1,summary="ipa",se.fit=1L,metrics="brier",contrasts=FALSE) X1 ``` -------------------------------- ### Plot Calibration Curves with Bars for Binary Outcomes Source: https://context7.com/tagteam/riskregression/llms.txt Visualize calibration curves with bars for a specific binary outcome model. This provides a segmented view of calibration performance. Requires 'riskRegression', 'survival', and 'prodlim' libraries. ```r library(riskRegression) library(survival) library(prodlim) # Binary outcome calibration set.seed(10) bin_data <- sampleData(300, outcome = "binary") lr1 <- glm(Y ~ X1 + X5 + X7, data = bin_data, family = binomial) lr2 <- glm(Y ~ X1 + X3 + X6 + X7, data = bin_data, family = binomial) xb <- Score(list(model1 = lr1, model2 = lr2), Y ~ 1, data = bin_data, plots = "cal") # Calibration with bars (for single model) plotCalibration(xb, bars = TRUE, models = "model1") ``` -------------------------------- ### Load Data for Logistic Regression Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Read a CSV file from a URL into a data frame for logistic regression analysis. The 'head' function displays the first few rows. ```R UCLA.dat <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv") knitr::kable(head(UCLA.dat)) ``` -------------------------------- ### Simulate Data from Logistic Model Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Simulate data from a fitted logistic regression model. Use set.seed for reproducibility. The simulated data is then used to fit a new logistic regression model. ```R set.seed(15) d <- sim(UCLA.syn,10000) fit <- glm(admit ~ gre + gpa + factor(rank), data = UCLA.dat) fit.s <- glm(admit ~ gre + gpa + rank, data = d) res <- as.data.frame(cbind(coef(fit),coef(fit.s))) colnames(res) <- c("Parameters of the model from original data", "Parameters of the model from simulated data") knitr::kable(res) ``` -------------------------------- ### Evaluate risk models with Score Source: https://github.com/tagteam/riskregression/blob/master/examples/useScore.org Compares multiple risk prediction models by calculating AUC and Brier scores on a test dataset. Requires the riskRegression, randomForestSRC, and survival packages. ```R library(riskRegression) library(randomForestSRC) library(survival) set.seed(437) d1 <- sampleData(400) d2 <- sampleData(400) f1 <- CSC(Hist(time,event)~X1+X6+X8,data=d1) ## sum(predictRisk(f1,newdata=d2,cause=1,times=4)>1) ## max(predict(f1,newdata=d2,cause=1,times=4,product.limit=TRUE)$absRisk) f2 <- rfsrc(Surv(time,event)~X6+X8,data=d1,ntree=20) x <- Score(list(f1,f2),data=d2,formula=Hist(time,event)~1,times=4,cause=1) x$AUC$score x$AUC$contrast x$Brier$score x$Brier$contrast summary(x) ``` -------------------------------- ### Cross-Validation with Bootstrap for Binary Outcomes Source: https://context7.com/tagteam/riskregression/llms.txt Perform cross-validation using bootstrap for binary outcome models. This helps in assessing the generalization performance of the models. Requires the 'riskRegression', 'survival', and 'prodlim' libraries. ```r library(riskRegression) library(survival) library(prodlim) # Cross-validation with bootstrap score_cv <- Score(list("Simple" = lr1, "Complex" = lr2), formula = Y ~ 1, data = bin_data, split.method = "bootcv", B = 100, metrics = "auc") print(score_cv) ``` -------------------------------- ### Evaluate Models with K-Fold Cross-Validation Source: https://github.com/tagteam/riskregression/blob/master/simulation/k-fold-CV.org Perform 5-fold cross-validation to evaluate model performance. ```R X1 <- Score(list("Exclusive ERG"=lrfit.ex,"Inclusive ERG"=lrfit.inc),data=astest, formula=Y1~1,summary="ipa",se.fit=0L,metrics="brier",contrasts=FALSE, split.method = "cv5", B=100) ``` ```R X1 ``` -------------------------------- ### Synthesize Survival Model Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Creates a lava object to synthesize survival outcomes based on the provided formula and dataset. ```R ms <- synthesize(Surv(time,eventBin)~log(thick)+sex+age+invasion,data=Melanoma) summary(ms) ``` -------------------------------- ### Synthesize a Recursive Event History Model Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Creates a recursive model using the synthesize function with the recursive parameter set to TRUE. ```R ms.rec <- synthesize(Surv(time,eventBin)~log(thick)+sex+age+invasion,data=Melanoma,recursive = TRUE) ms.rec ``` -------------------------------- ### Plot Calibration Curves for Binary Outcomes Source: https://context7.com/tagteam/riskregression/llms.txt Visualize calibration curves for binary outcome models, showing agreement between predicted probabilities and observed outcomes. Supports multiple smoothing methods. Requires 'riskRegression', 'survival', and 'prodlim' libraries. ```r library(riskRegression) library(survival) library(prodlim) # Binary outcome calibration set.seed(10) bin_data <- sampleData(300, outcome = "binary") lr1 <- glm(Y ~ X1 + X5 + X7, data = bin_data, family = binomial) lr2 <- glm(Y ~ X1 + X3 + X6 + X7, data = bin_data, family = binomial) xb <- Score(list(model1 = lr1, model2 = lr2), Y ~ 1, data = bin_data, plots = "cal") # Calibration curve plot plotCalibration(xb) ``` -------------------------------- ### Compute IPA for Model Source: https://github.com/tagteam/riskregression/blob/master/vignettes/IPA.org Computes the IPA for the full model on the validation set. ```R IPA(cscfit.inc,newdata=astest,times=3) ``` -------------------------------- ### Fit Cause-Specific Cox Model with riskRegression Source: https://context7.com/tagteam/riskregression/llms.txt Demonstrates fitting cause-specific Cox proportional hazards models for competing risks data using the CSC function. It shows how to fit models with a single formula or different formulas per cause, and how to predict absolute risk. ```R library(riskRegression) library(survival) library(prodlim) # Generate competing risks data set.seed(42) train <- sampleData(200, outcome = "competing.risks") test <- sampleData(50, outcome = "competing.risks") # Fit cause-specific Cox model for both causes csc_fit <- CSC(Hist(time, event) ~ X1 + X3 + X6 + X8, data = train, cause = 1) # Primary cause of interest # Fit with different formulas per cause csc_fit2 <- CSC(list(Hist(time, event) ~ X1 + X3 + X6, # Cause 1 model Hist(time, event) ~ X2 + X8 + X9), # Cause 2 model data = train) # Predict absolute risk at specific times pred <- predict(csc_fit, newdata = test, times = c(5, 10, 15), cause = 1) print(pred) # Predict with confidence intervals pred_ci <- predict(csc_fit, newdata = test, times = c(5, 10), cause = 1, se = TRUE, band = TRUE) print(pred_ci) ``` -------------------------------- ### Evaluate Survival Outcome Models Source: https://github.com/tagteam/riskregression/blob/master/examples/useScore.org Compares Cox proportional hazards and random forest models for survival outcomes at a specific time point. ```R library(riskRegression) library(survival) library(randomForestSRC) set.seed(437) d1 <- sampleData(400,outcome="survival") d2 <- sampleData(400,outcome="survival") f1 <- coxph(Surv(time,event)~X6+X8,data=d1,x=1,y=1) f2 <- rfsrc(Surv(time,event)~X6+X8,data=d1,ntree=20) x <- Score(list(f1,f2),data=d2,formula=Hist(time,event)~1,times=4,conservative=1L) x$AUC$score x$AUC$contrast x$Brier$score x$Brier$contrast summary(x) ``` -------------------------------- ### Compare Model Coefficients Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Extracts coefficients from fitted Cox models and presents them in a data frame for comparison between original and simulated data. Uses `knitr::kable` for table formatting. ```R res <- as.data.frame(cbind(coef(fit.comp),coef(fit.comp.s))) colnames(res) <- c("Parameters of the model from original data", "Parameters of the model from simulated data") knitr::kable(res) ``` -------------------------------- ### Evaluate censored survival models Source: https://github.com/tagteam/riskregression/blob/master/examples/demo.org Compares Cox proportional hazards models using the Score function with survival data. ```R set.seed(11) train=sampleData(n=474,outcome="survival") test=sampleData(n=736,outcome="survival") f1=coxph(Surv(time,event)~X1+X2+X8+X9,data=train,x=TRUE,y=TRUE) f2=coxph(Surv(time,event)~X3+X4+X7+X10,data=train,x=TRUE,y=TRUE) x=Score(list(f1=f1,f2=f2),data=test,formula=Surv(time,event)~X1+X2+X3+X4+X8+X9+X10,times=5) summary(x)$score x1=Score(list(f1=f1,f2=f2),data=test,formula=Surv(time,event)~1,times=5) summary(x1)$score ``` -------------------------------- ### Calculate and Publish Risk Regression Results Source: https://github.com/tagteam/riskregression/blob/master/vignettes/IPA.org This snippet calculates the 'risk.inc' column using a logistic regression model and then publishes a subset of the results. ```R astest[,risk.inc:=100*predictRisk(lrfit.inc,newdata=astest)] publish(head(astest[,-c(8,9)]),digits=1,org=TRUE) ``` -------------------------------- ### Compute Average Treatment Effects (ATE) with G-formula, IPTW, or AIPTW Source: https://context7.com/tagteam/riskregression/llms.txt Estimates average treatment effects (risk differences and ratios) for survival and competing risks data using G-formula, IPTW, or augmented IPTW (doubly robust) estimators. Requires 'riskRegression', 'survival', and 'prodlim' libraries. ```r library(riskRegression) library(survival) library(prodlim) # Generate survival data with treatment set.seed(10) n <- 200 dt <- sampleData(n, outcome = "survival") dt$time <- round(dt$time, 1) dt$X1 <- factor(rbinom(n, prob = 0.5, size = 1), labels = c("Control", "Treatment")) # G-formula estimator (outcome model only) fit_outcome <- coxph(Surv(time, event) ~ X1 + X6 + X8, data = dt, x = TRUE) ate_gformula <- ate(event = fit_outcome, treatment = "X1", data = dt, times = c(5, 10, 15), se = TRUE) print(ate_gformula) ``` ```r # AIPTW (doubly robust) estimator fit_treatment <- glm(X1 == "Treatment" ~ X6 + X8, data = dt, family = binomial) fit_censoring <- coxph(Surv(time, event == 0) ~ X1 + X6, data = dt, x = TRUE) ate_aiptw <- ate(event = fit_outcome, treatment = fit_treatment, censor = fit_censoring, data = dt, times = c(5, 10), estimator = "AIPTW", se = TRUE) print(ate_aiptw) ``` ```r # Competing risks with CSC model dt_cr <- sampleData(n, outcome = "competing.risks") dt_cr$X1 <- factor(rbinom(n, prob = 0.5, size = 1), labels = c("A", "B")) fit_csc <- CSC(Hist(time, event) ~ X1 + X6 + X8, data = dt_cr) ate_cr <- ate(event = fit_csc, treatment = "X1", data = dt_cr, times = c(5, 10), cause = 1, se = TRUE) print(ate_cr) ``` -------------------------------- ### Competing Risk Model Synthesis in R Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Use this function to synthesize a competing risk model. It requires a survival formula with time and status, and a data frame. The summary provides detailed model structure and parameters. ```r ms.comp <- synthesize(Surv(time,status)~log(thick)+sex+age+invasion,data=Melanoma) summary(ms.comp) ``` -------------------------------- ### Predict Survival Outcomes with predictCox Source: https://context7.com/tagteam/riskregression/llms.txt Demonstrates using predictCox to compute survival probabilities, cumulative hazards, and hazard rates from fitted Cox regression models. Supports standard errors and influence functions, and requires specifying the prediction type. ```R library(riskRegression) library(survival) # Fit Cox model set.seed(50) train <- sampleData(200, outcome = "survival") test <- sampleData(20, outcome = "survival") cox_fit <- coxph(Surv(time, event) ~ X1 + X6 + X8, data = train, x = TRUE) # Predict survival probabilities pred_surv <- predictCox(cox_fit, newdata = test, times = c(5, 10, 15), type = "survival") head(pred_surv$survival) # Predict cumulative hazard pred_cumhaz <- predictCox(cox_fit, newdata = test, times = c(5, 10, 15), type = "cumhazard") head(pred_cumhaz$cumhazard) ``` -------------------------------- ### Extract Risk Predictions with predictRisk Source: https://context7.com/tagteam/riskregression/llms.txt Shows how to use the generic predictRisk function to extract predicted event probabilities from various fitted models (logistic, Cox, CSC) for binary, survival, and competing risks outcomes. Requires specifying outcome type and optionally prediction times and cause. ```R library(riskRegression) library(survival) library(prodlim) # Binary outcome example set.seed(7) bin_data <- sampleData(100, outcome = "binary") bin_test <- sampleData(30, outcome = "binary") # Fit logistic regression lr_fit <- glm(Y ~ X1 + X2 + X6 + X8, data = bin_data, family = binomial) # Get predicted risks for binary outcome risks_binary <- predictRisk(lr_fit, newdata = bin_test) head(risks_binary) # Survival outcome example surv_data <- sampleData(100, outcome = "survival") surv_test <- sampleData(30, outcome = "survival") # Fit Cox model cox_fit <- coxph(Surv(time, event) ~ X1 + X6 + X8, data = surv_data, x = TRUE) # Get predicted risks at specific times risks_surv <- predictRisk(cox_fit, newdata = surv_test, times = c(5, 10, 15)) head(risks_surv) # Competing risks example cr_data <- sampleData(200, outcome = "competing.risks") cr_test <- sampleData(30, outcome = "competing.risks") csc_fit <- CSC(Hist(time, event) ~ X1 + X6 + X8, data = cr_data) risks_cr <- predictRisk(csc_fit, newdata = cr_test, times = c(5, 10), cause = 1) head(risks_cr) ``` -------------------------------- ### Logistic Regression with synthesize Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Fit a logistic regression model using the synthesize function with multiple predictor variables. The 'admit' variable is modeled based on 'gre', 'gpa', and 'rank'. ```R UCLA.syn <- synthesize(admit ~ gre + gpa + rank, data = UCLA.dat) ``` -------------------------------- ### Predict and Publish Risk Scores in R Source: https://github.com/tagteam/riskregression/blob/master/vignettes/IPA.org Calculates and publishes 3-year risk scores using Cox regression models. Requires pre-defined coxfit.ex and coxfit.inc models and astest dataset. ```R astest[,risk.ex:=100*predictRisk(coxfit.ex,newdata=astest,times=3)] astest[,risk.inc:=100*predictRisk(coxfit.inc,newdata=astest,times=3)] publish(head(astest[,-c(8,9)]),digits=1,org=TRUE) ``` -------------------------------- ### Define Simulation Boxplot Function Source: https://github.com/tagteam/riskregression/blob/master/simulation/synthesize.html Initializes a function to compare model coefficients across different sample sizes and recursion settings. ```R library(ggplot2) #> #> Attaching package: 'ggplot2' #> The following object is masked from 'package:lava': #> #> vars #library(gridExtra) make.boxplot.simulation <- function(object, object.rec, data, sample.sizes, n.sim){ #Copare to model from original data coef.compare <- coef(coxph(Surv(time,eventBin)~log(thick)+sex+age+invasion,data=data)) # make dataframe with columnnames of the form: parameters, sample.size, recursion df <- data.frame(matrix(ncol = length(coef.compare)+2, nrow = 0)) column.names <- c(names(coef.compare),"sample.size", "recursive") ``` -------------------------------- ### Plot Calibration Curves for Survival Outcomes Source: https://context7.com/tagteam/riskregression/llms.txt Visualize calibration curves for survival outcome models at a specific time point. Handles censored data using local Kaplan-Meier/Aalen-Johansen estimators. Requires 'riskRegression', 'survival', and 'prodlim' libraries. ```r library(riskRegression) library(survival) library(prodlim) # Survival outcome calibration surv_data <- sampleData(200, outcome = "survival") cox1 <- coxph(Surv(time, event) ~ X1 + X5 + X7, data = surv_data, x = TRUE) cox2 <- coxph(Surv(time, event) ~ X1 + X3 + X6 + X7, data = surv_data, x = TRUE) xs <- Score(list(Cox1 = cox1, Cox2 = cox2), Surv(time, event) ~ 1, data = surv_data, times = 10, plots = "cal") plotCalibration(xs) ``` -------------------------------- ### Evaluate Competing Risks Prediction Performance Source: https://context7.com/tagteam/riskregression/llms.txt Evaluate competing risks models using AUC at specified time points. Requires the 'riskRegression', 'survival', and 'prodlim' libraries. Set a seed for reproducibility. ```r library(riskRegression) library(survival) library(prodlim) # Competing risks evaluation cr_data <- sampleData(300, outcome = "competing.risks") csc1 <- CSC(Hist(time, event) ~ X1 + X6, data = cr_data) csc2 <- CSC(Hist(time, event) ~ X1 + X6 + X8 + X9, data = cr_data) score_cr <- Score(list("CSC1" = csc1, "CSC2" = csc2), formula = Hist(time, event) ~ 1, data = cr_data, cause = 1, times = c(5, 10), metrics = "auc") print(score_cr) ``` -------------------------------- ### Publish Model Results (Org Mode) Source: https://github.com/tagteam/riskregression/blob/master/vignettes/IPA.html Publishes the results of the fitted logistic regression model 'lrfit.inc' in Org mode format, including odds ratios, confidence intervals, and p-values. Useful for generating reports. ```R publish(lrfit.inc,org=TRUE) ``` -------------------------------- ### Calculate IPA (R-squared) for Binary, Survival, and Competing Risks Outcomes Source: https://context7.com/tagteam/riskregression/llms.txt Computes the Index of Prediction Accuracy (IPA), also known as R-squared, by comparing the Brier score of a model to a null model. Supports binary, survival, and competing risks outcomes. Requires loading 'riskRegression', 'survival', and 'prodlim' libraries. ```r library(riskRegression) library(survival) library(prodlim) # Binary outcome set.seed(18) bin_learn <- sampleData(200, outcome = "binary") bin_val <- sampleData(100, outcome = "binary") lr1 <- glm(Y ~ X1 + X2 + X7 + X9, data = bin_learn, family = binomial) # IPA on training data ipa_train <- IPA(lr1) print(ipa_train) # IPA on validation data ipa_val <- IPA(lr1, newdata = bin_val) print(ipa_val) ``` ```r # Survival outcome data(pbc, package = "survival") pbc <- na.omit(pbc) train_idx <- sample(1:nrow(pbc), size = 0.632 * nrow(pbc)) pbc_train <- pbc[train_idx, ] pbc_test <- pbc[-train_idx, ] cox1 <- coxph(Surv(time, status != 0) ~ age + sex + log(bili) + log(albumin), data = pbc_train, x = TRUE) # IPA at specific time ipa_surv <- IPA(cox1, formula = Surv(time, status != 0) ~ 1, newdata = pbc_test, times = 1000) print(ipa_surv) ``` ```r # Competing risks outcome data(Melanoma, package = "riskRegression") csc_fit <- CSC(Hist(time, status) ~ age + sex + epicel + ulcer, data = Melanoma) ipa_cr <- IPA(csc_fit, times = 1000, cause = 1) print(ipa_cr) ``` -------------------------------- ### Evaluate Binary Outcome Models Source: https://github.com/tagteam/riskregression/blob/master/examples/useScore.org Compares GLM and random forest models for binary outcomes using the Score function to calculate AUC and Brier scores. ```R library(riskRegression) library(randomForestSRC) set.seed(437) d1 <- sampleData(400,outcome="binary") d2 <- sampleData(400,outcome="binary") d1[,Y:=factor(Y,levels=c("0","1"),labels=c("0","1"))] f1 <- glm(Y~X6+X8,data=d1,family="binomial") f2 <- rfsrc(Y~X6+X8,data=d1,ntree=20) x <- Score(list(f1,f2),data=d2,formula=Y~1) x$AUC$score x$AUC$contrast x$Brier$score x$Brier$contrast summary(x) ``` -------------------------------- ### Fit Logistic Regression Models for Binary Outcome Source: https://github.com/tagteam/riskregression/blob/master/vignettes/IPA.org Fits two logistic regression models to predict 1-year progression status using the simulated training data. One model includes the 'erg.status' biomarker, while the other excludes it. The 'Publish' package is used for displaying model results. ```R lrfit.ex <- glm(Y1~age+lpsaden+ppb5+lmax+ct1+diaggs,data=astrain,family="binomial") lrfit.inc <- glm(Y1~age+lpsaden+ppb5+lmax+ct1+diaggs+erg.status,data=astrain,family="binomial") publish(lrfit.inc,org=TRUE) ```