# Generate example data with high negative skewness
set.seed(123)
# Parameters
n_patients <- 10000 # Total number of patients
# Skewed towards higher values
Ps <- plogis(rnorm(n_patients, mean = 2, sd = 1.5))
# Simulate survival outcomes based on Ps
survival_outcomes <- rbinom(n_patients,
size = 1,
prob = Ps
)
# Create data frame
data <- data.frame(Ps = Ps, survival = survival_outcomes) |>
dplyr::mutate(death = dplyr::if_else(survival == 1, 0, 1))
# Apply the nonlinear_bins function
results <- nonlinear_bins(data = data,
Ps_col = Ps,
outcome_col = survival,
divisor1 = 5,
divisor2 = 5,
threshold_1 = 0.9,
threshold_2 = 0.99)
# View results
results$intervals
results$bin_stats
# Example with grouping by a categorical variable
# Add random group variable
data$group <- sample(c("A", "B"), size = n_patients, replace = TRUE)
# Run the function using a single grouping variable
results_grouped <- nonlinear_bins(data,
Ps_col = Ps,
outcome_col = survival,
group_vars = "group"
)
# View grouped results
results_grouped$bin_stats
Run the code above in your browser using DataLab