<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>R Works</title>
<link>https://rworks.dev/</link>
<atom:link href="https://rworks.dev/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.9.37</generator>
<lastBuildDate>Tue, 28 Apr 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>March 2026 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/march-2026-top-40-new-cran-packages/</link>
  <description><![CDATA[ 





<p>Three hundred seventy-one of the new packages submitted to CRAN in March were still there in mid-April. Here are my Top 40 picks in fifteen categories: Causal Inference, Computational Methods, Data, Ecology, Health Technology Assessment, Mathematics, Medical Statistics, Probability Programming, Public Health, Risk Analysis, Statistics, Time Series, Utilities, and Visualization.</p>
<div class="columns">
<div class="column" style="width:45%;">
<section id="causal-inference" class="level3">
<h3 class="anchored" data-anchor-id="causal-inference">Causal Inference</h3>
<p><a href="https://cran.r-project.org/package=causaldef">causaldef</a> v0.2.0: Implements Le Cam deficiency theory for causal inference, as described in <a href="https://zenodo.org/records/18367347">Akdemir (2026)</a>. Provides theorem-backed bounds together with computable proxy diagnostics for information loss from confounding, selection bias, and distributional shift. Supports continuous, binary, count, survival, and competing risks outcomes. Key features include propensity-score total-variation deficiency proxies, negative control diagnostics, policy regret bounds, and sensitivity analysis via confounding frontiers. There are eleven vignettes including <a href="https://cran.r-project.org/web/packages/causaldef/index.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/causaldef/vignettes/survival_analysis.html">Survival Analysis</a>.</p>
<p><a href="https://cran.r-project.org/package=CausalSpline">CausalSpline</a> v0.1.0: Estimates nonlinear causal dose-response functions for continuous treatments using spline-based methods under standard causal assumptions (unconfoundedness / ignorability). Implements three identification strategies: inverse probability weighting via the generalized propensity score, G-computation (outcome regression), and a doubly-robust combination. Supports both natural cubic splines and B-splines and provides pointwise confidence bands and fragility diagnostics. See <a href="https://onlinelibrary.wiley.com/doi/10.1111/j.1468-0262.2004.00481.x">Hirano and Imbens (2004)</a> for background and the <a href="https://cran.r-project.org/web/packages/CausalSpline/vignettes/introduction.html">vignette</a> for examples.</p>
<p><a href="CausalSpline.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/CausalSpline.png" class="img-fluid" alt="Example of causal dose response curve"></a></p>
<p><a href="https://cran.r-project.org/package=flexCausal">flexCausal</a> v0.1.0: Provides doubly robust one-step and targeted maximum likelihood estimators for average causal effects in acyclic directed mixed graphs with unmeasured variables. Automatically determines whether the treatment effect is identified via backdoor adjustment or the extended front-door functional, and dispatches to the appropriate estimator. Supports incorporation of machine learning algorithms via <code>SuperLearner</code> and cross-fitting for nuisance estimation. Methods are described in <a href="https://arxiv.org/abs/2409.03962">Guo and Nabi (2024)</a>. See the <a href="https://cran.r-project.org/web/packages/flexCausal/vignettes/introduction.html">vignette</a>.</p>
<p><a href="flexcausal.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/flexcausal.png" class="img-fluid" alt="Schematic of workflow"></a></p>
</section>
<section id="computational-methods" class="level3">
<h3 class="anchored" data-anchor-id="computational-methods">Computational Methods</h3>
<p><a href="https://cran.r-project.org/package=opensimplex2">opensimplex2</a> v0.0.3: Provides a function to generate 2, 3, or 4-dimensional gradient noise. The noise function is comparable to classic Perlin noise, but with less directional artefacts and lower computational overhead. It can have applications in procedural generation or flow field simulations. See the <a href="https://cran.r-project.org/web/packages/opensimplex2/vignettes/opensimplex-on-fire.html">vignette</a>.</p>
<p><a href="opensimplex2.gif" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/opensimplex2.gif" class="img-fluid" alt="Simulation of flames"></a></p>
<p><a href="https://cran.r-project.org/package=ROOT">ROOT</a> v0.1.1: Implements a general framework for globally optimizing user-specified objective functionals over interpretable binary weight functions represented as sparse decision trees, called ROOT (Rashomon Set of Optimal Trees). It searches over candidate trees to construct a Rashomon set of near-optimal solutions and derives a summary tree highlighting stable patterns in the optimized weights. ROOT includes a built-in generalizability mode for identifying subgroups in trial settings for transportability analyses <a href="https://www.tandfonline.com/doi/full/10.1080/01621459.2025.2495319">Parikh et al.&nbsp;(2025)</a>. There are three vignettes including <a href="https://cran.r-project.org/web/packages/ROOT/vignettes/quickstart.html">Quickstart Guide</a> and <a href="https://cran.r-project.org/web/packages/ROOT/vignettes/optimization_path_example.html">Optimization Path Example</a>.</p>
<p><a href="ROOT.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/ROOT.png" class="img-fluid" alt="Plot of tree from the Rashomon set"></a></p>
</section>
<section id="data" class="level3">
<h3 class="anchored" data-anchor-id="data">Data</h3>
<p><a href="https://cran.r-project.org/package=edgarfundamentals">edgarfundamentals</a> v0.1.2: Provides a simple, ticker-based interface for retrieving fundamental financial data from the <a href="https://www.sec.gov/edgar/sec-api-documentation">United States Securities and Exchange Commission’s EDGAR XBRL API</a>. Functions return key financial ratios including earnings per share, return on equity, return on assets, debt-to-equity, current ratio, gross margin, operating margin, net margin, price-to-earnings, price-to-book, and dividend yield for any publicly traded U.S. company. Data is sourced directly from company 10-K annual filings, requiring no API key or paid subscription. Designed for use in quantitative finance courses and research workflows. See the <a href="https://cran.r-project.org/web/packages/edgarfundamentals/vignettes/edgarfundamentals.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=fred">fred</a> v0.2.0: Provides tidy access to economic data from the <a href="https://fred.stlouisfed.org/docs/api/fred/">Federal Reserve Economic Data (FRED) API</a> which is maintained by the Federal Reserve Bank of St.&nbsp;Louis and contains over 800,000 time series from 118 sources covering GDP, employment, inflation, interest rates, trade, and more. Functions include fetching series observations, search for series, browse categories, releases, and tags, and retrieve series metadata. Real-time and vintage helpers built on <a href="https://alfred.stlouisfed.org/">ALFRED</a> return a series as it appeared on a given date, the first-release version, every revision, or a panel of selected vintages. Look <a href="https://github.com/charlescoverdale/fred">here</a> for an introduction.</p>
<p><a href="https://cran.r-project.org/package=readnoaa">readnoaa</a> v0.1.1: Provides tidy access to climate and weather data from the National Oceanic and Atmospheric Administration (<strong>NOAA</strong>) via the <a href="https://www.ncei.noaa.gov/access/services/data/v1">National Centers for Environmental Information (NCEI) Data Service API</a>. Covers daily weather observations, monthly and annual summaries, and 30-year climate normals from over 100,000 stations across 180 countries. No API key is required. Dedicated functions handle the most common datasets, while a generic fetcher provides access to all NCEI datasets. See <a href="https://cran.r-project.org/web/packages/readnoaa/readme/README.html">README</a> to get started.</p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://cran.r-project.org/package=FuelDeep3D">FuelDeep3D</a> v0.1.1: Provides tools for preprocessing, feature extraction, and segmentation of three-dimensional forest point clouds derived from terrestrial laser scanning. Functions support creating height-above-ground metrics, tiling, and sampling point clouds, generating training datasets, applying trained models to new point clouds, and producing per-point fuel classes such as stems, branches, foliage, and surface fuels. Deep learning segmentation relies on the PointNeXt architecture described by <a href="https://arxiv.org/abs/2206.04670">Qian et al.&nbsp;(2022)</a> while ground classification utilizes the Cloth Simulation Filter algorithm by <a href="https://www.mdpi.com/2072-4292/8/6/501">Zhang et al.&nbsp;(2016)</a>. See <a href="https://cran.r-project.org/web/packages/FuelDeep3D/readme/README.html">README</a> for examples.</p>
<p><a href="FuelDeep3D.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/FuelDeep3D.png" class="img-fluid" alt="Example of Ras Las plot"></a></p>
<p><a href="https://cran.r-project.org/package=pep725">pep725</a> v1.0.2: Provides a framework for quality-aware analysis of ground-based phenological data from the PEP725 Pan-European Phenology Database <a href="https://link.springer.com/article/10.1007/s00484-018-1512-8">Templ et al.&nbsp;(2018)</a> and <a href="https://nph.onlinelibrary.wiley.com/doi/10.1111/nph.70869">Templ et al.&nbsp;(2026)</a> and similar observation networks. Implements station-level data quality grading, outlier detection, phenological normals (climate baselines), anomaly detection, elevation and latitude gradient estimation with robust regression, spatial synchrony quantification, partial least squares regression for identifying temperature-sensitive periods, and sequential Mann-Kendall trend analysis. There are four vignettes including <a href="https://cran.r-project.org/web/packages/pep725/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/pep725/vignettes/phenological-analysis.html">Phenological Analysis</a>.</p>
<p><a href="pep725.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/pep725.png" class="img-fluid" alt="Plot of Sequential Mann-Kendall Trend Analysis"></a></p>
<p><a href="https://cran.r-project.org/package=simpowa">simpowa</a> v1.0.3: Uses simulations from generalized linear mixed-effects models to incorporate random effects across multiple sources and levels of variation, and a dispersion parameter to account for overdispersion and capture unexplained variability. Covers design scenarios for both short-term and long-term trials evaluating the impact of single or combined vector control interventions. Methods build on <a href="https://link.springer.com/article/10.1186/s12936-025-05454-y">Kipingu et al.&nbsp;(2025)</a> and <a href="https://besjournals.onlinelibrary.wiley.com/doi/10.1111/2041-210X.12306">Johnson et al.&nbsp;(2015)</a>. See <a href="https://cran.r-project.org/web/packages/simpowa/readme/README.html">README</a> to get started.</p>
<p><a href="simpowa.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/simpowa.png" class="img-fluid" alt="Plot of power vs number of chambers per treatment"></a></p>
<p><a href="https://cran.r-project.org/package=soilFlux">soilFlux</a> v0.1.5: Implements a physics-informed one-dimensional convolutional neural network (CNN1D-PINN) for estimating the complete soil water retention curve (SWRC) as a continuous function of matric potential, from soil texture, organic carbon, bulk density, and depth. The network architecture ensures strict monotonic decrease of volumetric water content with increasing suction by construction, through cumulative integration of non-negative slope outputs (monotone integral architecture). Four physics-based residual constraints adapted from <a href="https://agupubs.onlinelibrary.wiley.com/doi/10.1029/2024WR038149">Norouzi et al.&nbsp;(2025)</a>. See <a href="https://cran.r-project.org/web/packages/soilFlux/readme/README.html">README</a> and the <a href="https://cran.r-project.org/web/packages/soilFlux/vignettes/introduction.html">vignette</a>.</p>
<p><a href="soilFlux.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/soilFlux.png" class="img-fluid" alt="Plot of predicted versus observed values"></a></p>
</section>
<section id="health-technology-assessment" class="level3">
<h3 class="anchored" data-anchor-id="health-technology-assessment">Health Technology Assessment</h3>
<p><a href="https://cran.r-project.org/package=drMAIC">drMAIC</a> v0.1.0: Implements Doubly Robust Matching-Adjusted Indirect Comparison for population-adjusted indirect treatment comparisons in health technology appraisal. Features include standard MAIC via entropy balancing, augmented/doubly robust MAIC combining inverse probability weighting with outcome regression, comprehensive covariate balance diagnostics, Love plots, sensitivity analyses, bootstrap confidence intervals and submission-ready outputs aligned with NICE Decision Support Unit Technical Support Document 18, Cochrane Handbook guidance on indirect comparisons, and ISPOR best practice guidelines. See the <a href="https://cran.r-project.org/web/packages/drMAIC/vignettes/drMAIC_intro.html">vignette</a>.</p>
</section>
<section id="mathematics" class="level3">
<h3 class="anchored" data-anchor-id="mathematics">Mathematics</h3>
<p><a href="https://cran.r-project.org/package=wkpool">wkpool</a> v0.3.0: Provides functions to establish and maintain vertex pool topology for geometry characterized by the <a href="https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry"><code>Well-known text</code></a> markup language. In this framework, segments are the atomic unit, vertices are shared via integer references into a pool. Topology is made discoverable via coincident vertex detection while not requiring modification of the input data. Topological data models follow principles described in <a href="https://www.amazon.com/GIS-Computing-Perspective-Michael-Worboys/dp/0415283752">Worboys and Duckham (2004)</a>. The edge-based topology geometry decomposed into vertices and directed edge pairs is a simplification of the quad-edge case in <a href="https://dl.acm.org/doi/10.1145/282918.282923">Guibas &amp; Stolfi (1985)</a>. See the vignettes <a href="https://cran.r-project.org/web/packages/wkpool/vignettes/wkpool-vignette-segments.html">Segments: the atoms of geometry</a> and <a href="https://cran.r-project.org/web/packages/wkpool/vignettes/wkpool.html">wkpool: Topology-based Geometry Handling</a>.</p>
</section>
<section id="medical-statistics" class="level3">
<h3 class="anchored" data-anchor-id="medical-statistics">Medical Statistics</h3>
<p><a href="https://cran.r-project.org/package=bmco">bmco</a> v0.1.0: Provides Bayesian methods for comparing groups on multiple binary outcomes. Includes basic tests using multivariate Bernoulli distributions, subgroup analysis via generalized linear models, and multilevel models for clustered data. For statistical underpinnings, see <a href="https://journals.sagepub.com/doi/10.1177/0962280220922256">Kavelaars, Mulder, and Kaptein (2020)</a>, <a href="https://www.tandfonline.com/doi/full/10.1080/00273171.2024.2337340">Kavelaars, Mulder, and Kaptein (2024)</a> and <a href="https://link.springer.com/article/10.1186/s12874-023-02034-z">Kavelaars, Mulder, and Kaptein (2023)</a>. See the <a href="https://xynthia-kavelaars.shinyapps.io/bmco-pwr/">Shiny app</a> for performing sample size computations. There are three vignettes including <a href="https://cran.r-project.org/web/packages/bmco/vignettes/introduction.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/bmco/vignettes/subgroup-analysis.html">Subgroup Analysis with Multivariate Binary Outcomes</a>.</p>
<p><a href="bmco.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/bmco.png" class="img-fluid" alt="Plot of posterior density"></a></p>
<p><a href="https://cran.r-project.org/package=CohortMethod">CohortMethod</a> v6.0.1: Provides functions for performing comparative cohort studies in an observational database in the <a href="https://ohdsi.github.io/CommonDataModel/">Observational Medical Outcomes Partnership (OMOP) Common Data Model</a> which implements large-scale propensity scores (LSPS) as described in <a href="https://academic.oup.com/ije/article/47/6/2005/5043131?login=false">Tian et al.&nbsp;(2018)</a>. Functions are included for trimming, stratifying, matching and weighting by propensity scores, and diagnostics. See <a href="https://dl.acm.org/doi/10.1145/2414416.2414791">Suchard et al.&nbsp;(2013)</a> for methods of using large scale regularized regression for fitting propensity models. There are three vignettes including <a href="https://cran.r-project.org/web/packages/CohortMethod/vignettes/SingleStudies.pdf">Single Studies</a> and <a href="https://cran.r-project.org/web/packages/CohortMethod/vignettes/MultipleAnalyses.pdf">Multiple Analyses</a>.</p>
<p><a href="CohortMethod.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/CohortMethod.png" class="img-fluid" alt="Plot of propensity score densities"></a></p>
<p><a href="https://cran.r-project.org/package=epicR">epicR</a> v1.0.1: Implements a Discrete Event Simulation model that simulates health outcomes of patients with Chronic Obstructive Pulmonary Disease (COPD) based on demographics and individual-level risk factors. It is based on the model published in <a href="https://journals.sagepub.com/doi/10.1177/0272989X18824098">Sadatsafavi et al.&nbsp;(2019)</a>. There are four vignettes including <a href="https://cran.r-project.org/web/packages/epicR/vignettes/GettingStarted.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/epicR/vignettes/BackgroundEPIC.html">Background</a>.</p>
<p><a href="https://cran.r-project.org/package=forestsearch">forestsearch</a> v0.1.0: Designed for clinical researchers conducting exploratory subgroup analyses in randomized controlled trials, particularly for multi-regional clinical trials requiring regional consistency evaluation. The package implements statistical methods for exploratory subgroup identification in clinical trials with survival endpoints. Features include machine learning tools for identifying patient subgroups with differential treatment effects and bootstrap bias correction methods using the infinitesimal jackknife methods to address selection bias in post-hoc analyses. See <a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.10163">León et al.&nbsp;(2024)</a> for background and the <a href="https://cran.r-project.org/web/packages/forestsearch/vignettes/forestsearch.html">vignette</a> for examples.</p>
<p><a href="forestsearch.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/forestsearch.png" class="img-fluid" alt="Kaplan-Meier curves by subgroups"></a></p>
<p><a href="https://cran.r-project.org/package=mrddGlobal">mrddGlobal</a> v0.1.0: Provides functions for global testing for regression discontinuity designs with more than one running variable including functions for testing whether there exist non-zero treatment effects along the boundary of the treated region, and testing whether there exist discontinuities in the joint density of the running variables along the boundary of the treated region. See <a href="https://arxiv.org/abs/2602.03819">Samiahulin (2026)</a> for background and the <a href="https://cran.r-project.org/web/packages/mrddGlobal/vignettes/mrddGlobal_vignette.html">vignette</a> for examples.</p>
<p><a href="mrddGlobal.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/mrddGlobal.png" class="img-fluid" alt="3D Boundary Plot"></a></p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="probability" class="level3">
<h3 class="anchored" data-anchor-id="probability">Probability</h3>
<p><a href="https://cran.r-project.org/web/packages/ZeroOneDists/readme/README.html">ZeroOneDists</a> v1.0.0: Implements new statistical distributions on the (0, 1) interval. Each distribution includes the traditional functions as well as an additional function called the family function, which can be used to estimate parameters using Generalized Additive Models for location, scale and shape, GAMLSS by <a href="https://academic.oup.com/jrsssc/article-abstract/54/3/507/7113027?redirectedFrom=fulltext&amp;login=false">Rigby &amp; Stasinopoulos (2005)</a>.</p>
</section>
<section id="programming" class="level3">
<h3 class="anchored" data-anchor-id="programming">Programming</h3>
<p><a href="https://CRAN.R-project.org/package=arl">arl</a> v0.1.4: Provides a Scheme-inspired Lisp dialect embedded in R, with macros, tail-call optimization, and seamless interoperability with R functions and data structures. (The name ‘arl’ is short for ‘An R Lisp.’) Implemented in pure R with no compiled code. Note that the twenty-two vignettes only contain links to the source code. Look <a href="https://github.com/wwbrannon/arl">here</a> to get started, and <a href="https://zenodo.org/records/19013720">here</a> for the layout of the source code.</p>
<p><a href="https://cran.r-project.org/package=restrictR">restrictR</a> v0.1.0: Provides functions to build reusable validators from small building blocks using the base pipe operator. Define runtime contracts once with <code>restrict()</code> and enforce them anywhere in code. Validators compose naturally, support dependent rules via formulas, and produce clear, path-aware error messages. No DSL, no operator overloading, just idiomatic R. See the <a href="https://cran.r-project.org/web/packages/restrictR/vignettes/restrictR.html">vignette</a>.</p>
</section>
<section id="public-health" class="level3">
<h3 class="anchored" data-anchor-id="public-health">Public Health</h3>
<p><a href="https://cran.r-project.org/package=episomer">episomer</a> v3.0.34: Provides functions to automatically monitor trends of social media messages by time, place and topic aiming at detecting public health threats early through the detection of signals (i.e., an unusual increase in the number of messages per time, topic and location). It was designed to focus on infectious diseases, and it can be extended to all hazards or other fields of study by modifying the topics and keywords. More information on the original package <code>epitweetr</code> is available in the peer-review publication <a href="https://www.eurosurveillance.org/content/10.2807/1560-7917.ES.2022.27.39.2200177">Espinosa et al.&nbsp;(2022)</a>. There are two vignettes: vignettes <a href="https://cran.r-project.org/web/packages/episomer/vignettes/episomer-vignette.html">user documentation</a> and <a href="https://cran.r-project.org/web/packages/episomer/vignettes/add_new_social_media.html">add new social media</a>. Look <a href="https://github.com/EU-ECDC/episomer/discussions/7">here</a> for additional documentation including information about the associated <code>Shiny</code> app.</p>
</section>
<section id="risk-analysis" class="level3">
<h3 class="anchored" data-anchor-id="risk-analysis">Risk Analysis</h3>
<p><a href="https://cran.r-project.org/package=bayespmtools">bayespmtools</a> v0.0.1: Provides functions to perform Bayesian sample size, precision, and value-of-information analysis for external validation of existing multi-variable prediction models using the approach proposed by <a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.70389">Sadatsafavi et al.&nbsp;(2025)</a>. There is a <a href="https://cran.r-project.org/web/packages/bayespmtools/vignettes/getting_started.html">Getting Started Guide</a> and a <a href="https://cran.r-project.org/web/packages/bayespmtools/vignettes/bayespmtools_tutorial.html">tutorial</a>.</p>
<p><a href="bayespmtools.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/bayespmtools.png" class="img-fluid" alt="Plot of expected gain vs sample size "></a></p>
</section>
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=badp">badp</a> v0.4.0.1: Implements Bayesian model averaging for dynamic panels with weakly exogenous regressors as described in the paper by <a href="https://www.tandfonline.com/doi/abs/10.1080/07350015.2013.818003">Moral-Benito (2013)</a> and provides functions to estimate dynamic panel data models and analyze the results of the estimation. See the <a href="https://cran.r-project.org/web/packages/badp/vignettes/badp_vignette.html">vignette</a>.</p>
<p><a href="badp.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/badp.png" class="img-fluid" alt="Plot contrasting of different priors"></a></p>
<p><a href="https://cran.r-project.org/package=BORG">BORG</a> v0.3.1: Provides tools for valid spatial, temporal, and grouped model evaluation. Automatically detects data dependencies (spatial autocorrelation, temporal structure, clustered observations), generates appropriate cross-validation schemes (spatial blocking, checkerboard, hexagonal, KNNDM, environmental blocking, leave-location-out, purged CV), and validates evaluation pipelines for leakage. Includes area of applicability assessment following <a href="https://besjournals.onlinelibrary.wiley.com/doi/10.1111/2041-210X.13650">Meyer &amp; Pebesma (2021)</a>. <a href="https://nsojournals.onlinelibrary.wiley.com/doi/10.1111/ecog.02881">Roberts et al.&nbsp;(2017)</a> and <a href="https://gmd.copernicus.org/articles/17/5897/2024/">Linnenbrink et al.&nbsp;(2024)</a>. There are three vignettes including <a href="https://cran.r-project.org/web/packages/BORG/vignettes/quickstart.html">Quick Start</a> and <a href="https://cran.r-project.org/web/packages/BORG/vignettes/risk-taxonomy.html">Risk Taxonomy</a>.</p>
<p><a href="https://cran.r-project.org/package=flexhaz">flexhaz</a> v0.5.1: Implements a framework for specifying survival distributions through their hazard (failure rate) functions. Functions enable defining arbitrary time-varying hazard functions to model complex failure patterns including bathtub curves, proportional hazards with covariates, automatically compute survival, CDF, PDF, and quantile estimates, and implement the likelihood model interface for maximum likelihood estimation with right-censored and left-censored survival data. There are five vignettes including <a href="https://cran.r-project.org/web/packages/flexhaz/vignettes/flexhaz-package.html">flexhaz: Hazard-First Survival Modeling</a> and <a href="https://cran.r-project.org/web/packages/flexhaz/vignettes/failure_rate.html">Dynamic Failure Rate Distributions</a>.</p>
<p><a href="https://cran.r-project.org/package=maskedcauses">maskedcauses</a> v0.9.3: Implements maximum likelihood estimation for series systems where the component cause of failure is masked. Includes analytical log-likelihood, score, and Hessian functions for exponential, homogeneous Weibull, and heterogeneous Weibull component lifetimes under masked cause conditions. Supports exact, right-censored, left-censored, and interval-censored observations via composable observation functors. Also provides random data generation, model fitting, and Fisher information for asymptotic inference. See <a href="https://ieeexplore.ieee.org/document/257799">Lin, Loh, and Bai (1993)</a> and <a href="https://academic.oup.com/biometrics/article-abstract/62/2/526/7321802?redirectedFrom=fulltext&amp;login=false">Craiu and Reiser (2006)</a> for background. There are seven vignettes including <a href="https://cran.r-project.org/web/packages/maskedcauses/vignettes/censoring_comparison.html">Censoring Types in Series System Masked Data</a> and <a href="https://cran.r-project.org/web/packages/maskedcauses/index.html">Model Selection for Masked Series Systems via Likelihood Ratio Tests</a>.</p>
<p><a href="maskedcauses.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/maskedcauses.png" class="img-fluid" alt="Plot of conditional cause of failure probability"></a></p>
<p><a href="https://cran.r-project.org/package=mmbcv">mmbcv</a> v0.3.0: Provides a function to compute robust and bias-corrected sandwich variance estimators for multi-state Cox models with clustered time-to-event data. The methodology extends the marginal Cox model bias-correction framework of <a href="https://onlinelibrary.wiley.com/doi/10.1002/bimj.202200113">Wang et al.&nbsp;(2023)</a> to the multi-state setting. See the <a href="https://cran.r-project.org/web/packages/mmbcv/vignettes/mmbcv-intro.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=osktnorm">osktnorm</a> v1.1.2: Implements a moment-targeting normality transformation based on the simultaneous optimization of Tukey g-h distribution parameters designed to minimize both asymmetry (skewness) and excess peakedness (kurtosis) in non-normal data by mapping it to a standard normal distribution as described by <a href="https://www.mdpi.com/2073-8994/18/3/458">Cebeci et al.&nbsp;(2026)</a>. Optimization is performed by minimizing an objective function derived from the Anderson-Darling goodness-of-fit statistic with Stephens’s correction factor, utilizing the L-BFGS-B algorithm for robust parameter estimation. This approach provides an effective alternative to power transformations like Box-Cox and Yeo-Johnson, particularly for data requiring precise tail-behavior adjustment. See the vignettes <a href="https://cran.r-project.org/web/packages/osktnorm/vignettes/osktnorm1.html">OSKT: Normality Transformation via Optimized Skewness and Kurtosis</a> and <a href="https://cran.r-project.org/web/packages/osktnorm/vignettes/osktnorm2.html">A Practical Example for Working with Datasets</a>.</p>
<p><a href="osktnorm.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/osktnorm.png" class="img-fluid" alt="Plots comparing actual densities with theoretical normal densities "></a></p>
<p><a href="https://cran.r-project.org/package=RFmstate">RFmstate</a> v0.1.2: Provides functions to fit cause-specific random survival forests for flexible multistate survival analysis with covariate-adjusted transition probabilities computed via product-integral. State transitions are modeled by random forests. Subject-specific transition probability matrices are assembled from predicted cumulative hazards using the product-integral formula. Also provides a standalone Aalen-Johansen nonparametric estimator as a covariate-free baseline, per-transition feature importance, bias-variance diagnostics, and comprehensive visualizations. Handles right censoring and competing transitions. Methods are described in <a href="https://projecteuclid.org/journals/annals-of-applied-statistics/volume-2/issue-3/Random-survival-forests/10.1214/08-AOAS169.full">Ishwaran et al.&nbsp;(2008)</a> for random survival forests, <a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.2712">Putter et al.&nbsp;(2007)</a> for multistate competing risks decomposition, and <a href="https://www.jstor.org/stable/4615704">Aalen and Johansen (1978)</a> for the nonparametric estimator. See the <a href="https://cran.r-project.org/web/packages/RFmstate/vignettes/introduction.html">vignette</a> for an introduction.</p>
<p><a href="RFmstate.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/RFmstate.png" class="img-fluid" alt="Plot of time-dependent Brier scores "></a></p>
<p><a href="https://cran.r-project.org/package=SuperSurv">SuperSurv</a> v0.1.1: Implements a Super Learner framework for right-censored survival data. The package fits convex combinations of parametric, semiparametric, and machine learning survival learners by minimizing cross-validated risk using inverse probability of censoring weighting. It provides tools for automated hyperparameter grid search, high-dimensional variable screening, and evaluation of prediction performance using metrics such as the Brier score, Uno’s C-index, and time-dependent area under the curve. See <a href="https://www.tandfonline.com/doi/full/10.1080/01621459.2023.2205060">Westling et al.&nbsp;(2024)</a> and <a href="https://www.biorxiv.org/content/10.64898/2026.03.11.711010v2">Lyu et al.&nbsp;(2026)</a> for background. There are eleven vignettes including <a href="https://cran.r-project.org/web/packages/SuperSurv/vignettes/base-learner-rfsrc.html">Machine Learning</a> and <a href="https://cran.r-project.org/web/packages/SuperSurv/vignettes/shap-explanations.html">8. Interpreting the Black Box with SHAP &amp; survex</a>.</p>
<p><a href="SuperSurv.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/SuperSurv.png" class="img-fluid" alt="Plot of SHAP summary of top five features"></a></p>
</section>
<section id="time-series" class="level3">
<h3 class="anchored" data-anchor-id="time-series">Time Series</h3>
<p><a href="https://cran.r-project.org/package=betaARMA">betaARMA</a> v1.1.0: Provides functions to fit Beta Autoregressive Moving Average (BARMA) models for time series data distributed in the standard unit interval (0, 1). The estimation is performed via the conditional maximum likelihood method using the Broyden-Fletcher-Goldfarb-Shanno (BFGS) quasi-Newton algorithm. The package includes tools for model fitting, diagnostic checking, and forecasting. Based on the work of <a href="https://link.springer.com/article/10.1007/s11749-008-0112-z">Rocha and Cribari-Neto (2009)</a> and the associated erratum <a href="https://link.springer.com/article/10.1007/s11749-017-0528-4">Rocha and Cribari-Neto (2017)</a>. Look <a href="https://github.com/Everton-da-Costa/betaARMA">here</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=boundedur">boundedur</a> v1.0.1: Implements unit root tests for bounded time series following <a href="https://www.sciencedirect.com/science/article/abs/pii/S0304407613001620?via%3Dihub">Cavaliere and Xu (2014)</a>. Standard unit root tests (ADF, Phillips-Perron) have non-standard limiting distributions when the time series is bounded. This package provides modified ADF and M-type tests (MZ-alpha, MZ-t, MSB) with p-values computed via Monte Carlo simulation of bounded Brownian motion. Supports one-sided (lower bound only) and two-sided bounds, with automatic lag selection using the MAIC criterion of <a href="https://onlinelibrary.wiley.com/doi/10.1111/1468-0262.00256">Ng and Perron (2001)</a>. See <a href="https://cran.r-project.org/web/packages/boundedur/readme/README.html">README</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=reviser">reviser</a> v0.1.1: Analyzes revisions in real-time time series vintages. Functions convert between wide revision triangles and tidy long vintages, extract selected releases, compute revision series, visualize vintage paths, and summarize revision properties such as bias, dispersion, autocorrelation, and news-noise diagnostics. There are also functions to identify efficient releases and estimate state-space models for revision nowcasting. Methods are based on <a href="https://www.jstor.org/stable/1924972?origin=crossref">Howrey (1978)</a>, <a href="https://www.sciencedirect.com/science/article/abs/pii/S0304407610002526?via%3Dihub">Jacobs and Van Norden (2011)</a>, and <a href="https://www.tandfonline.com/doi/full/10.1198/jbes.2010.08169">Kishor and Koenig (2012)</a>. There are seven vignettes including <a href="https://cran.r-project.org/web/packages/reviser/vignettes/reviser.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/reviser/vignettes/revision-analysis.html">revision analysis</a>.</p>
<p><a href="reviser.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/reviser.png" class="img-fluid" alt="Line plot showing GDP vintages over the publication date dimension"></a></p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=RDesk">RDesk</a> v1.0.5: Allows <code>R</code> developers to build native <code>Windows</code> desktop applications using <code>R</code> and <code>WebView</code> and create professional, standalone desktop tools with modern web-based user interfaces while maintaining a pure <code>R</code> backend. Provides a robust <code>R6</code>-based event loop, asynchronous background task management via <code>mirai</code> and <code>callr</code>, and a native <code>Win32</code> message bridge for seamless <code>R</code>-to-user-interface communication without listening ports or network overhead. There are seven vignettes including <a href="https://cran.r-project.org/web/packages/RDesk/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/RDesk/vignettes/cookbook.html">Cookbook</a>.</p>
<p><a href="RDesk.png" class="lightbox" data-gallery="quarto-lightbox-gallery-20"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/RDesk.png" class="img-fluid" alt="Sample application interface"></a></p>
<p><a href="https://cran.r-project.org/package=starburst">starburst</a> v0.3.8: Implements a <code>future</code> backend that enables seamless execution of parallel R workloads on <a href="https://aws.amazon.com">Amazon Web Services (‘AWS’)</a>, including <code>EC2</code> and <code>Fargate</code> that handles environment synchronization, data transfer, quota management, and worker orchestration automatically, allowing users to scale from local execution to 100+ cloud workers with a single line of code change. There are twelve vignettes including <a href="https://cran.r-project.org/web/packages/starburst/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/starburst/vignettes/example-risk-modeling.html">Example: Parallel Portfolio Risk Modeling</a>.</p>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=BayesVolcano">BayesVolcano</a> v1.0.1: Bayesian models are used to estimate effect sizes (e.g., gene expression changes, protein abundance differences, drug response effects) while accounting for uncertainty, small sample sizes, and complex experimental designs. However, Bayesian posteriors of models with many parameters are often difficult to interpret at a glance. One way to quickly identify important biological changes based on frequentist analysis is volcano plots (using fold-changes and p-values). Bayesian volcano plots bring together the explicit treatment of uncertainty in Bayesian models and the familiar visualization of volcano plots. See <a href="https://www.sciencedirect.com/science/article/abs/pii/S0003267019313492?via%3Dihub">Sousa (2020)</a> for background and the <a href="https://cran.r-project.org/web/packages/BayesVolcano/vignettes/BayesVolcano.html">vignette</a> for examples.</p>
<p><a href="BayesVolcano.png" class="lightbox" data-gallery="quarto-lightbox-gallery-21"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/BayesVolcano.png" class="img-fluid" alt="Bayesian Volcano Plot"></a></p>
<p><a href="https://cran.r-project.org/package=forrest">forrest</a> v0.3.0: Provides a function to create publication-ready forest plots from any tabular data containing point estimates and confidence intervals. Suitable for visualizing results from regression models, meta-analyses, subgroup analyses, or any comparative study. Supports group and subgroup headings, summary estimates displayed as diamonds, grouped estimates with automatic color and shape mapping, vertical dodging of multiple estimates within the same row, customisable text columns alongside the plot, and optional row striping. Provides a helper to export plots to PDF, PNG, SVG, or TIFF. Built on <code>tinyplot</code> for clean, consistent visual styling with a minimal dependency footprint. There are four vignettes including <a href="https://cran.r-project.org/web/packages/forrest/vignettes/intro.html">Introduction to forrest</a> and <a href="https://cran.r-project.org/web/packages/forrest/vignettes/internals.html">How forrest works</a>.</p>
<p><a href="forrest.png" class="lightbox" data-gallery="quarto-lightbox-gallery-22"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/forrest.png" class="img-fluid" alt="Sample forrest plot"></a></p>
<p><a href="https://cran.r-project.org/package=HeatmapR">HeatmapR</a> v1.0.0: Provides a lightweight framework for creating high-quality, complex heatmaps using base graphics. Supports hierarchical clustering with dendrograms, column and row scaling, cluster sub-divisions, customizable cell colors, shapes and sizes, legends, and flexible layouts for arranging multiple heatmaps. Designed to return plot objects that can be easily arranged with other plots without sacrificing resolution. Methods for hierarchical clustering and distance computations are described in <a href="https://wires.onlinelibrary.wiley.com/doi/10.1002/wics.53">Murtagh and Contreras (2012)</a>. Dendrogram visualization methods are based on the <code>ggdendro</code> package by <a href="https://CRAN.R-project.org/package=ggdendro">de Vries and Ripley (2020)</a>. See the <a href="https://cran.r-project.org/web/packages/HeatmapR/vignettes/HeatmapR.html">vignette</a>.</p>
<p><a href="HeatmapR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-23"><img src="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/HeatmapR.png" class="img-fluid" alt="Sample heatmap"></a></p>
</section>
</div>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/march-2026-top-40-new-cran-packages/</guid>
  <pubDate>Tue, 28 Apr 2026 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/march-2026-top-40-new-cran-packages/opensimplex2.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Stage II OSCC — Health Economics Model</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/oscc-patient-model/</link>
  <description><![CDATA[ 





<p>Most health care economics models are constructed from the perspective of a managed health care system such as those offered in Canada and several European countries, or from the perspective of some other third party such as an insurance company. Although the benefits of constructing models from the patient’s perspective have been discussed in the literature, see for example <a href="https://pmc.ncbi.nlm.nih.gov/articles/PMC3134464/">Ioannidis &amp; Garber (2011)</a> and <a href="https://pubmed.ncbi.nlm.nih.gov/27712720/">Tai et al.&nbsp;(2016)</a>, few models have been published. Obvious reasons for this situation would include the privacy issues associated with obtaining patient specific data and the apparent lack of economic incentives to commit resources required to abstract individual patient trajectories from patient level data.</p>
<p>This post explores the hypothesis that detailed, data-driven, patient-specific models may not be necessary in order to achieve most of the benefits of a health economics model from the patients perspective. It present a lightweight, cohort model that captures the majority of the benefits to both physicians and patients that might result from a patient focused model.</p>
<p>The model is intended to be a proof of concept, a minimal viable model that may be useful to physicians both in deciding on treatment options and in informing patients about the potential outcomes and how they may experience these outcomes. To see how the medical literature may be helpful for this kind of modeling I explore the specific case of choosing between surgery and definitive radiation therapy for patients with stage II oral squamous cell carcinoma (OSCC).</p>
<p><strong>Note that the model developed here is not being presented as a medical analysis. No medical experts have been consulted in its construction. It is merely being offered as an example of what could be done to build a health care economics model from a patient’s perspective with today’s modeling tools. Also note that I made significant use of <a href="https://posit.co/blog/introducing-ai-in-rstudio/">Posit Assistant</a> for the RStudio IDE configured with Claude Sonnet 4.6 which I found to be immensely helpful both for code construction and literature searches even though utility of the latter use was limited by the vexing proclivity of Claude to hallucinate. </strong></p>
<section id="overview-of-the-model" class="level2">
<h2 class="anchored" data-anchor-id="overview-of-the-model">Overview of the Model</h2>
<p>The recovery of patients undergoing treatment OSCC, either surgery or radiation treatment, is conceived as a stochastic journey through various health states. The states visited, the sequence in which they are visited and the length of stay in each health state are modeled as random variables developing in in the framework of an eight=state, continuous-time Markov chain. Estimates of the transition probabilities among states and the mean time patients would remain in a state drive Markov chain. In the model below, these estimates are derived from the medical literature, however, it is conceivable that clinicians may feel comfortable in making their own estimates, or modifying the literature derived estimates according to their experience and their evaluations of their patients.</p>
<p>The next step uses the theory of continuous time Markov chains (CTMC) to construct a synthetic data set for a cohort of patients who vary in age and tumor size. The key insight here is that the synthetic data set is the underlying statistical model. Empirical survival curves and the time spent in each state and other useful quantities are then calculated from the synthetic data. The patient specific healthcare model is then constructed by estimating the utility of each health state to the patient. This is accomplished by constructing quality adjust life years (QALYs) based on <a href="https://www.ncbi.nlm.nih.gov/books/NBK565680/">EQ-5D</a> values reported in the literature.</p>
</section>
<section id="state-diagram-for-a-continuous-time-markov-chain" class="level2">
<h2 class="anchored" data-anchor-id="state-diagram-for-a-continuous-time-markov-chain">State Diagram for a Continuous Time Markov Chain</h2>
<div class="cell">
<details class="code-fold">
<summary>Packages used throughout the post</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(msm)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gt)</span></code></pre></div></div>
</details>
</div>
<p>It is my understanding that although most patients wit stage II OSCC are treated with surgery there are times when surgery is either not possible or not preferable and the in these exceptional cases, radiation treatment is the primary alternative. The following state diagram is an attempt to abstract both the health states a patient will experience following treatment and the probable paths that will be taken among them. Eight states seemed to me to be the minimum number of states required to represent the complexities of treatment.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code to build state diagram</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># library(ggplot2)</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># library(grid)</span></span>
<span id="cb2-3"></span>
<span id="cb2-4">BG <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F0F3F7"</span></span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── 1. Nodes ─────────────────────────────────────────────────────────────────</span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Layout: left=initial treatment, center=adjuvant/NED, right=outcomes/death</span></span>
<span id="cb2-8">nodes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id    =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span id="cb2-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb2-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Surgery</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">± Neck Dissection"</span>,</span>
<span id="cb2-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Definitive</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Radiation"</span>,</span>
<span id="cb2-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Post-op</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Surveillance"</span>,</span>
<span id="cb2-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Adjuvant RT</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(PORT)"</span>,</span>
<span id="cb2-15">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Adjuvant</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ChemoRT (POCRT)"</span>,</span>
<span id="cb2-16">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S6</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">NED</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(Health)"</span>,</span>
<span id="cb2-17">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Locoregional</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Recurrence"</span>,</span>
<span id="cb2-18">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Death"</span></span>
<span id="cb2-19">  ),</span>
<span id="cb2-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.0</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.5</span>),</span>
<span id="cb2-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.5</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.5</span>,   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>),</span>
<span id="cb2-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E67E22"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8E44AD"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F39C12"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2980B9"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#16A085"</span>,</span>
<span id="cb2-23">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#27AE60"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C0392B"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C3E50"</span>),</span>
<span id="cb2-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb2-25">)</span>
<span id="cb2-26"></span>
<span id="cb2-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── 2. Edge definitions: from, to, label, curvature, nudge_x, nudge_y ────────</span></span>
<span id="cb2-28">edge_defs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-29">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Surgery → downstream</span></span>
<span id="cb2-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No high-risk</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">features"</span>,    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span>),</span>
<span id="cb2-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High-risk:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">margins/PNI/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">LVI/nodes"</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span>),</span>
<span id="cb2-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ECE/pos</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">margins"</span>,          <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span>),</span>
<span id="cb2-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Peri-op</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">death"</span>,           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.22</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span>),</span>
<span id="cb2-34">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Definitive RT → downstream</span></span>
<span id="cb2-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NED"</span>,                       <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>),</span>
<span id="cb2-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Treatment</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">failure"</span>,        <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>),</span>
<span id="cb2-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Death"</span>,                     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.14</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span>),</span>
<span id="cb2-38">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Post-op Surveillance → downstream</span></span>
<span id="cb2-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,                          <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>),</span>
<span id="cb2-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,                          <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.22</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>),</span>
<span id="cb2-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,                         <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>),</span>
<span id="cb2-42">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PORT → downstream</span></span>
<span id="cb2-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,                          <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>),</span>
<span id="cb2-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,                          <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>),</span>
<span id="cb2-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,                          <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.14</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>),</span>
<span id="cb2-46">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># POCRT → downstream</span></span>
<span id="cb2-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,                          <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>),</span>
<span id="cb2-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,                          <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.22</span>),</span>
<span id="cb2-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,                          <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>),</span>
<span id="cb2-50">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NED → outcomes</span></span>
<span id="cb2-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LR recurrence"</span>,             <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>),</span>
<span id="cb2-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Death"</span>,                     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.14</span>),</span>
<span id="cb2-53">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Locoregional Recurrence → outcomes</span></span>
<span id="cb2-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Death"</span>,                     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>),</span>
<span id="cb2-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Salvage</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">success"</span>,         <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.28</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.28</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>)</span>
<span id="cb2-56">)</span>
<span id="cb2-57"></span>
<span id="cb2-58">edges <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(edge_defs, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(r) {</span>
<span id="cb2-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(r[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(r[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]),</span>
<span id="cb2-60">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob=</span>r[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curvature=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(r[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]]),</span>
<span id="cb2-61">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_x=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(r[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_y=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(r[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>]]),</span>
<span id="cb2-62">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-63">}))</span>
<span id="cb2-64"></span>
<span id="cb2-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Attach node coordinates</span></span>
<span id="cb2-66">edges <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(edges, nodes[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by.x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"from"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by.y=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sort=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-67"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x0"</span>; <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y0"</span></span>
<span id="cb2-68">edges <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(edges, nodes[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by.x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by.y=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sort=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-69"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x1"</span>; <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y1"</span></span>
<span id="cb2-70"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(edges)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dest_col"</span></span>
<span id="cb2-71"></span>
<span id="cb2-72"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shorten endpoints to node perimeter</span></span>
<span id="cb2-73">NODE_R <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.58</span></span>
<span id="cb2-74">shorten <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x0,y0,x1,y1,r) {</span>
<span id="cb2-75">  dx<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>x1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>x0; dy<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>y0; d<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(dx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dy<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-76">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xs=</span>x0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>d, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ys=</span>y0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dy<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>d, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xe=</span>x1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>d, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ye=</span>y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dy<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>d)</span>
<span id="cb2-77">}</span>
<span id="cb2-78">segs     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Map</span>(shorten, edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x0, edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y0, edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x1, edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">MoreArgs=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r=</span>NODE_R))</span>
<span id="cb2-79">edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(segs,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">[[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xs"</span>); edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ys <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(segs,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">[[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ys"</span>)</span>
<span id="cb2-80">edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xe <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(segs,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">[[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xe"</span>); edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ye <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(segs,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">[[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ye"</span>)</span>
<span id="cb2-81">edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xe)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nudge_x</span>
<span id="cb2-82">edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ys<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ye)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> edges<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nudge_y</span>
<span id="cb2-83"></span>
<span id="cb2-84"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── 3. Build layers ───────────────────────────────────────────────────────────</span></span>
<span id="cb2-85">arrow_layers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(edges)), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i) {</span>
<span id="cb2-86">  e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> edges[i,,drop<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>]</span>
<span id="cb2-87">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_curve</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>e, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>xs,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>ys,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span>xe,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span>ye),</span>
<span id="cb2-88">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curvature=</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>curvature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dest_col[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]],</span>
<span id="cb2-89">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>,</span>
<span id="cb2-90">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pt"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ends=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"last"</span>),</span>
<span id="cb2-91">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-92">})</span>
<span id="cb2-93"></span>
<span id="cb2-94">label_layers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(edges)), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i) {</span>
<span id="cb2-95">  e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> edges[i,,drop<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>]</span>
<span id="cb2-96">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nchar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trimws</span>(e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>prob[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb2-97">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>e, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>lx,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>ly,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>prob),</span>
<span id="cb2-98">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dest_col[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>BG, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span id="cb2-99">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.22</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.r=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>),</span>
<span id="cb2-100">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.padding=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-101">})</span>
<span id="cb2-102">label_layers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Negate</span>(is.null), label_layers)</span>
<span id="cb2-103"></span>
<span id="cb2-104"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── 4. Base plot ──────────────────────────────────────────────────────────────</span></span>
<span id="cb2-105">base_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-106">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-107">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb2-108">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>BG, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb2-109">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>BG, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb2-110">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"serif"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>,</span>
<span id="cb2-111">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1A2535"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)),</span>
<span id="cb2-112">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#555555"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb2-113">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb2-114">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-115">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_fixed</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clip=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-116">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stage II Oral Squamous Cell Carcinoma"</span>,</span>
<span id="cb2-117">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CTMC State Transition Diagram"</span>)</span>
<span id="cb2-118"></span>
<span id="cb2-119"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── 5. Assemble ───────────────────────────────────────────────────────────────</span></span>
<span id="cb2-120">all_layers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(arrow_layers, label_layers,</span>
<span id="cb2-121">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-122">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column labels</span></span>
<span id="cb2-123">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Initial</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Treatment"</span>,</span>
<span id="cb2-124">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#555"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb2-125">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Post-surgical</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Pathway"</span>,</span>
<span id="cb2-126">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#555"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb2-127">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.0</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No Evidence</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">of Disease"</span>,</span>
<span id="cb2-128">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#555"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb2-129">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.5</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Disease</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Progression"</span>,</span>
<span id="cb2-130">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#555"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb2-131">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Absorbing</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">State"</span>,</span>
<span id="cb2-132">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#555"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb2-133">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nodes</span></span>
<span id="cb2-134">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>nodes, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span>fill),</span>
<span id="cb2-135">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>),</span>
<span id="cb2-136">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_identity</span>(),</span>
<span id="cb2-137">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>nodes, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span>label),</span>
<span id="cb2-138">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lineheight=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>),</span>
<span id="cb2-139">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Surgical candidate bracket</span></span>
<span id="cb2-140">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"segment"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.3</span>,</span>
<span id="cb2-141">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#888"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb2-142">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"segment"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.45</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.3</span>,</span>
<span id="cb2-143">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#888"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb2-144">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"segment"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.45</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>,</span>
<span id="cb2-145">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#888"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb2-146">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgical</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">candidate?</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Yes ↑</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">No ↓"</span>,</span>
<span id="cb2-147">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#666"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lineheight=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>)</span>
<span id="cb2-148">  )</span>
<span id="cb2-149">)</span>
<span id="cb2-150"></span>
<span id="cb2-151"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Reduce</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+"</span>, all_layers, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">init=</span>base_plot)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-2-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/oscc-patient-model/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<table class="caption-top table">
<colgroup>
<col style="width: 8%">
<col style="width: 30%">
<col style="width: 61%">
</colgroup>
<thead>
<tr class="header">
<th>State</th>
<th>Label</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>S1</td>
<td>Surgery</td>
<td>Initial surgical treatment (days–weeks)</td>
</tr>
<tr class="even">
<td>S2</td>
<td>DefinitiveRT</td>
<td>Non-surgical candidates; 6–7 week RT course</td>
</tr>
<tr class="odd">
<td>S3</td>
<td>PostOpSurveillance</td>
<td>Surgery, no high-risk pathological features</td>
</tr>
<tr class="even">
<td>S4</td>
<td>AdjuvantRT (PORT)</td>
<td>High-risk: margins, PNI, LVI, multiple nodes</td>
</tr>
<tr class="odd">
<td>S5</td>
<td>AdjuvantChemoRT (POCRT)</td>
<td>Highest-risk: ECE or positive margins</td>
</tr>
<tr class="even">
<td>S6</td>
<td>NED</td>
<td>No evidence of disease; long-term surveillance</td>
</tr>
<tr class="odd">
<td>S7</td>
<td>LR Recurrence</td>
<td>Locoregional recurrence at primary or nodes</td>
</tr>
<tr class="even">
<td>S8</td>
<td>Death</td>
<td>Absorbing state</td>
</tr>
</tbody>
</table>
</section>
<section id="synthetic-data-simulation" class="level2">
<h2 class="anchored" data-anchor-id="synthetic-data-simulation">Synthetic Data Simulation</h2>
<p>This section builds a synthetic patient cohort for Stage II (T2 N0 M0) oral squamous cell carcinoma using a continuous-time Markov chain (CTMC). The first code block sets up of the simulation by defining the covariates: patient age and tumor size. Mean patient age is set at 62 years old. Tumor size ranges from tumor 2-4 cm, DOI 5-10 mm</p>
<div class="cell">
<details class="code-fold">
<summary>Simulation setup</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6413</span>)</span>
<span id="cb3-2"></span>
<span id="cb3-3">N_PATIENTS <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb3-4">MAX_TIME   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># months (5-year follow-up)</span></span>
<span id="cb3-5">obs_base   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span>
<span id="cb3-6"></span>
<span id="cb3-7">state_labels_ii <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DefinitiveRT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PostOpSurveillance"</span>,</span>
<span id="cb3-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AdjuvantRT_PORT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AdjuvantChemoRT_POCRT"</span>,</span>
<span id="cb3-10">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NED"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LR_Recurrence"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Death"</span></span>
<span id="cb3-11">)</span>
<span id="cb3-12"></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Patient covariates ────────────────────────────────────────────────────────</span></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stage II OSCC (T2 N0 M0): tumor 2-4 cm, DOI 5-10 mm</span></span>
<span id="cb3-15">age           <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N_PATIENTS, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">62</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)))</span>
<span id="cb3-16">age           <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>L, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>L, age))</span>
<span id="cb3-17">tumor_size_cm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(N_PATIENTS, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-18">DOI_mm        <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(N_PATIENTS, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Surgical candidacy: logistic model, ~78% surgery at age 60</span></span>
<span id="cb3-21">surgery_prob <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plogis</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.04</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>))</span>
<span id="cb3-22">treatment    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(N_PATIENTS) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> surgery_prob, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DefinitiveRT"</span>)</span></code></pre></div></div>
</details>
</div>
<section id="clinician-inputs" class="level3">
<h3 class="anchored" data-anchor-id="clinician-inputs">Clinician Inputs</h3>
<p>The simulation is driven by two sets of clinician-interpret able inputs that are entered at the beginning of this section of code:</p>
<ul>
<li><strong>Jump chain probabilities</strong> <code>jump_P[i,j]</code>: given that a patient <em>leaves</em> state <em>i</em>, the probability they transition directly to state <em>j</em>.</li>
<li><strong>Mean sojourn times</strong> <code>mean_sojourn[i]</code>: the average time (months) a patient spends in state <em>i</em> before transitioning.</li>
</ul>
<p>The jump chain may be thought of as an embedded discrete time Markov chain that gives the probabilities of which state the chain will jump to next when it is time to jump. The sojourn times are inverted to obtain the transition rates. The values for these parameters have been derived from the literature, however, the model has been set up so that they can be easily changed if the physician thinks that a particular class of patients would be better represented by other values. The justifications for the default values for the parameters driving the model are provided in the comments in the code and in the references below.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code for model inputs</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># == CLINICIAN INPUTS ==========================================================</span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># jump_P[i, j]  : probability of moving to state j *given* a transition occurs</span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                 from state i.  Rows must sum to 1 for states 1-7.</span></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mean_sojourn  : average time (months) in each transient state.</span></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Together these fully specify the CTMC generator:</span></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   Q[i, j] = jump_P[i, j] / mean_sojourn[i]   (off-diagonal)</span></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All downstream simulation code derives Q_base automatically from these inputs.</span></span>
<span id="cb4-11"></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Jump chain transition probabilities ───────────────────────────────────────</span></span>
<span id="cb4-13"></span>
<span id="cb4-14"></span>
<span id="cb4-15">jump_P <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span id="cb4-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)))</span>
<span id="cb4-17"></span>
<span id="cb4-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S1 (Surgery) -&gt; {S3=no high-risk, S4=PORT, S5=POCRT, S8=peri-op death}</span></span>
<span id="cb4-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ~20-30% high-risk features -&gt; PORT (Tassone et al. 2023, NCDB n=53,503; DOI:10.1002/ohn.205)</span></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ~10-15% ECE/pos margins -&gt; POCRT; peri-operative mortality ~1-2% (Nathan et al. 2025, NSQIP n=866; DOI:10.18203/issn.2454-5929.ijohns20252980)</span></span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NB: S8 revised from 0.05 to 0.015 — the original 5% exceeded the literature range of 1-2%;</span></span>
<span id="cb4-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># freed mass (0.035) reallocated to S3 (residual no-adjuvant group), S4 and S5 unchanged.</span></span>
<span id="cb4-23">jump_P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.585</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.015</span>)</span>
<span id="cb4-24"></span>
<span id="cb4-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S2 (DefinitiveRT) -&gt; {S6=NED, S7=treatment failure, S8=death}</span></span>
<span id="cb4-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Definitive RT alone locoregional control ~65% for T2N0M0 (Dana-Farber Group 2011 PMID 21531515, 2-yr LRC 64%; Studer et al. 2007 T2-4 LC ~50-60%)</span></span>
<span id="cb4-27">jump_P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.28</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.07</span>)</span>
<span id="cb4-28"></span>
<span id="cb4-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S3 (PostOpSurveillance) -&gt; {S6=NED, S7=LR recurrence, S8=death}</span></span>
<span id="cb4-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5-yr LR recurrence ~12-18% after margin-negative R0 surgery alone (Ord 2006 15.5%; Luryi 2014 NCDB early-stage series)</span></span>
<span id="cb4-31">jump_P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.74</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.18</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.08</span>)</span>
<span id="cb4-32"></span>
<span id="cb4-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S4 (PORT) -&gt; {S6=NED, S7=LR recurrence, S8=death}</span></span>
<span id="cb4-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># After PORT, 3-yr OS ~71% (95% CI 67-75%) in adjuvant PORT cohort (Hosni et al. 2019, PMID: 30244160)</span></span>
<span id="cb4-35">jump_P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.67</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.23</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>)</span>
<span id="cb4-36"></span>
<span id="cb4-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S5 (POCRT) -&gt; {S6=NED, S7=LR recurrence, S8=death}</span></span>
<span id="cb4-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># After POCRT, locoregional control ~60%; Stage II mortality revised down to 10% (Bernier 2004, Cooper 2004; Stage II subgroup)</span></span>
<span id="cb4-39">jump_P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.60</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>)</span>
<span id="cb4-40"></span>
<span id="cb4-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S6 (NED) -&gt; {S7=LR recurrence, S8=death}</span></span>
<span id="cb4-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ~75% leave NED via recurrence vs other-cause death (SEER Stage II split)</span></span>
<span id="cb4-43">jump_P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb4-44"></span>
<span id="cb4-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S7 (LR Recurrence) -&gt; {S6=salvage success, S8=death}</span></span>
<span id="cb4-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Salvage success 0.43 / mortality 0.57: Lee et al. 2024 pooled 5-yr OS = 43% (n=2069); revised from c(0.25, 0.75) — previous value over-estimated post-recurrence mortality</span></span>
<span id="cb4-47">jump_P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.43</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.57</span>)</span>
<span id="cb4-48"></span>
<span id="cb4-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Mean sojourn times (months) ───────────────────────────────────────────────</span></span>
<span id="cb4-50">mean_sojourn <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb4-51">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S1  Surgery: weighted mean accounting for real-world S-PORT delays ~8-9 wks (Correia 2026; Dayan 2023; Graboyes 2017)</span></span>
<span id="cb4-52">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S2  DefinitiveRT: RT course (1.5 mo) + post-RT response assessment window &gt;=12 wks (NCCN HNC Guidelines; Mehanna et al. 2016 [PET-NECK] PMID 26958921)</span></span>
<span id="cb4-53">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.0</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S3  PostOpSurveillance: weighted mean to event (NED@24mo x0.74 + recur@18mo x0.18 + death@14mo x0.08) = 22 mo (Blatt 2022; Brands 2019; Ord 2006)</span></span>
<span id="cb4-54">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S4  PORT: ~6-week adjuvant RT + recovery — supported, unchanged</span></span>
<span id="cb4-55">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S5  POCRT: ~6-week concurrent chemoRT + extended recovery — supported, unchanged</span></span>
<span id="cb4-56">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">120.0</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S6  NED: calibrated to SEER ~20% 5-yr recurrence; exit rate 0.0083/mo -&gt; mean 120 mo (SEER localized OCC; Brands 2019; Luryi 2014)</span></span>
<span id="cb4-57">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.5</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S7  LR Recurrence: median OS ~12-18 mo all-comers (Liu 2007; Contrera 2022) — within supported range, unchanged</span></span>
<span id="cb4-58">  <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S8  Death (absorbing)</span></span>
<span id="cb4-59">)</span>
<span id="cb4-60"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(mean_sojourn) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb4-61"></span>
<span id="cb4-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># == </span><span class="re">END</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> CLINICIAN INPUTS ======================================================</span></span>
<span id="cb4-63"></span>
<span id="cb4-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Derive Q_base from clinician inputs ───────────────────────────────────────</span></span>
<span id="cb4-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Q[i,j] = jump_P[i,j] / mean_sojourn[i]</span></span>
<span id="cb4-66">Q_base <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb4-67"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>) {</span>
<span id="cb4-68">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(jump_P[i, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) {</span>
<span id="cb4-69">    Q_base[i, j] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> jump_P[i, j] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mean_sojourn[i]</span>
<span id="cb4-70">  }</span>
<span id="cb4-71">}</span>
<span id="cb4-72"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(Q_base) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(Q_base)</span></code></pre></div></div>
</details>
</div>
<div class="columns">
<div class="column" style="width:45%;">
<section id="jump-chain-transition-probabilities" class="level5">
<h5 class="anchored" data-anchor-id="jump-chain-transition-probabilities">Jump Chain Transition Probabilities</h5>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(jump_P)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>   S1 S2    S3   S4   S5   S6   S7    S8
S1  0  0 0.585 0.25 0.15 0.00 0.00 0.015
S2  0  0 0.000 0.00 0.00 0.65 0.28 0.070
S3  0  0 0.000 0.00 0.00 0.74 0.18 0.080
S4  0  0 0.000 0.00 0.00 0.67 0.23 0.100
S5  0  0 0.000 0.00 0.00 0.60 0.30 0.100
S6  0  0 0.000 0.00 0.00 0.00 0.75 0.250
S7  0  0 0.000 0.00 0.00 0.43 0.00 0.570
S8  0  0 0.000 0.00 0.00 0.00 0.00 0.000</code></pre>
</div>
</div>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="mean-sojourn-times-and-implied-rates" class="level5">
<h5 class="anchored" data-anchor-id="mean-sojourn-times-and-implied-rates">Mean Sojourn Times and Implied Rates</h5>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb7-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#State        = paste0("S", 1:7),</span></span>
<span id="cb7-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Label        =</span> state_labels_ii[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>],</span>
<span id="cb7-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Mean_sojourn =</span> mean_sojourn[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>],</span>
<span id="cb7-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Total_rate   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(Q_base)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb7-6">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                   Label Mean_sojourn Total_rate
S1               Surgery          1.5     0.6667
S2          DefinitiveRT          3.0     0.3333
S3    PostOpSurveillance         22.0     0.0455
S4       AdjuvantRT_PORT          3.0     0.3333
S5 AdjuvantChemoRT_POCRT          4.0     0.2500
S6                   NED        120.0     0.0083
S7         LR_Recurrence         12.5     0.0800</code></pre>
</div>
</div>
</section>
</div>
</div>
</section>
<section id="synthetic-data" class="level3">
<h3 class="anchored" data-anchor-id="synthetic-data">Synthetic Data</h3>
<p>This code block constructs the synthetic data and prints out the first few rows of the synthetic data set.</p>
<div class="cell">
<details class="code-fold">
<summary>show code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Patient-specific Q (covariates scale recurrence &amp; mortality rates) ─────────</span></span>
<span id="cb9-2">make_Q <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(age_i, size_i) {</span>
<span id="cb9-3">  Q <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Q_base</span>
<span id="cb9-4">  age_eff  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.025</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (age_i  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>))   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># older  -&gt; higher mortality</span></span>
<span id="cb9-5">  size_eff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.180</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (size_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># larger -&gt; higher recurrence</span></span>
<span id="cb9-6">  Q[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Q_base[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> size_eff        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PostOpSurv -&gt; LR Recurrence</span></span>
<span id="cb9-7">  Q[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Q_base[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> size_eff <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> age_eff</span>
<span id="cb9-8">  Q[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Q_base[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> age_eff</span>
<span id="cb9-9">  Q[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Q_base[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> age_eff</span>
<span id="cb9-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(Q) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb9-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(Q) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(Q)</span>
<span id="cb9-12">  Q</span>
<span id="cb9-13">}</span>
<span id="cb9-14"></span>
<span id="cb9-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Simulate one patient's full CTMC path ─────────────────────────────────────</span></span>
<span id="cb9-16">sim_ctmc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(init_s, Q_i, max_t) {</span>
<span id="cb9-17">  t_vec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>; s_vec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> init_s; s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> init_s; t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb9-18">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> max_t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>L) {</span>
<span id="cb9-19">    total_rate <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Q_i[s, s]</span>
<span id="cb9-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (total_rate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb9-21">    t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rexp</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L, total_rate)</span>
<span id="cb9-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> max_t) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb9-23">    probs    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(Q_i[s, ], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb9-24">    probs[s] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb9-25">    s        <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample.int</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> probs)</span>
<span id="cb9-26">    t_vec    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(t_vec, t); s_vec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(s_vec, s)</span>
<span id="cb9-27">  }</span>
<span id="cb9-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t_vec, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">states =</span> s_vec)</span>
<span id="cb9-29">}</span>
<span id="cb9-30"></span>
<span id="cb9-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># State at a given observation time (last known state)</span></span>
<span id="cb9-32">state_at <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(traj, obs_t) {</span>
<span id="cb9-33">  traj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>states[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(traj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>times <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> obs_t))]</span>
<span id="cb9-34">}</span>
<span id="cb9-35"></span>
<span id="cb9-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Generate panel data ────────────────────────────────────────────────────────</span></span>
<span id="cb9-37">records <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vector</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"list"</span>, N_PATIENTS)</span>
<span id="cb9-38"></span>
<span id="cb9-39"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(N_PATIENTS)) {</span>
<span id="cb9-40">  init_s  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (treatment[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>) <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>L</span>
<span id="cb9-41">  Q_i     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_Q</span>(age[i], tumor_size_cm[i])</span>
<span id="cb9-42">  traj    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sim_ctmc</span>(init_s, Q_i, MAX_TIME)</span>
<span id="cb9-43"></span>
<span id="cb9-44">  jitter  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(obs_base) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span>
<span id="cb9-45">  obs_t   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, obs_base <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> jitter)))</span>
<span id="cb9-46"></span>
<span id="cb9-47">  death_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>L <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> traj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>states)</span>
<span id="cb9-48">    traj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>times[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(traj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>states <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>L)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L]]</span>
<span id="cb9-49">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span></span>
<span id="cb9-50"></span>
<span id="cb9-51">  obs_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> obs_t[obs_t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> death_t]</span>
<span id="cb9-52">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.finite</span>(death_t) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> death_t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> MAX_TIME)</span>
<span id="cb9-53">    obs_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(obs_t, death_t)</span>
<span id="cb9-54"></span>
<span id="cb9-55">  obs_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(obs_t, state_at, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">traj =</span> traj)</span>
<span id="cb9-56"></span>
<span id="cb9-57">  records[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb9-58">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id            =</span> i,</span>
<span id="cb9-59">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time          =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(obs_t, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb9-60">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state         =</span> obs_s,</span>
<span id="cb9-61">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age           =</span> age[i],</span>
<span id="cb9-62">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tumor_size_cm =</span> tumor_size_cm[i],</span>
<span id="cb9-63">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">DOI_mm        =</span> DOI_mm[i],</span>
<span id="cb9-64">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">treatment     =</span> treatment[i],</span>
<span id="cb9-65">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb9-66">  )</span>
<span id="cb9-67">}</span>
<span id="cb9-68"></span>
<span id="cb9-69">oscc2_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, records)</span>
<span id="cb9-70"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(oscc2_data) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb9-71">oscc2_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state_label <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> state_labels_ii[oscc2_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state]</span>
<span id="cb9-72"></span>
<span id="cb9-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save synthetic data</span></span>
<span id="cb9-74"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.csv</span>(oscc2_data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OSCC_HE_Synthetic_Data.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb9-75"></span>
<span id="cb9-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First 10 rows</span></span>
<span id="cb9-77"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(oscc2_data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>   id  time state age tumor_size_cm DOI_mm treatment        state_label
1   1  0.00     1  59           2.2    7.8   Surgery            Surgery
2   1  1.38     1  59           2.2    7.8   Surgery            Surgery
3   1  1.82     1  59           2.2    7.8   Surgery            Surgery
4   1  3.01     1  59           2.2    7.8   Surgery            Surgery
5   1  6.04     3  59           2.2    7.8   Surgery PostOpSurveillance
6   1  8.50     3  59           2.2    7.8   Surgery PostOpSurveillance
7   1 11.61     3  59           2.2    7.8   Surgery PostOpSurveillance
8   1 17.91     3  59           2.2    7.8   Surgery PostOpSurveillance
9   1 24.01     3  59           2.2    7.8   Surgery PostOpSurveillance
10  1 35.86     6  59           2.2    7.8   Surgery                NED</code></pre>
</div>
</div>
<p>Note that two covariates: age of patient and tumor size are included in the data ane encoded in the <code>make_Q</code> function used to create the synthetic data as follows:</p>
<ul>
<li><code>age_eff  &lt;- exp(0.025 * (age_i  - 60))   # older  -&gt; higher mortality</code></li>
<li><code>size_eff &lt;- exp(0.180 * (size_i -  3.0))  # larger -&gt; higher recurrence</code></li>
</ul>
<hr>
</section>
<section id="summary-of-data-set" class="level3">
<h3 class="anchored" data-anchor-id="summary-of-data-set">Summary of Data Set</h3>
<p>This table shows some of the meta-data describing the synthetic data set.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code for cohort summary</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Collect components for footnotes</span></span>
<span id="cb11-2">trt_counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(treatment)</span>
<span id="cb11-3">died <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> oscc2_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-4">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(state <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-5">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>()</span>
<span id="cb11-7"></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Main table: state visit counts</span></span>
<span id="cb11-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">State =</span> oscc2_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state_label)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">table.font.size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">px</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols_label</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">State =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Freq =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observations"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_source_note</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb11-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Dataset:** "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(oscc2_data), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" observations; "</span>,</span>
<span id="cb11-15">    N_PATIENTS, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" patients"</span></span>
<span id="cb11-16">  ))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_source_note</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb11-18">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Treatment:** Surgery = "</span>, trt_counts[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>],</span>
<span id="cb11-19">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"; DefinitiveRT = "</span>, trt_counts[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DefinitiveRT"</span>]</span>
<span id="cb11-20">  ))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_source_note</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb11-22">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Mortality:** "</span>, died, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" / "</span>, N_PATIENTS,</span>
<span id="cb11-23">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" patients reached Death ("</span>,</span>
<span id="cb11-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> died <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N_PATIENTS, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%)"</span></span>
<span id="cb11-25">  )))</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="paewahtyoq" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#paewahtyoq table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#paewahtyoq thead, #paewahtyoq tbody, #paewahtyoq tfoot, #paewahtyoq tr, #paewahtyoq td, #paewahtyoq th {
  border-style: none;
}

#paewahtyoq p {
  margin: 0;
  padding: 0;
}

#paewahtyoq .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 12px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#paewahtyoq .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#paewahtyoq .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#paewahtyoq .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#paewahtyoq .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#paewahtyoq .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#paewahtyoq .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#paewahtyoq .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#paewahtyoq .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#paewahtyoq .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#paewahtyoq .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#paewahtyoq .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#paewahtyoq .gt_spanner_row {
  border-bottom-style: hidden;
}

#paewahtyoq .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#paewahtyoq .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#paewahtyoq .gt_from_md > :first-child {
  margin-top: 0;
}

#paewahtyoq .gt_from_md > :last-child {
  margin-bottom: 0;
}

#paewahtyoq .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#paewahtyoq .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#paewahtyoq .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#paewahtyoq .gt_row_group_first td {
  border-top-width: 2px;
}

#paewahtyoq .gt_row_group_first th {
  border-top-width: 2px;
}

#paewahtyoq .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#paewahtyoq .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#paewahtyoq .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#paewahtyoq .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#paewahtyoq .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#paewahtyoq .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#paewahtyoq .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#paewahtyoq .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#paewahtyoq .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#paewahtyoq .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#paewahtyoq .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#paewahtyoq .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#paewahtyoq .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#paewahtyoq .gt_left {
  text-align: left;
}

#paewahtyoq .gt_center {
  text-align: center;
}

#paewahtyoq .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#paewahtyoq .gt_font_normal {
  font-weight: normal;
}

#paewahtyoq .gt_font_bold {
  font-weight: bold;
}

#paewahtyoq .gt_font_italic {
  font-style: italic;
}

#paewahtyoq .gt_super {
  font-size: 65%;
}

#paewahtyoq .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#paewahtyoq .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#paewahtyoq .gt_indent_1 {
  text-indent: 5px;
}

#paewahtyoq .gt_indent_2 {
  text-indent: 10px;
}

#paewahtyoq .gt_indent_3 {
  text-indent: 15px;
}

#paewahtyoq .gt_indent_4 {
  text-indent: 20px;
}

#paewahtyoq .gt_indent_5 {
  text-indent: 25px;
}

#paewahtyoq .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#paewahtyoq div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="State" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">State</th>
<th id="Freq" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">Observations</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_center" headers="State">AdjuvantChemoRT_POCRT</td>
<td class="gt_row gt_right" headers="Freq">228</td>
</tr>
<tr class="even">
<td class="gt_row gt_center" headers="State">AdjuvantRT_PORT</td>
<td class="gt_row gt_right" headers="Freq">333</td>
</tr>
<tr class="odd">
<td class="gt_row gt_center" headers="State">Death</td>
<td class="gt_row gt_right" headers="Freq">370</td>
</tr>
<tr class="even">
<td class="gt_row gt_center" headers="State">DefinitiveRT</td>
<td class="gt_row gt_right" headers="Freq">570</td>
</tr>
<tr class="odd">
<td class="gt_row gt_center" headers="State">LR_Recurrence</td>
<td class="gt_row gt_right" headers="Freq">767</td>
</tr>
<tr class="even">
<td class="gt_row gt_center" headers="State">NED</td>
<td class="gt_row gt_right" headers="Freq">4407</td>
</tr>
<tr class="odd">
<td class="gt_row gt_center" headers="State">PostOpSurveillance</td>
<td class="gt_row gt_right" headers="Freq">2381</td>
</tr>
<tr class="even">
<td class="gt_row gt_center" headers="State">Surgery</td>
<td class="gt_row gt_right" headers="Freq">1578</td>
</tr>
</tbody><tfoot>
<tr class="gt_sourcenotes odd">
<td colspan="2" class="gt_sourcenote"><strong>Dataset:</strong> 10634 observations; 1000 patients</td>
</tr>
<tr class="gt_sourcenotes even">
<td colspan="2" class="gt_sourcenote"><strong>Treatment:</strong> Surgery = 794; DefinitiveRT = 206</td>
</tr>
<tr class="gt_sourcenotes odd">
<td colspan="2" class="gt_sourcenote"><strong>Mortality:</strong> 370 / 1000 patients reached Death (37%)</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
</section>
<section id="first-patient-trajectory" class="level3">
<h3 class="anchored" data-anchor-id="first-patient-trajectory">First Patient Trajectory</h3>
<p>This data frame shows the complete trajectory of sates visited for the first synthetic patient.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code for trajectory</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">oscc2_data[oscc2_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb12-2">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state_label"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treatment"</span>,</span>
<span id="cb12-3">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tumor_size_cm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DOI_mm"</span>)]</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>   id  time state        state_label treatment age tumor_size_cm DOI_mm
1   1  0.00     1            Surgery   Surgery  59           2.2    7.8
2   1  1.38     1            Surgery   Surgery  59           2.2    7.8
3   1  1.82     1            Surgery   Surgery  59           2.2    7.8
4   1  3.01     1            Surgery   Surgery  59           2.2    7.8
5   1  6.04     3 PostOpSurveillance   Surgery  59           2.2    7.8
6   1  8.50     3 PostOpSurveillance   Surgery  59           2.2    7.8
7   1 11.61     3 PostOpSurveillance   Surgery  59           2.2    7.8
8   1 17.91     3 PostOpSurveillance   Surgery  59           2.2    7.8
9   1 24.01     3 PostOpSurveillance   Surgery  59           2.2    7.8
10  1 35.86     6                NED   Surgery  59           2.2    7.8
11  1 47.91     6                NED   Surgery  59           2.2    7.8
12  1 59.89     6                NED   Surgery  59           2.2    7.8</code></pre>
</div>
</div>
<hr>
</section>
</section>
<section id="survival-analysis" class="level2">
<h2 class="anchored" data-anchor-id="survival-analysis">Survival Analysis</h2>
<section id="overall-survival" class="level3">
<h3 class="anchored" data-anchor-id="overall-survival">Overall Survival</h3>
<p>This next plot shows overall survival (Kaplan-Meier) curves for patients in both treatment arms. Note that surgery exhibits a slightly better overall survival curve (Approximately 73% vs approximately 65% at 5 years). However, both curves show very good five year survival probabilities.</p>
<p>Three external benchmark points are overlaid on the Kaplan–Meier curves as filled diamonds.</p>
<ul>
<li>Siegel et al.&nbsp;(2024) report approximately 84% five-year <em>relative</em> survival for localised oral cavity cancer in SEER (diagnoses 2013–2019); adjusting for background mortality at a median age of approximately 62 years using the SSA 2021 Period Life Table yields an estimated absolute five-year OS of ~77%, shown here against the Surgery arm curve. See the 5 year adjustment note below.</li>
<li>Hosni et al.&nbsp;(2019) observed a three-year OS of 71% (95% CI 67–75%) in 601 OSCC patients receiving adjuvant PORT at Princess Margaret Cancer Centre, also plotted against the Surgery arm.</li>
<li>Sher et al.&nbsp;(2011) reported a two-year OS of 63% in 12 oral cavity patients treated with definitive IMRT at Dana-Farber Cancer Institute, shown against the Definitive RT arm.</li>
</ul>
<p>While not validating the survival curves, the benchmark points indicate that they are a plausible starting point for analysis.</p>
<div class="cell">
<details class="code-fold">
<summary>Kaplan-Meier survival curves</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(survival)</span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(survminer)</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One row per patient: time to death or end of follow-up</span></span>
<span id="cb14-5">km_input <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> oscc2_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-6">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(id, treatment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-7">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb14-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(time),</span>
<span id="cb14-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">event =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">any</span>(state <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)),</span>
<span id="cb14-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb14-11">  )</span>
<span id="cb14-12"></span>
<span id="cb14-13">km_fit_2arm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">survfit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Surv</span>(time, event) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> treatment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> km_input)</span>
<span id="cb14-14"></span>
<span id="cb14-15">km_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsurvplot</span>(</span>
<span id="cb14-16">  km_fit_2arm,</span>
<span id="cb14-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data        =</span> km_input,</span>
<span id="cb14-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.int    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb14-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">risk.table  =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb14-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D55E00"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0072B2"</span>),   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># DefinitiveRT=orange, Surgery=blue (alphabetical strata order)</span></span>
<span id="cb14-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.labs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Definitive RT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>),</span>
<span id="cb14-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (months)"</span>,</span>
<span id="cb14-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Survival probability"</span>,</span>
<span id="cb14-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title       =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Overall Survival — Stage II OSCC"</span>,</span>
<span id="cb14-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb14-26">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ggtheme     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span>
<span id="cb14-27">)</span>
<span id="cb14-28"></span>
<span id="cb14-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Overlay external benchmark points (diamonds) on the KM plot</span></span>
<span id="cb14-30">km_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> km_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"point"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.77</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb14-32">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0072B2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.77</span>,</span>
<span id="cb14-34">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Siegel 2024</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(SEER ~77%)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"point"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.71</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb14-36">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0072B2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.71</span>,</span>
<span id="cb14-38">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hosni 2019</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(71% at 3 yr)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"point"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.63</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb14-40">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D55E00"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.63</span>,</span>
<span id="cb14-42">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sher 2011</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(63% at 2 yr)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>)</span>
<span id="cb14-43"></span>
<span id="cb14-44">km_plot</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-10-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/oscc-patient-model/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</section>
<section id="survival-by-age-and-tumour-size" class="level3">
<h3 class="anchored" data-anchor-id="survival-by-age-and-tumour-size">Survival by Age and Tumour Size</h3>
<p>This next panel of plots show the overall survival broken out by the covariates age and tumor size. Each panel indicates the four possible values of the covariates.</p>
<div class="cell">
<details class="code-fold">
<summary>KM curves stratified by age × tumour size</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb15-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb15-4"></span>
<span id="cb15-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── One row per patient with covariate categories ─────────────────────────────</span></span>
<span id="cb15-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Age split at 62 (simulation mean); tumour size split at 3 cm (Stage II midpoint)</span></span>
<span id="cb15-7">km_cov <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> oscc2_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-8">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(id, treatment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-9">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb15-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time          =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(time),</span>
<span id="cb15-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">event         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">any</span>(state <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)),</span>
<span id="cb15-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age           =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">first</span>(age),</span>
<span id="cb15-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tumor_size_cm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">first</span>(tumor_size_cm),</span>
<span id="cb15-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups       =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb15-15">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-16">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb15-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age_grp  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(age  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">62</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age &lt; 62"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age ≥ 62"</span>),</span>
<span id="cb15-18">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age &lt; 62"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age ≥ 62"</span>)),</span>
<span id="cb15-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size_grp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(tumor_size_cm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tumour &lt; 3 cm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tumour ≥ 3 cm"</span>),</span>
<span id="cb15-20">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tumour &lt; 3 cm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tumour ≥ 3 cm"</span>)),</span>
<span id="cb15-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">covariate_group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(</span>
<span id="cb15-22">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interaction</span>(age_grp, size_grp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>),</span>
<span id="cb15-23">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb15-24">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age &lt; 62, Tumour &lt; 3 cm"</span>,</span>
<span id="cb15-25">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age &lt; 62, Tumour ≥ 3 cm"</span>,</span>
<span id="cb15-26">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age ≥ 62, Tumour &lt; 3 cm"</span>,</span>
<span id="cb15-27">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age ≥ 62, Tumour ≥ 3 cm"</span></span>
<span id="cb15-28">      )</span>
<span id="cb15-29">    )</span>
<span id="cb15-30">  )</span>
<span id="cb15-31"></span>
<span id="cb15-32">pal4  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2166AC"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#92C5DE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D6604D"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#B2182B"</span>)</span>
<span id="cb15-33">labs4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age &lt; 62, Tumour &lt; 3 cm"</span>,</span>
<span id="cb15-34">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age &lt; 62, Tumour ≥ 3 cm"</span>,</span>
<span id="cb15-35">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age ≥ 62, Tumour &lt; 3 cm"</span>,</span>
<span id="cb15-36">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age ≥ 62, Tumour ≥ 3 cm"</span>)</span>
<span id="cb15-37"></span>
<span id="cb15-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Tidy survival estimates per arm ──────────────────────────────────────────</span></span>
<span id="cb15-39">tidy_arm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(arm, data) {</span>
<span id="cb15-40">  df  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(data, treatment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> arm)</span>
<span id="cb15-41">  fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">survfit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Surv</span>(time, event) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> covariate_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df)</span>
<span id="cb15-42">  broom<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.int =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-43">    dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb15-44">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">treatment =</span> arm,</span>
<span id="cb15-45">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">covariate_group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(</span>
<span id="cb15-46">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"covariate_group="</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, strata),</span>
<span id="cb15-47">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>covariate_group)</span>
<span id="cb15-48">      )</span>
<span id="cb15-49">    )</span>
<span id="cb15-50">}</span>
<span id="cb15-51"></span>
<span id="cb15-52">surv_tidy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(</span>
<span id="cb15-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy_arm</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>,      km_cov),</span>
<span id="cb15-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy_arm</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DefinitiveRT"</span>, km_cov)</span>
<span id="cb15-55">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-56">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">treatment =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(treatment,</span>
<span id="cb15-57">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DefinitiveRT"</span>),</span>
<span id="cb15-58">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery Arm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Definitive RT Arm"</span>)))</span>
<span id="cb15-59"></span>
<span id="cb15-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Two-panel KM plot ─────────────────────────────────────────────────────────</span></span>
<span id="cb15-61"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(surv_tidy, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> estimate,</span>
<span id="cb15-62">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> covariate_group,</span>
<span id="cb15-63">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group  =</span> covariate_group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_step</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>treatment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-66">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> pal4, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> labs4) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-67">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb15-68">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-69">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb15-71">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x      =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (months)"</span>,</span>
<span id="cb15-72">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y      =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Survival probability"</span>,</span>
<span id="cb15-73">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age / Tumour Size"</span>,</span>
<span id="cb15-74">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Overall Survival by Age and Tumour Size — Stage II OSCC"</span></span>
<span id="cb15-75">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-76">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-77">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb15-78">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb15-79">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"horizontal"</span>,</span>
<span id="cb15-80">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.text      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb15-81">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb15-82">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb15-83">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>)</span>
<span id="cb15-84">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-85">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-11-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/oscc-patient-model/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<hr>
</section>
<section id="what-the-subgroup-curves-show" class="level3">
<h3 class="anchored" data-anchor-id="what-the-subgroup-curves-show">What the subgroup curves show:</h3>
<ul>
<li><p><strong>Surgery arm</strong> (n = 794): age is the dominant predictor of survival. The youngest, smallest-tumour subgroup (Age &lt; 62, Tumour &lt; 3 cm; dark blue) reaches 74% at 5 years, while the youngest with larger tumours (Age &lt; 62, Tumour ≥ 3 cm; light blue) reaches 71% Among older patients (Age ≥ 62), those with smaller tumours (Tumour &lt; 3 cm; orange) fare worst at 58%, while those with larger tumours (dark red) reach 51%. Even if these counter intuitive orderings are due to sampling variation and not a more fundamental underlying cause they may indicate a limitation of the this particular synthetic data set.</p></li>
<li><p><strong>Definitive RT arm</strong> (n = 206): the four subgroups together contain only 206 patients, yielding wide confidence bands and coarse staircase steps; point estimates (69%, 68%, 55%, 51% for the dark-blue, light-blue, orange, and dark-red subgroups respectively) should be interpreted with caution. The broad pattern mirrors the Surgery arm in that older age is associated with lower survival, but the small-n instability makes subgroup comparisons across treatment arms unreliable.</p></li>
</ul>
</section>
<section id="progression-free-survival" class="level3">
<h3 class="anchored" data-anchor-id="progression-free-survival">Progression-Free Survival</h3>
<p>Progression-free survival (PFS) is defined here as the time from treatment start to the first occurrence of either locoregional recurrence (S7) or death (S8), whichever comes first. Patients who reached neither endpoint by month 60 are censored at their last observed time.</p>
<div class="cell">
<details class="code-fold">
<summary>Progression-free survival — Kaplan-Meier with at-risk table</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(survival)</span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(survminer)</span>
<span id="cb16-3"></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Build per-patient PFS endpoint ───────────────────────────────────────────</span></span>
<span id="cb16-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Event = first entry into LR_Recurrence (state 7) or Death (state 8)</span></span>
<span id="cb16-6">pfs_events <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> oscc2_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-7">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(state <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>L)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-8">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-9">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_min</span>(time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">with_ties =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-10">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-11">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pfs_time =</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pfs_event_type =</span> state_label)</span>
<span id="cb16-12"></span>
<span id="cb16-13">last_obs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> oscc2_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-14">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-15">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_max</span>(time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">with_ties =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-16">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-17">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">last_time =</span> time, treatment)</span>
<span id="cb16-18"></span>
<span id="cb16-19">pfs_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> last_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-20">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(pfs_events, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-21">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb16-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pfs_time  =</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coalesce</span>(pfs_time, last_time),</span>
<span id="cb16-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pfs_event =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(pfs_event_type))</span>
<span id="cb16-24">  )</span>
<span id="cb16-25"></span>
<span id="cb16-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Fit KM curves ─────────────────────────────────────────────────────────────</span></span>
<span id="cb16-27">km_pfs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">survfit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Surv</span>(pfs_time, pfs_event) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> treatment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> pfs_df)</span>
<span id="cb16-28">km_pfs_named <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> km_pfs</span>
<span id="cb16-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(km_pfs_named<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>strata) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Definitive RT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>)</span>
<span id="cb16-30"></span>
<span id="cb16-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Plot with at-risk table ───────────────────────────────────────────────────</span></span>
<span id="cb16-32"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsurvplot</span>(</span>
<span id="cb16-33">  km_pfs_named,</span>
<span id="cb16-34">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data              =</span> pfs_df,</span>
<span id="cb16-35">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette           =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D55E00"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0072B2"</span>),</span>
<span id="cb16-36">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.int          =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb16-37">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.int.alpha    =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>,</span>
<span id="cb16-38">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size              =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>,</span>
<span id="cb16-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">risk.table        =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb16-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">risk.table.col    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"strata"</span>,</span>
<span id="cb16-41">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">risk.table.height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>,</span>
<span id="cb16-42">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tables.theme      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cleantable</span>(),</span>
<span id="cb16-43">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab              =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (months)"</span>,</span>
<span id="cb16-44">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab              =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Progression-free probability"</span>,</span>
<span id="cb16-45">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title             =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Progression-Free Survival — Stage II OSCC"</span>,</span>
<span id="cb16-46">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title      =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Treatment"</span>,</span>
<span id="cb16-47">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.labs       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Definitive RT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>),</span>
<span id="cb16-48">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend            =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb16-49">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">surv.median.line  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hv"</span>,</span>
<span id="cb16-50">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">break.time.by     =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>,</span>
<span id="cb16-51">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim              =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>),</span>
<span id="cb16-52">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim              =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb16-53">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ggtheme           =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>),</span>
<span id="cb16-54">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontsize          =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb16-55">)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-13-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/oscc-patient-model/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<section id="pfs-at-key-timepoints" class="level4">
<h4 class="anchored" data-anchor-id="pfs-at-key-timepoints">PFS at key timepoints</h4>
<div class="cell">
<details class="code-fold">
<summary>PFS estimates at 12, 24, 36, 48, 60 months</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">tbl_fun <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(s) {</span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb17-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Treatment =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>strata),</span>
<span id="cb17-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Time      =</span> s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time,</span>
<span id="cb17-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">PFS       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>surv,  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb17-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Lower95   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb17-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Upper95   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb17-8">  )</span>
<span id="cb17-9">}</span>
<span id="cb17-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_fun</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(km_pfs_named, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-11">  knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb17-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Treatment"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PFS"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"95% CI lower"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"95% CI upper"</span>),</span>
<span id="cb17-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kaplan-Meier PFS estimates at annual checkpoints"</span></span>
<span id="cb17-14">  )</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Kaplan-Meier PFS estimates at annual checkpoints</caption>
<thead>
<tr class="header">
<th style="text-align: left;">Treatment</th>
<th style="text-align: right;">Month</th>
<th style="text-align: right;">PFS</th>
<th style="text-align: right;">95% CI lower</th>
<th style="text-align: right;">95% CI upper</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Definitive RT</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">0.631</td>
<td style="text-align: right;">0.569</td>
<td style="text-align: right;">0.701</td>
</tr>
<tr class="even">
<td style="text-align: left;">Definitive RT</td>
<td style="text-align: right;">24</td>
<td style="text-align: right;">0.573</td>
<td style="text-align: right;">0.509</td>
<td style="text-align: right;">0.645</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Definitive RT</td>
<td style="text-align: right;">36</td>
<td style="text-align: right;">0.544</td>
<td style="text-align: right;">0.480</td>
<td style="text-align: right;">0.616</td>
</tr>
<tr class="even">
<td style="text-align: left;">Definitive RT</td>
<td style="text-align: right;">48</td>
<td style="text-align: right;">0.519</td>
<td style="text-align: right;">0.455</td>
<td style="text-align: right;">0.592</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Definitive RT</td>
<td style="text-align: right;">60</td>
<td style="text-align: right;">0.449</td>
<td style="text-align: right;">0.386</td>
<td style="text-align: right;">0.523</td>
</tr>
<tr class="even">
<td style="text-align: left;">Surgery</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">0.783</td>
<td style="text-align: right;">0.755</td>
<td style="text-align: right;">0.813</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Surgery</td>
<td style="text-align: right;">24</td>
<td style="text-align: right;">0.683</td>
<td style="text-align: right;">0.651</td>
<td style="text-align: right;">0.716</td>
</tr>
<tr class="even">
<td style="text-align: left;">Surgery</td>
<td style="text-align: right;">36</td>
<td style="text-align: right;">0.617</td>
<td style="text-align: right;">0.584</td>
<td style="text-align: right;">0.652</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Surgery</td>
<td style="text-align: right;">48</td>
<td style="text-align: right;">0.564</td>
<td style="text-align: right;">0.531</td>
<td style="text-align: right;">0.600</td>
</tr>
<tr class="even">
<td style="text-align: left;">Surgery</td>
<td style="text-align: right;">60</td>
<td style="text-align: right;">0.511</td>
<td style="text-align: right;">0.477</td>
<td style="text-align: right;">0.548</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="what-the-pfs-curves-show" class="level4">
<h4 class="anchored" data-anchor-id="what-the-pfs-curves-show">What the PFS curves show</h4>
<ul>
<li>Surgery achieves notably higher early PFS: approximately 78% at 12 months versus 63% for Definitive RT — a 15 percentage point difference driven by the higher probability of early locoregional treatment failure in the RT arm.</li>
<li>The gap narrows progressively over the 60-month horizon, with Surgery at 51% and Definitive RT at 45% at month 60.</li>
<li>The wider confidence band on the Definitive RT curve reflects the smaller sample size in that arm.</li>
</ul>
</section>
</section>
</section>
<section id="time-patients-spend-in-each-health-state" class="level2">
<h2 class="anchored" data-anchor-id="time-patients-spend-in-each-health-state">Time Patients Spend in Each Health State</h2>
<p>The total times that patients are expected to spend in each state over a 60-month horizon drive the economics analysis. The ability to make this calculation is the primary motivation for modeling the treatment regimen as a continuous time Markov chain.</p>
<div class="cell">
<details class="code-fold">
<summary>State occupancy function</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(expm)</span>
<span id="cb18-2"></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adapted from plot_msm_state_occupancy() (Breast_Cancer_v3.qmd).</span></span>
<span id="cb18-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Accepts Q_base directly instead of a fitted msm model, and accepts</span></span>
<span id="cb18-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a start_state argument so the two treatment arms can be compared.</span></span>
<span id="cb18-6">plot_state_occupancy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(Q,</span>
<span id="cb18-7">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_state  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb18-8">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tmax         =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>,</span>
<span id="cb18-9">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tstep        =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb18-10">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state_labels =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb18-11">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State Occupancy Probabilities"</span>) {</span>
<span id="cb18-12">  n_states    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Q)</span>
<span id="cb18-13">  start_probs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, n_states)</span>
<span id="cb18-14">  start_probs[start_state] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb18-15">  time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, tmax, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> tstep)</span>
<span id="cb18-16"></span>
<span id="cb18-17">  occ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> n_states)</span>
<span id="cb18-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(occ) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n_states))</span>
<span id="cb18-19">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(time))</span>
<span id="cb18-20">    occ[i, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> start_probs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expm</span>(Q <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> time[i])</span>
<span id="cb18-21"></span>
<span id="cb18-22">  df_long <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(occ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-23">    dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-24">    tidyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prob"</span>)</span>
<span id="cb18-25"></span>
<span id="cb18-26">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(state_labels))</span>
<span id="cb18-27">    df_long<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(df_long<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state,</span>
<span id="cb18-28">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n_states)),</span>
<span id="cb18-29">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> state_labels)</span>
<span id="cb18-30"></span>
<span id="cb18-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df_long, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> prob, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> state)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-32">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-33">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (months)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Probability"</span>,</span>
<span id="cb18-34">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> title, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb18-36">}</span>
<span id="cb18-37"></span>
<span id="cb18-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected time in each state: trapezoid integral of P(t) over 0-60 months</span></span>
<span id="cb18-39">compute_state_time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(Q, start_state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tmax =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tstep =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) {</span>
<span id="cb18-40">  n_states    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Q)</span>
<span id="cb18-41">  start_probs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, n_states)</span>
<span id="cb18-42">  start_probs[start_state] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb18-43">  time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, tmax, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> tstep)</span>
<span id="cb18-44">  occ  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> n_states)</span>
<span id="cb18-45">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(time))</span>
<span id="cb18-46">    occ[i, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> start_probs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expm</span>(Q <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> time[i])</span>
<span id="cb18-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(occ, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p)</span>
<span id="cb18-48">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (p[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(p)]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb18-49">}</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Expected state time by arm</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">time_surg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compute_state_time</span>(Q_base, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_state =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb19-2">time_def  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compute_state_time</span>(Q_base, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_state =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb19-3"></span>
<span id="cb19-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb19-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">State      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb19-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Label      =</span> state_labels_ii,</span>
<span id="cb19-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Surgery_mo =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(time_surg, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb19-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">DefRT_mo   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(time_def,  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb19-9">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>  State                 Label Surgery_mo DefRT_mo
1    S1               Surgery       1.51     0.00
2    S2          DefinitiveRT       0.00     3.01
3    S3    PostOpSurveillance      11.96     0.00
4    S4       AdjuvantRT_PORT       0.75     0.00
5    S5 AdjuvantChemoRT_POCRT       0.60     0.00
6    S6                   NED      28.83    35.54
7    S7         LR_Recurrence       4.09     5.67
8    S8                 Death      12.26    15.78</code></pre>
</div>
</div>
<p>The state occupancy plots show the probability of being in each state as time progresses. For both plots, focusing on a particular time point gives a gives an estimate of a patient’s probable health state.</p>
<div class="columns">
<div class="column" style="width:45%;">
<section id="state-occupancy-surgery" class="level4">
<h4 class="anchored" data-anchor-id="state-occupancy-surgery">State Occupancy: Surgery</h4>
<div class="cell">
<details class="code-fold">
<summary>Surgery occupancy plot</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_state_occupancy</span>(</span>
<span id="cb21-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q            =</span> Q_base,</span>
<span id="cb21-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_state  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb21-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state_labels =</span> state_labels_ii,</span>
<span id="cb21-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State Occupancy Probabilities — Surgery Arm"</span></span>
<span id="cb21-6">)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-18-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/oscc-patient-model/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="state-occupancy-definitive-rt" class="level4">
<h4 class="anchored" data-anchor-id="state-occupancy-definitive-rt">State Occupancy: Definitive RT</h4>
<div class="cell">
<details class="code-fold">
<summary>Definitive RT occupancy plot</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_state_occupancy</span>(</span>
<span id="cb22-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q            =</span> Q_base,</span>
<span id="cb22-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_state  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb22-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state_labels =</span> state_labels_ii,</span>
<span id="cb22-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State Occupancy Probabilities — Definitive RT Arm"</span></span>
<span id="cb22-6">)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-19-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/oscc-patient-model/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</section>
</div>
</div>
<hr>
</section>
<section id="health-economic-evaluation-patient-perspective" class="level2">
<h2 class="anchored" data-anchor-id="health-economic-evaluation-patient-perspective">Health Economic Evaluation — Patient Perspective</h2>
<p>The health Economics model presented uses QALYs calculated with with EQ-5D utilities. Although these utilities are abstractions they do provide a measure, however imperfect, of how patients treated for OSCC have rated the ensemble of experiences related to each possible health state.</p>
<p>The model does not to calculate financial costs for the following reasons:</p>
<ol type="1">
<li>In the United States, there is no practical way to estimate the final direct cost to patients who are covered by private insurance or Medicare. The U.S. health care system is set up so that the final cost of treatments borne by patients are only made known after the treatments have be received.</li>
<li>For patients with adequate insurance it is not likely cost would not be directly relevant to patient decisions.</li>
<li>For patients without insurance, the financial burden of treatment would dominate all other considerations.</li>
<li>From a patient’s point of view, there is merit in clarifying the quality of life to be expected after undergoing a particular treatment. For example, it is not difficult to imagine that elderly patients would choose palliative care over a course of chemotherapy.</li>
</ol>
<section id="quality-adjusted-life-years-qalys" class="level3">
<h3 class="anchored" data-anchor-id="quality-adjusted-life-years-qalys">Quality-Adjusted Life Years (QALYs)</h3>
<p>A Quality-Adjusted Life Year (QALY) combines the quantity and quality of life into a single metric. One QALY represents one year lived in perfect health; a year spent in a health state with diminished quality counts for less than one QALY in proportion to that state’s utility weight.</p>
<p>In this model, QALYs are calculated in three steps:</p>
<ol type="1">
<li><p><strong>Assign utility weights:</strong> Each of the eight health states is assigned an EQ-5D utility value between 0 (death) and 1 (perfect health), drawn from peer-reviewed literature on head-and-neck cancer patients.</p></li>
<li><p><strong>Estimate time in each state:</strong> The continuous-time Markov chain (CTMC) simulation provides the expected number of months the cohort spends in each state over a 60-month horizon, separately for the Surgery arm and the Definitive Radiation Therapy arm.</p></li>
<li><p><strong>Compute utility-weighted life years:</strong> For each state, the time (in months) is multiplied by its utility weight and the products are summed across all states. Dividing by 12 converts the result to years:</p></li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BTotal%20QALYs%7D%20=%20%5Cfrac%7B1%7D%7B12%7D%20%5Csum_%7Bi=1%7D%5E%7B8%7D%20%5Ctext%7Btime%7D_i%20%5Ctimes%20u_i%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Btime%7D_i"> is the expected months in state <img src="https://latex.codecogs.com/png.latex?i"> and <img src="https://latex.codecogs.com/png.latex?u_i"> is its EQ-5D utility. The two treatment arms are evaluated on the same 60-month horizon, allowing a direct QALY comparison from the patient’s perspective.</p>
</section>
<section id="utility-values" class="level3">
<h3 class="anchored" data-anchor-id="utility-values">Utility Values</h3>
<p>Health state utility values map each model state to a preference-based quality-of-life weight on the 0–1 scale (0 = death, 1 = perfect health). The weights below are EQ-5D-based defaults drawn from the published literature. S6 (NED) carries the highest utility among living states and serves as the reference; a NED-normalized column (NED = 1.00) is included for sensitivity work.</p>
<p>The sources for the utility values are provided as comments in the code below. Complete references are provided in the Reference section at the end.</p>
<div class="cell">
<details class="code-fold">
<summary>Define and display utility weights</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Health state utility values (EQ-5D scale, 0 = death, 1 = perfect health).</span></span>
<span id="cb23-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S6 (NED) is the reference (highest) living state.</span></span>
<span id="cb23-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sources: </span></span>
<span id="cb23-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S1 de Almeida et al. 2014; Noel et al. 2015</span></span>
<span id="cb23-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S2 Sprave et al. 2022 (during-RT EQ-5D ~0.83, n=366); Truong/RTOG 0522 2017 retained for S5</span></span>
<span id="cb23-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S3 Govers et al. 2016</span></span>
<span id="cb23-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S4 Sprave et al. 2022 (during-RT EQ-5D ~0.83, n=366); Govers et al. 2016</span></span>
<span id="cb23-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S5 Truong et al. / RTOG 0522 2017 (primary); Sprave et al. 2022 adjuvant CRT data support ~0.83 but POCRT toxicity justifies retaining 0.775 — see inline note</span></span>
<span id="cb23-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S6 Noel et al. 2015 (reference state)</span></span>
<span id="cb23-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S7 Meregaglia &amp; Cairns 2017 (systematic review confirming evidence gap; only patient EQ-5D for recurrence found is median 0.70, del Barco et al. 2016, palliative/metastatic context); 0.55 is modeller's assumption</span></span>
<span id="cb23-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S8 Convention (absorbing state)</span></span>
<span id="cb23-12">utility <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb23-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S1 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.60</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Surgery: acute peri-operative period</span></span>
<span id="cb23-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S2 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.83</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># DefinitiveRT: during 6-7 week RT course (Sprave et al. 2022, mean EQ-5D at RT completion = 0.830, n=366)</span></span>
<span id="cb23-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S3 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.72</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PostOpSurveillance: recovery phase</span></span>
<span id="cb23-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S4 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.83</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PORT: adjuvant RT toxicity (Sprave et al. 2022, mean EQ-5D at RT completion = 0.830, n=366)</span></span>
<span id="cb23-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S5 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.775</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># POCRT: concurrent chemoRT (revised from 0.60). Primary source: Truong/RTOG 0522 3-month EQ-5D proxy (CIS arm 0.78, CET/CIS arm 0.77); end-of-treatment value collected but not reported in paper. Comparison: Sprave et al. (2022) adjuvant CRT cohort had baseline HI = 0.849 with no significant within-group change to RT completion, and CRT vs RT-alone did not differ at RT completion (p = 0.624); the adjuvant cohort data would support ~0.83. Value retained at 0.775 as a deliberate conservative estimate: POCRT involves high-dose cisplatin concurrent with post-surgical RT (higher toxicity than Sprave 2022 mixed RT cohort), and RTOG 0522 provides the only patient-reported utility from an actual CRT trial. A sensitivity analysis using 0.83 would narrow the Surgery vs. Def RT QALY difference by ~0.009 QALYs (0.598 months in S5).</span></span>
<span id="cb23-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S6 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.82</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NED: reference state (Noel 2015)</span></span>
<span id="cb23-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S7 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LR Recurrence: modeller's assumption (no directly reported mean EQ-5D for LR recurrence; Meregaglia &amp; Cairns 2017 confirms evidence gap; only available patient EQ-5D is median 0.70 from del Barco 2016 in palliative recurrent/metastatic HNC)</span></span>
<span id="cb23-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S8 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Death: absorbing state</span></span>
<span id="cb23-21">)</span>
<span id="cb23-22"></span>
<span id="cb23-23">df_utility <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb23-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">State        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb23-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Label        =</span> state_labels_ii,</span>
<span id="cb23-26">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Primary_Source =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb23-27">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"de Almeida (2014); Noel (2015)"</span>,</span>
<span id="cb23-28">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sprave (2022)"</span>,</span>
<span id="cb23-29">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Govers (2016)"</span>,</span>
<span id="cb23-30">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sprave (2022)"</span>,</span>
<span id="cb23-31">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Truong / RTOG 0522 (2017)"</span>,</span>
<span id="cb23-32">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Noel (2015)"</span>,</span>
<span id="cb23-33">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Modeller's assumption"</span>,</span>
<span id="cb23-34">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Convention"</span></span>
<span id="cb23-35">  ),</span>
<span id="cb23-36">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Utility_EQ5D =</span> utility,</span>
<span id="cb23-37">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Utility_NED1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(utility <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> utility[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S6"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb23-38">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">QALY_Surgery =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(time_surg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> utility <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb23-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">QALY_DefRT   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(time_def  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> utility <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb23-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">QALY_Diff    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>((time_surg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> time_def) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> utility <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb23-41">)</span>
<span id="cb23-42"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(df_utility) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb23-43"></span>
<span id="cb23-44">df_utility <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">table.font.size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">px</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols_label</span>(</span>
<span id="cb23-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">State        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State"</span>,</span>
<span id="cb23-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Label        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Health State"</span>,</span>
<span id="cb23-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Primary_Source =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Primary Source"</span>,</span>
<span id="cb23-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Utility_EQ5D =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EQ-5D Utility"</span>,</span>
<span id="cb23-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Utility_NED1 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NED-Normalised"</span>,</span>
<span id="cb23-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">QALY_Surgery =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QALYs — Surgery"</span>,</span>
<span id="cb23-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">QALY_DefRT   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QALYs — Definitive RT"</span>,</span>
<span id="cb23-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">QALY_Diff    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Difference (Surg − RT)"</span></span>
<span id="cb23-56">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_style</span>(</span>
<span id="cb23-58">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cell_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb23-59">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">locations =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cells_body</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> State <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S6"</span>)</span>
<span id="cb23-60">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_style</span>(</span>
<span id="cb23-62">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cell_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#27AE60"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb23-63">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">locations =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cells_body</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> QALY_Diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> QALY_Diff <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb23-64">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_style</span>(</span>
<span id="cb23-66">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cell_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C0392B"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb23-67">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">locations =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cells_body</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> QALY_Diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> QALY_Diff <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb23-68">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-69">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_spanner</span>(</span>
<span id="cb23-70">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Utility"</span>,</span>
<span id="cb23-71">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Utility_EQ5D, Utility_NED1)</span>
<span id="cb23-72">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-73">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_spanner</span>(</span>
<span id="cb23-74">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QALYs by Treatment Arm"</span>,</span>
<span id="cb23-75">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(QALY_Surgery, QALY_DefRT, QALY_Diff)</span>
<span id="cb23-76">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-77">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grand_summary_rows</span>(</span>
<span id="cb23-78">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(QALY_Surgery, QALY_DefRT, QALY_Diff),</span>
<span id="cb23-79">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fns      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(Total <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(.)),</span>
<span id="cb23-80">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fmt      =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt_number</span>(., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decimals =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb23-81">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-82">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_footnote</span>(</span>
<span id="cb23-83">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">footnote  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Modeller's assumption. No directly reported patient EQ-5D for LR recurrence exists; best available evidence is median 0.70 (del Barco et al. 2016, palliative/metastatic context). See Meregaglia &amp; Cairns (2017) in References."</span>),</span>
<span id="cb23-84">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">locations =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cells_body</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> Primary_Source, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> State <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S7"</span>)</span>
<span id="cb23-85">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-86">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_source_note</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NED-Normalised: utility relative to S6 NED = 1.00. QALYs = utility-weighted months / 12 over a 60-month horizon. Difference = Surgery − Definitive RT; green = Surgery advantage, red = RT advantage."</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="qrbcyoppds" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#qrbcyoppds table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#qrbcyoppds thead, #qrbcyoppds tbody, #qrbcyoppds tfoot, #qrbcyoppds tr, #qrbcyoppds td, #qrbcyoppds th {
  border-style: none;
}

#qrbcyoppds p {
  margin: 0;
  padding: 0;
}

#qrbcyoppds .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 10px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#qrbcyoppds .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#qrbcyoppds .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#qrbcyoppds .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#qrbcyoppds .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qrbcyoppds .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qrbcyoppds .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qrbcyoppds .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#qrbcyoppds .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#qrbcyoppds .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#qrbcyoppds .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#qrbcyoppds .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#qrbcyoppds .gt_spanner_row {
  border-bottom-style: hidden;
}

#qrbcyoppds .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#qrbcyoppds .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#qrbcyoppds .gt_from_md > :first-child {
  margin-top: 0;
}

#qrbcyoppds .gt_from_md > :last-child {
  margin-bottom: 0;
}

#qrbcyoppds .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#qrbcyoppds .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#qrbcyoppds .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#qrbcyoppds .gt_row_group_first td {
  border-top-width: 2px;
}

#qrbcyoppds .gt_row_group_first th {
  border-top-width: 2px;
}

#qrbcyoppds .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qrbcyoppds .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#qrbcyoppds .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#qrbcyoppds .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qrbcyoppds .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qrbcyoppds .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#qrbcyoppds .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#qrbcyoppds .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#qrbcyoppds .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qrbcyoppds .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qrbcyoppds .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qrbcyoppds .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qrbcyoppds .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qrbcyoppds .gt_left {
  text-align: left;
}

#qrbcyoppds .gt_center {
  text-align: center;
}

#qrbcyoppds .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#qrbcyoppds .gt_font_normal {
  font-weight: normal;
}

#qrbcyoppds .gt_font_bold {
  font-weight: bold;
}

#qrbcyoppds .gt_font_italic {
  font-style: italic;
}

#qrbcyoppds .gt_super {
  font-size: 65%;
}

#qrbcyoppds .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#qrbcyoppds .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#qrbcyoppds .gt_indent_1 {
  text-indent: 5px;
}

#qrbcyoppds .gt_indent_2 {
  text-indent: 10px;
}

#qrbcyoppds .gt_indent_3 {
  text-indent: 15px;
}

#qrbcyoppds .gt_indent_4 {
  text-indent: 20px;
}

#qrbcyoppds .gt_indent_5 {
  text-indent: 25px;
}

#qrbcyoppds .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#qrbcyoppds div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
</colgroup>
<thead>
<tr class="gt_col_headings gt_spanner_row header">
<th rowspan="2" id="a::stub" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"></th>
<th rowspan="2" id="State" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">State</th>
<th rowspan="2" id="Label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">Health State</th>
<th rowspan="2" id="Primary_Source" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">Primary Source</th>
<th colspan="2" id="Utility" class="gt_center gt_columns_top_border gt_column_spanner_outer" data-quarto-table-cell-role="th" scope="colgroup"><div class="gt_column_spanner">
Utility
</div></th>
<th colspan="3" id="QALYs by Treatment Arm" class="gt_center gt_columns_top_border gt_column_spanner_outer" data-quarto-table-cell-role="th" scope="colgroup"><div class="gt_column_spanner">
QALYs by Treatment Arm
</div></th>
</tr>
<tr class="gt_col_headings even">
<th id="Utility_EQ5D" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">EQ-5D Utility</th>
<th id="Utility_NED1" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">NED-Normalised</th>
<th id="QALY_Surgery" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">QALYs — Surgery</th>
<th id="QALY_DefRT" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">QALYs — Definitive RT</th>
<th id="QALY_Diff" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">Difference (Surg − RT)</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<th id="stub_1_1" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row"></th>
<td class="gt_row gt_left" headers="stub_1_1 State">S1</td>
<td class="gt_row gt_left" headers="stub_1_1 Label">Surgery</td>
<td class="gt_row gt_left" headers="stub_1_1 Primary_Source">de Almeida (2014); Noel (2015)</td>
<td class="gt_row gt_right" headers="stub_1_1 Utility_EQ5D">0.600</td>
<td class="gt_row gt_right" headers="stub_1_1 Utility_NED1">0.732</td>
<td class="gt_row gt_right" headers="stub_1_1 QALY_Surgery">0.076</td>
<td class="gt_row gt_right" headers="stub_1_1 QALY_DefRT">0.000</td>
<td class="gt_row gt_right" headers="stub_1_1 QALY_Diff" style="color: #27AE60; font-weight: bold">0.076</td>
</tr>
<tr class="even">
<th id="stub_1_2" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row"></th>
<td class="gt_row gt_left" headers="stub_1_2 State">S2</td>
<td class="gt_row gt_left" headers="stub_1_2 Label">DefinitiveRT</td>
<td class="gt_row gt_left" headers="stub_1_2 Primary_Source">Sprave (2022)</td>
<td class="gt_row gt_right" headers="stub_1_2 Utility_EQ5D">0.830</td>
<td class="gt_row gt_right" headers="stub_1_2 Utility_NED1">1.012</td>
<td class="gt_row gt_right" headers="stub_1_2 QALY_Surgery">0.000</td>
<td class="gt_row gt_right" headers="stub_1_2 QALY_DefRT">0.208</td>
<td class="gt_row gt_right" headers="stub_1_2 QALY_Diff" style="color: #C0392B; font-weight: bold">-0.208</td>
</tr>
<tr class="odd">
<th id="stub_1_3" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row"></th>
<td class="gt_row gt_left" headers="stub_1_3 State">S3</td>
<td class="gt_row gt_left" headers="stub_1_3 Label">PostOpSurveillance</td>
<td class="gt_row gt_left" headers="stub_1_3 Primary_Source">Govers (2016)</td>
<td class="gt_row gt_right" headers="stub_1_3 Utility_EQ5D">0.720</td>
<td class="gt_row gt_right" headers="stub_1_3 Utility_NED1">0.878</td>
<td class="gt_row gt_right" headers="stub_1_3 QALY_Surgery">0.718</td>
<td class="gt_row gt_right" headers="stub_1_3 QALY_DefRT">0.000</td>
<td class="gt_row gt_right" headers="stub_1_3 QALY_Diff" style="color: #27AE60; font-weight: bold">0.718</td>
</tr>
<tr class="even">
<th id="stub_1_4" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row"></th>
<td class="gt_row gt_left" headers="stub_1_4 State">S4</td>
<td class="gt_row gt_left" headers="stub_1_4 Label">AdjuvantRT_PORT</td>
<td class="gt_row gt_left" headers="stub_1_4 Primary_Source">Sprave (2022)</td>
<td class="gt_row gt_right" headers="stub_1_4 Utility_EQ5D">0.830</td>
<td class="gt_row gt_right" headers="stub_1_4 Utility_NED1">1.012</td>
<td class="gt_row gt_right" headers="stub_1_4 QALY_Surgery">0.052</td>
<td class="gt_row gt_right" headers="stub_1_4 QALY_DefRT">0.000</td>
<td class="gt_row gt_right" headers="stub_1_4 QALY_Diff" style="color: #27AE60; font-weight: bold">0.052</td>
</tr>
<tr class="odd">
<th id="stub_1_5" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row"></th>
<td class="gt_row gt_left" headers="stub_1_5 State">S5</td>
<td class="gt_row gt_left" headers="stub_1_5 Label">AdjuvantChemoRT_POCRT</td>
<td class="gt_row gt_left" headers="stub_1_5 Primary_Source">Truong / RTOG 0522 (2017)</td>
<td class="gt_row gt_right" headers="stub_1_5 Utility_EQ5D">0.775</td>
<td class="gt_row gt_right" headers="stub_1_5 Utility_NED1">0.945</td>
<td class="gt_row gt_right" headers="stub_1_5 QALY_Surgery">0.039</td>
<td class="gt_row gt_right" headers="stub_1_5 QALY_DefRT">0.000</td>
<td class="gt_row gt_right" headers="stub_1_5 QALY_Diff" style="color: #27AE60; font-weight: bold">0.039</td>
</tr>
<tr class="even">
<th id="stub_1_6" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row"></th>
<td class="gt_row gt_left" headers="stub_1_6 State" style="font-weight: bold">S6</td>
<td class="gt_row gt_left" headers="stub_1_6 Label" style="font-weight: bold">NED</td>
<td class="gt_row gt_left" headers="stub_1_6 Primary_Source" style="font-weight: bold">Noel (2015)</td>
<td class="gt_row gt_right" headers="stub_1_6 Utility_EQ5D" style="font-weight: bold">0.820</td>
<td class="gt_row gt_right" headers="stub_1_6 Utility_NED1" style="font-weight: bold">1.000</td>
<td class="gt_row gt_right" headers="stub_1_6 QALY_Surgery" style="font-weight: bold">1.970</td>
<td class="gt_row gt_right" headers="stub_1_6 QALY_DefRT" style="font-weight: bold">2.429</td>
<td class="gt_row gt_right" headers="stub_1_6 QALY_Diff" style="font-weight: bold; color: #C0392B">-0.459</td>
</tr>
<tr class="odd">
<th id="stub_1_7" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row"></th>
<td class="gt_row gt_left" headers="stub_1_7 State">S7</td>
<td class="gt_row gt_left" headers="stub_1_7 Label">LR_Recurrence</td>
<td class="gt_row gt_left" headers="stub_1_7 Primary_Source">Modeller's assumption<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></td>
<td class="gt_row gt_right" headers="stub_1_7 Utility_EQ5D">0.550</td>
<td class="gt_row gt_right" headers="stub_1_7 Utility_NED1">0.671</td>
<td class="gt_row gt_right" headers="stub_1_7 QALY_Surgery">0.188</td>
<td class="gt_row gt_right" headers="stub_1_7 QALY_DefRT">0.260</td>
<td class="gt_row gt_right" headers="stub_1_7 QALY_Diff" style="color: #C0392B; font-weight: bold">-0.072</td>
</tr>
<tr class="even">
<th id="stub_1_8" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row"></th>
<td class="gt_row gt_left" headers="stub_1_8 State">S8</td>
<td class="gt_row gt_left" headers="stub_1_8 Label">Death</td>
<td class="gt_row gt_left" headers="stub_1_8 Primary_Source">Convention</td>
<td class="gt_row gt_right" headers="stub_1_8 Utility_EQ5D">0.000</td>
<td class="gt_row gt_right" headers="stub_1_8 Utility_NED1">0.000</td>
<td class="gt_row gt_right" headers="stub_1_8 QALY_Surgery">0.000</td>
<td class="gt_row gt_right" headers="stub_1_8 QALY_DefRT">0.000</td>
<td class="gt_row gt_right" headers="stub_1_8 QALY_Diff">0.000</td>
</tr>
<tr class="odd">
<th id="grand_summary_stub_1" class="gt_row gt_left gt_stub gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row" data-quarto-table-cell-role="th" scope="row">Total</th>
<td class="gt_row gt_left gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row" headers="grand_summary_stub_1 State">—</td>
<td class="gt_row gt_left gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row" headers="grand_summary_stub_1 Label">—</td>
<td class="gt_row gt_left gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row" headers="grand_summary_stub_1 Primary_Source">—</td>
<td class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row" headers="grand_summary_stub_1 Utility_EQ5D">—</td>
<td class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row" headers="grand_summary_stub_1 Utility_NED1">—</td>
<td class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row" headers="grand_summary_stub_1 QALY_Surgery">3.043</td>
<td class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row" headers="grand_summary_stub_1 QALY_DefRT">2.897</td>
<td class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row" headers="grand_summary_stub_1 QALY_Diff">0.146</td>
</tr>
</tbody><tfoot>
<tr class="gt_footnotes odd">
<td colspan="9" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Modeller’s assumption. No directly reported patient EQ-5D for LR recurrence exists; best available evidence is median 0.70 (del Barco et al.&nbsp;2016, palliative/metastatic context). See Meregaglia &amp; Cairns (2017) in References.</td>
</tr>
<tr class="gt_sourcenotes even">
<td colspan="9" class="gt_sourcenote">NED-Normalised: utility relative to S6 NED = 1.00. QALYs = utility-weighted months / 12 over a 60-month horizon. Difference = Surgery − Definitive RT; green = Surgery advantage, red = RT advantage.</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
</section>
<section id="qaly-comparison-by-treatment-arm" class="level3">
<h3 class="anchored" data-anchor-id="qaly-comparison-by-treatment-arm">QALY Comparison by Treatment Arm</h3>
<p>With utility weights and state occupancy times established, QALYs can be compared across the two treatment arms. The stacked bar chart below shows the utility-weighted life years accrued in each health state over the 60-month horizon. Each bar is broken down by health state, so the chart reveals not just the total QALY difference between arms but <em>where</em> that difference arises. It also reveals which states contribute most of the advantages of the treatment and which states are similar for both treatments.</p>
<div class="cell">
<details class="code-fold">
<summary>QALY comparison chart</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected utility-weighted months per state per arm,</span></span>
<span id="cb24-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># using state occupancy integrals and EQ-5D utility weights.</span></span>
<span id="cb24-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total QALYs = sum(time_in_state_months * utility) / 12</span></span>
<span id="cb24-4">qaly_surg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> time_surg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> utility</span>
<span id="cb24-5">qaly_def  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> time_def  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> utility</span>
<span id="cb24-6"></span>
<span id="cb24-7">state_palette <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb24-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S1 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E67E22"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S2 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8E44AD"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S3 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F39C12"</span>,</span>
<span id="cb24-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S4 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2980B9"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S5 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#16A085"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S6 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#27AE60"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S7 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C0392B"</span></span>
<span id="cb24-10">)</span>
<span id="cb24-11"></span>
<span id="cb24-12">df_qaly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb24-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">State   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb24-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Label   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(state_labels_ii, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb24-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Arm     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Definitive RT"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb24-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">QALY_mo =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(qaly_surg, qaly_def)</span>
<span id="cb24-17">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb24-18">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(State <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S8"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb24-19">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb24-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">State =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(State, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)),</span>
<span id="cb24-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Arm   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Arm,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Definitive RT"</span>))</span>
<span id="cb24-22">  )</span>
<span id="cb24-23"></span>
<span id="cb24-24">totals_label <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb24-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Arm   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Definitive RT"</span>),</span>
<span id="cb24-26">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surgery"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Definitive RT"</span>)),</span>
<span id="cb24-27">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(qaly_surg), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(qaly_def)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>,</span>
<span id="cb24-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(qaly_surg), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(qaly_def)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" QALYs"</span>)</span>
<span id="cb24-29">)</span>
<span id="cb24-30"></span>
<span id="cb24-31"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df_qaly, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Arm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> QALY_mo <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> State)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(</span>
<span id="cb24-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data        =</span> totals_label,</span>
<span id="cb24-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Arm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> total <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.06</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label),</span>
<span id="cb24-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb24-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb24-38">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(</span>
<span id="cb24-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> state_palette,</span>
<span id="cb24-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</span>(state_labels_ii[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>], <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>))</span>
<span id="cb24-42">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(</span>
<span id="cb24-44">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expansion</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.08</span>)),</span>
<span id="cb24-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb24-46">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb24-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x        =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb24-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quality-Adjusted Life Years (QALYs)"</span>,</span>
<span id="cb24-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill     =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Health State"</span>,</span>
<span id="cb24-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Expected QALYs by Health State and Treatment Arm"</span>,</span>
<span id="cb24-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"60-month horizon; utility weights from EQ-5D literature defaults"</span>,</span>
<span id="cb24-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State S8 (Death, utility = 0) excluded; contributes 0 QALYs"</span></span>
<span id="cb24-54">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-23-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/oscc-patient-model/index_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<section id="a-few-things-worth-noting-from-the-chart" class="level4">
<h4 class="anchored" data-anchor-id="a-few-things-worth-noting-from-the-chart">A few things worth noting from the chart:</h4>
<ul>
<li>Surgery accumulates 3.04 QALYs vs.&nbsp;2.9 QALYs for Definitive RT over the 60-month horizon — a difference of ~0.14 QALYs.</li>
<li>The dominant contributor in both arms is NED (green), which is expected given its high utility (0.82) and long sojourn time.</li>
<li>Surgery’s advantage comes largely from the PostOpSurveillance (orange) segment, which has a better utility (0.72) than Definitive RT’s LR Recurrence (dark red) burden.</li>
<li>The LR Recurrence segment is visibly larger for the Definitive RT arm (0.26 QALYs vs.&nbsp;0.19), consistent with the higher recurrence probability built into that arm’s jump chain.</li>
<li>These QALY estimates are model-based (from Q_base × utility) and do not yet incorporate cost.</li>
</ul>
</section>
</section>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">Discussion</h2>
<p>The intended users of the class of models I am proposing are physicians and their support teams who are making treatment decisions and explaining the possible consequences of the treatments to their patients. In the first use case, physicians may find the formality of the model and its baseline estimates useful in finalizing their decisions and documenting their decision making process. Having a model to inform treatment decisions should be especially helpful in situations where multiple physicians collaborate on deciding treatment options. At the very least, a model would be useful in verifying shared assumptions.</p>
<p>The second use of the model to help physicians and their care teams communicate with patients about the consequences of various treatment options is more speculative. To be truly helpful to patients physicians and their care teams must generate a compelling narrative for each health state explaining the side effects of the treatments experienced in each health state along with the strategies that previous patients have employed to cope with them. At the very least the exercise of explaining a journey through various states and interpreting the QALY’s may be useful in structuring patient conversations and setting expectations.</p>
</section>
<section id="technical-notes" class="level2">
<h2 class="anchored" data-anchor-id="technical-notes">Technical Notes</h2>
<section id="five-year-adjustment-process" class="level3">
<h3 class="anchored" data-anchor-id="five-year-adjustment-process">Five year adjustment process</h3>
<p>Five-year background survival for the general US population at age 62, <img src="https://latex.codecogs.com/png.latex?S%5E%7B(5)%7D_%7B62%7D">, is computed from the <a href="https://www.ssa.gov/oact/STATS/table4c6_2021_TR2024.html">2021 SSA Period Life Table</a> as the product of annual survival probabilities across ages 62 to 66:</p>
<p><img src="https://latex.codecogs.com/png.latex?S%5E%7B(5)%7D_%7B62%7D%20=%20%5Cprod_%7Bx=62%7D%5E%7B66%7D(1%20-%20q_x)"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?q_x"> is the one-year death probability at exact age <img src="https://latex.codecogs.com/png.latex?x"> (SSA notation). Using the 2021 table values for ages 62–66: <img src="https://latex.codecogs.com/png.latex?q_x%5E%7B%5Ctext%7Bmale%7D%7D"> = 0.01648, 0.01762, 0.01876, 0.01991, 0.02110 and <img src="https://latex.codecogs.com/png.latex?q_x%5E%7B%5Ctext%7Bfemale%7D%7D"> = 0.01014, 0.01085, 0.01155, 0.01222, 0.01295, giving <img src="https://latex.codecogs.com/png.latex?S%5E%7B(5)%7D_%7B62%7D%20=%200.910"> (male) and <img src="https://latex.codecogs.com/png.latex?0.944"> (female). The sex-weighted background survival is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbar%7BS%7D%5E%7B(5)%7D%20=%20w_M%20%5Ccdot%20S%5E%7B(5)%7D_M%20+%20w_F%20%5Ccdot%20S%5E%7B(5)%7D_F%0A=%200.70%20%5Ctimes%200.910%20+%200.30%20%5Ctimes%200.944%20=%200.920"></p>
<p>where the 70:30 sex split is from SEER oral cavity incidence data. Absolute five-year OS is then obtained as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ctext%7BOS%7D_%7B%5Ctext%7Babs%7D%7D%20=%20r%20%5Ctimes%20%5Cbar%7BS%7D%5E%7B(5)%7D%20=%200.84%20%5Ctimes%200.920%20=%200.773"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?r%20=%200.84"> is the SEER five-year relative survival for localized oral cavity cancer (Siegel et al.&nbsp;2024, <em>CA Cancer J Clin</em>, Figure 5). The code below details this calculation and provides a sensitivity check at age 65 (note: age-65 values are pending verification).</p>
<div class="cell">
<details class="code-fold">
<summary>Siegel 2024: relative → absolute OS derivation</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># US Social Security Administration period life tables (2021 release, used for 2019 SEER cohort).</span></span>
<span id="cb25-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5-year survival probabilities by sex at age 62 (closest to OSCC median diagnosis age).</span></span>
<span id="cb25-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Computed as prod(1 - q_x) for x = 62:66 from Table 4c6 (2021 Trustees Report cycle).</span></span>
<span id="cb25-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Source: Social Security Administration. Period Life Table, 2021 (2024 Trustees Report).</span></span>
<span id="cb25-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://www.ssa.gov/oact/STATS/table4c6.html</span></span>
<span id="cb25-6">surv_male_62   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9096</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5-yr background survival, males age 62;   SSA 2021 prod(1-qx), x=62:66</span></span>
<span id="cb25-7">surv_female_62 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9436</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5-yr background survival, females age 62; SSA 2021 prod(1-qx), x=62:66</span></span>
<span id="cb25-8"></span>
<span id="cb25-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># OSCC sex distribution: ~70% male, ~30% female (SEER, oral cavity)</span></span>
<span id="cb25-10">prop_male <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.70</span></span>
<span id="cb25-11">expected_survival_5yr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prop_male <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> surv_male_62 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> prop_male) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> surv_female_62</span>
<span id="cb25-12"></span>
<span id="cb25-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SEER 5-year relative survival for localized oral cavity (Siegel 2024, Fig. 5)</span></span>
<span id="cb25-14">relative_survival_5yr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.84</span></span>
<span id="cb25-15"></span>
<span id="cb25-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Absolute OS = relative survival × expected (background) survival</span></span>
<span id="cb25-17">absolute_os_5yr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> relative_survival_5yr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> expected_survival_5yr</span>
<span id="cb25-18"></span>
<span id="cb25-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sensitivity: 5-yr background survival at age 65 (upper end of median diagnosis age range).</span></span>
<span id="cb25-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Source: Social Security Administration. Period Life Table, 2021 (2024 Trustees Report).</span></span>
<span id="cb25-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Computed as prod(1 - q_x) for x = 65:69. https://www.ssa.gov/oact/STATS/table4c6.html</span></span>
<span id="cb25-22">surv_male_65   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8923</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5-yr background survival, males age 65;   SSA 2021 prod(1-qx), x=65:69</span></span>
<span id="cb25-23">surv_female_65 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9320</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5-yr background survival, females age 65; SSA 2021 prod(1-qx), x=65:69</span></span>
<span id="cb25-24">expected_surv_65 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prop_male <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> surv_male_65 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> prop_male) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> surv_female_65</span>
<span id="cb25-25">absolute_os_65   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> relative_survival_5yr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> expected_surv_65</span>
<span id="cb25-26"></span>
<span id="cb25-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb25-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Assumption              =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age 62 (lower bound)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age 65 (upper bound)"</span>),</span>
<span id="cb25-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Background_5yr_survival =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(expected_survival_5yr, expected_surv_65), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb25-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Absolute_5yr_OS         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(absolute_os_5yr, absolute_os_65), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb25-31">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>            Assumption Background_5yr_survival Absolute_5yr_OS
1 Age 62 (lower bound)                   0.920           0.773
2 Age 65 (upper bound)                   0.904           0.760</code></pre>
</div>
<details class="code-fold">
<summary>Siegel 2024: relative → absolute OS derivation</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Age 62: background survival 0.920 × relative survival 0.84 = 0.773 (~77%).</span></span>
<span id="cb27-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Age 65 sensitivity: background survival 0.904 × relative survival 0.84 = 0.760 (~76%).</span></span></code></pre></div></div>
</details>
</div>
</section>
<section id="using-the-code-with-real-data" class="level3">
<h3 class="anchored" data-anchor-id="using-the-code-with-real-data">Using the code with Real Data</h3>
<p>There is no doubt that models based on real patient data would be more convincing. To this end, I have structured the format of the generated synthetic data file so that it is suitable for fitting a continuous time Markov chain using the <code>R</code> package <code>msm</code>. I have verified that <code>msm</code> is able to fit the eight state sample model from the synthetic data. It is reasonable to expect that models of similar complexity could be fit from real patient data.</p>
</section>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<p>The references in this section are neither complete nor definitive. The literature is enormous. They have been selected to support the default values for the user inputs.</p>
<section id="references-for-background-mortality" class="level3">
<h3 class="anchored" data-anchor-id="references-for-background-mortality">References for Background Mortality</h3>
<ul>
<li>Social Security Administration. <em>Period Life Table, 2021</em> (as used in the 2024 Trustees Report). Available at: <a href="https://www.ssa.gov/oact/STATS/table4c6.html" class="uri">https://www.ssa.gov/oact/STATS/table4c6.html</a>. Accessed 2026-03-18. Five-year background survival probabilities for the general US population at ages 62 and 65, computed as <img src="https://latex.codecogs.com/png.latex?S%5E%7B(5)%7D%20=%20%5Cprod_%7Bx%7D%5E%7Bx+4%7D(1-q_x)"> where <img src="https://latex.codecogs.com/png.latex?q_x"> is the one-year death probability at exact age <img src="https://latex.codecogs.com/png.latex?x">. Values at age 62: male <img src="https://latex.codecogs.com/png.latex?S%5E%7B(5)%7D%20=%200.910">, female <img src="https://latex.codecogs.com/png.latex?S%5E%7B(5)%7D%20=%200.944">. Used to convert SEER relative survival (Siegel 2024) to absolute OS for the calibration benchmark. Values at age 65: male <img src="https://latex.codecogs.com/png.latex?S%5E%7B(5)%7D%20=%200.892">, female <img src="https://latex.codecogs.com/png.latex?S%5E%7B(5)%7D%20=%200.932"> (computed from <img src="https://latex.codecogs.com/png.latex?q_%7B65%7D">–<img src="https://latex.codecogs.com/png.latex?q_%7B69%7D"> = 0.01991, 0.02110, 0.02242, 0.02385, 0.02536 male; 0.01222, 0.01295, 0.01384, 0.01486, 0.01603 female).</li>
</ul>
</section>
<section id="references-for-transition-probabilities-and-sojourn-times" class="level3">
<h3 class="anchored" data-anchor-id="references-for-transition-probabilities-and-sojourn-times">References for Transition Probabilities and Sojourn Times</h3>
<ul>
<li><p><a href="https://pubmed.ncbi.nlm.nih.gov/15128894/">Bernier et al.&nbsp;(2004)</a> “Postoperative Irradiation with or without Concomitant Chemotherapy for Locally Advanced Head and Neck Cancer”, <em>N Engl J Med.</em> 350:1945–1952. PMID: 15128894. Landmark EORTC 22931 RCT establishing concurrent chemoRT as standard of care for high-risk post-operative HNSCC. Supports jump_P[5,6] and the locoregional control rates in the POCRT arm (S5).</p></li>
<li><p><a href="https://doi.org/10.3390/jcm11237061">Blatt et al.&nbsp;(2022)</a> “Tumor Recurrence and Follow-Up Intervals in Oral Squamous Cell Carcinoma”, <em>J Clin Med.</em> 11(23):7061. PMID: 36498636. PMC: PMC9740063. DOI: 10.3390/jcm11237061. University Medical Centre Mainz, n = 760 OSCC patients. Supports S3 NED declaration timing ~24 months (mean recurrence interval 24 ± 26 months; 50% of recurrences by 24 months) and S3 mean sojourn revision to 22 months.</p></li>
<li><p><a href="https://onlinelibrary.wiley.com/doi/10.1002/cam4.2124">Brands et al.&nbsp;(2019)</a> “Time Patterns of Recurrence and Second Primary Tumors in a Large Cohort of Patients Treated for Oral Cavity Cancer”, <em>Cancer Med.</em> 8(12):5810–5819. DOI: 10.1002/cam4.2124. Retrospective cohort of 594 OSCC patients; the great majority of recurrences occur in the first 2 years; 5-year cumulative second-event risk ~30%. Supports S3 sojourn revision to 22 months and S6 NED calibration.</p></li>
<li><p><a href="https://pubmed.ncbi.nlm.nih.gov/36155359/">Contrera et al.&nbsp;(2022)</a> “Outcomes for Recurrent Oral Cavity Squamous Cell Carcinoma”, <em>Oral Oncol.</em> 134:106127. PMID: 36155359. DOI: 10.1016/j.oraloncology.2022.106127. MD Anderson, n = 259 salvage surgeries (1997-2011); 5-year OS 44.2% for surgical candidates vs.&nbsp;1.5% for nonsurgical therapy; 51% second recurrence at median 17 months. Provides contextual support for the salvage surgery framework; primary source for current values jump_P[7,6] = 0.43 and jump_P[7,8] = 0.57 is Lee et al.&nbsp;(2024).</p></li>
<li><p><a href="https://www.nejm.org/doi/full/10.1056/NEJMoa032646">Cooper et al.&nbsp;(2004)</a> “Postoperative Concurrent Radiotherapy and Chemotherapy for High-Risk Squamous-Cell Carcinoma of the Head and Neck”, <em>N Engl J Med.</em> 350:1937–1944. PMID: 15084618. RTOG 9501 RCT; locoregional control ~60% at 5 years in the concurrent chemoRT arm. Supports S5 jump probabilities.</p></li>
<li><p><a href="https://doi.org/10.1002/lio2.70363">Correia et al.&nbsp;(2026)</a> “Timely Matters: Predictors of Delay in Oral Cavity Cancer Patients Across the Care Continuum”. <em>Laryngoscope Investig Otolaryngol.</em> 11(2):e70363. DOI: 10.1002/lio2.70363. (Also available via <a href="https://escholarship.org/uc/item/9680b1bw">eScholarship</a>. n = 93 OCSCC patients. Median surgery-to-adjuvant RT interval 8.4 weeks (same institution) and 9.3 weeks (different facility). Supports S1 mean sojourn revision to 1.5 months.</p></li>
<li><p><a href="https://doi.org/10.1016/j.oraloncology.2023.106622">Dayan et al.&nbsp;(2023)</a> “Predictors of prolonged treatment time intervals in oral cavity cancer”, <em>Oral Oncol.</em> 106622. DOI: 10.1016/j.oraloncology.2023.106622. CHUM Montreal, n = 145 multimodal OCSCC patients. Median surgery-to-PORT interval 64 days = 2.1 months. Supports S1 mean sojourn revision.</p></li>
<li><p><a href="https://onlinelibrary.wiley.com/doi/10.1097/00005537-200003001-00001">Goodwin (2000)</a> “Salvage Surgery for Patients with Recurrent Squamous Cell Carcinoma of the Upper Aerodigestive Tract: When Do the Ends Justify the Means?”, <em>Laryngoscope.</em> 110(suppl 93):1–18. Cited in v2 for salvage success rate 15–25%. Superseded in v3 by Lee et al.&nbsp;2024 for jump_P[7,6]; retained for historical context.</p></li>
<li><p><a href="https://acsjournals.onlinelibrary.wiley.com/doi/10.1002/cncr.30651">Graboyes et al.&nbsp;(2017)</a> “Adherence to National Comprehensive Cancer Network Guidelines for Time to Initiation of Postoperative Radiation Therapy for Patients with Head and Neck Cancer”, <em>Cancer.</em> 123(14):2651–2660. DOI: 10.1002/cncr.30651. NCDB analysis, n = 47,273 HNSCC patients; over 50% failed to initiate PORT within the NCCN-recommended 6 weeks. Supports real-world S1 mean sojourn estimate of 1.5 months.</p></li>
<li><p><a href="https://doi.org/10.1016/j.ijrobp.2018.09.013">Hosni et al.&nbsp;(2019)</a> “Predictors of Early Recurrence Prior to Planned Postoperative Radiation Therapy for Oral Cavity Squamous Cell Carcinoma and Outcomes Following Salvage Intensified Radiation Therapy”, <em>Int J Radiat Oncol Biol Phys.</em> 103(2):363–373. PMID: 30244160. Princess Margaret Cancer Centre, n = 601 OSCC patients (2003–2015); 3-year OS ~71% (95% CI 67–75%) after adjuvant PORT. Supports S4 jump probabilities (jump_P[4, 6:8]).</p></li>
<li><p><a href="https://doi.org/10.1002/lary.27191">Katsoulakis et al.&nbsp;(2018)</a> “Long-term outcomes in oral cavity squamous cell carcinoma with adjuvant and salvage radiotherapy after surgery”, <em>Laryngoscope.</em> PMID: 29637571. DOI: 10.1002/lary.27191. Memorial Sloan Kettering / VA Tampa; Evangelia Katsoulakis (first author). Provides contextual reference for the adjuvant RT pathway; jump_P[2,6] is now sourced from Dana-Farber Group 2011 and Studer et al.&nbsp;2007 above.</p></li>
<li><p><a href="https://pubmed.ncbi.nlm.nih.gov/39243149/">Lee et al.&nbsp;(2024)</a> “Clinical Outcome of Salvage Surgery in Patients with Recurrent Oral Cavity Cancer: A Systematic Review and Meta-Analysis”, <em>Head Neck.</em> 46(11):2901–2909. PMID: 39243149. DOI: 10.1002/hed.27928. 14 retrospective cohort studies, n = 2,069; pooled 5-year OS after salvage surgery = 43.0%; late-relapse subgroup 63.8% vs.&nbsp;30.0% for early relapse. Primary source for jump_P[7,6] = 0.43 (salvage success) and jump_P[7,8] = 0.57 (mortality); revised from 0.25/0.75 — previous values over-estimated post-recurrence mortality relative to this pooled estimate.</p></li>
<li><p><a href="https://pubmed.ncbi.nlm.nih.gov/17210345/">Liu et al.&nbsp;(2007)</a> “Impact of Recurrence Interval on Survival of Oral Cavity Squamous Cell Carcinoma Patients after Local Relapse”, <em>Otolaryngol Head Neck Surg.</em> 136(1):112–118. PMID: 17210345. DOI: 10.1016/j.otohns.2006.07.002. n = 1,687 oral cancer patients; 5-year OS after local recurrence 31.56%; recurrence within 18 months predicted higher mortality. Supports S7 sojourn of 12.5 months.</p></li>
<li><p><a href="https://www.nejm.org/doi/full/10.1056/NEJMoa1514493">Mehanna et al.&nbsp;(2016)</a> “PET-CT Surveillance versus Neck Dissection in Advanced Head and Neck Cancer”, <em>N Engl J Med.</em> 374(15):1444–1454. PMID: 26958921. DOI: 10.1056/NEJMoa1514493. PET-NECK RCT, n = 564 patients; response imaging performed at 12 weeks post-chemoradiotherapy; established ≥12-week post-RT interval as standard for response assessment in HNSCC. Supports S2 mean sojourn of 3.0 months (RT course 1.5 mo + ≥12-week response assessment window).</p></li>
<li><p><a href="https://journals.sagepub.com/doi/abs/10.1177/0194599814551718">Luryi et al.&nbsp;(2014)</a> “Positive Surgical Margins in Early Stage Oral Cavity Cancer: An Analysis of 20,602 Cases”, <em>Otolaryngol Head Neck Surg.</em> 151(6):984–990. PMID: 25210849. Large NCDB analysis of surgical margin rates and downstream outcomes in early-stage oral cavity cancer; early-stage N0 patients with negative margins have materially lower recurrence rates than all-stage series. Supports S3 transition probability framework, jump_P[3,7] = 0.18, and S6 NED calibration to 120 months.</p></li>
<li><p><a href="https://doi.org/10.18203/issn.2454-5929.ijohns20252980">Nathan et al.&nbsp;(2025)</a> “The Influence of Reconstruction following Hemiglossectomy on Perioperative Outcomes”, <em>Int J Otorhinolaryngol Head Neck Surg.</em> DOI: 10.18203/issn.2454-5929.ijohns20252980. NSQIP database study, n = 866 hemiglossectomy patients (2008–2022); consistent with ~1–2% 30-day mortality in modern series. Supports jump_P[1,8] = 0.015.</p></li>
<li><p><a href="https://pubmed.ncbi.nlm.nih.gov/16916677/">Ord, Kolokythas &amp; Reynolds (2006)</a> “Surgical Salvage for Local and Regional Recurrence in Oral Cancer”, <em>J Oral Maxillofac Surg.</em> 64(9):1409–1414. PMID: 16916677. DOI: 10.1016/j.joms.2006.05.026. Reports outcomes of surgical salvage in recurrent oral cancer; data on local and regional recurrence rates and survival after primary surgery. Supports jump_P[3,7] = 0.18 (LR recurrence 12–18% for Stage II) and S3 mean sojourn revision to 22 months.</p></li>
<li><p><a href="https://www.sciencedirect.com/science/article/pii/S0360301611003245">Sher et al.&nbsp;(2011)</a> “Treatment of Oral Cavity Squamous Cell Carcinoma with Adjuvant or Definitive Intensity-Modulated Radiation Therapy”, <em>Int J Radiat Oncol Biol Phys.</em> 2011. PMID: 21531515. Dana-Farber Cancer Institute; n = 42 OCSCC (30 adjuvant IMRT, 12 definitive IMRT); Stage I–IV (24% Stage II); 2-yr LRC for definitive IMRT = 64%, 2-yr OS = 63%. Corroborated by Studer G, Zwahlen RA, Graetz KW et al.&nbsp;(2007), “IMRT in oral cavity cancer”, <em>Radiat Oncol.</em> 2:16. PMID: 17430599. DOI: 10.1186/1748-717X-2-16. University Hospital Zurich; n = 58 OCC IMRT patients; T1 LC = 95%; T2–4 and recurred stages LC ~50–60% at 2 years; definitive IMRT LC = 43% (but cohort was 69% T3/4 or recurred). <strong>Primary reference pair for jump_P[2,6] = 0.65</strong> — definitive RT arm locoregional control for T2N0M0 Stage II; value is consistent with the early-stage subgroup in both series.</p></li>
<li><p><a href="https://pmc.ncbi.nlm.nih.gov/articles/PMC8831824/">Shetty et al.&nbsp;(2022)</a> “Salvage Surgery in Recurrent Oral Squamous Cell Carcinoma”, <em>Front Oral Health.</em> PMC8831824. Review of salvage surgery outcomes; radiation-naive recurrent OCSCC after salvage surgery: 5-year OS 59%, recurrence-free survival 60%. Provides supporting context for the salvage surgery framework; current value jump_P[7,6] = 0.43 is sourced from Lee et al.&nbsp;(2024).</p></li>
<li><p><a href="https://doi.org/10.3322/caac.21820">Siegel et al.&nbsp;(2024)</a> “Cancer Statistics, 2024”, <em>CA Cancer J Clin.</em> 74(1):12–49. PMID: 38230766. DOI: 10.3322/caac.21820. Annual American Cancer Society SEER-based statistics report. Figure 5 reports 5-year relative survival for localized oral cavity cancer (SEER localised stage, encompassing Stage I–II) as approximately 84% (diagnoses 2013–2019, follow-up through 2020). Note: relative survival overstates absolute OS — adjusting for sex-weighted background mortality from the SSA 2021 Period Life Table at median age 62 (<img src="https://latex.codecogs.com/png.latex?%5C%5Cbar%7BS%7D%5E%7B(5)%7D%20=%200.920">) yields an estimated absolute 5-yr OS of approximately 77% (<img src="https://latex.codecogs.com/png.latex?0.84%20%5C%5Ctimes%200.920%20=%200.773">) for Stage II localised oral cavity. Used as the primary population-level calibration benchmark for the Surgery arm.</p></li>
<li><p><a href="https://pubmed.ncbi.nlm.nih.gov/28533474/">Szturz et al.&nbsp;(2017)</a> “Weekly Low-Dose Versus Three-Weekly High-Dose Cisplatin for Concurrent Chemoradiation in Locoregionally Advanced Non-Nasopharyngeal Head and Neck Cancer: A Systematic Review and Meta-Analysis”, <em>Oncologist.</em> 22(9):1056–1066. PMID: 28533474.</p></li>
<li><p><a href="https://jamanetwork.com/journals/jamaotolaryngology/fullarticle/2618943">Tam et al.&nbsp;(2017)</a> “Estimating Survival After Salvage Surgery for Recurrent Oral Cavity Cancer”, <em>JAMA Otolaryngol Head Neck Surg.</em> 143(7):685–690. PMID: 28448645. Reports survival outcomes following salvage surgery for recurrent oral cavity SCC. Supports S7 transition structure.</p></li>
<li><p><a href="https://doi.org/10.1002/ohn.205">Tassone et al.&nbsp;(2023)</a> “Going Off Guidelines: An NCDB Analysis of Missed Adjuvant Therapy Among Surgically Treated Oral Cavity Cancer”, <em>Otolaryngol Head Neck Surg.</em> 169(3):632–641. PMID: 36939392. DOI: 10.1002/ohn.205. NCDB analysis, n = 53,503; establishes PORT vs.&nbsp;POCRT indications and allocation rates. Supports jump_P[1,4] = 0.25 and jump_P[1,5] = 0.15.</p></li>
<li><p><a href="https://onlinelibrary.wiley.com/doi/abs/10.1002/hed.20234">Temam et al.&nbsp;(2005)</a> “Treatment of the N0 Neck during Salvage Surgery after Radiotherapy of Head and Neck Squamous Cell Carcinoma”, <em>Head Neck.</em> 27(8):653–658. Cited in v2 for salvage success rates. Superseded in v3 by Lee et al.&nbsp;2024; retained for historical context.</p></li>
</ul>
</section>
<section id="references-for-eq-5d-utilities" class="level3">
<h3 class="anchored" data-anchor-id="references-for-eq-5d-utilities">References for EQ-5D Utilities</h3>
<ul>
<li><p><a href="https://onlinelibrary.wiley.com/doi/abs/10.1002/hed.23340">de Almeida et al.&nbsp;(2014)</a> “Preferences and Utilities for Health States after Treatment for Oropharyngeal Cancer: Transoral Robotic Surgery versus Definitive (Chemo)radiotherapy”, <em>Head Neck.</em> 36(4):529–539. Reports EQ-5D utility values across treatment modalities; informs utility weights for treatment and NED states.</p></li>
<li><p><a href="https://onlinelibrary.wiley.com/doi/abs/10.1111/coa.12502">Govers et al.&nbsp;(2016)</a> “Quality of Life after Different Procedures for Regional Control in Oral Cancer Patients: Cross-Sectional Survey”, <em>Clin Otolaryngol.</em> 41(3):228–235. EQ-5D-3L measurement in oral cavity OSCC patients following different regional treatment approaches.</p></li>
<li><p><a href="https://doi.org/10.1186/s12955-017-0748-z">Meregaglia &amp; Cairns (2017)</a> “A Systematic Literature Review of Health State Utility Values in Head and Neck Cancer”, <em>Health Qual Life Outcomes.</em> 15(1):174. DOI: 10.1186/s12955-017-0748-z. PMID: 28865475. Systematic review of 28 studies and 346 health state utility values in HNC; recommends EQ-5D as the preferred instrument. Confirms that evidence for recurrent and metastatic HNC states is sparse: the only patient-reported EQ-5D for recurrence found in the review is a median of 0.70 (del Barco et al.&nbsp;2016), from a palliative-intent recurrent/metastatic cohort — a different clinical context from S7 (locoregional recurrence with potential salvage intent) in this model. The S7 utility of 0.55 is a modeller’s assumption informed by this evidence gap, not a directly reported value from this review.</p></li>
<li><p><a href="https://jamanetwork.com/journals/jamaotolaryngology/fullarticle/2397443#:~:text=Design%2C%20Setting%2C%20and%20Participants%20In,Mark%203%20(HUI3)%20questionnaire.">Noel et al.&nbsp;(2015)</a> “Comparison of Health State Utility Measures in Patients With Head and Neck Cancer”, <em>JAMA Otolaryngol Head Neck Surg.</em> 141(8):696–703. Prospective, cross-sectional, and longitudinal study of 100 patients with squamous cell carcinoma of the upper aerodigestive tract. Mean EQ-5D-5L = 0.82 three months post-treatment with no evidence of recurrence. Primary source for NED (S6) utility value.</p></li>
<li><p><a href="https://doi.org/10.1016/j.oraloncology.2011.05.012">Ramaekers et al.&nbsp;(2011)</a> “The Impact of Late Treatment-Toxicity on Generic Health-Related Quality of Life in Head and Neck Cancer Patients after Radiotherapy”, <em>Oral Oncol.</em> 47(8):768–774. DOI: 10.1016/j.oraloncology.2011.05.012. Multi-centre cross-sectional survey; EQ-5D measured in HNC patients <strong>at least 6 months post-RT</strong> (late-effects survivorship cohort, not active treatment). Reports xerostomia and dysphagia as independent predictors of reduced utility post-RT. Retained as contextual reference for late RT toxicity; <strong>not</strong> the primary source for S4 (PORT) utility — that cohort does not represent patients during active adjuvant radiotherapy.</p></li>
<li><p><a href="https://doi.org/10.1186/s12885-022-10346-4">Sprave et al.&nbsp;(2022)</a> “Patient Reported Outcomes Based on EQ-5D-5L Questionnaires in Head and Neck Cancer Patients: A Real-World Study”, <em>BMC Cancer</em> 22:1236. DOI: 10.1186/s12885-022-10346-4. PMID: 36447175. Freiburg University; n = 366 H&amp;N cancer patients undergoing modern RT; prospective real-world PRO study; mean EQ-5D-5L at end of RT = 0.830 (SD not reported for full cohort). <strong>Primary source for S2 (Definitive RT) and S4 (PORT) utility weights = 0.83.</strong> Corroborated by Sprave et al.&nbsp;(2020, n=49): mean EQ-5D at RT completion = 0.828.</p></li>
<li><p><a href="https://doi.org/10.1080/14737167.2020.1823220">Sprave et al.&nbsp;(2020)</a> “Characterization of Health-Related Quality of Life Based on the EQ-5D-5L Questionnaire in Head-and-Neck Cancer Patients Undergoing Modern Radiotherapy”, <em>Expert Rev Pharmacoecon Outcomes Res.</em> 20(6):673–682. DOI: 10.1080/14737167.2020.1823220. PMID: 32912005. Freiburg University Medical Center; n = 49 H&amp;N cancer patients (57% definitive, 41% adjuvant RT); mean EQ-5D-5L at RT completion = 0.828 (SD 0.16). Corroborates Sprave 2022; supports S4 utility revision. Note: no significant permanent HRQOL deterioration observed at 6-month follow-up.</p></li>
<li><p><a href="https://pmc.ncbi.nlm.nih.gov/articles/PMC5303682/">Truong et al.&nbsp;/ RTOG 0522 (2017)</a> “Quality of Life and Performance Status from a Substudy Conducted Within a Prospective Phase 3 Randomized Trial of Concurrent Accelerated Radiation Plus Cisplatin With or Without Cetuximab for Locally Advanced Head and Neck Carcinoma: NRG Oncology RTOG 0522”, <em>Int J Radiat Oncol Biol Phys.</em> 97(4):687–699. PMC5303682. PMID: 27727066. DOI: 10.1016/j.ijrobp.2016.08.003. Prospective phase III RCT, n = 818 analyzable patients (oropharynx, hypopharynx, larynx; Stage III–IV); EQ-5D-3L collected at pretreatment, last 2 weeks of treatment, 3 months, and annually. <strong>Reported EQ-5D values:</strong> baseline ~0.79 (CIS 0.78, CET/CIS 0.80); at 3 months from treatment start ~0.775 (CIS 0.78, CET/CIS 0.77); at 1 year ~0.84. <strong>Note: the EQ-5D value for the ‘within last 2 weeks of treatment’ time point was collected but not reported in the paper</strong> — the authors identified omission of an acute FACT-HN assessment as a study limitation. The 3-month value (0.775) is used as the closest available proxy for active-treatment utility in S5 (POCRT). Secondary limitation: cohort is definitively treated oropharynx/larynx (not postoperative oral cavity POCRT); adopted as best available CRT utility source. <strong>Comparison with Sprave et al.&nbsp;(2022):</strong> the Sprave 2022 adjuvant CRT cohort (baseline HI = 0.849, stable to RT completion; CRT vs RT-alone not significantly different at RT completion, p = 0.624) would support a value of ~0.83 for S5. The RTOG 0522-derived value of 0.775 is retained as a conservative estimate reflecting the higher toxicity of POCRT (high-dose cisplatin, post-surgical) relative to the mixed Sprave 2022 cohort. A sensitivity analysis using 0.83 would reduce the Surgery vs.&nbsp;Def RT QALY difference by ~0.009 QALYs. <strong>Primary source for S5 (POCRT) utility = 0.775.</strong> Not the primary source for S2 (RT alone); see Sprave et al.&nbsp;(2022).</p></li>
</ul>


</section>
</section>

 ]]></description>
  <guid>https://rworks.dev/posts/oscc-patient-model/</guid>
  <pubDate>Thu, 16 Apr 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>February 2026 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/Feb-2026-Top40/</link>
  <description><![CDATA[ 





<p>Two hundred and fifty-five of the new packages submitted to CRAN in February were still there in mid-March. Here are my Top 40 picks in seventeen categories: Artificial Intelligence, Biology, Buddhism, Climate Science, Computational Methods, Data, Ecology, Epidemiology, Genomics, Machine Learning, Medical Applications, Physics, Statistics, Surveys, Time Series, Utilities, and Visualization.</p>
<div class="columns">
<div class="column" style="width:45%;">
<section id="artificial-intelligence" class="level3">
<h3 class="anchored" data-anchor-id="artificial-intelligence">Artificial Intelligence</h3>
<p><a href="https://cran.r-project.org/package=quallmer">quallmer</a> v0.3.0: Provides tools for AI-assisted qualitative data coding using large language models (‘LLMs’) via the <code>ellmer</code> package, supporting providers including <em>OpenAI</em>, <em>Anthropic</em>, <em>Google</em>, <em>Azure</em>, and local models via <em>Ollama</em> including built-in <em>codebooks</em> for common applications such as sentiment analysis and policy coding. Functions enable creating custom notebooks, support systematic replication across models and settings, compute inter-coder reliability statistics and validation metrics, and provide audit trails for documenting coding workflows following <a href="https://www.amazon.com/Naturalistic-Inquiry-Yvonna-S-Lincoln/dp/0803924313">Lincoln and Guba’s (1985)</a> framework for establishing trustworthiness in qualitative research. See the <a href="https://cran.r-project.org/web/packages/quallmer/vignettes/getting-started.html">vignette</a> to get started.</p>
</section>
<section id="biology" class="level3">
<h3 class="anchored" data-anchor-id="biology">Biology</h3>
<p><a href="https://cran.r-project.org/package=BioGSP">BioGSP</a> v1.0.0: Implementation of Graph Signal Processing methods including Spectral Graph Wavelet Transform for analyzing spatial patterns in biological data and provides tools for multi-scale analysis of biology spatial signals, including forward and inverse transforms, energy analysis, and visualization functions tailored for biological applications. See <a href="https://www.sciencedirect.com/science/article/pii/S1063520310000552?via%3Dihub">Hammond, Vandergheynst, and Gribonval (2011)</a> and <a href="https://www.biorxiv.org/content/10.1101/2024.12.20.629650v1">Yao et al.&nbsp;(2024)</a> for biological application examples, and the <a href="https://cran.r-project.org/web/packages/BioGSP/vignettes/sgwt_simulation_demo.html">vignette</a> to get started.</p>
<p><a href="BioGSP.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/Feb-2026-Top40/BioGSP.png" class="img-fluid" alt="Plots of Fourier Modes"></a></p>
<p><a href="https://cran.r-project.org/package=RuHere">RuHere</a> v1.0.1: Automatically flags common spatial errors in biological data using metadata, a six stage workflow, and functions that specifically integrate specialist-curated range information to identify geographic errors and introductions that often escape standard automated validation procedures. For details on the methodology see <a href="https://www.biorxiv.org/content/10.64898/2026.02.02.703373v1">Trindade &amp; Caron (2026)</a>. There are five vignettes including <a href="https://cran.r-project.org/web/packages/RuHere/vignettes/sampling_bias.html">Reducing sampling bias</a> and <a href="https://cran.r-project.org/web/packages/RuHere/vignettes/spatial_consistency.html">Ensuring spatial consistency</a>.</p>
<p><a href="RuHere.jpeg" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/Feb-2026-Top40/RuHere.jpeg" class="img-fluid" alt="Snapshot of interactive map"></a></p>
</section>
<section id="buddhism" class="level3">
<h3 class="anchored" data-anchor-id="buddhism">Buddhism</h3>
<p><a href="https://cran.r-project.org/package=tipitaka.critical">tipitaka.critical</a> v1.0.0: A lemmatized critical edition of the complete Pali Canon (Tipitaka), the canonical scripture of Theravadin Buddhism. Based on a five-witness collation of the Pali Text Society edition via GRETIL, SuttaCentral, the Vipassana Research Institute Chattha Sangayana edition, the Buddha Jayanti Tipitaka, and the Thai Royal Edition. All text is lemmatized using the Digital Pali Dictionary, grouping inflected forms by dictionary headword. Covers all three pitakas, Sutta, Vinaya, Abhidhamma, with 5,777 individual text units. For background on the collation method, see <a href="https://github.com/dangerzig/tipitaka.critical">Zigmond (2026)</a> and the <a href="https://cran.r-project.org/web/packages/tipitaka.critical/vignettes/tipitaka-critical.html">vignette</a>.</p>
<p><a href="tipitaka.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/Feb-2026-Top40/tipitaka.png" class="img-fluid" alt="PCA plot of all Tipitaka texts"></a></p>
</section>
<section id="climate-science" class="level3">
<h3 class="anchored" data-anchor-id="climate-science">Climate Science</h3>
<p><a href="https://cran.r-project.org/package=tidyextreme">tidyextreme</a> v1.00: Provides functions to calculate <a href="https://www.wcrp-climate.org/etccdi">Expert Team on Climate Change Detection and Indices</a> from daily or hourly temperature and precipitation data along with functions for flexible data handling. See the <a href="https://cran.r-project.org/web/packages/tidyextreme/vignettes/tidyextreme-tutorial.html">vignette</a>.</p>
</section>
<section id="computational-methods" class="level3">
<h3 class="anchored" data-anchor-id="computational-methods">Computational Methods</h3>
<p><a href="https://cran.r-project.org/package=compositional.mle">compositional.mle</a> v2.0.0: Provides composable optimization strategies for maximum likelihood estimation. Solvers are first-class functions that combine via sequential chaining, parallel racing, and random restarts. Implements gradient ascent, Newton-Raphson, quasi-Newton (BFGS), and derivative-free methods with support for constrained optimization and tracing. Returns <code>mle</code> objects compatible with <code>algebraic.mle</code> for downstream analysis. Methods are based on <a href="https://link.springer.com/book/10.1007/978-0-387-40065-5">Nocedal J, Wright SJ (2006)</a>. There are five vignettes including <a href="https://cran.r-project.org/web/packages/compositional.mle/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/compositional.mle/vignettes/theory-and-intuition.html">Theory and Intuition Behind Numerical MLE</a>.</p>
<p><a href="compmle.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/Feb-2026-Top40/compmle.png" class="img-fluid" alt="Plot of log-likelihood surface"></a></p>
<p><a href="https://cran.r-project.org/package=nabla">nabla</a> v0.7.1: Enables exact automatic differentiation for <code>R</code> functions and provides a composable derivative operator D that computes gradients, Hessians, Jacobians, and arbitrary-order derivative tensors at machine precision. D(D(f)) gives Hessians, D(D(D(f))) gives third-order tensors for skewness of maximum likelihood estimators, and so on to any order. Works through any R code including loops, branches, and control flow. There are five vignettes including an <a href="https://cran.r-project.org/web/packages/nabla/vignettes/introduction.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/nabla/vignettes/mle-workflow.html">Gradient andHessian Computation</a>.</p>
<p><a href="nabla.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/Feb-2026-Top40/nabla.png" class="img-fluid" alt="Plot of log-likelihood and gradient"></a></p>
<p><a href="https://cran.r-project.org/package=rLifting">rLifting</a> v0.9.0: Performs Wavelet Lifting Transforms focusing on signal denoising and functional data analysis (FDA). Implements a hybrid architecture with a zero-allocation <code>C++</code> core for high-performance processing. Features include unified offline (batch) denoising, causal, real-time filtering using a ring buffer engine, and adaptive recursive thresholding. There are five vignettes including an <a href="https://cran.r-project.org/web/packages/rLifting/vignettes/introduction.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/rLifting/vignettes/realtime.html">Real-time signal smoothing</a>.</p>
<p><a href="rLifting.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/Feb-2026-Top40/rLifting.png" class="img-fluid" alt="Plot showing real-time denoising of a noisy sine wave"></a></p>
<p><a href="https://cran.r-project.org/package=setweaver">setweaver</a> v1.0.0: Create sets of variables based on a mutual information. In this context, a set is a collection of distinct elements (e.g., variables) that can also be treated as a single entity. Mutual information, quantifies the dependence between two variables by expressing how much information about one variable can be gained from observing the other. See the <a href="https://cran.r-project.org/web/packages/setweaver/vignettes/setweaver.html">vignette</a>.</p>
<p><a href="setweaver.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/Feb-2026-Top40/setweaver.png" class="img-fluid" alt="Logistic Regression Effect Probability Network"></a></p>
</section>
<section id="data" class="level3">
<h3 class="anchored" data-anchor-id="data">Data</h3>
<p><a href="https://cran.r-project.org/package=geobounds">geobounds</a> v0.1.1: Provides tools for downloading data from <a href="https://www.geoboundaries.org/">geoBoundaries</a>. Several administration levels are available. See <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0231866">Runfola, D. et al.&nbsp;(2020)</a> for background and the <a href="https://cran.r-project.org/web/packages/geobounds/vignettes/geobounds.html">vignette</a> for examples.</p>
<p><a href="geobounds.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/Feb-2026-Top40/geobounds.png" class="img-fluid" alt="World Bank Income Group LatinAmerica andthe Caribbean"></a></p>
<p><a href="https://cran.r-project.org/package=healthbR">healthbR</a> v0.2.0: Provides easy access to Brazilian public health data from multiple sources including VIGITEL (Surveillance of Risk Factors for Chronic Diseases by Telephone Survey), PNS (National Health Survey), PNAD Continua (Continuous National Household Sample Survey), POF (Household Budget Survey with food security and consumption data), Censo Demografico (population denominators), SIM (Mortality Information System), SINASC (Live Birth Information System) and several other repositories. Data is downloaded from the Brazilian Ministry of Health and is returned in tidy format following tidyverse conventions. There are seventeen vignettes including an <a href="https://cran.r-project.org/web/packages/healthbR/vignettes/healthbR.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/healthbR/vignettes/vigitel-telephone-survey.html">Chronic Disease Risk Factors</a>.</p>
<p><a href="https://cran.r-project.org/package=nhanesdata">nhanesdata</a> v4.1.0: Instant access to harmonized National Health and Nutrition Examination Survey <a href="https://www.cdc.gov/nchs/nhanes/">NHANES</a> data spanning 1999-2023. Retrieve pre-processed datasets from reliable cloud storage with automatic type reconciliation and integrated search tools for variables and datasets. Simplifies NHANES data workflows by handling cycle management and maintaining data consistency across survey waves. There are four vignettes including <a href="https://cran.r-project.org/web/packages/nhanesdata/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/nhanesdata/vignettes/getting-started.html">Available NAHANES Datasets</a>.</p>
<p><a href="NHANES.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/Feb-2026-Top40/NHANES.png" class="img-fluid" alt="Plots of NHANES Age Distribution bySurvey Cycle"></a></p>
<p><a href="https://cran.r-project.org/package=roadDB">roadDB</a> v1.1.0: Provides interface to the ROCEEH Out of Africa Database (<a href="https://www.roceeh.uni-tuebingen.de/roadweb/smarty_road_simple_search.php">ROAD</a>), a comprehensive resource for archaeological, anthropological, paleoenvironmental and geographic data from Africa and Eurasia dating from 3,000,000 to 20,000 years BP. Users can retrieve data from the online database at different levels of detail and customize search requests. See <a href="https://cran.r-project.org/web/packages/roadDB/readme/README.html">README</a> to get started.</p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://cran.r-project.org/package=spacemodR">spacemodR</a> v0.1.3: Provides tools for modeling food web transfer based on an initial ground raster. It provides a directed acyclic graph structure for a set of rasters representing the flow of elements (e.g., food, energy, contaminants). It also includes tools for working with dispersal algorithms, enabling the combination of flux data with population movement. See the <a href="https://cran.r-project.org/web/packages/spacemodR/vignettes/Tutorial.html">tutorial</a>.</p>
<p><a href="spacemodR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/Feb-2026-Top40/spacemodR.png" class="img-fluid" alt="Plot of OCS GE (Occupation du Sol à Grande Échelle) data set"></a></p>
</section>
<section id="epidemiology" class="level3">
<h3 class="anchored" data-anchor-id="epidemiology">Epidemiology</h3>
<p><a href="https://cran.r-project.org/package=baselinenowcast">baselinenowcast</a> v0.2.0: Provides nowcasting methods based on using empirical delay distributions and uncertainty from past performance as well as a baseline method for developers of new nowcasting methods. The package supports standard data frame inputs as well as the direct use of reporting triangles, and is compatible with <a href="https://www.epinowcast.org/">epinowcast</a> objects and accommodates a wide spectrum of reporting schedules, including mixed patterns of reference and reporting (daily-weekly, weekly-daily). For background see <a href="https://wellcomeopenresearch.org/articles/10-614">Johnson et al.&nbsp;(2026)</a>. There are five vignettes including <a href="https://cran.r-project.org/web/packages/baselinenowcast/vignettes/baselinenowcast.html">Getting Started</a> and <a href="">Mathematical Methods</a>.</p>
<p><a href="nowcast.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/Feb-2026-Top40/nowcast.png" class="img-fluid" alt="Plot comparing initially reported with subsequently observed cases"></a></p>
<p><a href="https://cran.r-project.org/package=multigroup.vaccine">multigroup.vaccine</a> v0.1.1: Provides functions for modeling infectious disease dynamics in populations with multiple subgroups having different vaccination rates, transmission characteristics, and contact patterns. Enables calculating outbreak sizes, automatically fetching U.S. census data, and exploring vaccination scenarios with an interactive <code>shiny</code> dashboard. See <a href="https://www.valueinhealthjournal.com/article/S1098-3015(24)00154-2/fulltext?_returnURL=https%3A%2F%2Flinkinghub.elsevier.com%2Fretrieve%2Fpii%2FS1098301524001542%3Fshowall%3Dtrue">Nguyen et al.&nbsp;(2024)</a> and <a href="https://academic.oup.com/ofid/article/13/Supplement_1/ofaf695.217/8420075?login=false">Duong et al.&nbsp;(2026)</a> for background. There are four vignettes including <a href="https://cran.r-project.org/web/packages/multigroup.vaccine/vignettes/run_model_on_command_line.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/multigroup.vaccine/vignettes/measles_agemodel.html">Measles Age-Structured Model</a>.</p>
<p><a href="https://cran.r-project.org/package=ViroReportR">ViroReportR</a> v1.0.4: Implements tools for reporting and forecasting viral respiratory infections, using case surveillance data. Report generation tools for short-term forecasts, and validation metrics for an arbitrary number of customizable respiratory viruses. Estimation of the effective reproduction number is based on the <em>EpiEstim</em> framework described in work by <a href="https://academic.oup.com/aje/article/178/9/1505/89262?login=false">Cori et al.&nbsp;(2013)</a>. See the <a href="https://academic.oup.com/aje/article/178/9/1505/89262?login=false">vignette</a>.</p>
</section>
<section id="genomics" class="level3">
<h3 class="anchored" data-anchor-id="genomics">Genomics</h3>
<p><a href="https://cran.r-project.org/package=archipelago">archipelago</a> v0.1.0: Provides a graphical method for joint visualization of Variant Set Association Test results and individual variant association statistics. The Archipelago method assigns genomic coordinates to variant set statistics, allowing simultaneous display of variant-level and set-level signals in a unified plot supporting interpretation of both collective and individual variant contributions. For more see <a href="https://onlinelibrary.wiley.com/doi/10.1002/gepi.70025">Lawless et al.&nbsp;(2026)</a> and the <a href="https://cran.r-project.org/web/packages/archipelago/vignettes/archipelago-intro.html">vignette</a>.</p>
<p><a href="archipelago.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/Feb-2026-Top40/archipelago.png" class="img-fluid" alt="Example of an Archipelago plot"></a></p>
<p><a href="https://cran.r-project.org/web/packages/SVG/vignettes/SVG-introduction.html">SVG</a> v1.0.0: Implements a unified framework for detecting spatially variable genes in spatial transcriptomics data. SVG detection methods including MERINGUE (Moran’s I based spatial autocorrelation), Giotto binSpect (binary spatial enrichment test), SPARK-X (non-parametric kernel-based test), and nnSVG (nearest-neighbor Gaussian processes) which are described in <a href="https://genome.cshlp.org/content/31/10/1843">Miller et al.&nbsp;(2021)</a>, <a href="https://link.springer.com/article/10.1186/s13059-021-02286-2">Dries et al.&nbsp;(2021)</a>, <a href="https://link.springer.com/article/10.1186/s13059-021-02404-0">Zhu et al.&nbsp;(2021)</a>, and <a href="https://www.nature.com/articles/s41467-023-39748-z">Weber et al.&nbsp;(2023)</a>. See the <a href="https://cran.r-project.org/web/packages/SVG/vignettes/SVG-introduction.html">vignette</a> for examples.</p>
<p><a href="SVG.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/Feb-2026-Top40/SVG.png" class="img-fluid" alt="Spatial pattern visualization"></a></p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="machine-learning" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning">Machine Learning</h3>
<p><a href="https://cran.r-project.org/package=nadir">nadir</a> v0.0.1: Provides a functional programming implementation of the super learner algorithm, <a href="https://biostats.bepress.com/ucbbiostat/paper222/">van der Laan et al.&nbsp;(2007)</a>, with an emphasis on supporting the use of formulas to specify learners. Includes the ability to use random-effects specified in formulas e.g.&nbsp;(y ~ (age | strata) + …) and to construct new learners by passing a functions. See the <a href="https://cran.r-project.org/web/packages/nadir/vignettes/Basic-Examples.html">vignette</a> for basic examples.</p>
</section>
<section id="medical-applications" class="level3">
<h3 class="anchored" data-anchor-id="medical-applications">Medical Applications</h3>
<p><a href="https://cran.r-project.org/package=bfbin2arm">bfbin2arm</a> v0.1.0: Provides tools to design and analyze two-arm binomial clinical (phase II) trials using Bayes factors. Implements Bayes factors for point-null and directional hypotheses, predictive densities under different hypotheses, and power and sample size calibration with optional frequentist type-I error and power. See the <a href="https://cran.r-project.org/web/packages/bfbin2arm/vignettes/bfbin2arm-intro.html">vignette</a>.</p>
<p><a href="bfbin2arm.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/Feb-2026-Top40/bfbin2arm.png" class="img-fluid" alt="Power and Type 1 Error Rate Plots"></a></p>
<p><a href="https://cran.r-project.org/package=neuromapr">neuromapr</a> v0.2.1: Implements spatial null models and coordinate-space transformations for statistical comparison of brain maps, following the framework described in <a href="https://www.nature.com/articles/s41592-022-01625-w">Markello et al.&nbsp;(2022)</a>. Provides variogram-matching surrogates, Moran spectral randomization, and spin-based permutation tests. Includes an <code>R</code> interface to the <a href="https://netneurolab.github.io/neuromaps/user_guide/annotations.html">neuromaps</a> annotation registry for browsing, downloading, and comparing brain map annotations from the Open Science Framework. There are five vignettes including <a href="https://cran.r-project.org/web/packages/neuromapr/vignettes/neuromapr.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/neuromapr/vignettes/surface-geometry.html">Surface Geometry</a>.</p>
<p><a href="neuromapr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/Feb-2026-Top40/neuromapr.png" class="img-fluid" alt="Plot of Null Correlation Distributions"></a></p>
</section>
<section id="physics" class="level3">
<h3 class="anchored" data-anchor-id="physics">Physics</h3>
<p><a href="https://cran.r-project.org/package=HaDeX2">HaDeX2</a> v1.0.0: Process, analyze and visualize Hydrogen Deuterium eXchange monitored by Mass Spectrometry experiments (HDX-MS) via a new reproducible workflow for the analysis of the HDX-MS data that includes uncertainty propagation, data aggregation and visualization on 3D structure, functions for data exploration, quality control and generation of publication-quality figures and a companion <code>Shiny</code> application. There are eleven vignettes including <a href="https://cran.r-project.org/web/packages/HaDeX2/vignettes/datafiles.html">Calculations</a> and <a href="https://cran.r-project.org/web/packages/HaDeX2/vignettes/visualization.html">Data Visualization</a>.</p>
<p><a href="HaDeX2.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/Feb-2026-Top40/HaDeX2.png" class="img-fluid" alt="Plot of Deuterium uptake vs Peptide ID"></a></p>
</section>
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=BCFM">BCFM</a> v1.0.0: Implements the Bayesian Clustering Factor Models for simultaneous clustering and latent factor analysis of multivariate longitudinal data. The model accounts for within-cluster dependence through shared latent factors while allowing heterogeneity across clusters, enabling flexible covariance modeling in high-dimensional settings. The methodology is described in <a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.70350">Shin, Ferreira, and Tegge (2018)</a>. See the <a href="https://cran.r-project.org/web/packages/BCFM/vignettes/introduction-to-BCFM.html">vignette</a> for examples.</p>
<p><a href="BCFM.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/Feb-2026-Top40/BCFM.png" class="img-fluid" alt="Plot of posterior densities for cluster probabilities"></a></p>
<p><a href="https://cran.r-project.org/package=clmstan">clmstan</a> v0.1.1: Provides functions to fit cumulative link models for ordinal categorical data using <code>CmdStanR</code>. Supports various link functions including logit, probit, cloglog, loglog, cauchit, and flexible parametric links such as Generalized Extreme Value, Asymmetric Exponential Power, and Symmetric Power. Methods are described in <a href="https://onlinelibrary.wiley.com/doi/10.1111/j.1467-842X.2011.00601.x">Agresti (2010)</a>, <a href="https://link.springer.com/article/10.1007/s10651-010-0154-8">Wang and Dey (2011)</a>, and <a href="https://dl.acm.org/doi/abs/10.1007/s11222-014-9449-1">Naranjo, Perez, and Martin (2015)</a>. See the <a href="https://cran.r-project.org/web/packages/clmstan/vignettes/getting-started.html">vignette</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=dtms">dtms</a> v0.4.2: Implements discrete-time multistate models, several ways of estimating parametric and nonparametric multistate models, and an extensive set of Markov chain methods which use transition probabilities derived from the multistate model. See <a href="https://www.tandfonline.com/doi/full/10.1080/00324728.2023.2176535">Schneider et al.&nbsp;(2024)</a>, <a href="https://journals.sagepub.com/doi/10.1177/0049124118782541">Dudel (2021)</a>, <a href="https://link.springer.com/article/10.1186/s12963-020-00217-0">Dudel &amp; Myrskylä (2020)</a>, and <a href="https://www.taylorfrancis.com/books/mono/10.1201/9781315374321/multi-state-survival-models-interval-censored-data-ardo-van-den-hout">van den Hout (2017)</a> for background and <a href="https://cran.r-project.org/web/packages/dtms/readme/README.html">README</a> to get started.</p>
<p><a href="dtms.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/Feb-2026-Top40/dtms.png" class="img-fluid" alt="Plot of evolution of transition probabilities"></a></p>
<p><a href="https://cran.r-project.org/package=GAReg">GAReg</a> v0.1.0: Provides a genetic algorithm framework for regression problems requiring discrete optimization over model spaces with unknown or varying dimension, where gradient-based methods and exhaustive enumeration are impractical. The computation is built on the <em>GA</em> engine of <a href="https://journal.r-project.org/articles/RJ-2017-008/index.html">Scrucca (2017)</a>, and <em>changepointGA</em> engine from <a href="https://arxiv.org/abs/2410.15571">Li and Lu (2024)</a>. In challenging high-dimensional settings, functions enable efficient search and delivers near-optimal solutions. See the <a href="https://cran.r-project.org/web/packages/GAReg/vignettes/vignette.html">vignette</a>.</p>
<p><a href="GAReg.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/Feb-2026-Top40/GAReg.png" class="img-fluid" alt="Plot showing spline options"></a></p>
<p><a href="https://cran.r-project.org/package=mixpower">mixpower</a> v0.1.0: Implements a simulation-based toolkit for power and sample-size analysis for linear and generalized linear mixed-effects models (LMMs and GLMMs). Supports Gaussian, binomial, Poisson, and negative binomial families via <code>lme4</code>; Wald and likelihood-ratio tests; multi-parameter sensitivity grids; power curves and minimum sample-size solvers; parallel evaluation with deterministic seeds; and functions for reproducibility. Run time diagnostics include failure rate, singular-fit rate, effective N and publication-ready summary tables. There are five brief vignettes including an <a href="https://cran.r-project.org/web/packages/mixpower/vignettes/mixpower-intro.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/mixpower/vignettes/mixpower-simulations.html">Running simulations</a>.</p>
<p><a href="https://cran.r-project.org/package=rblimp">rblimp</a> v1.0.: Provides an interface to <a href="https://www.appliedmissingdata.com/blimp"><code>Blimp</code></a> software for Bayesian latent variable modeling, missing data analysis, and multiple imputation. The package generates <code>Blimp</code> syntax, executes <code>Blimp</code> models, and imports results back into <code>R</code> as structured objects with methods for visualization and analysis. See <a href="https://cran.r-project.org/web/packages/rblimp/readme/README.html">README</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=rareflow">rareflow</a> v0.1.0: Provides variational flow-based methods for modeling rare events using Kullback–Leibler divergence, normalizing flows, Girsanov change of measure, and Freidlin–Wentzell action functionals and tools for rare-event inference, minimum-action paths, and quasi-potential computation in stochastic dynamical systems. Methods are based on <a href="https://arxiv.org/abs/1505.05770">Rezende and Mohamed (2015)</a>, <a href="https://epubs.siam.org/doi/10.1137/1105027">Girsanov (1960)</a>, and <a href="https://link.springer.com/book/10.1007/978-3-642-25847-3">Freidlin and Wentzell (2012)</a>. See the <a href="https://cran.r-project.org/web/packages/rareflow/vignettes/rareflow.html">vignette</a>.</p>
<p><a href="rareflow.png" class="lightbox" data-gallery="quarto-lightbox-gallery-20"><img src="https://rworks.dev/posts/Feb-2026-Top40/rareflow.png" class="img-fluid" alt="2D potential plot"></a></p>
<p><a href="https://cran.r-project.org/package=sshist">sshist</a> v0.1.3: Implements the Shimazaki-Shinomoto method for optimizing the bin width of a histogram. This method minimizes the mean integrated squared error and features a <code>C++</code> backend for high performance and shift-averaging to remove edge-position bias. Ideally suited for time-dependent rate estimation and identifying intrinsic data structures. Supports both 1D and 2D data distributions. See <a href="https://direct.mit.edu/neco/article-abstract/19/6/1503/7188/A-Method-for-Selecting-the-Bin-Size-of-a-Time?redirectedFrom=fulltext">Shimazaki and Shinomoto (2007)</a> for more details and the <a href="https://cran.r-project.org/web/packages/sshist/vignettes/introduction.html">vignette</a> for an introduction.</p>
<p><a href="sshist.png" class="lightbox" data-gallery="quarto-lightbox-gallery-21"><img src="https://rworks.dev/posts/Feb-2026-Top40/sshist.png" class="img-fluid" alt="Plots showing optimal histogram for cost minimization"></a></p>
</section>
<section id="surveys" class="level3">
<h3 class="anchored" data-anchor-id="surveys">Surveys</h3>
<p><a href="https://cran.r-project.org/package=heaping">heaping</a> v0.1.0: Provides methods for correcting heaping (digit preference) in survey data at the individual record level. Age heaping, where respondents disproportionately report ages ending in 0 or 5, is a common phenomenon that can distort demographic analyses. Unlike traditional smoothing methods that only correct aggregated statistics, this package corrects individual values by replacing a calculated proportion of heaped observations with draws from fitted truncated distributions. See the <a href="https://cran.r-project.org/web/packages/heaping/vignettes/heaping-intro.html">vignette</a>.</p>
<p><a href="heaping.png" class="lightbox" data-gallery="quarto-lightbox-gallery-22"><img src="https://rworks.dev/posts/Feb-2026-Top40/heaping.png" class="img-fluid" alt="Plots showing corrections for heaping"></a></p>
<p><a href="https://cran.r-project.org/package=metasurvey">metasurvey</a> v0.0.21: Provides a step-based pipeline for reproducible survey data processing, building on the <code>survey</code> package for complex sampling designs. Supports rotating panels with bootstrap replicate weights, and provides a recipe system for sharing and reproducing data transformation workflows across survey editions. There are thirteen vignettes including <a href="https://cran.r-project.org/web/packages/metasurvey/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/metasurvey/vignettes/complex-designs.html">Survey design and Validation</a>.</p>
</section>
<section id="time-series" class="level3">
<h3 class="anchored" data-anchor-id="time-series">Time Series</h3>
<p><a href="https://cran.r-project.org/package=mhpfilter">mhpfilter</a> v0.1.0: Implements the Modified Hodrick-Prescott Filter for decomposing macroeconomic time series into trend and cyclical components via efficient <code>C++</code> routines. Unlike the standard HP filter, functions estimate series-specific lambda values that minimize the GCV criterion. See <a href="https://www.tandfonline.com/doi/abs/10.1080/00036846.2014.894631">Choudhary, Hanif and Iqbal (2014)</a>, and <a href="https://www.elibrary.imf.org/view/journals/024/1997/001/article-A003-en.xml">Coe and McDermott (1997)</a> for background. There is an <a href="https://cran.r-project.org/web/packages/mhpfilter/vignettes/introduction.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/mhpfilter/vignettes/methodology.html">Modified HP Filter Theory</a>.</p>
<p><a href="mhpfilter.png" class="lightbox" data-gallery="quarto-lightbox-gallery-23"><img src="https://rworks.dev/posts/Feb-2026-Top40/mhpfilter.png" class="img-fluid" alt="Plots showing effects of lambda"></a></p>
<p><a href="https://cran.r-project.org/package=RegimeChange">RegimeChange</a> v0.1.1: Implements a unified framework for detecting regime changes (changepoints) in time series data that includes both frequentist and Bayesian methods for univariate and multivariate series with detection of changes in mean, variance, trend, and distributional properties. See <a href="https://academic.oup.com/biomet/article-abstract/41/1-2/100/456627?redirectedFrom=fulltext&amp;login=false">Page (1954)</a>, <a href="https://www.tandfonline.com/doi/full/10.1080/01621459.2012.737745">Killick, Fearnhead, and Eckley (2012)</a> for frequentist methods and <a href="https://arxiv.org/abs/0710.3742">Adams and MacKay (2007)</a>. for Bayesian methods. There are three vignettes including and <a href="https://cran.r-project.org/web/packages/RegimeChange/vignettes/introduction.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/RegimeChange/vignettes/bayesian-methods.html">Bayesian Changepoint Detection</a>.</p>
<p><a href="RegimeChange.png" class="lightbox" data-gallery="quarto-lightbox-gallery-24"><img src="https://rworks.dev/posts/Feb-2026-Top40/RegimeChange.png" class="img-fluid" alt="Plot of changepoint detection with PELT"></a></p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=birddog">birddog</a> v1.0.0: Provides a unified set of methods to detect scientific emergence and technological trajectories in academic papers and patents by combining citation network analysis with community detection and attribute extraction, also applying natural language processing and structural topic modeling to uncover the contents of research communities. Applications of the method include: <a href="https://regepe.org.br/regepe/article/view/1742">Souza et al.&nbsp;(2022)</a> and <a href="https://www.mdpi.com/2071-1050/15/2/967">Maria et al.&nbsp;(2023)</a>. See <a href="https://cran.r-project.org/web/packages/birddog/readme/README.html">README</a> for the methodology and look <a href="https://roneyfraga.com/birddog/articles/introduction_birddog.html">here</a> for an introduction and examples.</p>
<p><a href="birddog.svg" class="lightbox" data-gallery="quarto-lightbox-gallery-25"><img src="https://rworks.dev/posts/Feb-2026-Top40/birddog.svg" class="img-fluid" alt="Methodology Workflow"></a></p>
<p><a href="https://cran.r-project.org/package=phinterval">phinterval</a> v1.0.0: Implements the phinterval vector class for representing time spans that may contain gaps (disjoint intervals) or be empty. This class generalizes the <code>lubridate</code> package’s interval class to support vectorized set operations (intersection, union, difference, complement) that always return a valid time span, even when disjoint or empty intervals are created. See the <a href="https://cran.r-project.org/web/packages/phinterval/vignettes/phinterval.html">vignette</a>.</p>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=dtGAP">dtGAP</a> v0.0.2: Provides supervised generalized association plots based on decision trees and enhances decision tree visualization by incorporating Generalized Association Plots through matrix-based visualizations including confusion matrix maps, decision tree matrix maps, and predicted class membership maps. See <a href="https://cran.r-project.org/web/packages/dtGAP/readme/README.html">README</a>.</p>
<p><a href="dtGAP.png" class="lightbox" data-gallery="quarto-lightbox-gallery-26"><img src="https://rworks.dev/posts/Feb-2026-Top40/dtGAP.png" class="img-fluid" alt="Plot of decision tree with heatmap"></a></p>
<p><a href="https://cran.r-project.org/package=ggInterval">ggInterval</a> v0.2.4: Extends <code>ggplot2</code> to visualize symbolic interval-valued data with various plots via more general and flexible input arguments, and provides a function to transform classical data into symbolic data using both clustering algorithms and customized methods. See the <a href="https://cran.r-project.org/web/packages/ggInterval/vignettes/ggInterval_Intro.html">vignette</a>.</p>
<p><a href="ggInterval.png" class="lightbox" data-gallery="quarto-lightbox-gallery-27"><img src="https://rworks.dev/posts/Feb-2026-Top40/ggInterval.png" class="img-fluid" alt="Plot of Index Image-Column Condition"></a></p>
<p><a href="https://cran.r-project.org/package=nomiShape">nomiShape</a> v1.0.1: Provides tools for visualizing and analyzing the shape of discrete nominal frequency distributions and introduces centered frequency plots, in which nominal categories are ordered from the most frequent category at the center toward less frequent categories on both sides, facilitating the detection of distributional patterns such as uniformity, dominance, symmetry, skewness, and long-tail behavior. In addition, the package supports Pareto charts for the study of dominance and cumulative frequency structure in nominal data. There are twelve vignettes including <a href="https://cran.r-project.org/web/packages/nomiShape/vignettes/nominal_distribution_shapes.html">Visualizing and Analyzing Distributions of Nominal Variables</a> and <a href="https://cran.r-project.org/web/packages/nomiShape/vignettes/pareto.html">Pareto Plots for Nominal Distributions</a>.</p>
<p><a href="nomiShape.png" class="lightbox" data-gallery="quarto-lightbox-gallery-28"><img src="https://rworks.dev/posts/Feb-2026-Top40/nomiShape.png" class="img-fluid" alt="Example of a Pareto Plot"></a></p>
</section>
</div>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/Feb-2026-Top40/</guid>
  <pubDate>Thu, 26 Mar 2026 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/Feb-2026-Top40/archipelago.png" medium="image" type="image/png" height="80" width="144"/>
</item>
<item>
  <title>Can I use Skills with Posit Assistant for the RStudio IDE?</title>
  <dc:creator>Isabella Velásquez</dc:creator>
  <link>https://rworks.dev/posts/skills-in-rstudio-posit-assistant/</link>
  <description><![CDATA[ 





<p>I recently shared a blog post on <a href="https://rworks.dev/posts/claude-skills-for-r-users/" target="_blank">Claude Skills for R Users</a>, featuring excellent Skills developed by several community members. It brought me a lot of joy to share these impactful resources from the community.</p>
<p>One question that I got repeatedly (ok, two times) was: Can I use Skills with Posit Assistant?</p>
<p>In case you are not aware, <a href="https://posit.co/blog/introducing-ai-in-rstudio/" target="_blank">Posit Assistant</a> is the new offering from Posit that integrates a specialized AI chatbot directly into RStudio. It uses live session context, including loaded packages and environment variables, to perform data visualization, debugging, and complex transformations.</p>
<p><a href="images/wat.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/skills-in-rstudio-posit-assistant/images/wat.png" class="img-fluid"></a></p>
<p>And, like in many other AI coding tools, Skills are supported in Posit Assistant! So, if you’ve been using Skills for Claude Code or other platforms, port ’em on over. I asked Posit Assistant how to do exactly that; read a summary of what I learned below and see the full conversation export at the bottom of this post.</p>
<section id="can-you-use-skills-with-posit-assistant" class="level2">
<h2 class="anchored" data-anchor-id="can-you-use-skills-with-posit-assistant">1. Can you use Skills with Posit Assistant?</h2>
<p>Yes! Posit Assistant has several built-in Skills that it can load on demand, sourced from the <a href="https://github.com/posit-dev/skills" target="_blank">posit-dev/skills repo</a>. It will load the relevant Skill automatically when your request matches its domain; for example, if you ask it to build a Shiny app, it will load the <code>shiny-bslib</code> Skill before it starts. You can also explicitly ask Posit Assistant to use a specific Skill.</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Skill</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>predictive-modeling</code></td>
<td>ML and modeling with tidymodels</td>
</tr>
<tr class="even">
<td><code>quarto-authoring</code></td>
<td>Writing and authoring Quarto documents</td>
</tr>
<tr class="odd">
<td><code>quarto-report</code></td>
<td>Creating Quarto reports from conversations</td>
</tr>
<tr class="even">
<td><code>shiny-bslib</code></td>
<td>Building Shiny apps with bslib</td>
</tr>
<tr class="odd">
<td><code>shiny-bslib-theming</code></td>
<td>Advanced Shiny/bslib theming</td>
</tr>
<tr class="even">
<td><code>connect-cloud-deploy</code></td>
<td>Deploying to Posit Connect Cloud</td>
</tr>
<tr class="odd">
<td><code>snowflake</code></td>
<td>Connecting to and querying Snowflake</td>
</tr>
<tr class="even">
<td><code>create-skill</code></td>
<td>Creating new Agent Skills</td>
</tr>
</tbody>
</table>
</section>
<section id="can-you-add-a-new-skill-to-posit-assistant" class="level2">
<h2 class="anchored" data-anchor-id="can-you-add-a-new-skill-to-posit-assistant">2. Can you add a new Skill to Posit Assistant?</h2>
<p>Yes!</p>
</section>
<section id="how-do-you-add-a-skill-to-posit-assistant" class="level2">
<h2 class="anchored" data-anchor-id="how-do-you-add-a-skill-to-posit-assistant">2.5 How do you add a Skill to Posit Assistant?</h2>
<p>There are various ways. For the <a href="https://gist.github.com/sj-io/3828d64d0969f2a0f05297e59e6c15ad" target="_blank">Modern R Development Guide</a>, I gave it the URL and asked it to create a Skill from the gist. It worked, but it did require me approving a lot of permissions first!</p>
<p>There are two locations for Skills, depending on scope:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Type</th>
<th>Location</th>
<th>Use when…</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>User Skill</strong></td>
<td><code>~/.positai/skills/{skill-name}/SKILL.md</code></td>
<td>You want it available across all projects</td>
</tr>
<tr class="even">
<td><strong>Project Skill</strong></td>
<td><code>.positai/skills/{skill-name}/SKILL.md</code></td>
<td>You want it scoped to one project</td>
</tr>
</tbody>
</table>
<p>Each Skill needs at minimum a <code>SKILL.md</code> file. You can optionally add <code>scripts/</code>, <code>references/</code>, and <code>assets/</code> subdirectories. The Skill name must be:</p>
<ul>
<li>lowercase</li>
<li>hyphen-separated</li>
<li>max 64 characters</li>
</ul>
<p>The easiest way to get started is to ask Posit Assistant to create one using the <code>create-skill</code> Skill. It will write the <code>SKILL.md</code> for you following the official spec.</p>
<p>In terms of adding a Skill from a GitHub repo, the simplest approach is to clone or copy the Skill files directly into your user Skills directory:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> ~/.positai/skills/</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> clone https://github.com/someone/their-skill-repo skill-name</span></code></pre></div></div>
</section>
<section id="where-are-skills-located" class="level2">
<h2 class="anchored" data-anchor-id="where-are-skills-located">3. Where are Skills located?</h2>
<p>Project/user Skills live at <code>~/.positai/skills/</code>. In my case, that’s where the <code>modern-r-tidyverse</code> Skill is stored: <code>~/.positai/skills/modern-r-tidyverse/</code>.</p>
<p><a href="images/file-path.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/skills-in-rstudio-posit-assistant/images/file-path.png" class="img-fluid"></a></p>
<p>Managed Skills (most of the built-ins like shiny-bslib, predictive-modeling, etc.) are bundled with Posit Assistant itself and aren’t directly visible on disk. You can also place Skills inside a specific project by adding them to a <code>.positai/agents/skills/</code> directory within that project folder.</p>
</section>
<section id="how-do-you-load-a-skill" class="level2">
<h2 class="anchored" data-anchor-id="how-do-you-load-a-skill">4. How do you load a Skill?</h2>
<p>Just ask! For example, I asked Posit Assistant, “Can you load the <code>shiny-bslib</code> Skill?” and it let me know when it was loaded. I then asked it to create a simple Shiny app with <a href="https://allisonhorst.github.io/palmerpenguins/" target="_blank">palmerpenguins</a> data, and it produced some lovely code:</p>
<p><a href="images/penguins-app.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/skills-in-rstudio-posit-assistant/images/penguins-app.png" class="img-fluid"></a></p>
</section>
<section id="lets-see-skill-creation-and-activation-please" class="level2">
<h2 class="anchored" data-anchor-id="lets-see-skill-creation-and-activation-please">5. Let’s see Skill creation and activation, please</h2>
<p>Inspired by my colleague Nick Rohrbaugh, here is a recording of me getting Posit Assistant to create and enable a Skill.</p>
<div class="quarto-video"><video id="video_shortcode_videojs_video1" class="video-js vjs-default-skin vjs-big-play-centered vjs-fluid" controls="" preload="auto" data-setup="{}" title=""><source src="skills.mp4"></video></div>
</section>
<section id="learn-more" class="level2">
<h2 class="anchored" data-anchor-id="learn-more">Learn more</h2>
<p>Hopefully this was a helpful overview of Skills and Posit Assistant! Try out one of the Skills from the <a href="https://rworks.dev/posts/claude-skills-for-r-users/" target="_blank">blog post</a>, and check out the <a href="https://docs.posit.co/posit-ai/user/getting-started/" target="_blank">Post Assistant docs</a> for more information.</p>
<details>
<summary>
Read the conversation export
</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"># Explained Skills and their locations, loaded the `shiny-bslib` and `create-skill` skills, built a simple bslib Shiny app with palmerpenguins data, and explained how to add a third-party Skill from GitHub by cloning into `~/.positai/skills/`.</span></span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## User</span></span>
<span id="cb2-4"></span>
<span id="cb2-5">Can I use Skills with you?</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Databot</span></span>
<span id="cb2-8"></span>
<span id="cb2-9">Yes! Skills are a way to give me specialized instructions for specific domains or tasks. I have several available that I can load on demand:</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Skill <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Purpose <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-12"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|---|---|</span></span>
<span id="cb2-13"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`modern-r-tidyverse`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Modern R/tidyverse best practices <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-14"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`predictive-modeling`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> ML and modeling with tidymodels <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-15"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`quarto-authoring`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Writing and authoring Quarto documents <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-16"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`quarto-report`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Creating Quarto reports from conversations <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-17"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`shiny-bslib`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Building Shiny apps with bslib <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-18"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`shiny-bslib-theming`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Advanced Shiny/bslib theming <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-19"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`connect-cloud-deploy`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Deploying to Posit Connect Cloud <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-20"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`snowflake`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Connecting to and querying Snowflake <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-21"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`create-skill`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Creating new Agent Skills <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-22"></span>
<span id="cb2-23">I load the relevant skill automatically when your request matches its domain — for example, if you ask me to build a Shiny app, I'll load the <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`shiny-bslib`</span> skill before I start. You can also explicitly ask me to use a specific skill.</span>
<span id="cb2-24"></span>
<span id="cb2-25">Is there something specific you'd like to work on?</span>
<span id="cb2-26"></span>
<span id="cb2-27"></span>
<span id="cb2-28"></span>
<span id="cb2-29"></span>
<span id="cb2-30"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## User</span></span>
<span id="cb2-31"></span>
<span id="cb2-32">Where would the skills be located?</span>
<span id="cb2-33"></span>
<span id="cb2-34"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Databot</span></span>
<span id="cb2-35"></span>
<span id="cb2-36"></span>
<span id="cb2-37"></span>
<span id="cb2-38"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">thinking</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"thinking-1772570742217-rjb3w"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-39">The user is asking where skills are located. Based on the system prompt, skills have a <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`location`</span> property that can be either <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`project`</span> or <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`managed`</span>. Let me look at the available skills:</span>
<span id="cb2-40"></span>
<span id="cb2-41"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`modern-r-tidyverse`</span> - location: project</span>
<span id="cb2-42"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`connect-cloud-deploy`</span> - location: managed</span>
<span id="cb2-43"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`create-skill`</span> - location: managed</span>
<span id="cb2-44"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`predictive-modeling`</span> - location: managed</span>
<span id="cb2-45"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`quarto-authoring`</span> - location: managed</span>
<span id="cb2-46"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`quarto-report`</span> - location: managed</span>
<span id="cb2-47"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`shiny-bslib`</span> - location: managed</span>
<span id="cb2-48"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`shiny-bslib-theming`</span> - location: managed</span>
<span id="cb2-49"></span>
<span id="cb2-50"></span>
<span id="cb2-51"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`snowflake`</span> - location: managed</span>
<span id="cb2-52"></span>
<span id="cb2-53">Most skills are managed by Posit, with only <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`modern-r-tidyverse`</span> being a project-specific skill. Project skills would live in the project directory structure, while managed skills are built-in and handled by the Posit Assistant system. I should check if there's a skills directory in the workspace to understand the file structure better.</span>
<span id="cb2-54"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">thinking</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-55"></span>
<span id="cb2-56"></span>
<span id="cb2-57"></span>
<span id="cb2-58"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_01HKn16PWyMkc1921vNkpk5t"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ls"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/Users/isabella.velasquez"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"caption"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./ (isabella.velasquez Workspace)"</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-59"></span>
<span id="cb2-60"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> call-id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_01HKn16PWyMkc1921vNkpk5t"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"success"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"result"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".aws/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.cache/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.claude/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.config/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.duckdb/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.ipython/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.local/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.matplotlib/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.npm/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.nvm/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.oh-my...[truncated, total 1020 chars]"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"resultPreview"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".aws/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.cache/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.claude/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.config/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.duckdb/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.ipython/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.local/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.matplotlib/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.npm/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.nvm/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.oh-my-zsh/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.ollama/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.positai/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.positron/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.pyenv/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.ssh/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.streamlit/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.Trash/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.virtualenvs/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.vscode/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zsh_sessions/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">demo/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Desktop/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Documents/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Downloads/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">gasp2025-quarto-exercises/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Library/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Movies/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Music/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">my-empty-project/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">my-r-project/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Pictures/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">posit-blog-posts/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">posit-example/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Public/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">py/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">quarto-book/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">quarto-brand-codespaces/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rpy/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rrr/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">starting-ragchatbot-codebase/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">steph_fun/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">streamlit-example/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">typst-test/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">whr/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">whr-brand/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">whr-typst-template/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">whr2/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">workflowtest/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.bash_history</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.CFUserTextEncoding</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.claude.json</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.claude.json.backup</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.DS_Store</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.gitconfig</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.gitignore</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.lesshst</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.odbc.ini</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.profile</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.Renviron</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.viminfo</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.z</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zcompdump-Isabellas-MacBook-Pro-2-5.9</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zcompdump-Isabellas-MacBook-Pro-2-5.9.isabellavelás-M6HD.79211</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zcompdump-Isabellas-MacBook-Pro-2-5.9.isabellavelás-M6HD.80854</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zcompdump-Isabellas-MacBook-Pro-2-5.9.zwc</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zcompdump-isabellavelás-M6HD-5.9</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zprofile</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zsh_history</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zshrc</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.zshrc.swo</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">lincoln-weather.R</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ssh.txt</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ssh.txt.pub"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsedMs"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-61"></span>
<span id="cb2-62"></span>
<span id="cb2-63"></span>
<span id="cb2-64"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_017fondJSkxidffFAXdYS1Wg"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ls"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/Users/isabella.velasquez/.positai"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"caption"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".positai"</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-65"></span>
<span id="cb2-66"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> call-id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_017fondJSkxidffFAXdYS1Wg"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"success"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"result"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logs/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">skills/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">store/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">workspaces/"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"resultPreview"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logs/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">skills/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">store/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">workspaces/"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsedMs"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-67"></span>
<span id="cb2-68"></span>
<span id="cb2-69"></span>
<span id="cb2-70"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_01PtEbepMuCpLcunFDr8ZBjW"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ls"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/Users/isabella.velasquez/.positai/skills"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"caption"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".positai/skills"</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-71"></span>
<span id="cb2-72"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> call-id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_01PtEbepMuCpLcunFDr8ZBjW"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"success"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"result"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"modern-r-tidyverse/"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"resultPreview"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"modern-r-tidyverse/"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsedMs"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-73"></span>
<span id="cb2-74">Skills are stored in two places:</span>
<span id="cb2-75"></span>
<span id="cb2-76">**Managed skills** (most of the built-ins like <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`shiny-bslib`</span>, <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`predictive-modeling`</span>, etc.) are bundled with Posit Assistant itself and aren't directly visible on disk.</span>
<span id="cb2-77"></span>
<span id="cb2-78">**Project/user skills** live at <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`~/.positai/skills/`</span>. In your case, that's where the <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`modern-r-tidyverse`</span> skill is stored:</span>
<span id="cb2-79"></span>
<span id="cb2-80"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb2-81"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~/.positai/skills/modern-r-tidyverse/</span></span>
<span id="cb2-82"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb2-83"></span>
<span id="cb2-84">You can also place skills inside a specific project by adding them to a <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`.positai/agents/skills/`</span> directory within that project folder. Would you like to explore the contents of the <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`modern-r-tidyverse`</span> skill, or create a new one?</span>
<span id="cb2-85"></span>
<span id="cb2-86"></span>
<span id="cb2-87"></span>
<span id="cb2-88"></span>
<span id="cb2-89"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## User</span></span>
<span id="cb2-90"></span>
<span id="cb2-91">Can you load the shiny-bslib Skill?</span>
<span id="cb2-92"></span>
<span id="cb2-93"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Databot</span></span>
<span id="cb2-94"></span>
<span id="cb2-95"></span>
<span id="cb2-96"></span>
<span id="cb2-97"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_01BGpR8m16rPQZEqdX84RLma"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skill"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skill"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shiny-bslib"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"caption"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shiny-bslib"</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-98"></span>
<span id="cb2-99"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> call-id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_01BGpR8m16rPQZEqdX84RLma"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"success"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"result"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# Modern Shiny Apps with bslib</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Build professional Shiny dashboards using bslib's Bootstra...[truncated, total 9293 chars]"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"resultPreview"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# Modern Shiny Apps with bslib</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Build professional Shiny dashboards using bslib's Bootstrap 5 components and layouts. This skill focuses on modern UI/UX patterns that replace legacy Shiny approaches.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">## Quick Start</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">**Single-page dashboard:**</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">```r</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(shiny)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(bslib)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">..."</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"language"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"markdown"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsedMs"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-100"></span>
<span id="cb2-101">The <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`shiny-bslib`</span> skill is now loaded! I'm ready to help you build modern Shiny dashboards using bslib. The skill covers:</span>
<span id="cb2-102"></span>
<span id="cb2-103"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>**Page layouts** (<span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`page_sidebar()`</span>, <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`page_navbar()`</span>, etc.)</span>
<span id="cb2-104"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>**Grid systems** (<span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`layout_column_wrap()`</span>, <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`layout_columns()`</span>)</span>
<span id="cb2-105"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>**Cards, value boxes, navigation, sidebars**</span>
<span id="cb2-106"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>**Theming** with <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`bs_theme()`</span></span>
<span id="cb2-107"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>**Migrating** legacy Shiny apps to modern bslib</span>
<span id="cb2-108"></span>
<span id="cb2-109">What would you like to build?</span>
<span id="cb2-110"></span>
<span id="cb2-111"></span>
<span id="cb2-112"></span>
<span id="cb2-113"></span>
<span id="cb2-114"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## User</span></span>
<span id="cb2-115"></span>
<span id="cb2-116">Can you build a simple app with palmerpenguins data?</span>
<span id="cb2-117"></span>
<span id="cb2-118"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Databot</span></span>
<span id="cb2-119"></span>
<span id="cb2-120"></span>
<span id="cb2-121"></span>
<span id="cb2-122"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">Response canceled by user</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb2-123"></span>
<span id="cb2-124"></span>
<span id="cb2-125"></span>
<span id="cb2-126"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## User</span></span>
<span id="cb2-127"></span>
<span id="cb2-128">What is the best way of adding a new Skill?</span>
<span id="cb2-129"></span>
<span id="cb2-130"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Databot</span></span>
<span id="cb2-131"></span>
<span id="cb2-132"></span>
<span id="cb2-133"></span>
<span id="cb2-134"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">thinking</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"thinking-1772571074173-9j2ja"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-135">The user has two questions:</span>
<span id="cb2-136"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">1. </span>Build a simple Shiny app with palmerpenguins data</span>
<span id="cb2-137"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">2. </span>What is the best way of adding a new Skill?</span>
<span id="cb2-138"></span>
<span id="cb2-139">Let me load the create-skill skill to answer the second question, and also build the Shiny app. Let me do both in parallel - load the create-skill skill and start building the app.</span>
<span id="cb2-140"></span>
<span id="cb2-141">Actually, let me think about this. I should load the create-skill skill to answer question 2, and build the app for question 1. I can do these in parallel.</span>
<span id="cb2-142"></span>
<span id="cb2-143">For the app, I'll build a simple <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`page_sidebar()`</span> app with palmerpenguins data using bslib. Let me think about what to include:</span>
<span id="cb2-144"></span>
<span id="cb2-145"></span>
<span id="cb2-146">I'll add a sidebar with species filtering and variable selection options, then display a scatter plot or histogram in the main area. I'll throw in a couple value boxes to keep it visually interesting while staying simple and showcasing what bslib can do.</span>
<span id="cb2-147"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">thinking</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-148"></span>
<span id="cb2-149">I'll tackle both at once — loading the <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`create-skill`</span> skill for your second question while building the app.</span>
<span id="cb2-150"></span>
<span id="cb2-151"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_01D73onbTXkfZ3k4L7DdfJ2B"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skill"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skill"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"create-skill"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"caption"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"create-skill"</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-152"></span>
<span id="cb2-153"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> call-id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_01D73onbTXkfZ3k4L7DdfJ2B"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"success"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"result"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# Create Skill</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">This skill guides you through creating new Agent Skills that conform to th...[truncated, total 1837 chars]"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"resultPreview"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# Create Skill</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">This skill guides you through creating new Agent Skills that conform to the official specification.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">## Required Steps</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Before creating any skill, you MUST:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1. **Fetch the Agent Skills specification** from https://agentskills.io/specification.md</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">2. **Fetch best practices** from https://platform.claude.com/docs/en/agents-and-tools/agent-skills/best-practices.md</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">..."</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"language"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"markdown"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsedMs"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-154"></span>
<span id="cb2-155">Now let me build the app:</span>
<span id="cb2-156"></span>
<span id="cb2-157"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_017zyxQcZjkwfDe213nUuujb"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"runCode"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"library(shiny)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(bslib)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(ggplot2)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(palmerpenguins)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(dplyr)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ui ...[truncated, total 1752 chars]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"intent"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Launch a simple bslib Shiny app using palmerpenguins data"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"intent"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Launch a simple bslib Shiny app using palmerpenguins data"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inputPreview"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"library(shiny)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(bslib)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(ggplot2)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(palmerpenguins)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">library(dplyr)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ui &lt;- page_sidebar(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  title = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Palmer Penguins Explorer</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">,</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  theme = bs_theme(version = 5, preset = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">shiny</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  sidebar = sidebar(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    selectInput(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">x_var</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">X axis</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">,</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      choices = c(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">bill_length_mm</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">bill_depth_mm</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">flipper_length_mm</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">body_mass_g</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      selected = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">bill_length_mm</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    ),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    selectInput(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">y_var</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Y axis</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">,</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      choices = c(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">bill_length_mm</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">bill_depth_mm</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">flipper_length_mm</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">body_mass_g</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      selected = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">flipper_length_mm</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    ),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    checkboxGroupInput(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">species</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Species</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">,</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      choices = unique(penguins$species),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      selected = unique(penguins$species)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    )</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  ),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  layout_column_wrap(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    width = 1/3,</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    fill = FALSE,</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    value_box(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Total Penguins</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, textOutput(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">n_total</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">), theme = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">primary</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    value_box(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Species Selected</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, textOutput(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">n_species</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">), theme = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">info</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    value_box(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Islands</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, textOutput(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">n_islands</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">), theme = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">success</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  ),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  card(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    full_screen = TRUE,</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    card_header(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Scatterplot</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">),</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    plotOutput(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">scatter</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  )</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">server &lt;- function(input, output, session) {</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  filtered &lt;- reactive({</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    penguins |&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      filter(species %in% input$species, !is.na(.data[[input$x_var]]), !is.na(.data[[input$y_var]]))</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  })</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  output$n_total    &lt;- renderText(nrow(filtered()))</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  output$n_species  &lt;- renderText(n_distinct(filtered()$species))</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  output$n_islands  &lt;- renderText(n_distinct(filtered()$island))</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  output$scatter &lt;- renderPlot({</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    ggplot(filtered(), aes(.data[[input$x_var]], .data[[input$y_var]], color = species)) +</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      geom_point(alpha = 0.7, size = 2) +</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      labs(x = input$x_var, y = input$y_var, color = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Species</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">) +</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      theme_minimal()</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  })</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">}</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">shinyApp(ui, server)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"language"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>}}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-call</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-158"></span>
<span id="cb2-159"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> call-id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"toolu_017zyxQcZjkwfDe213nUuujb"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"success"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"result"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"content"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>[{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Output:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Attaching package: ‘bslib’</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">The following object is masked from ‘package:utils’:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">...[truncated, total 223 chars]"</span>}]}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"resultPreview"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Attaching package: ‘bslib’</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">The following object is masked from ‘package:utils’:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    page</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Want to understand how all</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">the pieces fit together?</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Read R for Data Science:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">https://r4ds.hadley.nz/"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsedMs"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30003</span>}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">tool-result</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-160"></span>
<span id="cb2-161">The app is running! It includes a sidebar with axis selectors and species filters, three value boxes showing counts, and a full-screen-capable scatterplot.</span>
<span id="cb2-162"></span>
<span id="cb2-163">---</span>
<span id="cb2-164"></span>
<span id="cb2-165">**Adding a new Skill** — there are two locations depending on scope:</span>
<span id="cb2-166"></span>
<span id="cb2-167"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Type <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Location <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Use when... <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-168"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|---|---|---|</span></span>
<span id="cb2-169"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> **User skill** <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`~/.positai/skills/{skill-name}/SKILL.md`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> You want it available across all projects <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-170"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> **Project skill** <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`.positai/skills/{skill-name}/SKILL.md`</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> You want it scoped to one project <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-171"></span>
<span id="cb2-172">Each skill needs at minimum a <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`SKILL.md`</span> file. You can optionally add <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`scripts/`</span>, <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`references/`</span>, and <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`assets/`</span> subdirectories. The skill name must be lowercase, hyphen-separated, max 64 characters.</span>
<span id="cb2-173"></span>
<span id="cb2-174">The easiest way to get started is to ask me to create one — I can write the <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`SKILL.md`</span> for you following the official spec. Is there a skill you'd like to create?</span>
<span id="cb2-175"></span>
<span id="cb2-176"></span>
<span id="cb2-177"></span>
<span id="cb2-178"></span>
<span id="cb2-179"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## User</span></span>
<span id="cb2-180"></span>
<span id="cb2-181">I have one from someone else's GitHub repo. What's the best way to add it?</span>
<span id="cb2-182"></span>
<span id="cb2-183"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Databot</span></span>
<span id="cb2-184"></span>
<span id="cb2-185"></span>
<span id="cb2-186"></span>
<span id="cb2-187"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">thinking</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"thinking-1772571242783-zngwa"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-188">The user wants to add a skill from someone else's GitHub repo. The best way would be to clone or download the skill files and place them in the appropriate skills directory. Let me think about this...</span>
<span id="cb2-189"></span>
<span id="cb2-190">The skill directory structure is:</span>
<span id="cb2-191"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>User skill: <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`~/.positai/skills/{skill-name}/SKILL.md`</span></span>
<span id="cb2-192"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>Project skill: <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`.positai/skills/{skill-name}/SKILL.md`</span></span>
<span id="cb2-193"></span>
<span id="cb2-194">The best way to add a skill from a GitHub repo would be to either:</span>
<span id="cb2-195"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">1. </span>Clone the repo into the skills directory</span>
<span id="cb2-196"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">2. </span>Download/copy the relevant files into the skills directory</span>
<span id="cb2-197"></span>
<span id="cb2-198">Let me think about what the most practical advice is. If someone has a skill on GitHub, they could:</span>
<span id="cb2-199"></span>
<span id="cb2-200"></span>
<span id="cb2-201"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">1. </span><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`git clone`</span> the repo directly into <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`~/.positai/skills/`</span> </span>
<span id="cb2-202"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">2. </span>Download just the skill files and place them in the right location</span>
<span id="cb2-203"></span>
<span id="cb2-204">I don't need to fetch the spec for this - I can just give practical advice based on what I know about the skill directory structure.</span>
<span id="cb2-205"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">thinking</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-206"></span>
<span id="cb2-207">The simplest approach is to clone or copy the skill files directly into your user skills directory:</span>
<span id="cb2-208"></span>
<span id="cb2-209"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```bash</span></span>
<span id="cb2-210"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> ~/.positai/skills/</span>
<span id="cb2-211"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> clone https://github.com/someone/their-skill-repo skill-name</span>
<span id="cb2-212"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb2-213"></span>
<span id="cb2-214">The cloned folder just needs to contain a <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`SKILL.md`</span> at its root (e.g., <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`~/.positai/skills/skill-name/SKILL.md`</span>) and Posit Assistant will pick it up automatically.</span>
<span id="cb2-215"></span>
<span id="cb2-216">A few things to keep in mind:</span>
<span id="cb2-217"></span>
<span id="cb2-218"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>**Naming**: The folder name becomes the skill name, so make sure it's lowercase and hyphen-separated.</span>
<span id="cb2-219"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>**Review before use**: Since skills provide instructions that guide my behavior, it's worth reading the <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`SKILL.md`</span> before loading it — treat it like any other code from a third party.</span>
<span id="cb2-220"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>**Updates**: Because it's a git clone, you can <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`git pull`</span> inside the folder to get updates from the repo owner.</span>
<span id="cb2-221"></span>
<span id="cb2-222">If the repo contains the skill nested inside a larger project (rather than at the root), you'd just copy the relevant subdirectory into <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`~/.positai/skills/`</span> instead of cloning the whole repo.</span></code></pre></div></div>
</details>


</section>

 ]]></description>
  <guid>https://rworks.dev/posts/skills-in-rstudio-posit-assistant/</guid>
  <pubDate>Mon, 16 Mar 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A Few Claude Skills for R Users</title>
  <dc:creator>Isabella Velásquez</dc:creator>
  <link>https://rworks.dev/posts/claude-skills-for-r-users/</link>
  <description><![CDATA[ 





<p>If you’re like me, you might be feeling a bit overwhelmed by all the new AI tools for coding. So, this post may be adding one more thing to your plate, but I promise to keep it as whelming as possible. 😄</p>
<p>This is a (very) short roundup of Skills created by members of the community that are especially helpful for R users. Note that I won’t show Claude output, but rather, point you to resources on where to find Skills. I’m still very much a newbie in this space. If I misrepresent anything, or if you know of another Skill that should be included, please reach out on <a href="https://bsky.app/profile/ivelasq3.bsky.social">Bluesky</a>.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Does it have to be Claude?
</div>
</div>
<div class="callout-body-container callout-body">
<p>Although I mention “Claude Skills” throughout this post, other providers have adopted similar features for modular, task-specific capabilities in their LLM tools. They often use the same <code>SKILL.md</code> format, with the AI tools designed to look for a folder (often called <code>.skills/</code>) containing these Markdown-based instructions.</p>
</div>
</div>
<section id="quick-definitions" class="level2">
<h2 class="anchored" data-anchor-id="quick-definitions">Quick definitions</h2>
<p>If you haven’t installed Claude Code yet, Anthropic has <a href="https://code.claude.com/docs/en/quickstart">great documentation</a> to get you started. Here are a few broad definitions to set the stage:</p>
<ul>
<li><a href="https://www.anthropic.com/">Anthropic</a>: An AI company that builds AI systems.</li>
<li><a href="https://claude.ai/">Claude</a>: An AI assistant created by Anthropic that can help with a wide range of tasks, including coding.</li>
<li>Claude Models: Different versions of Claude, such as <a href="https://www.anthropic.com/claude/opus">Claude Opus 4.5</a>, <a href="https://www.anthropic.com/claude/sonnet">Claude Sonnet 4.5</a>, and <a href="https://www.anthropic.com/claude/haiku">Claude Haiku 4.5</a>. Each model offers different trade-offs between performance, speed, and cost.</li>
<li><a href="https://claude.com/product/claude-code">Claude Code</a>: A command-line interface (CLI) that brings Claude into your terminal.</li>
<li><a href="https://claude.com/blog/using-claude-md-files">CLAUDE.md</a>: A configuration file where you can give Claude Code project-specific context, preferences, and instructions.</li>
<li><a href="https://claude.com/skills">Claude Skills</a>: Reusable, specialized commands that help Claude handle common development tasks more consistently.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>A question many people (<em>cough</em> I <em>cough</em>) have wondered about is the difference between CLAUDE.md and Claude Skills. The key distinction is how broadly the information should apply and how much of Claude’s context window you want to use. If you want Claude to always be aware of certain information for every task in a project, use CLAUDE.md. This might include project conventions, coding style, or high-level rules. If the information is only relevant for specific tasks, Claude Skills are a better fit. Skills let you scope guidance to when it’s actually needed, instead of filling up the context window with instructions that don’t apply most of the time. (Thanks to my colleague Nick Pelikan for helping clarify this.)</p>
</div>
</div>
</section>
<section id="claude-skills-for-r-users-a-roundup" class="level2">
<h2 class="anchored" data-anchor-id="claude-skills-for-r-users-a-roundup">Claude Skills for R users: a roundup</h2>
<section id="claude-r-tidyverse-expert-by-sarah-johnson" class="level3">
<h3 class="anchored" data-anchor-id="claude-r-tidyverse-expert-by-sarah-johnson">1. Claude R Tidyverse Expert by Sarah Johnson</h3>
<p>Ever asked an LLM for R code and it kindly gives you a response that uses <code>spread()</code> (deprecated 2010)?</p>
<p><a href="https://sarahjohnson.io/">Sarah Johnson</a> created the <a href="https://gist.github.com/sj-io/3828d64d0969f2a0f05297e59e6c15ad">Modern R Development Guide</a> to help Claude Code behave like a modern R user. Among other things, it guides Claude to prefer tidyverse-style solutions, use recent versions of packages like dplyr 1.1.0+, and avoid outdated patterns. Never see <code>cast()</code> again!</p>
<center>
<blockquote class="bluesky-embed blockquote" data-bluesky-uri="at://did:plc:z37oae56a45bzgiybi23p4my/app.bsky.feed.post/3lwvpothzjk2o" data-bluesky-cid="bafyreifbf5heboqaalqpo25ovz532myn6gsucfj7bxlwjzqoik6yakqkca" data-bluesky-embed-color-mode="system">
<p lang="en">
</p><p>I was loving Claude Code… until I tried it with #rstats. Constant errors, wouldn't use the tidyverse even when asked, "optimized" functions were slower.</p>
Frustrated, I started a session just to teach R to Claude and summarize what it learned into a CLAUDE.md file gist.github.com/sj-io/3828d6…<br><br><a href="https://bsky.app/profile/did:plc:z37oae56a45bzgiybi23p4my/post/3lwvpothzjk2o?ref_src=embed">[image or embed]</a>
<p></p>
— sarah (<a href="https://bsky.app/profile/did:plc:z37oae56a45bzgiybi23p4my?ref_src=embed"><span class="citation" data-cites="sarahjohnson.io">@sarahjohnson.io</span></a>) <a href="https://bsky.app/profile/did:plc:z37oae56a45bzgiybi23p4my/post/3lwvpothzjk2o?ref_src=embed">August 21, 2025 at 5:18 AM</a>
</blockquote>
<script async="" src="https://embed.bsky.app/static/embed.js" charset="utf-8"></script>
</center>
<p>One nice follow-up suggestion comes from <a href="https://bsky.app/profile/jeremy-data.bsky.social/post/3mc3lucxbks2v">Jeremy Allen</a>, who recommends breaking this Skill into smaller ones if you don’t need all the guidance at once. He has also created a Skill that can pull in <a href="https://github.com/jeremy-allen/claude-skills/tree/main/deliver-posit-news">recent updates</a> from Posit!</p>
</section>
<section id="claude-code-r-skills-by-alistair-bailey" class="level3">
<h3 class="anchored" data-anchor-id="claude-code-r-skills-by-alistair-bailey">2. Claude Code R Skills by Alistair Bailey</h3>
<p><a href="https://bsky.app/profile/ab604.uk">Alistair Bailey</a> used and built upon Sarah’s (and others’) Skills above to create <a href="https://github.com/ab604/claude-code-r-skills?tab=readme-ov-file#token-optimization">Claude Code R Skills</a>. I particularly enjoy the section on <a href="https://github.com/ab604/claude-code-r-skills?tab=readme-ov-file#recommended-workflow">recommended workflow</a>, which provides a specific order for Claude to follow when writing code.</p>
<center>
<blockquote class="bluesky-embed blockquote" data-bluesky-uri="at://did:plc:xlq5qg6yjvwxfa26oizvy43u/app.bsky.feed.post/3mdusyrw3ek26" data-bluesky-cid="bafyreicphe726tkqdttywpftnq7xpmy37ffevvuopvb4spb3zpkkgrboge" data-bluesky-embed-color-mode="system">
<p lang="en">
Based on others work, I've created Claude Code configurations for R: modular skills (tidyverse, rlang, performance, OOP, testing), enforcement rules (security, testing, git workflow), workflow commands (planning, code review, TDD), and context management hooks. #claudecode #rstats<br><br><a href="https://bsky.app/profile/did:plc:xlq5qg6yjvwxfa26oizvy43u/post/3mdusyrw3ek26?ref_src=embed">[image or embed]</a>
</p>
— Alistair Bailey (<a href="https://bsky.app/profile/did:plc:xlq5qg6yjvwxfa26oizvy43u?ref_src=embed"><span class="citation" data-cites="ab604.uk">@ab604.uk</span></a>) <a href="https://bsky.app/profile/did:plc:xlq5qg6yjvwxfa26oizvy43u/post/3mdusyrw3ek26?ref_src=embed">February 2, 2026 at 12:12 PM</a>
</blockquote>
<script async="" src="https://embed.bsky.app/static/embed.js" charset="utf-8"></script>
</center>
</section>
<section id="posit-claude-skills" class="level3">
<h3 class="anchored" data-anchor-id="posit-claude-skills">3. Posit Claude Skills</h3>
<p>Several folks at Posit have been experimenting with Claude Skills and sharing them in this <a href="https://github.com/posit-dev/skills">GitHub repository</a>. A couple that may be especially interesting for R users include:</p>
<ul>
<li><a href="https://github.com/posit-dev/skills/blob/main/quarto/README.md#quarto-authoring-skill">Quarto Authoring Skill</a>: Helpful for converting existing R Markdown projects to Quarto.</li>
<li><a href="https://github.com/posit-dev/skills/blob/main/open-source/create-release-checklist/SKILL.md">Create an R Package Release Checklist Skill</a>: Create a release checklist and GitHub issue for an R package, with automatic version calculation and customizable checklist generation.</li>
<li><a href="https://github.com/posit-dev/skills/blob/main/shiny/shiny-bslib/SKILL.md">Modern Shiny Apps with bslib Skill</a>: Build modern Shiny dashboards using bslib with Bootstrap 5 layouts, cards, value boxes, navigation, theming, and modern inputs. Includes migration guide from legacy Shiny patterns.</li>
</ul>
</section>
<section id="brand.yml-skills" class="level3">
<h3 class="anchored" data-anchor-id="brand.yml-skills">4. <code>_brand.yml</code> Skills</h3>
<p><a href="https://posit-dev.github.io/brand-yml/">brand.yml</a> allows you to create reports, apps, dashboards, plots, and more that match your company’s brand guidelines with a single YAML file. It is currently supported in Quarto and Shiny (for R and Python).</p>
<p>Here are a few Skills for you to create your <code>_brand.yml</code> file:</p>
<ul>
<li><a href="https://github.com/posit-dev/skills/blob/main/brand-yml/SKILL.md">brand.yml Skill by posit-dev</a></li>
<li><a href="https://github.com/stephenturner/skill-brand-yml">brand.yml Skill by Stephen Turner</a>: <a href="https://bsky.app/profile/stephenturner.us">Stephen Turner</a> walks through the process of developing this skill in his post, <a href="https://blog.stephenturner.us/p/brand-yml-claude-skill-uva-sds-quarto">A Claude Skill for _brand.yml, and sharing with Quarto 1.9</a>.</li>
</ul>
<center>
<blockquote class="bluesky-embed blockquote" data-bluesky-uri="at://did:plc:ppvxhapnptcy5v6cih3ynmzg/app.bsky.feed.post/3mfreyzjbok2q" data-bluesky-cid="bafyreiclocg2ivthqgnieq6nbchqcvymua3qqmjp4iae2mjs7oss7eyh6y" data-bluesky-embed-color-mode="system">
<p lang="en">
I created a Claude Skill to make _brand.yml files for your organization, and with the upcoming Quarto 1.9 release you can share brand.yml files via GitHub and <code>quarto use brand</code>. More details and how to use it: blog.stephenturner.us/p/brand-yml-… #Rstats 1/ 🧵<br><br><a href="https://bsky.app/profile/did:plc:ppvxhapnptcy5v6cih3ynmzg/post/3mfreyzjbok2q?ref_src=embed">[image or embed]</a>
</p>
— Stephen Turner (<a href="https://bsky.app/profile/did:plc:ppvxhapnptcy5v6cih3ynmzg?ref_src=embed"><span class="citation" data-cites="stephenturner.us">@stephenturner.us</span></a>) <a href="https://bsky.app/profile/did:plc:ppvxhapnptcy5v6cih3ynmzg/post/3mfreyzjbok2q?ref_src=embed">February 26, 2026 at 2:14 PM</a>
</blockquote>
<script async="" src="https://embed.bsky.app/static/embed.js" charset="utf-8"></script>
</center>
</section>
<section id="learning-opportunities-a-claude-code-skill-for-deliberate-skill-development-by-cat-hicks" class="level3">
<h3 class="anchored" data-anchor-id="learning-opportunities-a-claude-code-skill-for-deliberate-skill-development-by-cat-hicks">5. Learning Opportunities: A Claude Code Skill for Deliberate Skill Development by Cat Hicks</h3>
<p>If you are worried about all these Skills deteriorating your R coding skills, check out <a href="https://github.com/DrCatHicks/learning-opportunities">Learning Opportunities: A Claude Code Skill for Deliberate Skill Development</a> from <a href="https://bsky.app/profile/grimalkina.bsky.social">Cat Hicks</a>. It uses a “dynamic textbook” approach to help you deliberately work your coding muscles <strong>while</strong> you’re using LLM tools.</p>
<center>
<blockquote class="bluesky-embed blockquote" data-bluesky-uri="at://did:plc:yjvayj5thzisljwor7yykhlx/app.bsky.feed.post/3mevvbm3a6s26" data-bluesky-cid="bafyreigsjkiynnec4bfmouz7gfo34v7qyv67yn2px3rouxswkz7xezb7fa" data-bluesky-embed-color-mode="system">
<p lang="en">
</p><p>Can you learn AND offload? Yes. We do it all the time across our days. You just cannot do every single thing at once, and have to think about the structural support for different (sometimes competing) goals.</p>
Self-regulation isn't new, but it is a vital skill for developers now.
<p></p>
— Cat Hicks (<a href="https://bsky.app/profile/did:plc:yjvayj5thzisljwor7yykhlx?ref_src=embed"><span class="citation" data-cites="grimalkina.bsky.social">@grimalkina.bsky.social</span></a>) <a href="https://bsky.app/profile/did:plc:yjvayj5thzisljwor7yykhlx/post/3mevvbm3a6s26?ref_src=embed">February 15, 2026 at 3:51 PM</a>
</blockquote>
<script async="" src="https://embed.bsky.app/static/embed.js" charset="utf-8"></script>
</center>
</section>
</section>
<section id="how-to-add-a-claude-skill" class="level2">
<h2 class="anchored" data-anchor-id="how-to-add-a-claude-skill">How to add a Claude Skill</h2>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>While you can use Claude Code in RStudio, I have been using <a href="https://positron.posit.co/">Positron</a>. Posit has started rolling out <a href="https://posit.co/products/ai/">Posit AI</a> in RStudio, which also supports Skills.</p>
</div>
</div>
<section id="install-from-a-github-repository" class="level3">
<h3 class="anchored" data-anchor-id="install-from-a-github-repository">1. Install from a GitHub repository</h3>
<p>As shown in the <a href="https://github.com/posit-dev/skills?tab=readme-ov-file#installation">Posit Claude Skills README</a>, you can install Skills directly from a GitHub repository using a Claude Code command. For example, this installs all of the Quarto-related Skills from the Posit Claude Skills repo:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">/plugin</span> install quarto@posit-dev-skills</span></code></pre></div></div>
<p>This is a good option if you want to pull in a maintained set of Skills all at once.</p>
</section>
<section id="install-from-a-local-directory" class="level3">
<h3 class="anchored" data-anchor-id="install-from-a-local-directory">2. Install from a local directory</h3>
<p>If you’ve downloaded a Skill locally, you can install it directly from its folder:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">/plugin</span> add /path/to/skill-directory</span></code></pre></div></div>
</section>
<section id="manual-installation" class="level3">
<h3 class="anchored" data-anchor-id="manual-installation">3. Manual installation</h3>
<p>You can also install a Skill by placing it directly in the appropriate directory:</p>
<ul>
<li>For personal Skills: <code>~/.claude/skills/skill-name/</code> (in your home directory)</li>
<li>For project Skills: <code>.claude/skills/skill-name/</code> (in your project root)</li>
</ul>
<p>Once the files are in place, Claude Code will automatically discover and use the Skill when it’s relevant.</p>
</section>
</section>
<section id="create-your-own-skill" class="level2">
<h2 class="anchored" data-anchor-id="create-your-own-skill">Create your own Skill</h2>
<p>Anthropic has <a href="https://code.claude.com/docs/en/skills#create-your-first-skill">documentation</a> on creating your own Claude Skill. I enjoy looking through other people’s Skills to see how they organize and develop them. Trying them out and further tweaking them is a great way of creating a customized Skill of your own.</p>
<p>As both Stephen and <a href="https://bsky.app/profile/sarahjohnson.io/post/3lwxvrldnjc2o">Sarah note</a>, you can give Claude Code examples, documentation, and guidance, then ask it to help generate the Skill for you. Perhaps there’s a good Skill out there for creating Skills. It’s Skills all the way down!</p>
</section>
<section id="more-on-using-claude-code-for-r-development" class="level2">
<h2 class="anchored" data-anchor-id="more-on-using-claude-code-for-r-development">More on using Claude Code for R development</h2>
<p>A huge thank you to the awesome creators who developed and shared their Skills, best practices, and more!</p>
<p>If you want examples of Claude Code in action with R, Simon Couch has a couple of great blog posts on the subject: <a href="https://www.simonpcouch.com/blog/2025-03-26-claude-code/">Post 1</a>, <a href="https://www.simonpcouch.com/blog/2025-07-17-claude-code-2/">Post 2</a>. They’re a great complement to this roundup and show what this can look like in real workflows.</p>
<p>New features and techniques are popping up all the time. If you’re experimenting with Claude Code for R and find something useful, please reach out anytime!</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Shortly after publishing this post, Christopher Kenny started the <a href="https://github.com/christopherkenny/awesome-rstats-skills">Awesome R Stats Skills</a> repo. Check it out for more Skills that you can use in your workflows!</p>
</div>
</div>


</section>

 ]]></description>
  <guid>https://rworks.dev/posts/claude-skills-for-r-users/</guid>
  <pubDate>Mon, 02 Mar 2026 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/claude-skills-for-r-users/thumbnail.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>January 2026 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/january-2026-top-40-new-cran-packages/</link>
  <description><![CDATA[ 





<p>Two hundred forty-one of the new packages submitted to CRAN in January were still there in mid-February. Here are my Top 40 picks in nineteen categories: Artificial Intelligence, Computational Methods, Data, Dynamical Systems, Ecology, Economics, Epidemiology, Finance, Genetics, Genomics, High Performance Computing, Mathematics, Machine Learning, Medical Application, Networks, Statistics, Time Series, Utilities, and Visualization.</p>
<div class="columns">
<div class="column" style="width:45%;">
<section id="artificial-intelligence" class="level3">
<h3 class="anchored" data-anchor-id="artificial-intelligence">Artificial Intelligence</h3>
<p><a href="https://cran.r-project.org/package=kuzco">kuzco</a> v0.1.0: Provides functions to make computer vision tasks approachable in <code>R</code> by leveraging Large Language Models, including fine-tuned prompts, boilerplate functions, and input/output helpers for common computer vision workflows, such as classifying and describing images. Functions are designed to take images as input and return structured data, helping users build practical applications with minimal code. There are four vignettes, including <a href="https://cran.r-project.org/web/packages/kuzco/vignettes/getting-started.html">getting started</a> and <a href="https://cran.r-project.org/web/packages/kuzco/vignettes/batch-image-processing.html">batch image processing</a>.</p>
<p><a href="kuzco.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/kuzco.png" class="img-fluid" alt="A sad puppy start with kuzco."></a></p>
</section>
<section id="computational-methods" class="level3">
<h3 class="anchored" data-anchor-id="computational-methods">Computational Methods</h3>
<p><a href="https://cran.r-project.org/package=couplr">couplr</a> v1.0.10: Functions designed for matching plots, sites, samples, or any pairwise optimization problem, solve optimal pairing and matching problems using linear assignment algorithms. Several algorithms are provided, including Hungarian method <a href="https://onlinelibrary.wiley.com/doi/10.1002/nav.3800020109">Kuhn (1955)</a>, the Jonker-Volgenant shortest path algorithm <a href="https://link.springer.com/article/10.1007/BF02278710">Jonker and Volgenant (1987)</a>, and the auction algorithm <a href="https://link.springer.com/article/10.1007/BF02186476">(Bertsekas (1988)</a>. There are six vignettes, including <a href="https://cran.r-project.org/web/packages/couplr/vignettes/getting-started.html">Quick Start</a> and <a href="https://cran.r-project.org/web/packages/couplr/vignettes/algorithms.html">The Algorithm Collection</a>.</p>
<p><a href="couplr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/couplr.png" class="img-fluid" alt="Illustration of Hungarian method for optimal matching of two sets of points"></a></p>
<p><a href="https://cran.r-project.org/package=hexify">hexify</a> v0.3.10: Implements the ISEA discrete global grid system (<a href="https://utppublishing.com/doi/10.3138/27H7-8K88-4882-1752">Sahr, White and Kimerling (2003)</a>). Includes a fast <code>C++</code> core for projection and aperture quantization, and <code>sf</code>/<code>terra</code>-compatible R wrappers for grid generation and coordinate assignment. Output is compatible with <code>dggridR</code> for interoperability. There are three vignettes: <a href="https://cran.r-project.org/web/packages/hexify/vignettes/quickstart.html">Quick Start</a>, <a href="https://cran.r-project.org/web/packages/hexify/vignettes/visualization.html">Visualization</a>, and <a href="https://cran.r-project.org/web/packages/hexify/vignettes/workflows.html">Practical Workflows</a>.</p>
<p><a href="hexify.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/hexify.png" class="img-fluid" alt="Map of Europe with hexagonal grid overlay"></a></p>
</section>
<section id="data" class="level3">
<h3 class="anchored" data-anchor-id="data">Data</h3>
<p><a href="https://cran.r-project.org/package=dsBaseClient">dsBaseClient</a> v6.3.5: Provides client-side base functions for the <a href="https://cran.r-project.org/web/packages/dsBaseClient/readme/README.html">DataSHIELD</a> software suite, which allows non-disclosive federated analysis on sensitive data. Functions have been designed to only share non-disclosive summary statistics, with built-in automated output checking based on statistical disclosure control with data sites setting the threshold values for the automated output checks. See <a href="https://wiki.datashield.org/en/home">README</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=mongolstats">mongolstats</a> v0.1.1: Provides a <code>tidyverse</code>-friendly client for the National Statistics Office of Mongolia <a href="https://data.1212.mn/">PXWeb API</a> with helpers to discover tables, variables, and fetch statistical data. Also includes utilities to retrieve Mongolia administrative boundaries (ADM0-ADM2) as <code>sf</code> objects from open sources for mapping and spatial analysis. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/mongolstats/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/mongolstats/vignettes/discovery.html">Discovering Public Health Data</a>.</p>
<p><a href="https://cran.r-project.org/package=read.abares">read.abares</a> v2.0.0: Provides functions to download and import agricultural data from the Australian Bureau of Agricultural and Resource Economics and Sciences <a href="https://www.agriculture.gov.au/abares">ABARES</a> and Australian Bureau of Statistics <a href="https://www.abs.gov.au">ABS</a>. Data types serviced include spreadsheets, comma-separated value (CSV) files, geospatial data, including shape files and geotiffs, covering topics including broadacre crops, livestock, soil data, commodities, and more. See the vignettes, <a href="https://cran.r-project.org/web/packages/read.abares/vignettes/options.html">Setting Global Options</a> and <a href="https://cran.r-project.org/web/packages/read.abares/vignettes/read.abares.html">Working with spatial data</a>.</p>
<p><a href="ABARES.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/ABARES.png" class="img-fluid" alt="Maps showing ABARES regions"></a></p>
</section>
<section id="dynamical-systems" class="level3">
<h3 class="anchored" data-anchor-id="dynamical-systems">Dynamical Systems</h3>
<p><a href="https://cran.r-project.org/package=blvim">blvim</a> v0.1.1: Provides functions to estimate Boltzmann–Lotka–Volterra (BLV) interaction model efficiently. Enables programmatic and graphical exploration of the solution space of BLV models when parameters are varied. See <a href="https://royalsocietypublishing.org/rsif/article-abstract/5/25/865/65383/Boltzmann-Lotka-and-Volterra-and-spatial?redirectedFrom=fulltext">Wilson, A. (2008)</a> for background and the vignettes: <a href="https://cran.r-project.org/web/packages/blvim/vignettes/grid.html">Systematic exploration of the BLV solution space</a> and <a href="https://cran.r-project.org/web/packages/blvim/vignettes/theory.html">Theoretical Background</a>.</p>
<p><a href="blvim.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/blvim.png" class="img-fluid" alt="Plots showing cluster variability"></a></p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://CRAN.R-project.org/package=mrangr">mrangr</a> v1.0.1: Implements a mechanistic and spatially explicit simulator of metacommunities and extends the <a href="https://github.com/ropensci/rangr"><code>rangr</code></a> by adding the ability to simulate multiple species interacting through an asymmetric matrix of pairwise relationships, allowing users to model all types of biotic interactions — competitive, facilitative, or neutral — within spatially explicit virtual environments. See the <a href="https://cran.r-project.org/web/packages/mrangr/vignettes/mrangr.html">vignette</a>.</p>
<p><a href="mranger.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/mranger.png" class="img-fluid" alt="A sequence of plots that step through a species simulation"></a></p>
</section>
<section id="epidemiology" class="level3">
<h3 class="anchored" data-anchor-id="epidemiology">Epidemiology</h3>
<p><a href="https://cran.r-project.org/package=rsurvstat">rsurvstat</a> v0.1.4 Provides an interface to the <a href="https://tools.rki.de/SurvStat/SurvStatWebService.svc">SurvStat</a> web service from the <a href="https://www.rki.de/EN/Home/home_node.html">Robert Koch Institute</a>, allowing downloads of disease time series stratified by pathogen type and subtype, age, and geography from notifiable disease reports in Germany. See the <a href="https://cran.r-project.org/web/packages/rsurvstat/vignettes/rsurvstat.html">vignette</a>.</p>
<p><a href="rsurvstat.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/rsurvstat.png" class="img-fluid" alt="Plot of weekly incidence of Enterovirus"></a></p>
</section>
<section id="economics" class="level3">
<h3 class="anchored" data-anchor-id="economics">Economics</h3>
<p><a href="https://cran.r-project.org/package=emburden">emburden</a> v0.6.1: Provides functions to calculate and analyze household energy burden using the Net Energy Return aggregation methodology. Functions support weighted statistical calculations across geographic and demographic cohorts, with utilities for formatting results into publication-ready tables. Methods are based on <a href="https://www.nature.com/articles/s41467-021-27673-y">Scheier &amp; Kittner (2022)</a>. There are three vignettes including <a href="https://cran.r-project.org/web/packages/emburden/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/emburden/vignettes/jss-emburden.html">Temporal Energy Burden Analysis</a>.</p>
</section>
<section id="finance" class="level3">
<h3 class="anchored" data-anchor-id="finance">Finance</h3>
<p><a href="https://cran.r-project.org/package=cre.dcf">cre.dcf</a> v0.0.3: Provides utilities to build unlevered and levered discounted cash flow tables for commercial real estate assets. Functions generate bullet and amortising debt schedules, compute credit metrics such as debt coverage ratios, debt service coverage ratios, interest coverage ratios, debt yield ratios, and forward loan-to-value ratios based on net operating income. The toolkit evaluates refinancing feasibility under alternative market scenarios and supports end-to-end scenario execution. There are nine vignettes including <a href="https://cran.r-project.org/web/packages/cre.dcf/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/cre.dcf/vignettes/investment-styles-panorama.html">Investment styles panorama</a>.</p>
<p><a href="credcf.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/credcf.png" class="img-fluid" alt="Plot of risk return by investment style"></a></p>
<p><a href="https://cran.r-project.org/package=OptimalBinningWoE">OptimalBinningWoE</a> v1.0.8 Implements 36 high-performance binning algorithms for Weight of Evidence transformation in credit scoring and risk modeling, including advanced methods such as Mixed Integer Linear Programming, Genetic Algorithms, Simulated Annealing, and Monotonic Regression. Features automatic method selection based on information value maximization, strict monotonicity enforcement, and efficient handling of large datasets. Fully integrated with the <code>tidymodels</code> ecosystem for building robust machine learning pipelines. Based on methods described in <a href="https://onlinelibrary.wiley.com/doi/book/10.1002/9781119201731">Siddiqi (2006)</a> and <a href="https://arxiv.org/abs/2001.08025">Navas-Palencia (2020)</a>. The <a href="https://cran.r-project.org/web/packages/OptimalBinningWoE/vignettes/introduction.html">vignette</a> demonstrates practical applications using real-world credit data.</p>
<p><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/OptBin.png" class="img-fluid" alt="Plot of Scorecard ROC curve with optimal binning"> “}</p>
</section>
<section id="genetics" class="level3">
<h3 class="anchored" data-anchor-id="genetics">Genetics</h3>
<p><a href="https://cran.r-project.org/package=bifrost">bifrost</a> v0.1.3: Implements methods for detecting and visualizing cladogenic shifts in multivariate trait data on phylogenies. Implements penalized-likelihood multivariate generalized least squares models, enabling analyses of high-dimensional trait datasets and large trees. Includes a greedy step-wise shift-search algorithm following approaches developed in <a href="https://nph.onlinelibrary.wiley.com/doi/10.1111/nph.19099">Smith et al.&nbsp;(2023)</a>, <a href="https://www.science.org/doi/10.1126/sciadv.adp0114">Berv et al.&nbsp;(2024)</a>, and methods described in <a href="https://academic.oup.com/sysbio/article-abstract/68/1/93/5040209?redirectedFrom=fulltext&amp;login=false">Clavel et al.&nbsp;(2019)</a>. See the <a href="https://cran.r-project.org/web/packages/bifrost/vignettes/jaw-shape-vignette.html">vignette</a>.</p>
<p><a href="bifrost.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/bifrost.png" class="img-fluid" alt="Plot sgowing phylogenetic traitgram of the first principal component (PC1) of lower jaw shape in early osteichthyans (bony fishes)"></a></p>
<p><a href="https://cran.r-project.org/package=visPedigree">visPedigree</a> v1.0.1: Provides tools for tidying, analyzing, and visualizing animal pedigrees by modeling pedigrees as directed acyclic graphs. This ensures robust loop detection, efficient generation assignment, and optimal sub-population splitting. Key features include standardizing pedigree formats, flexible ancestry tracing, and generating legible vector-based PDF graphs. A unique compaction algorithm enables the visualization of massive pedigrees by grouping full-sib families. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/visPedigree/vignettes/draw-pedigree.html">How to draw a pedigree</a> and <a href="https://cran.r-project.org/web/packages/visPedigree/vignettes/relationship-matrix.html">Calculation and visualization of relationship matrix</a>.</p>
<p><a href="visped.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/visped.png" class="img-fluid" alt="Pedigree plot"></a></p>
</section>
<section id="genomics" class="level3">
<h3 class="anchored" data-anchor-id="genomics">Genomics</h3>
<p><a href="https://cran.r-project.org/package=OmicNetR">OmicNetR</a> v0.1.1: Provides an end-to-end workflow for integrative analysis of two omics layers using sparse canonical correlation analysis, including sample alignment, feature selection, network edge construction, and visualization of gene-metabolite relationships. The underlying methods are based on penalized matrix decomposition and sparse CCA <a href="https://academic.oup.com/biostatistics/article-abstract/10/3/515/293026?redirectedFrom=fulltext&amp;login=false">Witten, Tibshirani and Hastie (2009)</a> with design principles inspired by multivariate integrative frameworks such as mixOmics <a href="https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1005752">Rohart et al.&nbsp;(2017)</a>. See the <a href="https://cran.r-project.org/web/packages/OmicNetR/vignettes/OmicNetR-intro.html">vignette</a>.</p>
<p><a href="OmicNetR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/OmicNetR.png" class="img-fluid" alt="Example plot of bipartite omic data"></a></p>
</section>
<section id="high-performance-computing" class="level3">
<h3 class="anchored" data-anchor-id="high-performance-computing">High Performance Computing</h3>
<p><a href="https://cran.r-project.org/package=futurize">futurize</a> v0.1.0: Provides a straightforward path to scalable parallel computing via the <a href="https://journal.r-project.org/articles/RJ-2021-048/index.html"><code>future</code> ecosystem</a>. The <code>futurize()</code> function, which transpiles calls to sequential map-reduce functions, is combined with R’s native pipe operator to provide a way for speeding up iterative computations with minimal refactoring, e.g., <code>lapply(xs, fcn) |&gt; futurize()</code>, <code>purrr::map(xs, fcn) |&gt; futurize()</code>, and <code>foreach::foreach(x = xs) %do% { fcn(x) } |&gt; futurize()</code>. Other map-reduce packages that can be “futurized” are <code>BiocParallel</code>, <code>plyr</code>, and <code>crossmap</code>. There is also support for a growing set of domain-specific packages, including <code>boot</code>, <code>glmnet</code>, <code>mgcv</code>, <code>lme4</code>, and <code>tm</code>. See <a href="https://cran.r-project.org/web/packages/futurize/readme/README.html">README</a> to get started. There are twelve vignettes, including <a href="https://cran.r-project.org/web/packages/futurize/vignettes/futurize-11-apply.html">Parallelize base-R apply functions</a> and <a href="https://cran.r-project.org/web/packages/futurize/vignettes/futurize-21-purrr.html">Parallelize <code>purrr</code> functions</a>.</p>
</section>
<section id="mathematics" class="level3">
<h3 class="anchored" data-anchor-id="mathematics">Mathematics</h3>
<p><a href="https://cran.r-project.org/package=codyna">codyna</a> v0.1.0: Perform analysis of complex dynamic systems with a focus on the temporal unfolding of patterns, changes, and state transitions in behavioral data. Supports both time series and sequence data and provides tools for the analysis and visualization of complexity, pattern identification, trends, regimes, sequence typology, as well as early warning signals. See the <a href="https://cran.r-project.org/web/packages/codyna/vignettes/codyna.html">vignette</a>.</p>
<p><a href="codyna.jpeg" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/codyna.jpeg" class="img-fluid" alt="Plot of time series with detected warnings"></a></p>
<p><a href="https://cran.r-project.org/package=EmpiricalDynamics">EmpiricalDynamics</a> v0.1.2: Implements a comprehensive toolkit for discovering differential and difference equations from empirical time series data using symbolic regression. The package implements a complete workflow from data preprocessing, including Total Variation Regularized differentiation for noisy economic data, visual exploration of dynamical structure, and symbolic equation discovery via genetic algorithms. Functions leverage a high-performance <code>Julia</code> backend, <code>SymbolicRegression.jl</code> to provide industrial-grade robustness, physics-informed constraints, and rigorous out-of-sample validation. Designed for economists, physicists, and researchers studying dynamical systems from observational data. See the <a href="https://cran.r-project.org/web/packages/EmpiricalDynamics/vignettes/getting-started.html">vignette</a>.</p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="machine-learning" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning">Machine Learning</h3>
<p><a href="https://cran.r-project.org/package=multiRL">multiRL</a> v0.2.3: Provides a general purpose toolbox for implementing Rescorla-Wagner models in multi-armed bandit tasks. As the successor and functional extension of the <code>binaryRL</code> package, <code>multiRL</code> modularizes the Markov Decision Process (MDP) into six core components that enable constructing custom models via intuitive if-else syntax and define latent learning rules for agents. See <a href="https://elifesciences.org/articles/49547">Wilson &amp; Collins (2019)</a> and look <a href="https://yuki-961004.github.io/multiRL/">here</a> for an overview.</p>
<p><a href="https://cran.r-project.org/package=rCISSVAE">rCISSVAE</a> v0.0.4: Implements the clustering-Informed Shared-Structure Variational Autoencoder, a deep learning framework for missing data imputation introduced in <a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.70335">Khadem Charvadeh et al.&nbsp;(2025</a>. The model accommodates all three types of missing data mechanisms: Missing Completely At Random, Missing At Random, and Missing Not At Random. There are seven vignettes, including a <a href="https://cran.r-project.org/web/packages/rCISSVAE/vignettes/vignette.html">quick start guide</a> and <a href="https://cran.r-project.org/web/packages/rCISSVAE/vignettes/binary_variables_tutorial.html">Handling Binary and Categorical Variables</a>.</p>
<p><a href="https://cran.r-project.org/package=slideimp">slideimp</a> v0.5.4: Provides fast k-nearest neighbors (K-NN) and principal component analysis (PCA) imputation algorithms for missing values in high-dimensional numeric matrices, i.e., epigenetic data. For extremely high-dimensional data with ordered features, a sliding window approach for K-NN or PCA imputation is provided. Additional features include group-wise imputation (e.g., by chromosome), hyperparameter tuning with repeated cross-validation, multi-core parallelization, and optional subset imputation. See <a href="https://www.jstatsoft.org/article/view/v070i01">Josse and Husson (2016)</a> for background and the <a href="https://cran.r-project.org/web/packages/slideimp/vignettes/slideimp.html">vignette</a> for an example.</p>
<p><a href="https://cran.r-project.org/package=SportMiner">SportMiner</a> v0.1.0: Provides a toolkit for mining, analyzing, and visualizing scientific literature in sport science and includes functions for retrieving abstracts from <a href="https://www.elsevier.com/products/scopus">Scopus</a>, preprocessing text data, performing advanced topic modeling using Latent Dirichlet Allocation, Structural Topic Models, and Correlated Topic Models, and for creating publication-ready visualizations, including keyword co-occurrence networks and topic trends. See <a href="https://www.jmlr.org/papers/volume3/blei03a/blei03a.pdf">Blei et al.&nbsp;(2003)</a>, <a href="https://onlinelibrary.wiley.com/doi/10.1111/ajps.12103">Roberts et al.&nbsp;(2014)</a>, and <a href="https://projecteuclid.org/journals/annals-of-applied-statistics/volume-1/issue-1/A-correlated-topic-model-of-Science/10.1214/07-AOAS114.full">Blei and Lafferty (2007)</a> for background. There are two vignettes: <a href="https://cran.r-project.org/web/packages/SportMiner/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/SportMiner/vignettes/SportMiner-JSS.html">Text Mining and Topic Modeling for Sport Science Literature</a>.</p>
<p><a href="https://cran.r-project.org/package=xplainfi">xplainfi</a> v1.0.0: Provides a consistent interface for common feature importance methods as described in <a href="https://link.springer.com/chapter/10.1007/978-3-031-63797-1_22">Ewald et al.&nbsp;(2024)</a>, including permutation feature importance, conditional and relative feature importance, leave one covariate out, and Shapley additive global importance as well as feature sampling mechanisms to support conditional importance methods. See the vignettes <a href="https://cran.r-project.org/web/packages/xplainfi/vignettes/xplainfi.html">Getting Started</a>, <a href="https://cran.r-project.org/web/packages/xplainfi/vignettes/feature-samplers.html">Feature Samplers</a>, and <a href="https://cran.r-project.org/web/packages/xplainfi/vignettes/simulation-settings.html">Simulation Settings</a>.</p>
<p><a href="xplainfi.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/xplainfi.png" class="img-fluid" alt="DAG for mediated effects DGP"></a></p>
</section>
<section id="medical-applications" class="level3">
<h3 class="anchored" data-anchor-id="medical-applications">Medical Applications</h3>
<p><a href="https://cran.r-project.org/package=autoFlagR">autoFlagR</a> v1.0.0: Provides automated data quality auditing using unsupervised machine learning and AI-driven anomaly detection for data quality assessment. Primarily designed for Electronic Health Records (EHR) data, with benchmarking capabilities for validation and publication. Methods based on <a href="https://ieeexplore.ieee.org/document/4781136">Liu et al.&nbsp;(2008)</a> and <a href="https://dl.acm.org/doi/10.1145/342009.335388">Breunig et al.&nbsp;(2000)</a>. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/autoFlagR/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/autoFlagR/vignettes/healthcare-example.html">Healthcare Data Quality Example</a>.</p>
<p><a href="autoFlagR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/autoFlagR.png" class="img-fluid" alt="Distribution of Anomaly Scores"></a></p>
<p><a href="https://cran.r-project.org/package=repfun">repfun</a> v0.1.2: Provides functions to mimic the style of traditional reporting macros for clinical trials. The purpose is to generate tables, listings, and figures that support clinical research. This package is well-suited for firms or individuals who wish to incorporate <code>R</code> without changing their ways of working, as it follows a traditional clinical research workflow. Invoke functions (instead of macros) to summarize data and produce formatted reports. This package differs from others in that it includes tools (wrappers) for both analyzing and reporting data. There are twenty-seven vignettes, including <a href="https://cran.r-project.org/web/packages/repfun/vignettes/Global-Reporting-Setup.html">Global Reporting Setup</a> and <a href="https://cran.r-project.org/web/packages/repfun/vignettes/SAS-Type-Variable-Expansion.html">SAS Type Variable Expansion</a>.</p>
<p><a href="repfun.gif" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/repfun.gif" class="img-fluid" alt="R plot with clinical trial style footnotes and formatting"></a></p>
</section>
<section id="networks" class="level3">
<h3 class="anchored" data-anchor-id="networks">Networks</h3>
<p><a href="https://cran.r-project.org/package=flownet">flownet</a> v0.1.2: Provides high-performance tools for transport modeling: network processing, route enumeration, and traffic assignment. The package implements the Path-Sized Logit model for traffic assignment <a href="https://link.springer.com/chapter/10.1007/978-1-4615-5203-1_2">Ben-Akiva and Bierlaire (1999)</a>, an efficient route enumeration algorithm, and provides powerful utility functions for (multimodal) network generation, consolidation/contraction, and/or simplification. See the <a href="https://cran.r-project.org/web/packages/flownet/vignettes/introduction.html">vignette</a>.</p>
<p><a href="flownet.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/flownet.png" class="img-fluid" alt="Visualization of assigned flows in a network"></a></p>
</section>
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=bayesDiagnostics">bayesDiagnostics</a> v0.1.0: Provides comprehensive tools for Bayesian model diagnostics and comparison, including prior sensitivity analysis, posterior predictive checks <a href="https://www.taylorfrancis.com/books/mono/10.1201/b16018/bayesian-data-analysis-david-dunson-donald-rubin-john-carlin-andrew-gelman-hal-stern-aki-vehtari">Gelman et al.&nbsp;(2013)</a>, advanced model comparison using Pareto-smoothed importance sampling leave-one-out cross-validation <a href="https://link.springer.com/article/10.1007/s11222-016-9696-4">Vehtari et al.&nbsp;(2017)</a>, convergence diagnostics, and prior elicitation tools. Integrates with <code>brms</code>, <code>rstan</code>, and <code>rstanarm</code> packages. See <a href="https://cran.r-project.org/web/packages/bayesDiagnostics/readme/README.html">README</a> to get started and the <a href="https://cran.r-project.org/web/packages/bayesDiagnostics/vignettes/introduction-to-bayesDiagnostics.html">vignette</a> for an introduction.</p>
<p><a href="https://cran.r-project.org/package=gradLasso">gradLasso</a> v0.1.1: Implements LASSO regression using gradient descent with support for Gaussian, Binomial, Negative Binomial, and Zero-Inflated Negative Binomial (ZINB) families. Features cross-validation for determining lambda, stability selection, and bootstrapping for confidence intervals. Methods described in <a href="https://academic.oup.com/jrsssb/article/58/1/267/7027929?login=false">Tibshirani (1996)</a> and <a href="https://academic.oup.com/jrsssb/article-abstract/72/4/417/7076513?redirectedFrom=fulltext&amp;login=false">Meinshausen and Buhlmann (2010)</a>. Look <a href="https://github.com/ddefranza/gradLasso">here</a> for a quick start and see the <a href="https://cran.r-project.org/web/packages/gradLasso/vignettes/intro.html">vignette</a> for an introduction.</p>
<p><a href="gradLasso.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/gradLasso.png" class="img-fluid" alt="Stabiity selection Plot"></a></p>
<p><a href="https://cran.r-project.org/package=mfcurve">mfcurve</a> v1.0.2: Implements multi-factor curve analysis for grouped data replicating and extending the functionality of the <code>Stata mfcurve</code>. See <a href="https://ideas.repec.org/c/boc/bocode/s459224.html">Krähmer (2023)</a> and <a href="https://www.nature.com/articles/s41562-020-0912-z">Simonsohn, Simmons, and Nelson (2020)</a> for background. Functions for preprocessing, statistical testing, and visualization of results with confidence intervals are included. There is an <a href="https://cran.r-project.org/web/packages/mfcurve/vignettes/mfcurve-intro.html">Introduction</a>.</p>
<p><a href="mfcurve.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/mfcurve.png" class="img-fluid" alt="Multi-factor curve analysis plot with confidence intervals"></a></p>
<p><a href="https://cran.r-project.org/package=NMAR">NMAR</a> v0.1.2: Implements methods to estimate finite-population parameters under nonresponse that are not missing at random. Incorporates auxiliary information and user-specified response models, and supports independent samples and complex survey designs via objects from the <code>survey</code> package. See <a href="https://www.tandfonline.com/doi/abs/10.1198/016214502753479338">Qin, Leung and Shao (2002)</a> and <a href="https://academic.oup.com/jssam/article-abstract/4/2/215/2580514?redirectedFrom=fulltext&amp;login=false">Riddles, Kim and Im (2016)</a> for background. There are five vignettes including <a href="https://cran.r-project.org/web/packages/NMAR/vignettes/exptilt_nonparam_theory.html">exptilt nonparam theory</a> and <a href="https://cran.r-project.org/web/packages/NMAR/vignettes/tutorial_empirical_likelihood.html">Empirical Likelihood</a>.</p>
<p><a href="https://cran.r-project.org/package=pmrm">pmrm</a> v0.0.2: A progression model for repeated measures is a continuous-time nonlinear mixed-effects model for longitudinal clinical trials in progressive diseases. Unlike mixed models for repeated measures which estimate treatment effects as linear combinations of additive effects on the outcome scale, PMRMs characterize treatment effects in terms of the underlying disease trajectory yielding clinically interpretable quantities. See <a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.9581">Raket (2022)</a> and <a href="https://www.jstatsoft.org/article/view/v070i05">Kristensen (2016)</a> for background. There are three vignettes: <a href="https://cran.r-project.org/web/packages/pmrm/vignettes/models.html">Models</a>, <a href="https://cran.r-project.org/web/packages/pmrm/vignettes/usage.html">Usage</a> and <a href="https://cran.r-project.org/web/packages/pmrm/vignettes/validation.html">Validation</a>.</p>
<p><a href="pmrm.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/pmrm.png" class="img-fluid" alt="Predictions by trial arm"></a></p>
<p><a href="https://cran.r-project.org/package=RSTr">RSTr</a> v1.1.4: Implements a Gibbs Sampler for Poisson or Binomial discrete spatial data for a variety of Spatiotemporal Conditional Autoregressive (CAR) models. Includes measures to prevent estimate over-smoothing through a restriction of model informativeness for select models. Also provides tools to load output and get median estimates. Methods are from <a href="https://link.springer.com/article/10.1007/BF00116466">Besag, York, and Mollié (1991)</a>, <a href="https://academic.oup.com/biostatistics/article-abstract/4/1/11/246085?redirectedFrom=fulltext&amp;login=false">Gelfand and Vounatsou (2003)</a>, <a href="https://projecteuclid.org/journals/annals-of-applied-statistics/volume-11/issue-4/Multivariate-spatiotemporal-modeling-of-age-specific-stroke-mortality/10.1214/17-AOAS1068.full">Quick et al.&nbsp;(2017)</a>, and <a href="https://www.sciencedirect.com/science/article/abs/pii/S1877584521000198?via%3Dihub">Quick et al.&nbsp;(2021)</a>. There are twelve vignettes, including an <a href="https://cran.r-project.org/web/packages/RSTr/vignettes/RSTr.html">Introduction</a> and the <a href="https://cran.r-project.org/web/packages/RSTr/vignettes/RSTr-car.html">CAR Models</a>.</p>
<p><a href="https://cran.r-project.org/package=uniLasso">uniLasso</a> v2.11: Fits a univariate-guided sparse regression (lasso) by a two-stage procedure. The first stage fits p-separate univariate models to the response. The second stage gives more weight to the more important univariate features, and preserves their signs. It returns an objects that inherit from class <code>glmnet</code>. See <a href="https://hdsr.mitpress.mit.edu/pub/3i97j340/release/4">Chatterjee, Hastie and Tibshirani (2025)</a> for details.</p>
</section>
<section id="time-series" class="level3">
<h3 class="anchored" data-anchor-id="time-series">Time Series</h3>
<p><a href="https://cran.r-project.org/package=rjd3toolkit">rjd3toolkit</a> v3.6.0: Implements an <code>R</code> interface to <a href="https://github.com/jdemetra"><code>JDemetra+ 3.x</code></a> <code>R</code> ecosystem of time series analysis software which provides functions to create outlier regressors, define calendar regressors, fit Unobserved Components AutoRegressive Integrated Moving Average (UCARIMA) models, to test the presence of trading days or seasonal effects, and also to set specifications in pre-adjustment and benchmarking when using <code>rjd3x13</code> or <code>rjd3tramoseats</code>. See the <code>JDemetra</code> link above for details.</p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=automerge">automerge</a> v0.3.1: Provides <code>R</code> bindings to the <a href="https://automerge.org/docs/hello/">Automerge</a> Conflict-free Replicated Data Type (CRDT) library, which enables automatic merging of concurrent changes without conflicts, making it ideal for distributed systems, collaborative applications, and offline-first architectures. See <a href="https://dl.acm.org/doi/10.1145/3359591.3359737">Kleppmann et al.&nbsp;(2019)</a> for background. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/automerge/vignettes/automerge.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/automerge/vignettes/crdt-concepts.html">Understanding CRDTs in Automerge</a>.</p>
<p><a href="https://cran.r-project.org/package=h5lite">h5lite</a> v2.0.0.2: Implements an interface for the Hierarchical Data Format 5 <a href="ttps://www.hdfgroup.org/">HDF5</a> library that bundles the necessary system libraries to ensure easy installation on all platforms. Features smart defaults that automatically map <code>R</code> objects (vectors, matrices, data frames) to efficient <code>HDF5</code> types, removing the need to manage low-level details like data spaces or property lists. There are nine vignettes, including <a href="https://cran.r-project.org/web/packages/h5lite/vignettes/h5lite.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/h5lite/vignettes/parallel-io.html">Parallel Processing</a>.</p>
<p><a href="https://cran.r-project.org/package=softwareRisk">softwareRisk</a> v0.1.0: Provides functions that leverage the network-like architecture of scientific models together with software quality metrics to identify chains of function calls that are more prone to generating and propagating errors. Functions operate on tbl_graph objects representing call dependencies between functions (callers and callees) and compute risk scores for individual functions and for paths (sequences of function calls) based on cyclomatic complexity, in-degree, and betweenness centrality. Supports variance-based uncertainty and sensitivity analyses after <a href="https://www.jstatsoft.org/article/view/v102i05">Puy et al.&nbsp;(2022)</a> to assess how risk scores change under alternative risk definitions. See the <a href="https://cran.r-project.org/web/packages/softwareRisk/vignettes/softwareRisk.html">vignette</a>.</p>
<p><a href="softwareRisk.png" class="lightbox" data-gallery="quarto-lightbox-gallery-20"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/softwareRisk.png" class="img-fluid" alt="Risk network for a Fortran model"></a></p>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=ggguides">ggguides</a> v1.1.4: Extends <code>ggplot2</code> by providing one-liner functions for common legend and guide operations in <code>ggplot2</code>. Simplifies legend positioning, styling, wrapping, and collection across multi-panel plots created with <code>patchwork</code> or <code>cowplot</code>. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/ggguides/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/ggguides/vignettes/styling.html">Styling &amp; Customization</a>.</p>
<p><a href="ggguides.png" class="lightbox" data-gallery="quarto-lightbox-gallery-21"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/ggguides.png" class="img-fluid" alt="Plot with rotated legend labels"></a></p>
<p><a href="https://cran.r-project.org/package=gglycan">gglycan</a> v0.0.3: Extends <code>ggplot2</code> to plot <a href="https://www.ncbi.nlm.nih.gov/glycans/">glycans</a> following the symbol <a href="https://www.ncbi.nlm.nih.gov/glycans/snfg.html">nomenclature for glycans</a> using a standardized visual representation of glycan structures. See the <a href="https://cran.r-project.org/web/packages/gglycan/vignettes/gglycan.html">vignette</a>.</p>
<p><a href="gglycan.png" class="lightbox" data-gallery="quarto-lightbox-gallery-22"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/gglycan.png" class="img-fluid" alt="Sample glycal plot"></a></p>
<p><a href="https://cran.r-project.org/package=ggsced">ggsced</a> v0.1.6: Extends <code>ggplot2</code> to create publication-ready graphics with professional phase change lines, support for multiple baseline designs, and styling functions that follow Single-Case Experimental Design (SCED) visualization conventions. Key functions include adding phase change demarcation lines to existing plots and formatting axes with broken axis appearance, commonly used in single-case research. See the <a href="https://cran.r-project.org/web/packages/ggsced/vignettes/ggsced-vignette.html">vignette</a>.</p>
<p><a href="ggsced.png" class="lightbox" data-gallery="quarto-lightbox-gallery-23"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/ggsced.png" class="img-fluid" alt="Plot of Responding by Session faceted by for multiple conditions faceted by participant"></a></p>
<p><a href="https://cran.r-project.org/package=ggskewboxplots">ggskewboxplots</a> V1.0.0: Extends <code>ggplot2</code> for creating skewed boxplots using several statistical methods, including those of <a href="https://www.jstor.org/stable/2347808?origin=crossref">Kimber (1990)</a>, <a href="https://www.sciencedirect.com/science/article/abs/pii/S0167947307004434?via%3Dihub">Hubert and Vandervieren (2008)</a>, <a href="https://www.pjsor.com/index.php/pjsor/article/view/500">Adil et al.&nbsp;(2015)</a>, <a href="https://pubs.aip.org/aip/acp/article-abstract/1842/1/030034/931355/Modified-boxplot-for-extreme-data?redirectedFrom=fulltext">Babura et al., (2017)</a>, and <a href="https://www.tandfonline.com/doi/full/10.1080/00031305.2018.1448891">Walker et al.&nbsp;(2018)</a>. See the <a href="https://cran.r-project.org/web/packages/ggskewboxplots/vignettes/introduction.html">vignette</a>.</p>
<p><a href="ggskew.png" class="lightbox" data-gallery="quarto-lightbox-gallery-24"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/ggskew.png" class="img-fluid" alt="Boxplot using the Walker method"></a></p>
<p><a href="https://cran.r-project.org/package=vbracket">vbracket</a> v1.1.0: Extends <code>ggplot2</code> by adding publication-quality custom legends with vertical brackets. Designed for displaying statistical comparisons between groups, commonly used in scientific publications for showing significance levels. Features include adaptive positioning, automatic bracket spacing for overlapping comparisons, font family inheritance, and support for asterisks, p-values, or custom labels. Look <a href="https://github.com/h20gg702/vbracket">here</a> for examples.</p>
<p><a href="vbracket.png" class="lightbox" data-gallery="quarto-lightbox-gallery-25"><img src="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/vbracket.png" class="img-fluid" alt="Plots with brackets and statistical annotations"></a></p>
</section>
</div><p>v</p>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/january-2026-top-40-new-cran-packages/</guid>
  <pubDate>Fri, 27 Feb 2026 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/january-2026-top-40-new-cran-packages/kuzco.png" medium="image" type="image/png" height="103" width="144"/>
</item>
<item>
  <title>December 2025 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/december-2025-top-40-new-cran-packages/</link>
  <description><![CDATA[ 





<p>In December, one hundred ninety-six new packages made it to CRAN. Here are my Top 40 picks in nineteen categories: Agriculture, Artificial Intelligence, Audio Analysis, Causal Inference, Computational Methods, Ecology, Econometrics, Epidemiology, Genetics, Genomics, Machine Learning, Medicine, Mathematics, Mediation Analysis, Medical Statistics, Pharma, Statistics, Time Series, Utilities, and Visualization.</p>
<div class="columns">
<div class="column" style="width:45%;">
<section id="agriculture" class="level3">
<h3 class="anchored" data-anchor-id="agriculture">Agriculture</h3>
<p><a href="https://cran.r-project.org/package=manureshed">manureshed</a> v0.1.2: Implements a framework for analyzing agricultural nutrient balances across multiple spatial scales with integration of wastewater treatment plant effluent loads for both nitrogen and phosphorus. Supports classification of spatial units as nutrient sources, sinks, or balanced areas based on agricultural surplus and deficit calculations. Built-in datasets include agricultural nutrient balance data from <a href="https://nugis.tfi.org/tabular_data/">NuGIS; The Fertilizer Institute and Plant Nutrition Canada, 1987-2016)</a> and U.S. Environmental Protection Agency wastewater discharge <a href="https://echo.epa.gov/trends/loading-tool/water-pollution-search">ECHO Discharge Monitoring Report (2007-2016)</a>. See <a href="https://www.sciencedirect.com/science/article/pii/S0921344925005749">Akanbi et al.&nbsp;(2025)</a> for the methodology. There are seven vignettes, including <a href="https://cran.r-project.org/web/packages/manureshed/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/manureshed/vignettes/advanced-features.html">Advanced Features</a>.</p>
</section>
<section id="artificial-intelligence" class="level3">
<h3 class="anchored" data-anchor-id="artificial-intelligence">Artificial Intelligence</h3>
<p><a href="https://cran.r-project.org/package=gooseR">gooseR</a> v0.1.1: Integrates <a href="https://github.com/block/goose">Goose AI</a> capabilities including memory management, visualization enhancements, and workflow automation. Save <code>R</code> objects to <code>Goose</code> memory, apply Block branding to visualizations, and manage data science project workflows. There are six vignettes, including <a href="https://cran.r-project.org/web/packages/gooseR/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/gooseR/vignettes/code-review-testing.html">Code Review and Testing</a>.</p>
<p><a href="https://cran.r-project.org/package=pairwiseLLM">pairwiseLLM</a> v1.1.0: Provides a unified framework for generating, submitting, and analyzing pairwise comparisons of writing quality using large language models (LLMs). The package supports live and/or batch evaluation workflows across multiple providers (OpenAI, Anthropic, Google Gemini, Together AI, and locally-hosted Ollama models). Results can be modeled using <a href="https://www.jstor.org/stable/2334029?origin=crossref">Bradley–Terry (1952)</a> or Elo rating methods as described in <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0190393">Clark et al.&nbsp;(2018)</a> to derive writing quality scores. For information on the method of pairwise comparisons, see <a href="https://psycnet.apa.org/doiLanding?doi=10.1037%2Fh0070288">Thurstone (1927)</a> and <a href="https://link.springer.com/article/10.1007/BF03216919">Heldsinger &amp; Humphry (2010)</a>. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/pairwiseLLM/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/pairwiseLLM/vignettes/advanced-batch-workflows.html">Advanced</a>.</p>
</section>
<section id="audio-analysis" class="level3">
<h3 class="anchored" data-anchor-id="audio-analysis">Audio Analysis</h3>
<p><a href="https://cran.r-project.org/package=ReVAMP">ReVAMP</a> v1.0.1: Provides an interface to the <a href="https://www.vamp-plugins.org/">Vamp</a> audio analysis plugin system developed by Queen Mary University of London’s Centre for Digital Music that enables loading and running <code>Vamp</code> plugins for various audio analysis tasks, including tempo detection, onset detection, spectral analysis, and audio feature extraction. Supports mono and stereo audio with automatic channel adaptation and domain conversion. There is an <a href="https://cran.r-project.org/web/packages/ReVAMP/vignettes/introduction.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/ReVAMP/vignettes/vamp-paths.html">Managing Plugin Paths</a>.</p>
<p><a href="ReVAMP.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/ReVAMP.png" class="img-fluid" alt="Schema of VAMP plugin architecture"></a></p>
</section>
<section id="causal-inference" class="level3">
<h3 class="anchored" data-anchor-id="causal-inference">Causal Inference</h3>
<p><a href="https://cran.r-project.org/package=caugi">caugi</a> v1.0.0: Implements a simple interface to build, structure, and examine causal relationships. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/caugi/vignettes/get_started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/caugi/vignettes/visualization.html">Visualizing Causal Graphs</a>.</p>
<p><a href="caugi.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/caugi.png" class="img-fluid" alt="Causal graph"></a></p>
</section>
<section id="computational-methods" class="level3">
<h3 class="anchored" data-anchor-id="computational-methods">Computational Methods</h3>
<p><a href="https://cran.r-project.org/package=bigPLSR">bigPLSR</a> v0.7.2: Provides fast partial least squares (PLS) for dense and out-of-core data that is optimized for <code>bigmemory</code>-backed matrices with streamed cross-products and chunked BLAS. For details, see <a href="https://hal.science/hal-05352069">Bertrand and Maumy (2023)</a>, <a href="https://hal.science/hal-05352069">Bertrand and Maumy (2023b)</a>, and <a href="https://analyticalsciencejournals.onlinelibrary.wiley.com/doi/10.1002/%28SICI%291099-128X%28199701%2911%3A1%3C73%3A%3AAID-CEM435%3E3.0.CO%3B2-%23">Dayal and MacGregor (1997)</a>. Features include kernel logistic PLS with <code>C++</code>-accelerated alternating iteratively reweighted least squares (IRLS) updates, streamed reproducing kernel Hilbert space (RKHS) solvers with reusable centering statistics, and bootstrap diagnostics with graphical summaries for coefficients, scores, and cross-validation workflows. There are fourteen vignettes, including <a href="https://cran.r-project.org/web/packages/bigPLSR/vignettes/bigPLSR-auto-selection.html">Automatic Algorithm Selection</a> and <a href="https://cran.r-project.org/web/packages/bigPLSR/vignettes/plotting-guide.html">Visualizing PLS Fits</a>.</p>
<p><a href="bigPLSR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/bigPLSR.png" class="img-fluid" alt="Bilot with groped individuals"></a></p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://cran.r-project.org/package=ambiR">ambiR</a> v0.1.1: Provides functions to calculate <a href="https://ambi.azti.es/">AZTI’s Marine Biotic Index</a> - AMBI of benthic fauna species according to their sensitivity to pollution. The Shannon Diversity Index H’ and the Danish benthic fauna quality index DKI (Dansk Kvalitetsindeks) can also be calculated, as well as the multivariate M-AMBI index. See <a href="https://www.sciencedirect.com/science/article/abs/pii/S0025326X00000618">Borja et al.&nbsp;(2000)</a> for background. There are four vignettes, including <a href="https://cran.r-project.org/web/packages/ambiR/vignettes/ambiR.html">Get Started</a> and <a href="https://cran.r-project.org/web/packages/ambiR/vignettes/background.html">The AMBI index</a>.</p>
<p><a href="ambiR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/ambiR.png" class="img-fluid" alt="Plot of Species groub vs. Ecological Group"></a></p>
<p><a href="https://cran.r-project.org/package=SHARK4R">SHARK4R</a> v1.0.3: Provides functions to retrieve, process, analyze, and quality-control marine physical, chemical, and biological data. The main focus is on Swedish monitoring data available through the <a href="https://shark.smhi.se/en/">SHARK database</a>, with additional API support for <a href="https://nordicmicroalgae.org/">Nordic Microalgae</a>, <a href="https://artfakta.se/">Dyntaxa</a>, World Register of Marine Species <a href="https://www.marinespecies.org">WoRMS</a>, <a href="https://www.algaebase.org">AlgaeBase</a>, OBIS <a href="https://iobis.github.io/xylookup/">xylookup</a> web service, and Intergovernmental Oceanographic Commission (IOC) - UNESCO databases on <a href="https://www.marinespecies.org/hab/">harmful algae</a> and <a href="https://toxins.hais.ioc-unesco.org/">toxins</a>. There are five vignettes including <a href="https://cran.r-project.org/web/packages/SHARK4R/vignettes/quality_control.html">Quality Control of SHARK4R Data</a> and <a href="https://cran.r-project.org/web/packages/SHARK4R/vignettes/retrieve_shark_data.html">Retrieve Data From SHARK</a>.</p>
</section>
<section id="econometrics" class="level3">
<h3 class="anchored" data-anchor-id="econometrics">Econometrics</h3>
<p><a href="https://cran.r-project.org/package=gvcAnalyzer">gvcAnalyzer</a> v0.1.1: Provides tools for decomposing Global Value Chain (GVC) participation and value-added trade. It implements the frameworks proposed by <a href="https://www.tandfonline.com/doi/abs/10.1080/09535314.2022.2153221">Borin and Mancini (2023)</a> for source-based and sink-based decompositions, and by <a href="https://academic.oup.com/wber/advance-article/doi/10.1093/wber/lhaf017/8237808">Borin, Mancini, and Taglioni (2025)</a> for tripartite and output-based GVC measures. There are three vignettes including an <a href="https://cran.r-project.org/web/packages/gvcAnalyzer/vignettes/gvcAnalyzer-intro.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/gvcAnalyzer/vignettes/bm2023-vs-bm2025.html">Trade vs Output Perspectives</a>.</p>
<p><a href="gvcA.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/gvcA.png" class="img-fluid" alt="Plot of trade-based vs. output-based GVC participation"></a></p>
</section>
<section id="epidemiology" class="level3">
<h3 class="anchored" data-anchor-id="epidemiology">Epidemiology</h3>
<p><a href="https://cran.r-project.org/package=MetaRVM">MetaRVM</a> v1.0.1: Simulates respiratory virus epidemics using meta-population compartmental models. Following <a href="https://www.medrxiv.org/content/10.1101/2025.05.05.25327021v1">Fadikar et. al.&nbsp;(2025)</a> it implements a stochastic SEIRD (Susceptible-Exposed-Infected-Recovered-Dead) framework with demographic stratification by age, race, and geographic zones that supports complex epidemiological scenarios including asymptomatic and presymptomatic transmission, hospitalization dynamics, vaccination schedules, and time-varying contact patterns via mixing matrices. There are four vignettes including <a href="">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/MetaRVM/vignettes/running-a-simulation.html">Running a Simulation</a>.</p>
<p><a href="MetaRVM.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/MetaRVM.png" class="img-fluid" alt="Plot of hospitialization days by age group"></a></p>
<p><a href="https://cran.r-project.org/package=sitrep">sitrep</a> v0.4.0: Loads the complete sitrep ecosystem for applied epidemiology analysis. This package provides report templates and automatically loads companion packages, including <code>epitabulate</code> (for epidemiological tables), <code>epidict</code> (for data dictionaries), <code>epikit</code> (for epidemiological utilities), and <code>apyramid</code> (for age-sex pyramids). There are seven vignettes including <a href="https://cran.r-project.org/web/packages/sitrep/vignettes/Background.html">Origin Story</a> and <a href="https://cran.r-project.org/web/packages/sitrep/vignettes/measles_intersectional_outbreak_guide.html">Guide: Measles Outbreak report</a>.</p>
</section>
<section id="genetics" class="level3">
<h3 class="anchored" data-anchor-id="genetics">Genetics</h3>
<p><a href="https://cran.r-project.org/package=EZbakR">EZbakR</a> v0.1.0: A complete rewrite and reimagining of <code>bakR</code>, <a href="https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1013179">Vock et al.&nbsp;(2025)</a>, designed to support a wide array of analyses of nucleotide recoding RNA-seq datasets of any type, including TimeLapse-seq/SLAM-seq/TUC-seq, Start-TimeLapse-seq, TT-TimeLapse-seq, and subcellular NR-seq. It extends standard NR-seq standard NR-seq mutational modeling to support multi-label analyses, and implements an improved hierarchical model to better account for transcript-to-transcript variance in metabolic label incorporation, and also generalized dynamical systems modeling of NR-seq data to support analyses of premature mRNA processing and flow between subcellular compartments. There are seven vignettes, including <a href="https://cran.r-project.org/web/packages/EZbakR/vignettes/Quickstart.html">Quickstart</a> and <a href="https://cran.r-project.org/web/packages/EZbakR/vignettes/Linear-modeling.html">Linear Modeling</a>.</p>
<p><a href="EZbakR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/EZbakR.png" class="img-fluid" alt="Volcano plot showing a comparative analysis"></a></p>
<p><a href="https://cran.r-project.org/package=gpyramid">gpyramid</a> v0.0.1: Provides functions to identify efficient crossing schemes for gene pyramiding and calculates the cost of crossing in terms of the number of individuals and generations, which has been theoretically formulated by <a href="https://academic.oup.com/genetics/article-abstract/168/1/513/6059522?redirectedFrom=fulltext&amp;login=false">Servin et al.&nbsp;(2004)</a>. The package has been designed for selecting appropriate parental genotypes and finding the most efficient crossing scheme for gene pyramiding, especially for plant breeding. There is an <a href="https://cran.r-project.org/web/packages/gpyramid/vignettes/Introduction.html">Introduction</a> and a <a href="https://cran.r-project.org/web/packages/gpyramid/vignettes/Tutorial.html">Tutorial</a>.</p>
<p><a href="gpyramid.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/gpyramid.png" class="img-fluid" alt="Example gene pyramid plot"></a></p>
<p><a href="https://cran.r-project.org/package=phylospatial">phylospatial</a> v1.2.1: Provides functions to analyze spatial phylogenetic diversity patterns. Use your data on an evolutionary tree and geographic distributions of the terminal taxa to compute diversity and endemism metrics, test significance with null model randomization, analyze community turnover and biotic regionalization, and perform spatial conservation prioritizations. All functions support quantitative community data in addition to binary data. There are four vignettes, including <a href="https://cran.r-project.org/web/packages/phylospatial/vignettes/phylospatial-data.html">phylospatial data</a> and <a href="https://cran.r-project.org/web/packages/phylospatial/vignettes/prioritization.html">Prioritization</a>.</p>
<p><a href="phylospatial.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/phylospatial.png" class="img-fluid" alt="Plots of Phylogenetic community ordination for first four PCA values"></a></p>
</section>
<section id="genomics" class="level3">
<h3 class="anchored" data-anchor-id="genomics">Genomics</h3>
<p><a href="https://cran.r-project.org/package=CimpleG">CimpleG</a> v1.0.1: Provides a method for the detection of small CpG methylation signatures used for cell-type classification and deconvolution that is time efficient and performs as well as top performing methods for cell-type classification of blood cells and other somatic cells. Predictions are based on a single DNA methylation site per cell type, but users can also select more sites if they so wish. Users can train cell type classifiers and directly apply these in a deconvolution of cell mixes context. See <a href="https://link.springer.com/article/10.1186/s13059-023-03000-0">Maié et al.&nbsp;(2023)</a> for details and the vignettes <a href="https://cran.r-project.org/web/packages/CimpleG/vignettes/generate-signatures.html">Genetic signatures</a> and <a href="https://cran.r-project.org/web/packages/CimpleG/vignettes/save_load_objects.html">Quickly save and load large objects</a>.</p>
</section>
<section id="machine-learning" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning">Machine Learning</h3>
<p><a href="https://cran.r-project.org/package=LBBNN">LBBNN</a> v0.1.4: Implements an interface to the <code>LibTorch</code> backend to provide latent binary Bayesian neural networks via <code>torch</code>. Supports mean-field variational inference as well as flexible variational posteriors using normalizing flows. The standard LBBNN implementation follows <a href="https://www.mdpi.com/2227-7390/12/6/788">Hubin and Storvik (2024)</a> and uses the local reparametrization trick as in <a href="https://openreview.net/pdf?id=d6kqUKzG3V">Skaaret-Lund et al.&nbsp;(2024)</a>. Input-skip connections are implemented as described in <a href="https://arxiv.org/abs/2503.10496">Høyheim et al.&nbsp;(2025)</a>. See the <a href="https://cran.r-project.org/web/packages/LBBNN/vignettes/LBBNN_tutorial.html">vignette</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=snic">snic</a> v0.6.1: Implements the Simple Non-Iterative Clustering algorithm for superpixel segmentation of multi-band images, as introduced by <a href="https://ieeexplore.ieee.org/document/8100003">Achanta and Susstrunk (2017)</a>, and supports both standard image arrays and geospatial raster objects, with a design that can be extended to other spatial data frameworks. The algorithm groups adjacent pixels into compact, coherent regions based on spectral similarity and spatial proximity. A high-performance implementation supports images with arbitrary spectral bands. See the vignettes about <a href="https://cran.r-project.org/web/packages/snic/vignettes/snic-array-pipeline.html">Arrays</a> and <a href="https://cran.r-project.org/web/packages/snic/vignettes/snic-spatraster-pipeline.html">SpatRaster</a> segmentation pipelines.</p>
<p><a href="snic.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/snic.png" class="img-fluid" alt="Clownfish image with superpixel segmentation boundaries overlaid."></a></p>
</section>
<section id="mathematics" class="level3">
<h3 class="anchored" data-anchor-id="mathematics">Mathematics</h3>
<p><a href="https://cran.r-project.org/package=algebraic.dist">algebraic.dist</a> v0.1.0: Provides an algebra over probability distributions enabling composition, sampling, and automatic simplification to closed forms. Supports normal, exponential, multivariate normal, and empirical distributions with operations like addition and subtraction that automatically simplify when mathematical identities apply (e.g., the sum of independent normal distributions is normal).</p>
<p><a href="algebraic.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/algebraic.png" class="img-fluid" alt="Scatter plot of sampled vs. computed distribution values"></a></p>
</section>
<section id="mediation-analysis" class="level3">
<h3 class="anchored" data-anchor-id="mediation-analysis">Mediation Analysis</h3>
<p><a href="https://cran.r-project.org/package=wsMed">wsMed</a> v1.0.2: Provides tools for within-subject mediation analysis using structural equation modeling to examine how changes in an outcome variable between two conditions are mediated through one or more variables. Supports within-subject mediation analysis using the <code>lavaan</code> package by <a href="https://www.jstatsoft.org/article/view/v048i02">Rosseel (2012)</a>, and extends Monte Carlo confidence interval estimation to missing data scenarios using the <code>semmcci</code> package by <a href="https://doi.org/10.3758%2Fs13428-023-02114-4">Pesigan and Cheung (2023)</a>. There are six vignettes, including <a href="https://cran.r-project.org/web/packages/wsMed/vignettes/WsMed.html">wsMed</a> and <a href="https://cran.r-project.org/web/packages/wsMed/vignettes/GenerateModelP.html">GenerateModelIP</a>.</p>
<p><a href="wsMed.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/wsMed.png" class="img-fluid" alt="Plot of moderation curve"></a></p>
</section>
<section id="medical-statistics" class="level3">
<h3 class="anchored" data-anchor-id="medical-statistics">Medical Statistics</h3>
<p><a href="https://cran.r-project.org/package=cifmodeling">cifmodeling</a> v0.9.8: Implements a publication-ready toolkit for modern survival and competing risks analysis with a minimal, formula-based interface. Both nonparametric estimation and direct polytomous regression of cumulative incidence functions (CIFs) are supported. Functions estimate survival and CIF curves, produce high-quality graphics with risk tables, censoring and competing-risk marks, and multi-panel or inset layouts. All core functions adopt a formula-and-data syntax and return tidy and extensible outputs. Key numerical routines are implemented in <code>C++</code>. There are seven vignettes including <a href="https://cran.r-project.org/web/packages/cifmodeling/vignettes/v1_overview.html">Overview</a> and <a href="https://cran.r-project.org/web/packages/cifmodeling/vignettes/v4_examples.html">Examples</a>.</p>
<p><a href="cifmodeling.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/cifmodeling.png" class="img-fluid" alt="Example of a survivl curve"></a></p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="medical-statistics-continued" class="level3">
<h3 class="anchored" data-anchor-id="medical-statistics-continued">Medical Statistics Continued</h3>
<p><a href="https://cran.r-project.org/package=eVCGsampler">eVCGsampler</a> v0.9.2: Provides a principled framework for sampling Virtual Control Group (VCG) using energy distance-based covariate balancing, offers visualization tools to assess covariate balance, and includes a permutation test to evaluate the statistical significance of observed deviations. See the <a href="https://cran.r-project.org/web/packages/eVCGsampler/vignettes/eVCGsampler_user_guide.html">vignette</a>.</p>
<p><a href="eVCGsampler.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/eVCGsampler.png" class="img-fluid" alt="Plot of energy Balancing Results"></a></p>
<p><a href="https://cran.r-project.org/package=fgdiR">fgdiR</a> v0.1.0: provides tools for computing the Functional Gait Deviation Index, a novel index for quantifying gait pathology using multivariate functional principal component analysis and supports analysis at the level of both legs combined, individual legs, and individual joints/planes. It includes functions for functional data preprocessing, multivariate functional principal component decomposition, FGDI computation, and visualization of gait abnormality scores. See <a href="https://www.tandfonline.com/doi/full/10.1080/02664763.2025.2514150">Minhas et al.&nbsp;(2025)</a> for background and the vignettes <a href="https://cran.r-project.org/web/packages/fgdiR/vignettes/fgdiR-amutee.html">Amputee Gait Analysis</a> and <a href="https://cran.r-project.org/web/packages/fgdiR/vignettes/fgdiR-parkinson.html">Parkinson Gait Analysis</a>.</p>
<p><a href="fgdiR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/fgdiR.png" class="img-fluid" alt="Plot of Pelvis Titl by % of Gait Cycle"></a></p>
</section>
<section id="pharma" class="level3">
<h3 class="anchored" data-anchor-id="pharma">Pharma</h3>
<p><a href="https://cran.r-project.org/package=aNCA">aNCA</a> v0.1.0: Provides an interactive <code>shiny</code> application for performing non-compartmental analysis on pre-clinical and clinical pharmacokinetic data and provides interactive visualizations, CDISC outputs (ADNCA, PP, ADPP) and configurable TLGs (tables, listings, and graphs). Methods and core estimators are described in <a href="https://link.springer.com/article/10.1007/s10928-015-9432-2">Denney, Duvvuri, and Buckeridge (2015)</a>. There are three vignettes, including a <a href="https://cran.r-project.org/web/packages/aNCA/vignettes/aNCA.html">User Guide</a> and <a href="https://cran.r-project.org/web/packages/aNCA/vignettes/adding_tlg.html">Adding new TLGs</a>.</p>
<p><a href="aNCA.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/aNCA.png" class="img-fluid" alt="Shiny app screenshot"></a></p>
<p><a href="https://cran.r-project.org/package=httkexamples">httkexamples</a> v0.0.1: Provide examples as vignettes using High throughput toxicokinetics (HTTK) methods to solve various problems in bioinformatics, toxicology, and exposure science. In accordance with <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0321321">Davidson-Fritz et al.&nbsp;(2025)</a>, whenever a new HTTK model is developed, the code to generate the figures evaluating that model is added as a new vignette. HTTKis the combination of chemical-specific in vitro measurements or in silico predictions and generic mathematical models, to predict absorption, distribution, metabolism, and excretion by the body. See <a href="https://www.jstatsoft.org/article/view/v079i04">Pearce et al.&nbsp;(2017)</a> and <a href="https://www.tandfonline.com/doi/full/10.1080/17425255.2021.1935867">Breen et al.&nbsp;(2021)</a> for background. There are thirteen vignettes including <a href="https://cran.r-project.org/web/packages/httkexamples/vignettes/Va_Pearce2017.html">HTTK Basics</a> and <a href="https://cran.r-project.org/web/packages/httkexamples/vignettes/Vb_Ring2017.html">Generating subpopulations</a>.</p>
<p><a href="httk.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/httk.png" class="img-fluid" alt="Plot of plasma concentration by day"></a></p>
<p><a href="distionary.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/distionary.png" class="img-fluid" alt="Diagram showing how distributions are created"></a></p>
</section>
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=distionary">distionary</a> v0.1.0: Provides functions to create and evaluate probability distribution objects from a variety of families or define custom distributions and automatically compute distributional properties, even when they have not been specified. This package supports statistical modeling and simulations, and forms the core of the <code>probaverse</code> suite of R packages. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/distionary/vignettes/evaluate.html">Evaluate a Distribution</a> and <a href="https://cran.r-project.org/web/packages/distionary/vignettes/specify-user-defined.html">Specifying Your Own Distribution</a>.</p>
<p><a href="https://cran.r-project.org/package=jollofR">jollofR</a> v0.6.5: Provides functions to automatically disaggregate small-area population estimates by demographic groups (e.g., age, sex, race, marital status, educational level, etc) along with the estimates of uncertainty, using advanced Bayesian statistical modelling approaches based on integrated nested Laplace approximation (INLA) <a href="https://academic.oup.com/jrsssb/article-abstract/71/2/319/7092907?redirectedFrom=fulltext&amp;login=false">Rue et al.&nbsp;(2009)</a>, and stochastic partial differential equation methods <a href="https://academic.oup.com/jrsssb/article-abstract/73/4/423/7034732?redirectedFrom=fulltext&amp;login=false">Lindgren et al.&nbsp;(2011)</a>, hierarchical Bayesian modeling frameworks for small area estimation as described in <a href="https://www.pnas.org/doi/full/10.1073/pnas.1913050117">Leasure et al.&nbsp;(2020)</a>, and <a href="https://www.nature.com/articles/s41467-025-59862-4">Nnanatu et al.&nbsp;(2025)</a>. See <a href="https://cran.r-project.org/web/packages/jollofR/readme/README.html">README</a> for package details and examples.</p>
<p><a href="https://cran.r-project.org/package=PSsurvival">PSsurvival</a> v0.2.0: Implements propensity score weighting methods for estimating counterfactual survival functions, marginal hazard ratios, and weighted Kaplan-Meier and cumulative risk curves in observational studies with time-to-event outcomes. Supports binary and multiple treatment groups with inverse probability of treatment weighting, overlap weighting, and average treatment effect on the treated, and includes symmetric trimming (Crump extension) for extreme propensity scores. Implements variance estimation via analytical M-estimation or bootstrap. Methods based on <a href="https://www.tandfonline.com/doi/full/10.1080/01621459.2016.1260466">Li et al.&nbsp;(2018)</a>, <a href="https://projecteuclid.org/journals/annals-of-applied-statistics/volume-13/issue-4/Propensity-score-weighting-for-causal-inference-with-multiple-treatments/10.1214/19-AOAS1282.full">Li &amp; Li (2019)</a>, and <a href="https://academic.oup.com/aje/article/191/6/1140/6541565?login=false">Cheng et al.&nbsp;(2022)</a>. See the <a href="https://cran.r-project.org/web/packages/PSsurvival/vignettes/PSsurvival-tutorial.html">vignette</a>.</p>
<p><a href="PSsurvival.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/PSsurvival.png" class="img-fluid" alt="Plot of estimated treatmenteffects"></a></p>
<p><a href="https://cran.r-project.org/package=quantbayes">quantbayes</a> v0.1.0: Implements the Quantification Evidence Standard algorithm for computing Bayesian evidence sufficiency from binary evidence matrices and provides posterior estimates, credible intervals, percentiles, and optional visual summaries. The method is universal, reproducible, and independent of any specific clinical or rule-based framework. See <a href="https://www.medrxiv.org/content/10.64898/2025.12.02.25341503v3">The Quantitative Omics Epidemiology Group et al.&nbsp;(2025)</a> for details and the <a href="https://cran.r-project.org/web/packages/quantbayes/index.html">vignette</a> for examples.</p>
<p><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/quantbayes.png" class="img-fluid" alt="Plot of posterior theta distribution"> <a href="https://cran.r-project.org/package=tipse">tipse</a> v1.2: Implements tipping point sensitivity analysis for time-to-event endpoints under different missing data scenarios, as described in <a href="https://arxiv.org/abs/2506.19988">Oodally et al.&nbsp;(2025)</a>. Supports both model-based and model-free imputation, multiple imputation workflows, plausibility assessment, and visualizations. Enables robust assessment for regulatory and exploratory analyses. See the <a href="https://cran.r-project.org/web/packages/tipse/vignettes/analysis_examples.html">vignette</a>.</p>
<p><a href="tipse.png" class="lightbox" data-gallery="quarto-lightbox-gallery-20"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/tipse.png" class="img-fluid" alt="Plot of pooled Kaplan-Meier curves"></a></p>
<p><a href="https://cran.r-project.org/package=weightedsurv">weightedsurv</a> v0.1.0: Provides survival analysis functions with support for time-dependent and subject-specific (e.g., propensity score) weighting. Implements weighted estimation for Cox models, Kaplan-Meier survival curves, and treatment differences with point-wise and simultaneous confidence bands. Includes restricted mean survival time comparisons evaluated across all potential truncation times with both point-wise and simultaneous confidence bands. See <a href="https://www.sciencedirect.com/science/article/abs/pii/S0169260703001378">Cole &amp; Hernán (2004)</a> for methodological background and the <a href="https://cran.r-project.org/web/packages/weightedsurv/vignettes/weightedsurv_examples.html">vignette</a> for examples.</p>
<p><a href="weightedsurv.png" class="lightbox" data-gallery="quarto-lightbox-gallery-21"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/weightedsurv.png" class="img-fluid" alt="Plots of weighted and unweighted survival curves."></a></p>
</section>
<section id="time-series" class="level3">
<h3 class="anchored" data-anchor-id="time-series">Time Series</h3>
<p><a href="https://cran.r-project.org/package=bpvars">bpvars</a> v1.0: Provides Bayesian estimation and forecasting of dynamic panel data using Bayesian Panel Vector Autoregressions with hierarchical prior distributions. Models include country-specific VARs that share a global prior distribution that extends the model by <a href="https://onlinelibrary.wiley.com/doi/10.1002/jae.1082">Jarociński (2010)</a>. Functions provide model specification, estimation, and forecasting routines, facilitating coherent workflows and reproducibility and includes automated pseudo-out-of-sample forecasting and computation of forecasting performance measures and algorithms written in <code>C++</code>. See <a href="https://cran.r-project.org/web/packages/bpvars/readme/README.html">README</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=gctsc">gctsc</a> V0.1.3: Implements Gaussian copula models for count time series. Includes simulation utilities, likelihood approximation, maximum-likelihood estimation, residual diagnostics, and predictive inference. Implements the Time Series Minimax Exponential Tilting method, an adaptation of Minimax Exponential Tilting <a href="https://academic.oup.com/jrsssb/article/79/1/125/7041924?login=false">Botev, 2017</a>, and the Vecchia-based tilting framework of <a href="https://www.tandfonline.com/doi/full/10.1080/01621459.2025.2546586">Cao and Katzfuss (2025)</a>. Also provides a linear-cost implementation of the Geweke–Hajivassiliou–Keane simulator inspired by <a href="https://projecteuclid.org/journals/electronic-journal-of-statistics/volume-6/issue-none/Gaussian-copula-marginal-regression/10.1214/12-EJS721.full">Masarotto and Varin (2012)</a>, and the Continuous Extension approximation of <a href="https://www.tandfonline.com/doi/full/10.1080/02664763.2025.2498502">Nguyen and De Oliveira (2025)</a>. See the <a href="https://cran.r-project.org/web/packages/gctsc/vignettes/gctsc_vignette.html">vignette</a>.</p>
<p><a href="gctsc.png" class="lightbox" data-gallery="quarto-lightbox-gallery-22"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/gctsc.png" class="img-fluid" alt="Plot of weeklt campylobacter incidence"></a></p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=fjoin">fjoin</a> v0.1.0: Extends <code>data.table</code> join functionality to work with any data frame class, and provides a familiar x/y-style interface, enabling broad use across R. Offers NA-safe matching by default, on-the-fly column selection, multiple match-handling on both sides, x or y row order, and a row origin indicator. Performs inner, left, right, full, semi- and anti-joins with equality and inequality conditions, plus cross joins. Specific support for <code>data.table</code>, (grouped) tibble, and sf/sfc objects and their attributes; returns a plain data frame otherwise. Avoids data-copying of inputs and outputs. Allows displaying the <code>data.table</code> code instead of (or as well as) executing it. See the <a href="https://cran.r-project.org/web/packages/fjoin/vignettes/fjoin.html">vignette</a>.</p>
<p><a href="fjoin.png" class="lightbox" data-gallery="quarto-lightbox-gallery-23"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/fjoin.png" class="img-fluid" alt="Median time to execute commo tasks"></a></p>
<p><a href="https://cran.r-project.org/package=meetupr">meetupr</a> v0.3.1: Provides programmatic access to the <a href="https://www.meetup.com/graphql/">Meetup GraphQL API</a> enabling users to retrieve information about groups, events, and members from <a href="https://www.meetup.com/">Meetup</a>. Supports authentication via <code>OAuth2</code> and includes functions for common queries and data manipulation tasks. There are four vignettes, including <a href="https://cran.r-project.org/web/packages/meetupr/vignettes/meetupr.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/meetupr/vignettes/introspection.html">Meetup API Schema Introspection</a>.</p>
<p><a href="https://cran.r-project.org/package=odiffr">odiffr</a> v0.5.1: Implements <code>R</code> bindings to <a href="https://github.com/dmtrKovalenko/odiff"><code>odiff</code></a>, a pixel-by-pixel image comparison tool. Supports PNG, JPEG, WEBP, and TIFF with configurable thresholds, antialiasing detection, and region ignoring. Requires system installation of <code>odiff</code>. Ideal for visual regression testing in automated workflows. See the <a href="https://cran.r-project.org/web/packages/odiffr/vignettes/getting-started.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=svgedit">svgedit</a> v1.0.0: Provides functions to edit <code>SVG</code> files created in <code>Inkscape</code> by replacing placeholders (e.g., a rectangle element or {} in a text box by <code>ggplot2</code> objects, images, or text. This helps automate the creation of figures with complex layouts. See the <a href="https://cran.r-project.org/web/packages/svgedit/vignettes/multi-panel-figure.html">vignette</a>.</p>
<p><a href="svgedit.svg" class="lightbox" data-gallery="quarto-lightbox-gallery-24"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/svgedit.svg" class="img-fluid" alt="Edit of multipanel SVG figure"></a></p>
<p><a href="https://cran.r-project.org/package=taskqueue">taskqueue</a> v0.2.0: Implements a task queue system for asynchronous parallel computing using <a href="https://www.postgresql.org/"><code>PostgreSQL</code></a> as a backend. Designed for embarrassingly parallel problems where tasks do not communicate with each other. Dynamically distributes tasks to workers, handles uneven load balancing, and allows new workers to join at any time. Particularly useful for running large numbers of independent tasks on high-performance computing (HPC) clusters with <code>SLURM</code> <a href="https://slurm.schedmd.com/" class="uri">https://slurm.schedmd.com/</a> job schedulers.</p>
<p><a href="https://cran.r-project.org/package=yaml12">yaml12</a> v0.1.0: Provides a fast, correct, safe, and ergonomic <code>YAML</code> 1.2 parser and generator written in <code>Rust</code>, enabling conversion between <code>YAML</code> and simple <code>R</code> objects with full support for multi-document streams, tags, anchors, and aliases. Offers opt-in handlers for custom tag behavior and round-trips common <code>R</code> data structures. Implements the <a href="https://yaml.org/spec/1.2.2/"><code>YAML</code> 1.2.2 specification</a>. There are two vignettes, <a href="https://cran.r-project.org/web/packages/yaml12/vignettes/yaml-2-minute-intro.html">YAML in 2 minutes</a> and <a href="https://cran.r-project.org/web/packages/yaml12/vignettes/yaml-tags-and-advanced-features.html">YAML Tags</a>.</p>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=blockr.ggplot">blockr.ggplot</a> v0.1.0: Extends <code>blockr.core</code> with interactive blocks for data visualization using <code>ggplot2</code>. Users can build charts through a graphical interface without writing code directly. Includes common chart types (bar charts, line charts, pie charts, scatter plots) as well as statistical plots (boxplots, histograms, density plots, violin plots) with rich customization options and intuitive user interfaces. See the <a href="https://cran.r-project.org/web/packages/blockr.ggplot/vignettes/blockr-ggplot-showcase.html">vignette</a>.</p>
<p><a href="blockr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-25"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/blockr.png" class="img-fluid" alt="Example plot"></a></p>
<p><a href="https://cran.r-project.org/package=mapycusmaximus">mapycusmaximus</a> v1.0.0: Provides focus-glue-context fisheye transformations to two-dimensional coordinates and spatial vector geometries. Implements a smooth radial distortion that enlarges a focal region, transitions through a glue ring, and preserves outside context. Methods build on generalized fisheye views and focus+context mapping. See <a href="https://dl.acm.org/doi/10.1145/22339.22342">Furnas (1986)</a>, <a href="https://dl.acm.org/doi/10.1145/1124772.1124921">Furnas (2006)</a>, and <a href="https://dl.acm.org/doi/10.1145/1653771.1653788">Yamamoto et al.&nbsp;(2009)</a> for details and the <a href="https://cran.r-project.org/web/packages/mapycusmaximus/vignettes/Mapycus.html">vignette</a> for examples.</p>
<p><a href="mapy.png" class="lightbox" data-gallery="quarto-lightbox-gallery-26"><img src="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/mapy.png" class="img-fluid" alt="Plot of Fisheye transformation"></a></p>
</section>
</div>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/december-2025-top-40-new-cran-packages/</guid>
  <pubDate>Mon, 02 Feb 2026 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/december-2025-top-40-new-cran-packages/snic.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>November 2025 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/november-2025-top-40-new-cran-packages/</link>
  <description><![CDATA[ 





<div class="columns">
<div class="column" style="width:45%;">
<section id="ai" class="level3">
<h3 class="anchored" data-anchor-id="ai">AI</h3>
<p><a href="https://cran.r-project.org/package=btw">btw</a> v1.1.0: Implements a toolkit for connecting <code>R</code> environments with Large Language Models (LLMs). Provides utilities for describing <code>R</code> objects, package documentation, and workspace state in plain text formats optimized for LLM consumption. Supports multiple workflows: interactive copy-paste to external chat interfaces, programmatic tool registration with <code>ellmer</code> chat clients, batteries-included chat applications via <code>shinychat</code>, and exposure to external coding agents through the Model Context Protocol. Project configuration files enable stable, repeatable conversations with project-specific context and preferred LLM settings. See <a href="https://cran.r-project.org/web/packages/btw/readme/README.html">README</a>.</p>
</section>
<section id="astronomy" class="level3">
<h3 class="anchored" data-anchor-id="astronomy">Astronomy</h3>
<p><a href="https://CRAN.R-project.org/package=marsrad">marsrad</a> v1.0.0: A set of functions to calculate solar irradiance and insolation on Mars horizontal and inclined surfaces. Based on NASA Technical Memoranda 102299, 103623, 105216, 106321, and 106700, i.e., the canonical Mars solar radiation papers. See <a href="https://cran.r-project.org/web/packages/marsrad/readme/README.html">README</a> for examples.</p>
</section>
<section id="causal-inference" class="level3">
<h3 class="anchored" data-anchor-id="causal-inference">Causal Inference</h3>
<p><a href="https://cran.r-project.org/package=infocausality">infocausality</a> v1.0: Provides methods for quantifying temporal and spatial causality through information flow, and decomposing it into unique, redundant, and synergistic components, following the framework described in <a href="https://www.nature.com/articles/s41467-024-53373-4">Martinez-Sanchez et al.&nbsp;(2024)</a>. See the <a href="https://cran.r-project.org/web/packages/infocausality/vignettes/surd.html">vignette</a>.</p>
<p><a href="infocausality.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/infocausality.png" class="img-fluid" alt="Time series of air pollutants and confirmed CVD cases in Hong Kong from March 1995 to November 1997."></a></p>
</section>
<section id="data" class="level3">
<h3 class="anchored" data-anchor-id="data">Data</h3>
<p><a href="https://cran.r-project.org/package=MaddisonData">MaddisonData</a> v1.0.2: Provides access to Maddison project data, which collates all the credible data on population and GDP for 169 countries. <code>MaddisonLeaders</code> makes it easy to find the leaders for each year, allowing users to exclude countries like OPEC with narrow economies to focus on the technology leaders. <code>ggplotPath</code> makes it easy to plot data for only selected countries or years. See the vignettes <a href="https://cran.r-project.org/web/packages/MaddisonData/vignettes/IndustrialRevolution.html">Industrial Revolution</a> and <a href="https://cran.r-project.org/web/packages/MaddisonData/vignettes/UpdateMaddisonData.html">Update Madison Data</a>.</p>
<p><a href="Maddison.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/Maddison.png" class="img-fluid" alt="Plot of sources for leader countries."></a></p>
<p><a href="https://cran.r-project.org/package=traktok">traktok</a> v0.1.1: Provides functions for getting <a href="https://www.tiktok.com/">TikTok</a> through the official and unofficial APIs—in other words, you can track TikTok. See the vignettes, <a href="https://cran.r-project.org/web/packages/traktok/vignettes/research-api.html">research api</a> and <a href="https://cran.r-project.org/web/packages/traktok/vignettes/unofficial-api.html">unofficial api</a>.</p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://cran.r-project.org/package=estar">estar</a> v1.0-1: Standardises established stability properties used to assess systems’ responses to press or pulse disturbances at different ecological levels (e.g.&nbsp;population, community). There are two sets of functions. The first set corresponds to functions that measure stability at any level of organisation. The second set of functions, when applied to Jacobian matrices, measure the stability of a community at short and long time scales. See <a href="https://ecoevorxiv.org/repository/view/8592/">Figueiredo et al.&nbsp;(2025)</a> for the theory. There are two vignettes: <a href="https://cran.r-project.org/web/packages/estar/vignettes/functional_stability_properties.html">Functional stability properties</a> and <a href="https://cran.r-project.org/web/packages/estar/vignettes/jacobian_stability_properties.html">Jacobian stability properties</a>.</p>
<p><a href="estar.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/estar.png" class="img-fluid" alt="Organism count (mean +- sd in grey) of the five functional groups over the course of ecotoxicological experiment. ."></a></p>
</section>
<section id="epidemiology" class="level3">
<h3 class="anchored" data-anchor-id="epidemiology">Epidemiology</h3>
<p><a href="https://cran.r-project.org/package=healthiar">healthiar</a> v0.2.1: Provides functions to quantify and monetize the health impacts of environmental stressors (air pollution &amp; noise). See the document <a href="https://www.who.int/publications/i/item/9241546204">WHO (2003a)</a>: “Assessing the environmental burden of disease at national and local levels” for background and the <a href="https://cran.r-project.org/web/packages/healthiar/vignettes/intro_to_healthiar.html">vignette</a> for examples.</p>
<p><a href="healthiar.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/healthiar.png" class="img-fluid" alt="Package Overview"></a></p>
<p><a href="https://cran.r-project.org/package=vaccinationimpact">vaccinationimpact</a> v0.1.0: Provides tools to estimate the impact of vaccination campaigns at population level (number of events averted, number of avertable events, number needed to vaccinate). Inspired by the methodology proposed by <a href="https://www.sciencedirect.com/science/article/pii/S0264410X15002315">Foppa et al.&nbsp;(2015)</a> and <a href="https://www.eurosurveillance.org/content/10.2807/1560-7917.ES.2019.24.45.1900268">Machado et al.&nbsp;(2019)</a> for influenza vaccination impact. See the <a href="https://cran.r-project.org/web/packages/vaccinationimpact/vignettes/vaccination_impact_estimates.html">vignette</a> for examples.</p>
<p><a href="vacimpact.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/vacimpact.png" class="img-fluid" alt="Plot of vacciation impact over time"></a></p>
</section>
<section id="genomics" class="level3">
<h3 class="anchored" data-anchor-id="genomics">Genomics</h3>
<p><a href="https://cran.r-project.org/package=XYomics">XYomics</a> v0.1.2: Provides tools to analyze sex differences in omics data for complex diseases. It includes functions for differential expression analysis using the limma method as described in <a href="https://academic.oup.com/nar/article/43/7/e47/2414268">Ritchie et al.&nbsp;(2015)</a>, interaction testing between sex and disease, pathway enrichment with <code>clusterProfiler</code> <a href="https://journals.sagepub.com/doi/10.1089/omi.2011.0118">Yu et al.&nbsp;(2012)</a>, and gene regulatory network construction and analysis using <code>igraph</code>. See the vignettes <a href="https://cran.r-project.org/web/packages/XYomics/vignettes/XYomics_bulk_example.html">Bulk RNA-Seq Example</a> and <a href="https://cran.r-project.org/web/packages/XYomics/vignettes/XYomics_sc_example.html">Single Cell Example</a>.</p>
<p><a href="XYomics.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/XYomics.png" class="img-fluid" alt="Plot of cell clusters"></a></p>
</section>
<section id="machine-learning" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning">Machine Learning</h3>
<p><a href="https://cran.r-project.org/package=autotab">autotab</a> v0.1.1: Provides tools to build and train a variational autoencoder (VAE) for mixed-type tabular data (continuous, binary, categorical). Models are implemented using <code>TensorFlow</code> and <code>Keras</code> via the <code>reticulate</code> interface, enabling reproducible VAE training for heterogeneous tabular datasets. See <a href="https://cran.r-project.org/web/packages/autotab/readme/README.html">README</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=fairGATE">fairGATE</a> v0.1.1: Provides tools for training and analysing fairness-aware gated neural networks for subgroup-aware prediction and interpretation in clinical datasets. Methods draw on prior work in mixture-of-experts neural networks by <a href="https://link.springer.com/chapter/10.1007/978-1-4471-2097-1_113">Jordan and Jacobs (1994)</a>, fairness-aware learning by <a href="https://arxiv.org/abs/1610.02413">Hardt, Price, and Srebro (2016)</a>, and personalised treatment prediction for depression by <a href="https://www.sciencedirect.com/science/article/pii/S0022395616300541">Iniesta, Stahl, and McGuffin (2016)</a>. See the <a href="https://cran.r-project.org/web/packages/fairGATE/vignettes/introduction-to-fairGATE.html">vignette</a>.</p>
<p><a href="fairGATE.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/fairGATE.png" class="img-fluid" alt="Plot of gate weight distribution by subgroup"></a></p>
</section>
<section id="marketing" class="level3">
<h3 class="anchored" data-anchor-id="marketing">Marketing</h3>
<p><a href="https://cran.r-project.org/package=rbranding">rbranding</a> v0.1.1: Implements a tool for building projects that are visually consistent, accessible, and easy to maintain. It provides functions for managing branding assets, applying organization-wide themes using <a href="https://posit-dev.github.io/brand-yml/"><code>brand.yml</code></a>, and setting up new projects with accessibility features and correct branding, and supports <code>quarto</code>, <code>shiny</code>, and <code>rmarkdown</code> projects, and integrates with <code>ggplot2</code>. The accessibility features are based on the <a href="https://www.w3.org/WAI/WCAG22/quickref/?versions=2.1">Web Content Accessibility Guidelines</a> and <a href="https://www.w3.org/WAI/ARIA/apg/">Accessible Rich Internet Applications specifications</a>. There are three vignettes including <a href="https://cran.r-project.org/web/packages/rbranding/vignettes/rbranding.html">rebranding</a> and <a href="https://cran.r-project.org/web/packages/rbranding/vignettes/templates.html">Templates</a>.</p>
</section>
<section id="medical-statistics" class="level3">
<h3 class="anchored" data-anchor-id="medical-statistics">Medical Statistics</h3>
<p><a href="https://cran.r-project.org/package=carts">carts</a> v0.1.0: Implements a Monte Carlo simulation framework for different randomized clinical trial designs with a special emphasis on estimators based on covariate adjustment. Functions include a regression-based covariate adjustment based on <a href="https://www.degruyterbrill.com/document/doi/10.2202/1557-4679.1138/html">Rosenblum &amp; van der Laan (2010)</a> a one-step estimator based on <a href="https://arxiv.org/abs/2404.11150">Van Lancker et al (2024)</a> for trials with continuous, binary and count outcomes and functions to estimate the minimum sample-size required to reach a specified statistical power for a given estimator, <a href="https://projecteuclid.org/journals/annals-of-mathematical-statistics/volume-22/issue-3/A-Stochastic-Approximation-Method/10.1214/aoms/1177729586.full">Robbins-Monro (1951)</a>. There are two vignettes <a href="https://cran.r-project.org/web/packages/carts/vignettes/gettingstarted.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/carts/vignettes/param.html">Parametrization of the negative binomial and gamma distributions</a>.</p>
<p><a href="carts.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/carts.png" class="img-fluid" alt="Workflow diagram"></a></p>
<p><a href="https://cran.r-project.org/package=cgmguru">cgmguru</a> v0.1.0: Provides tools for advanced analysis of continuous glucose monitoring (CGM) time-series, implementing GRID (Glucose Rate Increase Detector) and GRID-based algorithms for postprandial peak detection, and detection of hypoglycemic and hyperglycemic episodes (Levels 1/2/Extended) aligned with international consensus CGM metrics. There are ninteen vignetttes, including <a href="https://cran.r-project.org/web/packages/cgmguru/vignettes/intro.html">Complete CGM Analysis Workflow</a> and <a href="https://cran.r-project.org/web/packages/cgmguru/vignettes/detect_all_events.html">detect all events</a>.</p>
<p><a href="https://cran.r-project.org/package=twoCoprimary">twoCoprimary</a> v1.0.0: Provides functions to calculate sample size and power for clinical trials with two co-primary endpoints that support five endpoint combinations: two continuous endpoints (<a href="https://www.tandfonline.com/doi/full/10.1080/10543406.2011.551329">Sozu et al.&nbsp;(2011)</a>), two binary endpoints using asymptotic methods (<a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.3972">Sozu et al.&nbsp;(2010)</a>) exact methods (<a href="https://journals.sagepub.com/doi/10.1177/09622802251368697">Homma and Yoshida (2025)</a>), mixed continuous and binary endpoints (<a href="https://onlinelibrary.wiley.com/doi/10.1002/bimj.201100221">Sozu et al.&nbsp;(2012)</a>), and mixed count and continuous endpoints (<a href="https://onlinelibrary.wiley.com/doi/10.1002/pst.2337">Homma and Yoshida (2024)</a>). There are six vignettes including an <a href="https://cran.r-project.org/web/packages/twoCoprimary/vignettes/overview.html">Overview</a> and <a href="https://cran.r-project.org/web/packages/twoCoprimary/vignettes/mixed-count-continuous.html">Mixed and Continuous Co-Primary Endpoints</a>.</p>
<p><a href="twoCoprimary.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/twoCoprimary.png" class="img-fluid" alt="Power contours for different effect sizes"></a></p>
<p><a href="https://cran.r-project.org/package=whatifbandit">whatifbandit</a> v0.3.0: Provides functions to simulate the results of completed randomized controlled trials, as if they had been conducted as adaptive Multi-Arm Bandit trials instead. Uses augmented inverse probability weighted estimation, outlined by <a href="https://www.pnas.org/doi/full/10.1073/pnas.2014602118">Hadad et al.&nbsp;(2021)</a>, to estimate the probability of success for each treatment arm under the adaptive design. See <a href="https://onlinelibrary.wiley.com/doi/10.1111/ajps.12597">Offer-Westort et al.&nbsp;(2021)</a> for background and the <a href="https://cran.r-project.org/web/packages/whatifbandit/vignettes/whatifbandit.html">vignette</a> for examples.</p>
<p><a href="whatifbandit.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/whatifbandit.png" class="img-fluid" alt="Plot of Thompson sampling over time"></a></p>
</section>
<section id="risk-analysis" class="level3">
<h3 class="anchored" data-anchor-id="risk-analysis">Risk Analysis</h3>
<p><a href="https://cran.r-project.org/package=raptools">raptools</a> v1.23.0: Provides tools for assessing the comparative performance of two logistic regression models or results of such models or classification models. Discrimination metrics include integrated discrimination improvement, net reclassification improvement, difference in area under curves, and Brier scores. Methods are described in <a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.2929">Pencina et al.&nbsp;(2008)</a>. Look <a href="https://researchverse.github.io/raptools/">here</a> for examples.</p>
<p><a href="raptools.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/raptools.png" class="img-fluid" alt="Risk assessment plot"></a></p>
</section>
<section id="shiny" class="level3">
<h3 class="anchored" data-anchor-id="shiny">Shiny</h3>
<p><a href="https://cran.r-project.org/package=pandemonium">pandemonium</a> v0.2.4: Implements a <code>shiny</code> GUI that performs high dimensional cluster analysis. This tool performs data preparation, clustering and visualisation within a dynamic GUI. With interactive methods allowing the user to change settings all without having to leave the GUI. See <a href="https://link.springer.com/article/10.1140/epjp/s13360-021-02310-1">Laa and Valencia (2022)</a> for background. There are seven vignettes, including <a href="https://cran.r-project.org/web/packages/pandemonium/vignettes/analysis-pages.html">Data Analysis</a> and <a href="https://cran.r-project.org/web/packages/pandemonium/vignettes/dim-reduction.html">dimReduction</a>.</p>
<p><a href="pandemonium.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/pandemonium.png" class="img-fluid" alt="Shiny page with cluster analysis plots"></a></p>
<p><a href="https://cran.r-project.org/package=rAccess">rAccess</a> v0.1.1.3: Provides a framework for implementing hierarchical access control in <code>shiny</code> applications. Features include user permission management through a two-tier system of access panels and units, pluggable <code>shiny</code> module for administrative interfaces, and support for multiple storage backends (local, <code>AWS S3</code>, <code>Posit Connect</code>). The system enables fine-grained control over application features, with built-in audit trails and user management capabilities. See the <a href="https://cran.r-project.org/web/packages/rAccess/vignettes/Tutorial.html">Tutorial</a>.</p>
<p><a href="rAccess.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/rAccess.png" class="img-fluid" alt="Workflow Diagram"></a></p>
<p><a href="https://CRAN.R-project.org/package=shinyOAuth">shinyOAuth</a> v0.1.4: Provides a configurable, provider-agnostic <code>OAuth 2.0</code> and <code>OpenID Connect</code> authentication framework for <code>shiny</code> applications using S7 classes. Defines providers, clients, and tokens, as well as various supporting functions and a <code>shiny</code> module. Features include cross-site request forgery protection, state encryption, Proof Key for Code Exchange handling, validation of OIDC identity tokens, automatic user info retrieval, asynchronous flows, and hooks for audit logging. There are four vignettes, including <a href="https://cran.r-project.org/web/packages/shinyOAuth/vignettes/usage.html">Usage</a> and <a href="https://cran.r-project.org/web/packages/shinyOAuth/vignettes/example-spotify.html">Example: Spotify login</a>.</p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=aramappings">aramappings</a> v0.1.2: Computes low-dimensional point representations of high-dimensional numerical data according to the data visualization method Adaptable Radial Axes described in: <a href="https://onlinelibrary.wiley.com/doi/10.1111/cgf.13196">Rubio-Sánchez et al.&nbsp;(2017)</a>. See the <a href="https://cran.r-project.org/web/packages/aramappings/vignettes/intro_to_aramappings.html">vignette</a>.</p>
<p><a href="aramappings.jpeg" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/aramappings.jpeg" class="img-fluid" alt="Example of ARA Plot"></a></p>
<p><a href="https://cran.r-project.org/package=deepspat">deepspat</a> v0.3.1: Deep compositional spatial models are standard spatial covariance models coupled with an injective warping function of the spatial domain. The warping function is constructed through a composition of multiple elemental injective functions in a deep-learning framework. The package implements two cases for the univariate setting; first, when these warping functions are known up to some weights that need to be estimated, and, second, when the weights in each layer are random. Estimation and inference are done using <code>tensorflow</code>, which makes use of graphics processing units. For more details see <a href="https://www.tandfonline.com/doi/full/10.1080/01621459.2021.1887741">Zammit-Mangion et al.&nbsp;(2022)</a>, <a href="https://www3.stat.sinica.edu.tw/statistica/oldpdf/A32n415.pdf">Vu et al.&nbsp;(2022)</a> and <a href="https://arxiv.org/abs/2505.12548">Shao et al.&nbsp;(2025)</a>. See <a href="https://cran.r-project.org/web/packages/deepspat/readme/README.html">README</a> for an example.</p>
<p><a href="deepspat.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/deepspat.png" class="img-fluid" alt="Deepspat images"></a></p>
<p><a href="https://cran.r-project.org/package=fmi">fmi</a> v0.1.7: Provides functions to test for functional measurement invariance between two groups. Implements hierarchical permutation tests for configural, metric, and scalar invariance, adapting concepts from multi-group confirmatory factor analysis to functional data. Methods are based on concepts from: <a href="https://www.cambridge.org/core/journals/psychometrika/article/abs/measurement-invariance-factor-analysis-and-factorial-invariance/914A4C29515ACA7E0554B9E79F20D36C">Meredith, W. (1993)</a>, <a href="https://www.tandfonline.com/doi/abs/10.1198/016214504000001745">Yao et al.&nbsp;(2005)</a> and <a href="https://academic.oup.com/jrsssb/article/84/2/600/7056125?login=false">Lee &amp; Li (2022)</a>. See the <a href="https://cran.r-project.org/web/packages/fmi/vignettes/introduction-to-fmi.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=gkwdist">gkwdist</a> v1.1.1: Implements the five-parameter Generalized Kumaraswamy distribution proposed by <a href="https://arxiv.org/abs/1004.0911">Carrasco, Ferrari and Cordeiro (2010)</a> and its seven nested sub-families for modeling bounded continuous data on the unit interval (0,1). Provides density, distribution, quantile, and random generation functions, along with analytical log-likelihood, gradient, and Hessian functions. See the <a href="https://cran.r-project.org/web/packages/gkwdist/vignettes/into-gkwdist.html">Introduction</a> and the vignette on <a href="https://cran.r-project.org/web/packages/gkwdist/vignettes/theory-gkwdist.html">Statistical Properties</a>.</p>
<p><a href="gkwdist.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/gkwdist.png" class="img-fluid" alt="Plot of density comparisons"></a></p>
<p><a href="https://cran.r-project.org/package=grasps">grasps</a> v0.1.0: Provides a unified framework for sparse-group regularization and precision matrix estimation in Gaussian graphical models and implements multiple sparse-group penalties, including sparse-group lasso, sparse-group adaptive lasso, sparse-group SCAD, and sparse-group MCP. The package is designed for high-dimensional network inference where both sparsity and group structure are present. There are two vignettes: <a href="https://cran.r-project.org/web/packages/grasps/vignettes/crit.html">Selection Criteria</a> and <a href="https://cran.r-project.org/web/packages/grasps/vignettes/pen_est.html">Penal Precision Matrix Estimation</a>.</p>
<p><a href="grasps.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/grasps.png" class="img-fluid" alt="Plot comparing derivatives of various penalties"></a></p>
<p><a href="https://cran.r-project.org/package=PCBN">PCBN</a> v0.1.1: Provides functions to create fit and sample Pair-Copula Bayesian networks (PCBN) under some restrictions on the underlying Directed Acyclic Graph (DAG), that is, no active cycles nor interfering v-structures. See <a href="https://arxiv.org/abs/2510.03518">Derumigny, Horsman and Kurowicka (2025)</a> for background and the vignettes <a href="https://cran.r-project.org/web/packages/PCBN/vignettes/Bsets-v-structs.html">B-sets and interfering v-structures</a> and <a href="https://cran.r-project.org/web/packages/PCBN/vignettes/internals-estimation.html">How to use the estimation procedures</a>.</p>
<p><a href="PCBN.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/PCBN.png" class="img-fluid" alt="Saple DAG"></a></p>
<p><a href="https://cran.r-project.org/package=PublicationBiasBenchmark">PublicationBiasBenchmark</a> v0.1.3: Implements a unified interface for benchmarking meta-analytic publication bias correction methods through simulation studies, which provide predefined data-generating mechanisms from the literature, functions for running meta-analytic methods on simulated data, pre-simulated datasets and pre-computed results for reproducible benchmarks, and tools for visualizing and comparing method performance. See <a href="https://arxiv.org/abs/2510.19489">Bartoš et al.(2025)</a> for background. There seven vignettes, including <a href="https://cran.r-project.org/web/packages/PublicationBiasBenchmark/vignettes/Computing_Method_Measures.html">Computing Method Measures</a> and <a href="https://cran.r-project.org/web/packages/PublicationBiasBenchmark/vignettes/Adding_New_Methods.html">Adding New Methods</a>.</p>
<p><a href="https://cran.r-project.org/package=SelectBoost.gamlss">SelectBoost.gamlss</a> v0.2.2: Extends the <code>SelectBoost</code> approach to Generalized Additive Models for Location, Scale and Shape (GAMLSS). Implements bootstrap stability-selection across parameter-specific formulas (mu, sigma, nu, tau). Includes optional standardization of predictors and helper functions for corrected AIC calculation. See <a href="https://hal.science/hal-05352041">Bertrand and Maumy (2024)</a> for details on correlation-aware resampling to improve variable selection for GAMLSS and quantile regression. There are nine vignettes including <a href="https://cran.r-project.org/web/packages/SelectBoost.gamlss/vignettes/advanced-real-data-examples.html">Advanced data examples</a> and <a href="https://cran.r-project.org/web/packages/SelectBoost.gamlss/vignettes/confidence-functionals.html">Confidence Functionals</a>.</p>
<p><a href="https://cran.r-project.org/package=SCoRES">SCoRES</a> v0.1.2: Provides computational tools for estimating inverse regions and constructing the corresponding simultaneous outer and inner confidence regions for logistic, functional, and spatial generalized least squares regression models. Functions are also available for constructing simultaneous confidence bands. See <a href="https://www.tandfonline.com/doi/full/10.1080/01621459.2017.1341838">Sommerfeld et al.&nbsp;(2018)</a> for the definition of a simultaneous confidence region, and see <a href="https://academic.oup.com/jrsssc/article/73/4/1082/7685821?login=false">Ren et al.&nbsp;(2024)</a>, <a href="https://www.taylorfrancis.com/books/mono/10.1201/9781003278726/functional-data-analysis-ciprian-crainiceanu-jeff-goldsmith-andrew-leroux-erjia-cui">Crainiceanu et al.&nbsp;(2024)</a>, and <a href="https://www.sciencedirect.com/science/article/abs/pii/S0378375821000598">Telschow et al.&nbsp;(2022)</a> for background. There are four vignettes including <a href="https://cran.r-project.org/web/packages/SCoRES/vignettes/Methods.html">Methods</a> and <a href="https://cran.r-project.org/web/packages/SCoRES/vignettes/Functional_Data_Example.html">Functional Data Example</a>.</p>
<p><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/SCoRES.png" class="img-fluid" alt="Plot ofconfidence regions"> <img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/maki.png" class="img-fluid" alt="Time series plot with regime shift"></p>
<p><a href="https://cran.r-project.org/package=UGarima">UGarima</a> v0.1.0: Provides density, distribution function, quantile function, and random generating function of the Unit-Garima distribution based on <a href="https://pjsor.com/pjsor/article/view/4307">Ayuyuen &amp; Bodhisuwan (2024)</a>.</p>
<p><a href="UGarima.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/UGarima.png" class="img-fluid" alt="Plots of the pdf and cdf of X∼UGa-W(α,θ,k,λ)"></a></p>
</section>
<section id="time-series" class="level3">
<h3 class="anchored" data-anchor-id="time-series">Time Series</h3>
<p><a href="https://cran.r-project.org/package=aplms">aplms</a> v0.1.0: Provides tools for fitting the additive partial linear models with symmetric autoregressive errors of order p, or APLMS-AR(p), enabling the modeling of a time series response variable using linear and nonlinear structures of a set of explanatory variables, with nonparametric components approximated by natural cubic splines or P-splines. Functions include various error distributions, such as normal, generalized normal, Student’s t, generalized Student’s t, power-exponential, and Cauchy distributions. <a href="https://link.springer.com/article/10.1007/s00362-024-01590-w">Chou-Chen et al.&nbsp;(2024)</a> for background. Look <a href="https://github.com/shuwei325/aplms">here</a> for examples.</p>
<p><a href="https://cran.r-project.org/package=BLSloadR">BLSloadR</a> v0.2: Implements an interface for downloading data from the <a href="https://www.bls.gov">U.S. Bureau of Labor Statistics</a>. Files include employment, unemployment, wages, prices, industry and occupational data at a national, state, and sub-state level, depending on the series. Individual functions are included for those programs that have data available at the state level. The core functions provide direct access to the Current Employment Statistics <a href="https://www.bls.gov/ces/">CES</a>, Local Area Unemployment Statistics <a href="https://www.bls.gov/lau/">LAUS</a>, Occupational Employment and Wage Statistics <a href="https://www.bls.gov/oes/">OEWS</a>, and Alternative Measures of Labor Underutilization <a href="https://www.bls.gov/lau/stalt.htm">SALT</a>. See the <a href="https://cran.r-project.org/web/packages/BLSloadR/vignettes/BLSloadR-intro.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=makicoint">makicoint</a> v1.0.0: Implements the <a href="https://www.sciencedirect.com/science/article/abs/pii/S0264999312001162">Maki (2012)</a> cointegration test that allows for an unknown number of structural breaks. The test detects cointegration relationships in the presence of up to five structural breaks in the intercept and/or slope coefficients. Four different model specifications are supported: level shifts, level shifts with trend, regime shifts, and trend with regime shifts. See the <a href="https://cran.r-project.org/web/packages/makicoint/vignettes/introduction.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=organik">organik</a> v1.0.1: Provides functions to train per-horizon probabilistic ensembles from a univariate time series. It supports <code>rpart</code>, <code>glmnet</code>, and <code>kNN</code> engines with flexible residual distributions and heteroscedastic scale models, weighting variants by calibration-aware scores. A Gaussian/t copula couples the marginals to simulate joint forecast paths, returning quantiles, means, and step increments across horizons. Look <a href="https://rpubs.com/giancarlo_vercellino/organik">here</a> for the details.</p>
<p><a href="organik.png" class="lightbox" data-gallery="quarto-lightbox-gallery-20"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/organik.png" class="img-fluid" alt="The organik process"></a></p>
<p><a href="https://cran.r-project.org/package=trendseries">trendseries</a> v1.1.0: Provides functions to extract trends from monthly and quarterly economic time series: <code>augment_trends()</code> for pipe-friendly <code>tibble</code> workflows and <code>extract_trends()</code> for direct time series analysis. Includes key econometric filters and modern parameter experimentation tools. There are three vignettes: <a href="https://cran.r-project.org/web/packages/trendseries/vignettes/trendseries.html">Getting Started</a>, <a href="https://cran.r-project.org/web/packages/trendseries/vignettes/moving-averages.html">Moving Averages</a>, and <a href="https://cran.r-project.org/web/packages/trendseries/vignettes/economic-filters.html">Economic Filters for Business Cycle Analysis</a>.</p>
<p><a href="trendseries.png" class="lightbox" data-gallery="quarto-lightbox-gallery-21"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/trendseries.png" class="img-fluid" alt="Plot of HP filter with different lambda values"></a></p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=hdf5lib">hdf5lib</a> v2.0.0.4: <a href="https://www.hdfgroup.org/">HDF5</a> (Hierarchical Data Format 5) is a high-performance library and file format for storing and managing large, complex data. This package, which provides the static libraries and headers for the <code>HDF5</code> <code>C</code> library (release 2.0.0), is intended for developers to use in the LinkingTo field, which eliminates the need for users to install system-level dependencies. This build is compiled with thread-safety enabled and supports dynamic loading of external compression filters. Look <a href="https://cmmr.github.io/hdf5lib/">here</a> for more information.</p>
<p><a href="https://cran.r-project.org/package=privacyR">privacyR</a> v1.0.1: Provides tools for anonymizing sensitive patient and research data. Helps protect privacy while keeping data useful for analysis. Anonymizes IDs, names, dates, locations, and ages while maintaining referential integrity. Methods based on: <a href="https://www.worldscientific.com/doi/abs/10.1142/S0218488502001648">Sweeney (2002)</a>, <a href="https://link.springer.com/chapter/10.1007/11681878_14">Dwork et al.&nbsp;(2006)</a>, <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0028071">El Emam et al.&nbsp;(2011)</a>, and <a href="https://dl.acm.org/doi/10.1145/1749603.1749605">Fung et al.&nbsp;(2010)</a>. See the <a href="https://cran.r-project.org/web/packages/privacyR/vignettes/privacyR.html">vignette</a>.</p>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=colorify">colorify</a> v0.1.2: Provides one-stop shop for intuitive and dependency-free color and palette creation and modification. Includes palettes and functionality from popular packages such as <code>viridis</code>, <code>RColorBrewer</code>, and base R <code>grDevices</code>, as well as <code>ggplot2</code> plot bindings. Users can generate perceptually uniform and colorblind-friendly palettes, adjust palettes in HSL and RGB color spaces, map color gradients to value ranges, and create color-generating functions. See the <a href="https://cran.r-project.org/web/packages/colorify/vignettes/colorify.html">vignette</a>.</p>
<p><a href="colorify.png" class="lightbox" data-gallery="quarto-lightbox-gallery-22"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/colorify.png" class="img-fluid" alt="Sample palettes"></a></p>
<p><a href="https://cran.r-project.org/package=GGenemy">GGenemy</a> v0.1.0: Audits <code>ggplot2</code> visualizations for accessibility issues, misleading practices, and readability problems. Checks for color accessibility concerns, including colorblind-unfriendly palettes, misleading scale manipulations such as truncated axes and dual y-axes, text readability issues like small fonts and overlapping labels, and general accessibility barriers. Provides comprehensive audit reports with actionable suggestions for improvement. Color vision deficiency simulation uses methods from the <code>colorspace</code> package <a href="https://www.jstatsoft.org/article/view/v096i01">Zeileis et al.&nbsp;(2020)</a> <a href="doi:10.18637/jss.v096.i01" class="uri">doi:10.18637/jss.v096.i01</a>. Contrast calculations follow <a href="https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum">WCAG 2.1 guidelines (W3C 2018)</a>. See the <a href="https://cran.r-project.org/web/packages/GGenemy/vignettes/getting-started.html">vignette</a>.</p>
<p><a href="GGenemy.png" class="lightbox" data-gallery="quarto-lightbox-gallery-23"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/GGenemy.png" class="img-fluid" alt="Simulated deutananopia view of a ggplot2 scatter plot"></a></p>
<p><a href="https://cran.r-project.org/package=ggincerta">ggincerta</a> v0.1.0: Extends <code>ggplot2</code> with Layers and Scales for Spatial Uncertainty Visualization. See <a href="https://cran.r-project.org/web/packages/ggincerta/readme/README.html">README</a>.</p>
<p><a href="ggincerta.png" class="lightbox" data-gallery="quarto-lightbox-gallery-24"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/ggincerta.png" class="img-fluid" alt="Example glyph map"></a></p>
<p><a href="https://cran.r-project.org/package=poisonfrogs">poisonfrogs</a> v1.0.2: Provides a collection of color palettes inspired by the enormous diversity of skin colors in Neotropical poison frog species. Suitable for use with <code>ggplot2</code> and base R graphics. See <a href="https://cran.r-project.org/web/packages/poisonfrogs/readme/README.html">README</a>.</p>
<p><a href="poisonfrogs.png" class="lightbox" data-gallery="quarto-lightbox-gallery-25"><img src="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/poisonfrogs.png" class="img-fluid" alt="Poison frogs pallettes"></a></p>
</section>
</div>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/november-2025-top-40-new-cran-packages/</guid>
  <pubDate>Mon, 05 Jan 2026 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/november-2025-top-40-new-cran-packages/poisonfrogs.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Smoothed ROC Curves, Calculus and Curvature</title>
  <dc:creator>Joseph B. Rickert</dc:creator>
  <link>https://rworks.dev/posts/roc-and-differential-geometry/</link>
  <description><![CDATA[ 





<p>A common use of ROC (Receiver Operating Characteristic) curves in data science is to evaluate performance of binary classifiers. In this use case, the data set is usually a sample from a population and so the ROC curve itself is a random object. Although it is not common to have multiple sample ROC curves constructed from applying the same classifier to multiple sample data sets from the same population, it would be interesting to be able to construct a mean ROC curve from multiple sample ROC curves. One way to go about this would be to use the theory of <a href="https://rviews.rstudio.com/2021/05/04/functional-data-analysis-in-r/">functional data analysis</a> (FDA) to construct curves from the sample points in such a way that the curves themselves become the random objects of study. The first step in the FDA process typically is to use splines to construct a set of basis functions to smooth the sample points into functional objects. In this post, I am going to explore this first step of smoothing ROC curves, and point out that once you have a smoothed ROC curve, it is possible to use calculus and concepts from basic differential geometry to analyze the curves. The flow of the post is as follows:</p>
<ul>
<li>Select some data sets to analyze. (I am only going to work with one data set in this post, but I have set up the code so that you can choose between three available data sets if you want to explore on your own.)</li>
<li>Fit three different binary classifiers to the data set and compute the standard ROC curves and AUC values.</li>
<li>Use spline basis functions to smooth the ROC curves and compute AUC and arc length for the smoothed curves, and discuss some of the difficulties involved in smoothing ROC curves in a meaningful way.</li>
<li>Provide some basic analysis.</li>
</ul>
<div class="cell">
<details class="code-fold">
<summary>Show the packages required</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidymodels) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For modeling and evaluation</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For data manipulation</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For plotting</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MASS) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for Pima.tr</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mlbench) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for data</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(pROC) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For ROC curve analysis</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for plot layouts</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gt) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For tables</span></span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(katex) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For rendering math</span></span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidymodels_prefer</span>()</span></code></pre></div></div>
</details>
</div>
<section id="the-data" class="level2">
<h2 class="anchored" data-anchor-id="the-data">The Data</h2>
<p>Rather than only using synthetic data, I thought that the ideas in the post would make more of a positive impression if they were illustrated with small, familiar data sets. So in addition to the <code>two_class_dat</code> artificial data set from the <code>modeldata</code> package, I have included <code>Pima.tr</code> from the <code>MASS</code> package and the <code>aSAH</code> data set from the <code>pROC</code> package. I am going to use the <code>aSAH2</code> data set, a subset of <code>aSAH</code>, in the rest of the post, but you can easily switch to one of the other two data sets by changing a single line of code. All three of these data sets contain three variables: two numeric features and a binary class label. I believe that these simple data sets are sufficient to provide a minimal viable demonstration of the issues I am going to discuss. Using simple data sets that conform to the same structure also makes it easy to fit multiple classification models with a single <code>tidymodels</code> workflow.</p>
<p>This next section of code prepares the data sets. The way of selecting which data set to use should be clear.</p>
<div class="cell">
<details class="code-fold">
<summary>Look at the available data sets</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load a sample dataset (e.g., `two_class_dat` from `modeldata`)</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(two_class_dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"modeldata"</span>)</span>
<span id="cb2-3">two_class_dat2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> two_class_dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Class =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">recode</span>(Class,</span>
<span id="cb2-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Class1"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>,</span>
<span id="cb2-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Class2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span></span>
<span id="cb2-6">))</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(Pima.tr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MASS"</span>)</span>
<span id="cb2-9"></span>
<span id="cb2-10">Pima.tr2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Pima.tr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Class =</span> type,</span>
<span id="cb2-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Class =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">recode</span>(Class,</span>
<span id="cb2-14">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>,</span>
<span id="cb2-15">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span></span>
<span id="cb2-16">    )</span>
<span id="cb2-17">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(bmi, bp, Class))</span>
<span id="cb2-19"></span>
<span id="cb2-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(aSAH, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pROC"</span>)</span>
<span id="cb2-21"></span>
<span id="cb2-22">aSAH2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> aSAH <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Class =</span> outcome,</span>
<span id="cb2-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Class =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">recode</span>(Class,</span>
<span id="cb2-26">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Good"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>,</span>
<span id="cb2-27">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Poor"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span></span>
<span id="cb2-28">    )</span>
<span id="cb2-29">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(s100b, ndka, Class))</span>
<span id="cb2-31"></span>
<span id="cb2-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set a seed for reproducibility</span></span>
<span id="cb2-33"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb2-34"></span>
<span id="cb2-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SELECT A DATA SET TO USE HERE</span></span>
<span id="cb2-36"></span>
<span id="cb2-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#df&lt;- two_class_dat2 # try FPC = 1, FNC = 10</span></span>
<span id="cb2-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#df &lt;- Pima.tr2 # try FPC = 10 , FNC = 1</span></span>
<span id="cb2-39">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> aSAH2 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#try FPC = 1, FNC = 1</span></span>
<span id="cb2-40"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(df)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>   s100b  ndka Class
29  0.13  3.01     1
30  0.14  8.54     1
31  0.10  8.09     1
32  0.04 10.42     1
33  0.13 17.40     2
34  0.10 12.75     2</code></pre>
</div>
<details class="code-fold">
<summary>Look at the available data sets</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split the data into training and testing sets</span></span>
<span id="cb4-2">data_split <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">initial_split</span>(df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strata =</span> Class)</span>
<span id="cb4-3">train_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">training</span>(data_split)</span>
<span id="cb4-4">test_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">testing</span>(data_split)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="the-classifiers" class="level2">
<h2 class="anchored" data-anchor-id="the-classifiers">The Classifiers</h2>
<p>The workflows use three classifiers to fit the models: Logistic Regression, SVM, and Decision Trees.</p>
<div class="cell">
<details class="code-fold">
<summary>Show the workflow code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the models</span></span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Logistic Regression</span></span>
<span id="cb5-4">log_reg_spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logistic_reg</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_mode</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classification"</span>)</span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Support Vector Machine (SVM)</span></span>
<span id="cb5-9">svm_spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">svm_linear</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kernlab"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_mode</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classification"</span>)</span>
<span id="cb5-12"></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Decision Tree</span></span>
<span id="cb5-14">tree_spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">decision_tree</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rpart"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_mode</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classification"</span>)</span>
<span id="cb5-17"></span>
<span id="cb5-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create workflows for each model</span></span>
<span id="cb5-19">log_reg_wf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">workflow</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_model</span>(log_reg_spec) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_formula</span>(Class <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .)</span>
<span id="cb5-22"></span>
<span id="cb5-23">svm_wf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">workflow</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_model</span>(svm_spec) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_formula</span>(Class <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .)</span>
<span id="cb5-26"></span>
<span id="cb5-27">tree_wf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">workflow</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_model</span>(tree_spec) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_formula</span>(Class <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .)</span>
<span id="cb5-30"></span>
<span id="cb5-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit the models to the training data</span></span>
<span id="cb5-32">log_reg_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span>(log_reg_wf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> train_data)</span>
<span id="cb5-33">svm_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span>(svm_wf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> train_data)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> Setting default kernel parameters  </code></pre>
</div>
<details class="code-fold">
<summary>Show the workflow code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">tree_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span>(tree_wf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> train_data)</span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Collect predictions for each model on the test data</span></span>
<span id="cb7-4">log_reg_preds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(log_reg_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> test_data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prob"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(test_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Class)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span>)</span>
<span id="cb7-7"></span>
<span id="cb7-8">svm_preds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(svm_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> test_data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prob"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(test_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Class)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>)</span>
<span id="cb7-11"></span>
<span id="cb7-12">tree_preds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(tree_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> test_data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prob"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(test_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Class)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>)</span>
<span id="cb7-15"></span>
<span id="cb7-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute AUCs and relabel models with AUC values</span></span>
<span id="cb7-17">auc_tree <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc_auc</span>(tree_preds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> Class, .pred_1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.estimate</span>
<span id="cb7-18">auc_svm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc_auc</span>(svm_preds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> Class, .pred_1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.estimate</span>
<span id="cb7-19">auc_log_reg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc_auc</span>(log_reg_preds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> Class, .pred_1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.estimate</span>
<span id="cb7-20"></span>
<span id="cb7-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Update model labels to include AUC</span></span>
<span id="cb7-22">log_reg_preds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> log_reg_preds <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression (AUC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(auc_log_reg, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span>))</span>
<span id="cb7-24"></span>
<span id="cb7-25">svm_preds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> svm_preds <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM (AUC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(auc_svm, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span>))</span>
<span id="cb7-27"></span>
<span id="cb7-28">tree_preds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tree_preds <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree (AUC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(auc_tree, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span>))</span>
<span id="cb7-30"></span>
<span id="cb7-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine predictions with updated labels</span></span>
<span id="cb7-32">all_preds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(log_reg_preds, svm_preds, tree_preds)</span></code></pre></div></div>
</details>
</div>
<p>This section of code fits the models, computes AUC for each model, and plots the basic ROC curves.</p>
<div class="cell">
<details class="code-fold">
<summary>Show Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### Plot the ROC Curves</span></span>
<span id="cb8-2"></span>
<span id="cb8-3">all_preds <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc_curve</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> Class, .pred_1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">autoplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb8-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ROC Curves for Multiple Classifiers"</span>,</span>
<span id="cb8-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Model"</span></span>
<span id="cb8-10">  )</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-4-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/roc-and-differential-geometry/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Show Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make a copy of all_preds with cleaned model names</span></span>
<span id="cb9-2">all_preds_2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_preds <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb9-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model_AUC =</span> model,</span>
<span id="cb9-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(AUC.*$"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, model_AUC)</span>
<span id="cb9-6">  )</span>
<span id="cb9-7"></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute ROC data for each model</span></span>
<span id="cb9-9">roc_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_preds_2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc_curve</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> Class, .pred_1)</span>
<span id="cb9-12"></span>
<span id="cb9-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute AUC for each model</span></span>
<span id="cb9-14">auc_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_preds_2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc_auc</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> Class, .pred_1)</span>
<span id="cb9-17"></span>
<span id="cb9-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Inspect actual model names</span></span>
<span id="cb9-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#print(unique(roc_data$model))</span></span>
<span id="cb9-20"></span>
<span id="cb9-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build legend labels with AUC</span></span>
<span id="cb9-22">legend_labels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb9-23">  auc_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model,</span>
<span id="cb9-24">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" (AUC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.3f"</span>, auc_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.estimate), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span></span>
<span id="cb9-25">)</span>
<span id="cb9-26"></span>
<span id="cb9-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># IMPORTANT: match colors to the actual values in your data</span></span>
<span id="cb9-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Replace the strings below with the exact output from unique(roc_data$model)</span></span>
<span id="cb9-29">color_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb9-30">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1b9e77"</span>,</span>
<span id="cb9-31">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>                 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7570b3"</span>,</span>
<span id="cb9-32">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d95f02"</span></span>
<span id="cb9-33">)</span>
<span id="cb9-34"></span>
<span id="cb9-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ensure model is a factor with levels matching auc_data$model</span></span>
<span id="cb9-36">roc_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> roc_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> auc_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model))</span></code></pre></div></div>
</details>
</div>
</section>
<section id="smooth-roc-curves" class="level2">
<h2 class="anchored" data-anchor-id="smooth-roc-curves">Smooth ROC Curves</h2>
<p>In this section, a spline bases is used to construct smoothed curves. AUC is computed for both raw and smoothed ROC curves. Arc length is only computed for the smoothed curves. The major technical challenge in smoothing ROC curves is to ensure that the smoothed curves are monotone, non-decreasing in both TPR and FPR. I have addressed this issue by using the “monoH.FC” method in R’s <code>splinefun</code> function. This method, which constructs a monotone Hermite cubic spline using the <a href="https://web.archive.org/web/20160212093324id_/http://www.ams.sunysb.edu:80/~jiao/teaching/ams527_spring16/lectures/SNA000238.pdf">Fritsch–Carlson</a> method, was designed to minimize the creation of artifacts that could jeopardize monotonicity. It adjusts slopes at knots to prevent oscillations while keeping the interpolation smooth.</p>
<p>A second problem is that the smoothing process affects the TPR and FPR values in a way that can affect the AUC values. We will examine both of these issues below.</p>
<div class="cell">
<details class="code-fold">
<summary>Show Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_preds</span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Numerical integration using the trapezoidal rule</span></span>
<span id="cb10-3">trapz <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, y) {</span>
<span id="cb10-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>((y[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y)]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(x))</span>
<span id="cb10-5">}</span>
<span id="cb10-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute discrete ROC points from predictions</span></span>
<span id="cb10-7">discrete_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb10-8">  roc_obj <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc</span>(</span>
<span id="cb10-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">response =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Class,</span>
<span id="cb10-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predictor =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.pred_1,</span>
<span id="cb10-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># control first, positive second</span></span>
<span id="cb10-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;"</span></span>
<span id="cb10-13">  )</span>
<span id="cb10-14"></span>
<span id="cb10-15">  rc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coords</span>(roc_obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"all"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ret =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"specificity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sensitivity"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">transpose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb10-16">  FPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>specificity</span>
<span id="cb10-17">  TPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sensitivity</span>
<span id="cb10-18"></span>
<span id="cb10-19">  FPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, FPR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-20">  TPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, TPR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-21">  ord <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(FPR, TPR)</span>
<span id="cb10-22">  FPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> FPR[ord]</span>
<span id="cb10-23">  TPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cummax</span>(TPR[ord]) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># enforce monotonicity</span></span>
<span id="cb10-24"></span>
<span id="cb10-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> TPR)</span>
<span id="cb10-26">}</span>
<span id="cb10-27"></span>
<span id="cb10-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   smooth_roc takes raw ROC points (FPR, TPR) and produces a smoothed ROC curve with:</span></span>
<span id="cb10-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     1. a monotone spline interpolation of TPR vs. FPR</span></span>
<span id="cb10-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#.    2. a dense grid of points (n = 400 by default)</span></span>
<span id="cb10-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#.    3. Computes  AUC (area under the curve)</span></span>
<span id="cb10-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#.    4. Computes arc length (geometric length of the ROC trajectory)</span></span>
<span id="cb10-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#.    5. Returns a tibble with the smoothed ROC coordinates and the two summary metrics.  </span></span>
<span id="cb10-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span></span>
<span id="cb10-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "monoH.FC" is a special option in R’s splinefun that stands for Monotone Hermite cubic spline</span></span>
<span id="cb10-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (Fritsch–Carlson method) that attempts to guarantee that the interpolated function is monotone increasing</span></span>
<span id="cb10-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if the data are monotone. Unlike ordinary cubic splines, which can overshoot and produce non‑monotone</span></span>
<span id="cb10-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># artifacts, "monoH.FC" preserves the monotonicity of ROC curves (TPR should not decrease as FPR increases).</span></span>
<span id="cb10-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  The algorithm adjusts slopes at knots to prevent oscillations while keeping the interpolation smooth.</span></span>
<span id="cb10-40"></span>
<span id="cb10-41">smooth_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(FPR, TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>) {</span>
<span id="cb10-42">  df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> TPR) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-43">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(FPR) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep_all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb10-45">  mono_fun <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">splinefun</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"monoH.FC"</span>)</span>
<span id="cb10-46">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n)</span>
<span id="cb10-47">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mono_fun</span>(x), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-48"></span>
<span id="cb10-49">  auc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trapz</span>(x, y)</span>
<span id="cb10-50">  dy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mono_fun</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-51">  arc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trapz</span>(x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> dy<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb10-52"></span>
<span id="cb10-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auc =</span> auc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arc =</span> arc)</span>
<span id="cb10-54">}</span>
<span id="cb10-55"></span>
<span id="cb10-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalize model names</span></span>
<span id="cb10-57">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-58"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model_norm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(.*$"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, model))</span>
<span id="cb10-59"></span>
<span id="cb10-60">smooth_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model_norm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-62">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_modify</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> {</span>
<span id="cb10-63">    dr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">discrete_roc</span>(.x)</span>
<span id="cb10-64">    sr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">smooth_roc</span>(dr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>FPR, dr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>)</span>
<span id="cb10-65">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sr %&gt;% mutate(model_norm = unique(.x$model_norm))</span></span>
<span id="cb10-66">  }) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-67">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb10-68"></span>
<span id="cb10-69">metrics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model_norm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AUC =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(auc), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Arc =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(arc), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span></code></pre></div></div>
</details>
</div>
<p>Here we plot the smoothed ROC curves for each of the three classifiers, overlaying them on the raw ROC curves. For each classifier, the legend includes AUC for both the raw and smoothed ROC curves, and arc length for the smoothed curves.</p>
<div class="cell">
<details class="code-fold">
<summary>Show Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># df_raw: columns .pred_1, .pred_2, Class, model</span></span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># smooth_results: columns model_norm, FPR, TPR, auc, arc</span></span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1) Normalize model names in the raw data so they match smooth_results$model_norm</span></span>
<span id="cb11-5">df_raw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_preds <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model_norm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(.*$"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, model))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># e.g., "Logistic Regression (AUC = ...)" -&gt; "Logistic Regression"</span></span>
<span id="cb11-7"></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2) Compute raw ROC coordinates and raw AUC per model</span></span>
<span id="cb11-9">raw_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model_norm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_map</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>{</span>
<span id="cb11-12">    roc_obj <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">response =</span> .x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Class,</span>
<span id="cb11-13">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predictor =</span> .x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.pred_1,</span>
<span id="cb11-14">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>),</span>
<span id="cb11-15">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;"</span>)</span>
<span id="cb11-16"></span>
<span id="cb11-17">    rc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coords</span>(roc_obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"all"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ret =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"specificity"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sensitivity"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">transpose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb11-18">    FPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>specificity</span>
<span id="cb11-19">    TPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sensitivity</span>
<span id="cb11-20"></span>
<span id="cb11-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pad ends, order, enforce monotone TPR</span></span>
<span id="cb11-22">    FPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, FPR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb11-23">    TPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, TPR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb11-24">    ord <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(FPR, TPR)</span>
<span id="cb11-25">    FPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> FPR[ord]</span>
<span id="cb11-26">    TPR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cummax</span>(TPR[ord])</span>
<span id="cb11-27"></span>
<span id="cb11-28">    auc_raw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">auc</span>(roc_obj))</span>
<span id="cb11-29">    m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> .y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># group label; safer than looking back into .x</span></span>
<span id="cb11-30"></span>
<span id="cb11-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb11-32">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model_norm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(m, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(FPR)),</span>
<span id="cb11-33">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> FPR,</span>
<span id="cb11-34">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> TPR,</span>
<span id="cb11-35">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auc_raw =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(auc_raw, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(FPR)),</span>
<span id="cb11-36">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curve_type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"raw"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(FPR))</span>
<span id="cb11-37">    )</span>
<span id="cb11-38">  }) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>()</span>
<span id="cb11-40"></span>
<span id="cb11-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3) Prepare smoothed results to match columns (add curve_type and placeholder auc_raw)</span></span>
<span id="cb11-42">smooth_results_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb11-44">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curve_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"smooth"</span>,</span>
<span id="cb11-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auc_raw =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># placeholder so bind_rows columns align</span></span>
<span id="cb11-46">  )</span>
<span id="cb11-47"></span>
<span id="cb11-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4) Combine raw + smooth results</span></span>
<span id="cb11-49">roc_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(raw_results, smooth_results_plot)</span>
<span id="cb11-50"></span>
<span id="cb11-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5) Build legend metrics: Raw AUC (from raw_results), Smooth AUC and Arc (from smooth_results)</span></span>
<span id="cb11-52">metrics_raw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> raw_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(model_norm, auc_raw) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AUC_raw =</span> auc_raw)</span>
<span id="cb11-55"></span>
<span id="cb11-56">metrics_smooth <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(model_norm, auc, arc) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-58">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AUC_smooth =</span> auc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Arc =</span> arc)</span>
<span id="cb11-59"></span>
<span id="cb11-60">metrics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> metrics_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(metrics_smooth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"model_norm"</span>)</span>
<span id="cb11-62"></span>
<span id="cb11-63">legend_labels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</span>(</span>
<span id="cb11-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm,</span>
<span id="cb11-65">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Raw AUC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.3f"</span>, metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AUC_raw),</span>
<span id="cb11-66">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Smooth AUC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.3f"</span>, metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AUC_smooth),</span>
<span id="cb11-67">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Arc = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.3f"</span>, metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Arc)),</span>
<span id="cb11-68">  metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm</span>
<span id="cb11-69">)</span>
<span id="cb11-70"></span>
<span id="cb11-71"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 6) Pastel colors (keep model association). Ensure names match smooth_results$model_norm exactly.</span></span>
<span id="cb11-72">color_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb11-73">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>        <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#fcae91"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel red</span></span>
<span id="cb11-74">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span>  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a1d99b"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel green</span></span>
<span id="cb11-75">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>                  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9ecae1"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel blue</span></span>
<span id="cb11-76">)</span>
<span id="cb11-77"></span>
<span id="cb11-78"></span>
<span id="cb11-79"></span>
<span id="cb11-80">legend_labels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</span>(</span>
<span id="cb11-81">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm,</span>
<span id="cb11-82">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Raw AUC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.3f"</span>, metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AUC_raw),</span>
<span id="cb11-83">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | Smooth AUC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.3f"</span>, metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AUC_smooth),</span>
<span id="cb11-84">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Arc = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.3f"</span>, metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Arc),</span>
<span id="cb11-85">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>),   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># blank line between models</span></span>
<span id="cb11-86">  metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm</span>
<span id="cb11-87">)</span>
<span id="cb11-88"></span>
<span id="cb11-89"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(roc_results, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>TPR,</span>
<span id="cb11-90">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>model_norm,</span>
<span id="cb11-91">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype=</span>curve_type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-92">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-93">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">intercept=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb11-94">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey60"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-95">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values=</span>color_values, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels=</span>legend_labels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-96">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_linetype_manual</span>(</span>
<span id="cb11-97">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"raw"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"smooth"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>),</span>
<span id="cb11-98">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"raw"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Raw ROC: dashed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"smooth"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Smoothed ROC: solid"</span>)</span>
<span id="cb11-99">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-100">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-101">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-102">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ROC curves by model (raw vs smoothed)"</span>,</span>
<span id="cb11-103">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"False Positive Rate"</span>,</span>
<span id="cb11-104">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True Positive Rate"</span>,</span>
<span id="cb11-105">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-106">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(</span>
<span id="cb11-107">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.theme =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)),</span>
<span id="cb11-108">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.theme =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>))</span>
<span id="cb11-109">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-110">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-111">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb11-112">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb11-113">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb11-114">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>),</span>
<span id="cb11-115">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>),   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inside plot, under diagonal</span></span>
<span id="cb11-116">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lineheight=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>),</span>
<span id="cb11-117">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">alpha</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb11-118">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key.size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>),</span>
<span id="cb11-119">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb11-120">  )</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-6-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/roc-and-differential-geometry/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<p>The first thing to observe is that the smooth curves do not perfectly overlay the stair-step, raw curves, as one might expect for curves with large steps. Nevertheless, the smooth curve AUC numbers are close to the raw curve numbers. Whether they are close enough depends on the application. My intuition is that as the number of data points used to construct the raw curves increases, the smoothed curves will better fit the raw curves and the AUC values will be closer. Also, note that selecting the curves based on arc length would lead to the same results.</p>
<p>The following code, which looks at adjacent differences in the smoothed ROC curves, flags monotonicity violations for Logistic Regression TPR.</p>
<div class="cell">
<details class="code-fold">
<summary>Show Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Check monotonicity of smoothed ROC curves ---</span></span>
<span id="cb12-2">check_monotone <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model_norm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb12-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">monotone_TPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(TPR) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># allow tiny numerical tolerance</span></span>
<span id="cb12-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">monotone_FPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(FPR) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>),</span>
<span id="cb12-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb12-8">  )</span>
<span id="cb12-9"></span>
<span id="cb12-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(check_monotone)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 3
  model_norm          monotone_TPR monotone_FPR
  &lt;chr&gt;               &lt;lgl&gt;        &lt;lgl&gt;       
1 Decision Tree       TRUE         TRUE        
2 Logistic Regression FALSE        TRUE        
3 SVM                 TRUE         TRUE        </code></pre>
</div>
</div>
<p>Looking further into these violations for the Logistic Regression models shows that there are on the order of <img src="https://latex.codecogs.com/png.latex?10%5E%7B-5%7D">, essentially numerical noise.</p>
<div class="cell">
<details class="code-fold">
<summary>Show Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">violations <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model_norm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb14-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dFPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(FPR)),</span>
<span id="cb14-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dTPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(TPR)),</span>
<span id="cb14-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR_violation =</span> dFPR <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>,</span>
<span id="cb14-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR_violation =</span> dTPR <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span></span>
<span id="cb14-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(FPR_violation <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> TPR_violation)</span>
<span id="cb14-10"></span>
<span id="cb14-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(violations)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 9
# Groups:   model_norm [1]
  model_norm            FPR   TPR   auc   arc    dFPR        dTPR FPR_violation
  &lt;chr&gt;               &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;   &lt;dbl&gt;       &lt;dbl&gt; &lt;lgl&gt;        
1 Logistic Regression 0.173 0.722 0.818  1.70 0.00251 -0.0000272  FALSE        
2 Logistic Regression 0.175 0.722 0.818  1.70 0.00251 -0.0000469  FALSE        
3 Logistic Regression 0.178 0.722 0.818  1.70 0.00251 -0.0000491  FALSE        
4 Logistic Regression 0.180 0.722 0.818  1.70 0.00251 -0.0000339  FALSE        
5 Logistic Regression 0.183 0.722 0.818  1.70 0.00251 -0.00000581 FALSE        
# ℹ 1 more variable: TPR_violation &lt;lgl&gt;</code></pre>
</div>
</div>
<p>As was mentioned above, the smoothing process slightly changes the FPR and TPR values because they are now calculated with respect to the dense grid to perform smoothing. To get a feel for how this process may affect the FPR and TPR values, we plot the distributions of TPR and FPR for both the raw and smoothed ROC curves. This next plot shows the distributions for the raw ROC curves.</p>
<div class="cell">
<details class="code-fold">
<summary>Show Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define pastel colors</span></span>
<span id="cb16-2">model_colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb16-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>        <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#fcae91"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel red</span></span>
<span id="cb16-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span>  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a1d99b"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel green</span></span>
<span id="cb16-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>                  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9ecae1"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel blue</span></span>
<span id="cb16-6">)</span>
<span id="cb16-7"></span>
<span id="cb16-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Plotting function for one model ---</span></span>
<span id="cb16-9">plot_distributions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df, model_label, color_hex) {</span>
<span id="cb16-10">  n_samples <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(df)</span>
<span id="cb16-11">  fpr_mean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb16-12">  fpr_sd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb16-13">  tpr_mean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb16-14">  tpr_sd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb16-15"></span>
<span id="cb16-16">  p_fpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> FPR)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)),</span>
<span id="cb16-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,</span>
<span id="cb16-19">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span></span>
<span id="cb16-20">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb16-23">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— FPR distribution"</span>),</span>
<span id="cb16-24">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb16-25">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n="</span>, n_samples,</span>
<span id="cb16-26">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | mean="</span>, fpr_mean,</span>
<span id="cb16-27">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | sd="</span>, fpr_sd</span>
<span id="cb16-28">      ),</span>
<span id="cb16-29">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"False Positive Rate"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span></span>
<span id="cb16-30">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-32">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb16-33">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb16-34">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb16-35">    )</span>
<span id="cb16-36"></span>
<span id="cb16-37">  p_tpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> TPR)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)),</span>
<span id="cb16-39">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,</span>
<span id="cb16-40">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span></span>
<span id="cb16-41">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-42">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-43">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb16-44">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— TPR distribution"</span>),</span>
<span id="cb16-45">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb16-46">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n="</span>, n_samples,</span>
<span id="cb16-47">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | mean="</span>, tpr_mean,</span>
<span id="cb16-48">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | sd="</span>, tpr_sd</span>
<span id="cb16-49">      ),</span>
<span id="cb16-50">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True Positive Rate"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span></span>
<span id="cb16-51">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-52">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-53">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb16-54">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb16-55">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb16-56">    )</span>
<span id="cb16-57"></span>
<span id="cb16-58">  p_fpr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_tpr <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># side by side</span></span>
<span id="cb16-59">}</span>
<span id="cb16-60"></span>
<span id="cb16-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Build plots for each model ---</span></span>
<span id="cb16-62">plots <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> roc_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">imap</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df, model_label) {</span>
<span id="cb16-65">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_distributions</span>(df, model_label, model_colors[[model_label]])</span>
<span id="cb16-66">  })</span>
<span id="cb16-67"></span>
<span id="cb16-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Arrange vertically: one row per model ---</span></span>
<span id="cb16-69">final_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap_plots</span>(plots, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb16-70"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(final_plot)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-9-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/roc-and-differential-geometry/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<p>And here are the distributions for the smoothed curves.</p>
<div class="cell">
<details class="code-fold">
<summary>Show Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define pastel colors</span></span>
<span id="cb17-2">model_colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb17-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>        <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#fcae91"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel red</span></span>
<span id="cb17-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span>  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a1d99b"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel green</span></span>
<span id="cb17-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>                  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9ecae1"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel blue</span></span>
<span id="cb17-6">)</span>
<span id="cb17-7"></span>
<span id="cb17-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Plotting function for smoothed results ---</span></span>
<span id="cb17-9">plot_smooth_distributions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df, model_label, color_hex) {</span>
<span id="cb17-10">  n_samples <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(df)</span>
<span id="cb17-11">  fpr_mean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb17-12">  fpr_sd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb17-13">  tpr_mean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb17-14">  tpr_sd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb17-15"></span>
<span id="cb17-16">  p_fpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> FPR)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)),</span>
<span id="cb17-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb17-19">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span></span>
<span id="cb17-20">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb17-23">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— Smoothed FPR distribution"</span>),</span>
<span id="cb17-24">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb17-25">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n="</span>, n_samples,</span>
<span id="cb17-26">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | mean="</span>, fpr_mean,</span>
<span id="cb17-27">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | sd="</span>, fpr_sd</span>
<span id="cb17-28">      ),</span>
<span id="cb17-29">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"False Positive Rate"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span></span>
<span id="cb17-30">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-32">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb17-33">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb17-34">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb17-35">    )</span>
<span id="cb17-36"></span>
<span id="cb17-37">  p_tpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> TPR)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)),</span>
<span id="cb17-39">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb17-40">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span></span>
<span id="cb17-41">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-42">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-43">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb17-44">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— Smoothed TPR distribution"</span>),</span>
<span id="cb17-45">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb17-46">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n="</span>, n_samples,</span>
<span id="cb17-47">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | mean="</span>, tpr_mean,</span>
<span id="cb17-48">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | sd="</span>, tpr_sd</span>
<span id="cb17-49">      ),</span>
<span id="cb17-50">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True Positive Rate"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span></span>
<span id="cb17-51">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-52">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-53">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb17-54">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb17-55">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb17-56">    )</span>
<span id="cb17-57"></span>
<span id="cb17-58">  p_fpr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_tpr <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># side by side</span></span>
<span id="cb17-59">}</span>
<span id="cb17-60"></span>
<span id="cb17-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Build plots for each model ---</span></span>
<span id="cb17-62">smooth_plots <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">imap</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df, model_label) {</span>
<span id="cb17-65">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_smooth_distributions</span>(df, model_label, model_colors[[model_label]])</span>
<span id="cb17-66">  })</span>
<span id="cb17-67"></span>
<span id="cb17-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Arrange vertically: one row per model ---</span></span>
<span id="cb17-69">final_smooth_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap_plots</span>(smooth_plots, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb17-70"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(final_smooth_plot)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-10-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/roc-and-differential-geometry/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<p>They look to be pretty close. How close could be quantified, but again, are they close enough would depend on the application.</p>
</section>
<section id="a-little-calculus-with-roc-curves" class="level2">
<h2 class="anchored" data-anchor-id="a-little-calculus-with-roc-curves">A Little Calculus with ROC curves</h2>
<p>In this section, we examine what we can do using a little calculus on the smoothed ROC curves. We begin by computing some derivatives. This next plot shows the ROC curves for the three classifiers in the left column and the corresponding derivative of TPR with respect to FPR, <img src="https://latex.codecogs.com/png.latex?d(TPR)%20/%20d(FPR)"> in the right-hand column.</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code for computing slope</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Palette: pastel per model ---</span></span>
<span id="cb18-2">model_colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb18-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F28E8E"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel red</span></span>
<span id="cb18-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8FD19E"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel green</span></span>
<span id="cb18-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>                 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8EB8FF"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel blue</span></span>
<span id="cb18-6">)</span>
<span id="cb18-7"></span>
<span id="cb18-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Clean ROC per group: sort, drop duplicate FPR, enforce monotone TPR ---</span></span>
<span id="cb18-9">clean_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb18-10">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(FPR) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep_all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb18-14">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(FPR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb18-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cummax</span>(TPR), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb18-16">    )</span>
<span id="cb18-17">}</span>
<span id="cb18-18"></span>
<span id="cb18-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The goal of make_spline and compute_roc_geometry is to build a spline representation of the ROC curve </span></span>
<span id="cb18-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   that allows derivatives (first and second) to be computed easily.</span></span>
<span id="cb18-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  splinefun(..., method = "natural") — uses natural cubic splines which are smoother and differentiable up to second order.</span></span>
<span id="cb18-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># note that "monoH.FC" splines guarantee monotonicity but can sometimes produce derivative discontinuities or numerical  </span></span>
<span id="cb18-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   instability in higher higher‑order derivatives.</span></span>
<span id="cb18-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The output is a tibble with columns FPR, TPR, dTPR (first derivative), and d2TPR (second derivative) evaluated on a uniform grid of FPR values from 0 to 1.</span></span>
<span id="cb18-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use case: When you need geometry (curvature, slope, phase space analysis) rather than just smoothed ROC points</span></span>
<span id="cb18-26"></span>
<span id="cb18-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Spline builder (use "natural" to avoid strict monotonicity errors) ---</span></span>
<span id="cb18-28">make_spline <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb18-29">  df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clean_roc</span>(df)</span>
<span id="cb18-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">splinefun</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"natural"</span>)</span>
<span id="cb18-31">}</span>
<span id="cb18-32"></span>
<span id="cb18-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Geometry over a uniform grid: y, y', y'' ---</span></span>
<span id="cb18-34">compute_roc_geometry <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1001</span>) {</span>
<span id="cb18-35">  f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_spline</span>(df)</span>
<span id="cb18-36">  xg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n_grid)</span>
<span id="cb18-37">  yg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(xg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb18-38">  y1g <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(xg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb18-39">  y2g <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(xg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb18-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> xg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> yg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dTPR =</span> y1g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d2TPR =</span> y2g)</span>
<span id="cb18-41">}</span>
<span id="cb18-42"></span>
<span id="cb18-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Plotters with small text and single-color line per model ---</span></span>
<span id="cb18-44">plot_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(geom_df, model_label, color_hex) {</span>
<span id="cb18-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(geom_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> TPR)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-46">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-47">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb18-48">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— Smoothed ROC"</span>),</span>
<span id="cb18-49">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FPR"</span>,</span>
<span id="cb18-50">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TPR"</span></span>
<span id="cb18-51">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-52">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-53">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-54">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb18-55">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb18-56">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb18-57">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb18-58">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()</span>
<span id="cb18-59">    )</span>
<span id="cb18-60">}</span>
<span id="cb18-61"></span>
<span id="cb18-62">plot_derivative <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(geom_df, model_label, color_hex) {</span>
<span id="cb18-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(geom_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> dTPR)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-64">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey80"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-65">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-66">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb18-67">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— dTPR/dFPR vs FPR"</span>),</span>
<span id="cb18-68">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FPR"</span>,</span>
<span id="cb18-69">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dTPR/dFPR"</span></span>
<span id="cb18-70">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-71">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-72">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-73">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb18-74">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb18-75">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb18-76">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb18-77">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()</span>
<span id="cb18-78">    )</span>
<span id="cb18-79">}</span>
<span id="cb18-80"></span>
<span id="cb18-81"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Main: build 3x2 grid with ROC on the LEFT, derivative on the RIGHT ---</span></span>
<span id="cb18-82">plot_roc_geometry_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1001</span>) {</span>
<span id="cb18-83">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ensure intended order of rows</span></span>
<span id="cb18-84">  model_order <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>)</span>
<span id="cb18-85">  models <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(model_order, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(smooth_results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm))</span>
<span id="cb18-86">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(models) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb18-87"></span>
<span id="cb18-88">  rows <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(models, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(m) {</span>
<span id="cb18-89">    df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(model_norm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> m)</span>
<span id="cb18-90">    geom_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compute_roc_geometry</span>(df, n_grid)</span>
<span id="cb18-91">    col_hex <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model_colors[[m]]</span>
<span id="cb18-92">    p_left <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_roc</span>(geom_df, m, col_hex)</span>
<span id="cb18-93">    p_right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_derivative</span>(geom_df, m, col_hex)</span>
<span id="cb18-94">    p_left <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_right</span>
<span id="cb18-95">  })</span>
<span id="cb18-96"></span>
<span id="cb18-97">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stack rows into 3x2 (or as many as available)</span></span>
<span id="cb18-98">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>(rows, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">/</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb18-99">}</span>
<span id="cb18-100"></span>
<span id="cb18-101"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Usage ---</span></span>
<span id="cb18-102"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># smooth_results must have columns: model_norm, FPR, TPR</span></span>
<span id="cb18-103">grid_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_roc_geometry_grid</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2001</span>)</span>
<span id="cb18-104"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(grid_plot)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-11-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/roc-and-differential-geometry/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<p>It a well known result from mathematical statistics, not usually emphasized in a machine learning context, that the slope of the tangent to the ROC curve at any point is equal to the the instantaneous likelihood ratio at that point. <a href="https://academic.oup.com/aje/article-abstract/148/11/1127/123231?redirectedFrom=PDF">Choi (1998)</a>. This is exactly what is plotted in the second column. However, to understand the connection with differential geometry, the following section of code re-derives the plot by considering ROC curves parameterized by threshold <img src="https://latex.codecogs.com/png.latex?t%20%5Cin%20%5B0,1%5D">. The curve is expressed as the set of points <img src="https://latex.codecogs.com/png.latex?(FPR(t),TPR(t))">. This point of view is preferred for analyzing likelihood ratios and connecting with diagnostic test theory. In both cases, the likelihood ratio is identical since likelihood is a property of the curve and <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd(TPR)%7D%7Bd(FPR)%7D%20=%20%5Cfrac%7Bd(TPR)/dt))%7D%7Bd(FPR)/dt%7D">.</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code for parameterized ROC curves</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Pastel palette per model ---</span></span>
<span id="cb19-2">model_colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb19-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F28E8E"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel red</span></span>
<span id="cb19-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8FD19E"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel green</span></span>
<span id="cb19-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>                 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8EB8FF"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel blue</span></span>
<span id="cb19-6">)</span>
<span id="cb19-7"></span>
<span id="cb19-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Ensure threshold column exists ---</span></span>
<span id="cb19-9">add_threshold <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb19-10">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model_norm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb19-14">}</span>
<span id="cb19-15"></span>
<span id="cb19-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Clean ROC per group ---</span></span>
<span id="cb19-17">clean_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb19-18">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(threshold) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep_all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb19-22">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(FPR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb19-23">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(TPR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb19-24">    )</span>
<span id="cb19-25">}</span>
<span id="cb19-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Spline builders for parametric ROC (FPR(t), TPR(t)) ---</span></span>
<span id="cb19-27">make_spline_pair <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb19-28">  df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clean_roc</span>(df)</span>
<span id="cb19-29">  f_fpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">splinefun</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"natural"</span>)</span>
<span id="cb19-30">  f_tpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">splinefun</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"natural"</span>)</span>
<span id="cb19-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">f_fpr =</span> f_fpr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">f_tpr =</span> f_tpr)</span>
<span id="cb19-32">}</span>
<span id="cb19-33"></span>
<span id="cb19-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Compute ROC geometry with respect to threshold t ---</span></span>
<span id="cb19-35">compute_roc_geometry <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1001</span>) {</span>
<span id="cb19-36">  spl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_spline_pair</span>(df)</span>
<span id="cb19-37">  f_fpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> spl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f_fpr</span>
<span id="cb19-38">  f_tpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> spl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f_tpr</span>
<span id="cb19-39"></span>
<span id="cb19-40">  tg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>threshold), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>threshold), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n_grid)</span>
<span id="cb19-41"></span>
<span id="cb19-42">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_fpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># FPR(t)</span></span>
<span id="cb19-43">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_tpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># TPR(t)</span></span>
<span id="cb19-44">  x1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_fpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dFPR/dt</span></span>
<span id="cb19-45">  y1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_tpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dTPR/dt</span></span>
<span id="cb19-46"></span>
<span id="cb19-47">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># slope of ROC curve = dTPR/dFPR = (dTPR/dt) / (dFPR/dt)</span></span>
<span id="cb19-48">  slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> x1</span>
<span id="cb19-49"></span>
<span id="cb19-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb19-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> tg,</span>
<span id="cb19-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> x,</span>
<span id="cb19-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> y,</span>
<span id="cb19-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dFPR_dt =</span> x1,</span>
<span id="cb19-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dTPR_dt =</span> y1,</span>
<span id="cb19-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope_dTPR_dFPR =</span> slope</span>
<span id="cb19-57">  )</span>
<span id="cb19-58">}</span>
<span id="cb19-59"></span>
<span id="cb19-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Build combined data frame for all models ---</span></span>
<span id="cb19-61">make_derivative_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1001</span>) {</span>
<span id="cb19-62">  smooth_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_threshold</span>(smooth_results)</span>
<span id="cb19-63"></span>
<span id="cb19-64">  model_order <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>)</span>
<span id="cb19-65">  models <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(model_order, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(smooth_results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm))</span>
<span id="cb19-66"></span>
<span id="cb19-67">  results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(models, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(m) {</span>
<span id="cb19-68">    df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(model_norm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> m)</span>
<span id="cb19-69">    geom_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compute_roc_geometry</span>(df, n_grid)</span>
<span id="cb19-70">    geom_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> m)</span>
<span id="cb19-71">  })</span>
<span id="cb19-72"></span>
<span id="cb19-73">  results</span>
<span id="cb19-74">}</span>
<span id="cb19-75"></span>
<span id="cb19-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Plotters ---</span></span>
<span id="cb19-77">plot_curvature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(geom_df, model_label, color_hex) {</span>
<span id="cb19-78">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(geom_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> TPR)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-79">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-80">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb19-81">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— ROC curve TPR(t)"</span>),</span>
<span id="cb19-82">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TPR"</span></span>
<span id="cb19-83">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-84">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-85">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb19-86">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb19-87">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb19-88">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb19-89">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()</span>
<span id="cb19-90">    )</span>
<span id="cb19-91">}</span>
<span id="cb19-92"></span>
<span id="cb19-93">plot_slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(geom_df, model_label, color_hex) {</span>
<span id="cb19-94">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(geom_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> slope_dTPR_dFPR)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-95">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey80"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-96">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-97">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb19-98">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— dTPR/dFPR (Likelihood Ratio)"</span>),</span>
<span id="cb19-99">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dTPR/dFPR"</span></span>
<span id="cb19-100">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-101">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-102">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb19-103">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb19-104">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb19-105">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb19-106">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()</span>
<span id="cb19-107">    )</span>
<span id="cb19-108">}</span>
<span id="cb19-109"></span>
<span id="cb19-110"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Main: build 3x2 grid (ROC curve | slope dTPR/dFPR) ---</span></span>
<span id="cb19-111">plot_roc_geometry_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1001</span>) {</span>
<span id="cb19-112">  smooth_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_threshold</span>(smooth_results)</span>
<span id="cb19-113"></span>
<span id="cb19-114">  model_order <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>)</span>
<span id="cb19-115">  models <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(model_order, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(smooth_results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm))</span>
<span id="cb19-116">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(models) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb19-117"></span>
<span id="cb19-118">  rows <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(models, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(m) {</span>
<span id="cb19-119">    df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(model_norm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> m)</span>
<span id="cb19-120">    geom_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compute_roc_geometry</span>(df, n_grid)</span>
<span id="cb19-121">    col_hex <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model_colors[[m]]</span>
<span id="cb19-122">    p_left <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_curvature</span>(geom_df, m, col_hex)</span>
<span id="cb19-123">    p_right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_slope</span>(geom_df, m, col_hex)</span>
<span id="cb19-124">    patchwork<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap_plots</span>(p_left, p_right, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb19-125">  })</span>
<span id="cb19-126"></span>
<span id="cb19-127">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>(rows, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">/</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb19-128">}</span>
<span id="cb19-129"></span>
<span id="cb19-130"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Usage ---</span></span>
<span id="cb19-131">derivative_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_derivative_df</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2001</span>)</span>
<span id="cb19-132">grid_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_roc_geometry_grid</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2001</span>)</span>
<span id="cb19-133"></span>
<span id="cb19-134"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(grid_plot)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-12-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/roc-and-differential-geometry/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Show the code for parameterized ROC curves</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#head(derivative_df)</span></span></code></pre></div></div>
</details>
</div>
<p>The plot above shows the Likelihood ratios as a function of threshold for each of the three models. These values are stored in <code>slope_DTPR_dFPR</code> column of the <code>derivative_df</code> data frame. The following code extracts the maximum positive likelihood ratio and minimum negative likelihood ratio for each model. It follows Choi (1998), where he suggests comparing LR values to decision thresholds that are convenient for diagnostic testing:</p>
<ul>
<li>LR+ (positive test): slope of the operating point where TPR is high and FPR is low (upper left of ROC curve)</li>
<li>LR- negative test: slope of the operating point where TPR is low and FPR is high (lower right of ROC curve)</li>
</ul>
<div class="cell">
<details class="code-fold">
<summary>Show the LR test code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">derivative_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb21-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_LR_plus =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(slope_dTPR_dFPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb21-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min_LR_minus =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(slope_dTPR_dFPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb21-6">  )</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 3
  model               max_LR_plus min_LR_minus
  &lt;chr&gt;                     &lt;dbl&gt;        &lt;dbl&gt;
1 Decision Tree              2.21   -0.0000530
2 Logistic Regression        7.64   -0.0203   
3 SVM                        3.87   -0.00814  </code></pre>
</div>
</div>
<p>A standard interpretation for a diagnostic test is that LR+ values above 10 are considered strong evidence to rule in a condition while LR- values below 0.1 are considered strong evidence to rule out a condition.</p>
</section>
<section id="curvature" class="level2">
<h2 class="anchored" data-anchor-id="curvature">Curvature</h2>
<p>We continue exploring ROC curves with ideas from elementary differential geometry. As was noted above, a smoothed ROC curve is a two-dimensional planar curve parameterized by threshold <img src="https://latex.codecogs.com/png.latex?t"> which also directly represents the relationship between TPR and FPR. Each point (x,y) on the ROC curve yields the conditional distribution of TPR given the distribution of FPR, <img src="https://latex.codecogs.com/png.latex?P(TPR%20%5Cle%20y%20%5Cmid%20FPR%20%5Cle%20x)">.</p>
<p>Curvature, <img src="https://latex.codecogs.com/png.latex?%5Ckappa(x)%20=%20%5Cfrac%7B%7Cy''(x)%7C%7D%7B%5Cleft(1%20+%20%5Cleft%5By'(x)%5Cright%5D%5E2%5Cright)%5E%7B3/2%7D%7D"> of a two-dimensional planar curve, measures how sharply the curve bends at a given threshold, <img src="https://latex.codecogs.com/png.latex?t">. High values of curvature imply a rapidly changing slope, while low curvature indicates that the slope is changing slowly.</p>
<p>So curvature and its derivative with respect to threshold may be helpful in selecting threshold values for a particular application. Large positive values of <img src="https://latex.codecogs.com/png.latex?d%5Ckappa(t)/dt"> can indicate threshold zone where small changes in the decision rule can produce large changes in discrimination. Large negative values of <img src="https://latex.codecogs.com/png.latex?d%5Ckappa(t)/dt"> can indicate zones where slope changes are stabilizing, suggesting diminishing returns for tightening or loosening the decision rules. Zones with values near zero indicate regions that are relatively stable with respect to the threshold. The following code calculates <img src="https://latex.codecogs.com/png.latex?%5Ckappa(t)"> and <img src="https://latex.codecogs.com/png.latex?d%5Ckappa(t)/dt"> for each of the three models and plots them side by side.</p>
<div class="cell">
<details class="code-fold">
<summary>Code for curvature plots</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Pastel palette per model ---</span></span>
<span id="cb23-2">model_colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb23-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F28E8E"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel red</span></span>
<span id="cb23-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8FD19E"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel green</span></span>
<span id="cb23-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>                 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8EB8FF"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pastel blue</span></span>
<span id="cb23-6">)</span>
<span id="cb23-7"></span>
<span id="cb23-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Ensure threshold column exists ---</span></span>
<span id="cb23-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If smooth_results has only FPR/TPR, add a synthetic threshold</span></span>
<span id="cb23-10">add_threshold <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb23-11">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model_norm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb23-15">}</span>
<span id="cb23-16"></span>
<span id="cb23-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Clean ROC per group ---</span></span>
<span id="cb23-18">clean_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb23-19">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(threshold) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep_all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb23-23">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(FPR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb23-24">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(TPR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb23-25">    )</span>
<span id="cb23-26">}</span>
<span id="cb23-27"></span>
<span id="cb23-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Spline builders for parametric ROC (FPR(t), TPR(t)) ---</span></span>
<span id="cb23-29">make_spline_pair <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df) {</span>
<span id="cb23-30">  df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clean_roc</span>(df)</span>
<span id="cb23-31">  f_fpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">splinefun</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>FPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"natural"</span>)</span>
<span id="cb23-32">  f_tpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">splinefun</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"natural"</span>)</span>
<span id="cb23-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">f_fpr =</span> f_fpr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">f_tpr =</span> f_tpr)</span>
<span id="cb23-34">}</span>
<span id="cb23-35"></span>
<span id="cb23-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Compute ROC geometry with respect to threshold t ---</span></span>
<span id="cb23-37">compute_roc_geometry <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1001</span>) {</span>
<span id="cb23-38">  spl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_spline_pair</span>(df)</span>
<span id="cb23-39">  f_fpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> spl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f_fpr</span>
<span id="cb23-40">  f_tpr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> spl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f_tpr</span>
<span id="cb23-41"></span>
<span id="cb23-42">  tg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>threshold), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>threshold), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n_grid)</span>
<span id="cb23-43"></span>
<span id="cb23-44">  x   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_fpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb23-45">  y   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_tpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb23-46">  x1  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_fpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb23-47">  y1  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_tpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb23-48">  x2  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_fpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb23-49">  y2  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_tpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb23-50">  x3  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_fpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb23-51">  y3  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f_tpr</span>(tg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deriv =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb23-52"></span>
<span id="cb23-53">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># curvature κ(t)</span></span>
<span id="cb23-54">  num   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x2</span>
<span id="cb23-55">  denom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (x1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb23-56">  kappa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(num) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denom</span>
<span id="cb23-57"></span>
<span id="cb23-58">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># derivative of curvature wrt t</span></span>
<span id="cb23-59">  dnum   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y3 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x3 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x2</span>
<span id="cb23-60">  ddenom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (x1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>y2)</span>
<span id="cb23-61">  dkappa_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (dnum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> denom <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> num <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ddenom) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (denom<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb23-62"></span>
<span id="cb23-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb23-64">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> tg,</span>
<span id="cb23-65">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR =</span> x,</span>
<span id="cb23-66">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR =</span> y,</span>
<span id="cb23-67">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dFPR_dt =</span> x1,</span>
<span id="cb23-68">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dTPR_dt =</span> y1,</span>
<span id="cb23-69">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa =</span> kappa,</span>
<span id="cb23-70">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dkappa_dt =</span> dkappa_dt</span>
<span id="cb23-71">  )</span>
<span id="cb23-72">}</span>
<span id="cb23-73"></span>
<span id="cb23-74"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Build combined data frame for all models ---</span></span>
<span id="cb23-75">make_derivative_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1001</span>) {</span>
<span id="cb23-76">  smooth_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_threshold</span>(smooth_results)</span>
<span id="cb23-77"></span>
<span id="cb23-78">  model_order <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>)</span>
<span id="cb23-79">  models <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(model_order, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(smooth_results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm))</span>
<span id="cb23-80"></span>
<span id="cb23-81">  results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(models, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(m) {</span>
<span id="cb23-82">    df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(model_norm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> m)</span>
<span id="cb23-83">    geom_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compute_roc_geometry</span>(df, n_grid)</span>
<span id="cb23-84">    geom_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> m)</span>
<span id="cb23-85">  })</span>
<span id="cb23-86"></span>
<span id="cb23-87">  results</span>
<span id="cb23-88">}</span>
<span id="cb23-89"></span>
<span id="cb23-90"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Plotters ---</span></span>
<span id="cb23-91">plot_curvature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(geom_df, model_label, color_hex) {</span>
<span id="cb23-92">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(geom_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> kappa)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-93">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-94">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— curvature κ(t)"</span>),</span>
<span id="cb23-95">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"κ(t)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-96">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-97">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb23-98">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb23-99">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb23-100">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span>
<span id="cb23-101">}</span>
<span id="cb23-102"></span>
<span id="cb23-103">plot_dcurvature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(geom_df, model_label, color_hex) {</span>
<span id="cb23-104">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(geom_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> dkappa_dt)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-105">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey80"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-106">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_hex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-107">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(model_label, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"— dκ/dt"</span>),</span>
<span id="cb23-108">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dκ/dt"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-109">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-110">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb23-111">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb23-112">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb23-113">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span>
<span id="cb23-114">}</span>
<span id="cb23-115"></span>
<span id="cb23-116"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Main: build 3x2 grid (curvature | dκ/dt) ---</span></span>
<span id="cb23-117">plot_roc_geometry_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1001</span>) {</span>
<span id="cb23-118">  smooth_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_threshold</span>(smooth_results)</span>
<span id="cb23-119"></span>
<span id="cb23-120">  model_order <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decision Tree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logistic Regression"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SVM"</span>)</span>
<span id="cb23-121">  models <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(model_order, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(smooth_results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_norm))</span>
<span id="cb23-122">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(models) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb23-123"></span>
<span id="cb23-124">  rows <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(models, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(m) {</span>
<span id="cb23-125">    df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smooth_results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(model_norm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> m)</span>
<span id="cb23-126">    geom_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compute_roc_geometry</span>(df, n_grid)</span>
<span id="cb23-127">    col_hex <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model_colors[[m]]</span>
<span id="cb23-128">    p_left  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_curvature</span>(geom_df, m, col_hex)</span>
<span id="cb23-129">    p_right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_dcurvature</span>(geom_df, m, col_hex)</span>
<span id="cb23-130">    patchwork<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap_plots</span>(p_left, p_right, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb23-131">  })</span>
<span id="cb23-132"></span>
<span id="cb23-133">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>(rows, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">/</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb23-134">}</span>
<span id="cb23-135"></span>
<span id="cb23-136"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Usage ---</span></span>
<span id="cb23-137">curvature_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_derivative_df</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2001</span>)</span>
<span id="cb23-138"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#head(derivative_df)</span></span>
<span id="cb23-139">grid_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_roc_geometry_grid</span>(smooth_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_grid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2001</span>)</span>
<span id="cb23-140"></span>
<span id="cb23-141">grid_plot</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-14-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/roc-and-differential-geometry/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<p>The first thing that you may notice about the plots is that the curvature for the decision trees model looks smooth and stable. This is to be expected because the ROC curve for decision trees is piece wise linear with a small number of segments. The curvature is zero everywhere except at the corners, where it is undefined. The smoothing spline used here smooths out the corners, producing a smooth curve with small curvature values.</p>
<p>The areas around threshold values <img src="https://latex.codecogs.com/png.latex?t=0.5"> appear to behave differently for the Logistic Regression and SVM models. However, it is not clear that these differences would make any practical difference in selecting thresholds for these models.</p>
<p>Using curvature in ROC studies is a relatively new idea, and it does not appear to be well studied. However, evaluating the curvature of ROC curves seems to be an idea that holds some promise. In their 2022 paper, <a href="https://academic.oup.com/bioinformatics/article/38/24/5390/6769888?login=false">Defining the extent of gene function using ROC curvature</a>, Fischer and Gillis introduce the curvature of ROC curves as a method to evaluate gene function prediction. They write: &gt; We identify Functional Equivalence Classes (FECs), subsets of annotated and unannotated genes that jointly drive performance, by assessing the presence of straight lines in ROC curves built from gene-centric prediction tasks, such as function or interaction predictions.</p>
<section id="the-case-for-arc-length" class="level3">
<h3 class="anchored" data-anchor-id="the-case-for-arc-length">The Case for Arc Length</h3>
<p>Finally, I would like to make a case for using the arc length <img src="https://latex.codecogs.com/png.latex?%5Cint_0%5Et%20%5Csqrt%7B1+(f'(x))%5E2%7D%5C,dx"> of a smoothed ROC curve as a metric for comparing classifiers that appears to be consistent and complementary to AUC. Arc length is not a useful concept for raw, stair-step ROC curves. Any stair-step curve that goes from (0,0) to (1,1) will have the same arc length of 2.0. However, the arc length for a viable smoothed ROC curve will range from <img src="https://latex.codecogs.com/png.latex?%5Csqrt2"> to 2, and may be a useful metric for smoothed ROC curves because it captures information about the geometry of the curve that AUC does not.</p>
<p>Arc length is more sensitive to the shape of the ROC curve than AUC. Two ROC curves with identical AUC values can have very different shapes and therefore very different arc lengths. Arc length captures information about the slope changes, curvature, and smoothness of the ROC curve that AUC does not. This could be important in applications where the shape of the ROC curve is relevant to decision-making. For example, because the trajectory of most viable ROC curves will stay well above the diagonal from (0,0) to (1,1), arc length mostly avoids the criticism of AUC that led to the development of <a href="https://en.wikipedia.org/wiki/Partial_Area_Under_the_ROC_Curve">Partial Arc length</a>. It is relatively easy to exclude segments of regions that are not important to the application. And, unless the ROC curve pathologically crosses the diagonal below FPR = 0 .5 it will not enter the region of <a href="https://en.wikipedia.org/wiki/Receiver_operating_characteristic#:~:text=The%20main%20criticism%20to%20the,the%20plot%20on%20the%20right.">low sensitivity and low specificity</a>.</p>
<p>It is also the case that arc length is a linear measure while AUC is an area. I may very well be wrong about this, but I think most people have a better intuition of the practical significance of a linear difference of 0.3 inches than an area difference of 0.3 square inches. The following table contrasts AUC and arc length.</p>
</section>
<section id="table-comparing-arclength-with-auc" class="level3">
<h3 class="anchored" data-anchor-id="table-comparing-arclength-with-auc">Table Comparing Arclength with AUC</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code to build the table</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Data for table ---</span></span>
<span id="cb24-2">tbl_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb24-3">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Aspect, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>AUC, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>ArcLength,</span>
<span id="cb24-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Definition"</span>,</span>
<span id="cb24-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">int_0^1 f(x)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">,dx$"</span>,</span>
<span id="cb24-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">int_0^t </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">sqrt{1+(f'(x))^2}</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">,dx$"</span>,</span>
<span id="cb24-7">  </span>
<span id="cb24-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bounds"</span>,</span>
<span id="cb24-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.5 (random) to 1.0 (perfect)"</span>,</span>
<span id="cb24-10">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">sqrt{2} </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">approx 1.414$ (diagonal) to 2.0 (perfect staircase ROC)"</span>,</span>
<span id="cb24-11">  </span>
<span id="cb24-12">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Interpretability"</span>,</span>
<span id="cb24-13">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Widely used, intuitive for clinicians; benchmarks exist (e.g., &gt;0.9 = excellent)"</span>,</span>
<span id="cb24-14">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Linear measure, easier to visualize by eye; highlights curve geometry and trajectory"</span>,</span>
<span id="cb24-15">  </span>
<span id="cb24-16">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensitivity to curve shape"</span>,</span>
<span id="cb24-17">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Less sensitive — curves with different shapes can yield similar AUC"</span>,</span>
<span id="cb24-18">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"More sensitive — captures slope changes, curvature, and smoothness differences"</span>,</span>
<span id="cb24-19">  </span>
<span id="cb24-20">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Partial evaluation"</span>,</span>
<span id="cb24-21">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Partial AUC requires normalization; less visually obvious"</span>,</span>
<span id="cb24-22">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"It is easy to avoid problematic regions for most reasonable ROC curves"</span>,</span>
<span id="cb24-23">  </span>
<span id="cb24-24">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Noise robustness"</span>,</span>
<span id="cb24-25">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Relatively robust; integrates over curve"</span>,</span>
<span id="cb24-26">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"More sensitive to noise or jaggedness; small oscillations inflate length"</span>,</span>
<span id="cb24-27">  </span>
<span id="cb24-28">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clinical adoption"</span>,</span>
<span id="cb24-29">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Standard metric with established thresholds"</span>,</span>
<span id="cb24-30">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Novel metric; not yet widely adopted, requires new benchmarks"</span>,</span>
<span id="cb24-31">  </span>
<span id="cb24-32">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Use cases"</span>,</span>
<span id="cb24-33">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ranking accuracy, overall discrimination power"</span>,</span>
<span id="cb24-34">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diagnostic trajectory, geometric comparison, highlighting regional performance differences"</span></span>
<span id="cb24-35">)</span>
<span id="cb24-36">gt_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tbl_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_header</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Comparison of ROC Metrics: AUC vs. Arc Length"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols_label</span>(</span>
<span id="cb24-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Aspect =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Aspect"</span>,</span>
<span id="cb24-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AUC =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AUC (Area Under Curve)"</span>,</span>
<span id="cb24-42">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ArcLength =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Arc Length (ROC Curve Length)"</span></span>
<span id="cb24-43">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt_markdown</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_options</span>(</span>
<span id="cb24-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">table.font.size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">px</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># smaller text</span></span>
<span id="cb24-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_row.padding =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">px</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tighter row spacing</span></span>
<span id="cb24-48">  )</span>
<span id="cb24-49"></span>
<span id="cb24-50">gt_tbl</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="uhugtrqabq" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#uhugtrqabq table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#uhugtrqabq thead, #uhugtrqabq tbody, #uhugtrqabq tfoot, #uhugtrqabq tr, #uhugtrqabq td, #uhugtrqabq th {
  border-style: none;
}

#uhugtrqabq p {
  margin: 0;
  padding: 0;
}

#uhugtrqabq .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 12px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#uhugtrqabq .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#uhugtrqabq .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#uhugtrqabq .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#uhugtrqabq .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#uhugtrqabq .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#uhugtrqabq .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#uhugtrqabq .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#uhugtrqabq .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#uhugtrqabq .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#uhugtrqabq .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#uhugtrqabq .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#uhugtrqabq .gt_spanner_row {
  border-bottom-style: hidden;
}

#uhugtrqabq .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#uhugtrqabq .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#uhugtrqabq .gt_from_md > :first-child {
  margin-top: 0;
}

#uhugtrqabq .gt_from_md > :last-child {
  margin-bottom: 0;
}

#uhugtrqabq .gt_row {
  padding-top: 2px;
  padding-bottom: 2px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#uhugtrqabq .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#uhugtrqabq .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#uhugtrqabq .gt_row_group_first td {
  border-top-width: 2px;
}

#uhugtrqabq .gt_row_group_first th {
  border-top-width: 2px;
}

#uhugtrqabq .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#uhugtrqabq .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#uhugtrqabq .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#uhugtrqabq .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#uhugtrqabq .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#uhugtrqabq .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#uhugtrqabq .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#uhugtrqabq .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#uhugtrqabq .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#uhugtrqabq .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#uhugtrqabq .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#uhugtrqabq .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#uhugtrqabq .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#uhugtrqabq .gt_left {
  text-align: left;
}

#uhugtrqabq .gt_center {
  text-align: center;
}

#uhugtrqabq .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#uhugtrqabq .gt_font_normal {
  font-weight: normal;
}

#uhugtrqabq .gt_font_bold {
  font-weight: bold;
}

#uhugtrqabq .gt_font_italic {
  font-style: italic;
}

#uhugtrqabq .gt_super {
  font-size: 65%;
}

#uhugtrqabq .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#uhugtrqabq .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#uhugtrqabq .gt_indent_1 {
  text-indent: 5px;
}

#uhugtrqabq .gt_indent_2 {
  text-indent: 10px;
}

#uhugtrqabq .gt_indent_3 {
  text-indent: 15px;
}

#uhugtrqabq .gt_indent_4 {
  text-indent: 20px;
}

#uhugtrqabq .gt_indent_5 {
  text-indent: 25px;
}

#uhugtrqabq .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#uhugtrqabq div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_heading header">
<td colspan="3" class="gt_heading gt_title gt_font_normal gt_bottom_border">Comparison of ROC Metrics: AUC vs. Arc Length</td>
</tr>
<tr class="gt_col_headings even">
<th id="Aspect" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">Aspect</th>
<th id="AUC" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">AUC (Area Under Curve)</th>
<th id="ArcLength" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">Arc Length (ROC Curve Length)</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="Aspect">Definition</td>
<td class="gt_row gt_left" headers="AUC">(_0^1 f(x),dx)</td>
<td class="gt_row gt_left" headers="ArcLength">(_0^t ,dx)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Aspect">Bounds</td>
<td class="gt_row gt_left" headers="AUC">0.5 (random) to 1.0 (perfect)</td>
<td class="gt_row gt_left" headers="ArcLength">( ) (diagonal) to 2.0 (perfect staircase ROC)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Aspect">Interpretability</td>
<td class="gt_row gt_left" headers="AUC">Widely used, intuitive for clinicians; benchmarks exist (e.g., &gt;0.9 = excellent)</td>
<td class="gt_row gt_left" headers="ArcLength">Linear measure, easier to visualize by eye; highlights curve geometry and trajectory</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Aspect">Sensitivity to curve shape</td>
<td class="gt_row gt_left" headers="AUC">Less sensitive — curves with different shapes can yield similar AUC</td>
<td class="gt_row gt_left" headers="ArcLength">More sensitive — captures slope changes, curvature, and smoothness differences</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Aspect">Partial evaluation</td>
<td class="gt_row gt_left" headers="AUC">Partial AUC requires normalization; less visually obvious</td>
<td class="gt_row gt_left" headers="ArcLength">It is easy to avoid problematic regions for most reasonable ROC curves</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Aspect">Noise robustness</td>
<td class="gt_row gt_left" headers="AUC">Relatively robust; integrates over curve</td>
<td class="gt_row gt_left" headers="ArcLength">More sensitive to noise or jaggedness; small oscillations inflate length</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Aspect">Clinical adoption</td>
<td class="gt_row gt_left" headers="AUC">Standard metric with established thresholds</td>
<td class="gt_row gt_left" headers="ArcLength">Novel metric; not yet widely adopted, requires new benchmarks</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Aspect">Use cases</td>
<td class="gt_row gt_left" headers="AUC">Ranking accuracy, overall discrimination power</td>
<td class="gt_row gt_left" headers="ArcLength">Diagnostic trajectory, geometric comparison, highlighting regional performance differences</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</section>
<section id="conclusion" class="level3">
<h3 class="anchored" data-anchor-id="conclusion">Conclusion</h3>
<p>ROC curves, fundamental tools in evaluating binary classifiers, are naturally expressed as parameterized curves. However, especially for small samples, the raw ROC curves are stair-step functions that are not differentiable. Smoothing techniques, such as cubic splines, can produce smooth ROC curves that are differentiable and amenable to analysis beyond computing the area under the curve (AUC). Moreover, ideas from elementary differential geometry, such as curvature and arc length, may provide additional insights into the performance and behavior of classifiers that are not captured by AUC alone.</p>
</section>
</section>
<section id="appendix-equations-for-derivatives" class="level2">
<h2 class="anchored" data-anchor-id="appendix-equations-for-derivatives">Appendix: Equations for Derivatives</h2>
<section id="derivatives-of-equations-expressed-in-xy-coordinates" class="level4">
<h4 class="anchored" data-anchor-id="derivatives-of-equations-expressed-in-xy-coordinates">Derivatives of equations expressed in (x,y) coordinates</h4>
<p><img src="https://latex.codecogs.com/png.latex?y'(x)%20=%20%5Cfrac%7Bd%5C,%5Ctext%7BTPR%7D%7D%7Bd%5C,%5Ctext%7BFPR%7D%7D">, <img src="https://latex.codecogs.com/png.latex?y''(x)%20=%20%5Cfrac%7Bd%5E2%5C,%5Ctext%7BTPR%7D%7D%7Bd%5C,%5Ctext%7BFPR%7D%5E2%7D"></p>
<p><img src="https://latex.codecogs.com/png.latex?y'''(x)%20=%20%5Cfrac%7Bd%5E3%5C,%5Ctext%7BTPR%7D%7D%7Bd%5C,%5Ctext%7BFPR%7D%5E3%7D"></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ckappa(x)%20=%20%5Cfrac%7B%7Cy''(x)%7C%7D%7B%5Cleft(1%20+%20%5Cleft%5By'(x)%5Cright%5D%5E2%5Cright)%5E%7B3/2%7D%7D">, <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%5Ckappa%7D%7Bdx%7D%20=%20%5Cfrac%7By'''(x)%5C,%5Cleft(1%20+%20%5Cleft%5By'(x)%5Cright%5D%5E2%5Cright)%20-%203%5C,y'(x)%5C,%5Cleft(y''(x)%5Cright)%5E2%7D%7B%5Cleft(1%20+%20%5Cleft%5By'(x)%5Cright%5D%5E2%5Cright)%5E%7B5/2%7D%7D"></p>
</section>
<section id="derivatives-of-equations-expressed-in-parametric-t-coordinates" class="level4">
<h4 class="anchored" data-anchor-id="derivatives-of-equations-expressed-in-parametric-t-coordinates">Derivatives of equations expressed in parametric (t) coordinates</h4>
<p><img src="https://latex.codecogs.com/png.latex?x'(t)%20=%20%5Cfrac%7Bdx%7D%7Bdt%7D,%20%5Cquad%20y'(t)%20=%20%5Cfrac%7Bdy%7D%7Bdt%7D"></p>
<p><img src="https://latex.codecogs.com/png.latex?x''(t)%20=%20%5Cfrac%7Bd%5E2x%7D%7Bdt%5E2%7D,%20%5Cquad%20y''(t)%20=%20%5Cfrac%7Bd%5E2y%7D%7Bdt%5E2%7D"></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ckappa(t)%20=%20%5Cfrac%7B%5Cleft%7C%5C,x'(t)%5C,y''(t)%20-%20y'(t)%5C,x''(t)%5C,%5Cright%7C%7D%7B%5Cbig(x'(t)%5E2%20+%20y'(t)%5E2%5Cbig)%5E%7B3/2%7D%7D"></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%5Ckappa%7D%7Bdt%7D%20=%20%5Coperatorname%7Bsgn%7D%5C!%5Cbig(x'(t)%20y''(t)%20-%20y'(t)%20x''(t))%5Cbig)%5Ccdot"> <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B%5CBig(x'(t)%20y%5E%7B(3)%7D(t)%20-%20y'(t)%20x%5E%7B(3)%7D(t)%20+%20x''(t)%20y''(t)%20-%20y''(t)%20x''(t)%5CBig)%5C,(x'(t)%5E2%20+%20y'(t)%5E2)%5E%7B3/2%7D%20-%20%5Cbig(x'(t)%20y''(t)%20-%20y'(t)%20x''(t)%5Cbig)%5C,%5Ctfrac%7B3%7D%7B2%7D(x'(t)%5E2%20+%20y'(t)%5E2)%5E%7B1/2%7D%5C,(2x'(t)x''(t)%20+%202y'(t)y''(t))%7D%7B%5Cbig(x'(t)%5E2%20+%20y'(t)%5E2%5Cbig)%5E3%7D"></p>


</section>
</section>

 ]]></description>
  <guid>https://rworks.dev/posts/roc-and-differential-geometry/</guid>
  <pubDate>Fri, 12 Dec 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>October 2025 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/october-2025-top-40-new-cran-packages/</link>
  <description><![CDATA[ 





<div class="columns">
<div class="column" style="width:45%;">
<section id="data" class="level3">
<h3 class="anchored" data-anchor-id="data">Data</h3>
<p><a href="https://cran.r-project.org/package=AlgeriAPIs">AlgeriAPIs</a> v0.1.0: Provides functions to access data from public RESTful APIs, including <em>World Bank API</em> and <em>REST Countries API</em>, retrieving real-time or historical information related to Algeria. The package enables users to query economic indicators and international demographic and geopolitical statistics in a reproducible way. See the <a href="https://cran.r-project.org/web/packages/AlgeriAPIs/vignettes/AlgeriAPIs_vignette.html">vignette</a>.</p>
<p><a href="Algeria.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/Algeria.png" class="img-fluid" alt="% of Algerian population using internet compared to other regions"></a></p>
<p><a href="https://cran.r-project.org/package=CopernicusClimate">CopernicusClimate</a> v0.0.3: Provides functions to subset and download data from <a href="https://cds.climate.copernicus.eu/">EU Copernicus Climate Data Service</a>, including information about the Earth’s past, present, and future climate. See the vignettes <a href="https://cran.r-project.org/web/packages/CopernicusClimate/vignettes/download.html">Downloading from Copernicus</a> and <a href="https://cran.r-project.org/web/packages/CopernicusClimate/vignettes/translate.html">Translate API Code</a>.</p>
<p><a href="https://cran.r-project.org/package=FakeDataR">FakeDataR</a> v0.2.2: Provides functions to generate privacy-preserving synthetic datasets that mirror structure, types, factor levels, and missingness; export bundles for LLM workflows and build fake data directly from <code>SQL</code> database tables without reading real rows. See <a href="https://journal.r-project.org/articles/RJ-2016-019/index.html">Nowok et al.&nbsp;(2016)</a> for background methods and <a href="https://arxiv.org/abs/2108.07258">Bommasani et al.&nbsp;(2021)</a> for an overview of the foundation model. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/FakeDataR/vignettes/getting-started.html">Getting started</a> and <a href="https://cran.r-project.org/web/packages/FakeDataR/vignettes/privacy-and-validation.html">Privacy and validation</a>.</p>
<p><a href="https://cran.r-project.org/package=faunabr">faunabr</a> v1.0.0: Provides functions to retrieve, filter, and spatialize data from the <a href="https://fauna.jbrj.gov.br/fauna/listaBrasil/PrincipalUC/PrincipalUC.do;jsessionid=224496C2BF225A44F7B2B31C67858904">Catálogo Taxônomico da Faunado Brasil</a>. There are eight vignettes, including <a href="https://cran.r-project.org/web/packages/faunabr/vignettes/getting_started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/faunabr/vignettes/Spatialize_faunabr.html">Flag Erroneous Records</a>.</p>
<p><a href="faunabr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/faunabr.png" class="img-fluid" alt="Map showing true and false records."></a></p>
<p><a href="https://CRAN.R-project.org/package=ForCausality">ForCausality</a> v0.1.0: Provides a comprehensive set of datasets and tools for causal inference research that includes data from clinical trials, cancer studies, epidemiological surveys, environmental exposures, and health-related observational studies. The package is inspired by the foundational work of <a href="https://www.cambridge.org/core/books/causality/B0046844FAE10CBF274D4ACBDAEB5F5B">Pearl (2009)</a>. See the <a href="https://cran.r-project.org/web/packages/ForCausality/vignettes/ForCausality_vignette.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=healthmotionR">healthmotionR</a> v0.2.0: Provides a broad collection of datasets focused on health, biomechanics, and human motion, including clinical, physiological, and kinematic information from diverse sources, covering aspects such as surgery outcomes, vital signs, rheumatoid arthritis, osteoarthritis, accelerometry, gait analysis, motion sensing, and biomechanics experiments. See the <a href="https://cran.r-project.org/web/packages/healthmotionR/vignettes/healthmotionR_vignette.html">vignette</a>.</p>
<p><a href="healthmotionR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/healthmotionR.png" class="img-fluid" alt="Distribution of Body Temperature by Sex"></a></p>
<p><a href="https://cran.r-project.org/package=imfapi">imfapi</a> v0.1.2: Provides user-friendly functions for programmatic access to macroeconomic data from the International Monetary Fund’s <a href="https://data.imf.org/en/Resource-Pages/IMF-API">SDMX 3.0 IMF Data API</a>. See <a href="https://cran.r-project.org/web/packages/imfapi/readme/README.html">README</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=scf">scf</a> v1.0.5: Provides functions to analyze public use microdata from the <a href="https://www.federalreserve.gov/econres/scfindex.htm">Survey of Consumer Finances</a>, including tools to download prepared data files, construct replicate-weighted multiply imputed survey designs, compute descriptive statistics and model estimates, and produce plots and tables. See the <a href="https://cran.r-project.org/web/packages/scf/vignettes/SCF-guide.html">vignette</a>.</p>
<p><a href="scf.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/scf.png" class="img-fluid" alt="Plot of households by age"></a></p>
</section>
<section id="decision-analysis" class="level3">
<h3 class="anchored" data-anchor-id="decision-analysis">Decision Analysis</h3>
<p><a href="https://cran.r-project.org/package=andorR">andorR</a> v0.3.1: Implements a decision support tool to strategically prioritize evidence gathering in complex, hierarchical AND-OR decision trees. It is designed for situations with incomplete or uncertain information where the goal is to reach a confident conclusion as efficiently as possible (responding to the minimum number of questions, and only spending resources on generating improved evidence when it is of significant value to the final decision). There are five vignettes, including <a href="https://cran.r-project.org/web/packages/andorR/vignettes/andorR-intro.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/andorR/vignettes/example-data-files.html">Example Data Files</a>.</p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://cran.r-project.org/package=greenSD">greenSD</a> v0.1.1: Access and analyze multi-band greenspace seasonality data cubes (available for 1,028 major global cities), global Normalized Difference Vegetation Index / land cover data from the European Space Agency WorldCover 10m Dataset, and Sentinel-2-l2a images. The package also supports calculating human exposure to greenspace using a population-weighted greenspace exposure model introduced by <a href="https://www.nature.com/articles/s41467-022-32258-4">Chen et al.&nbsp;(2022)</a> based on Global Human Settlement Layer population data. See the vignette to <a href="https://cran.r-project.org/web/packages/greenSD/vignettes/get_data.html">get data</a>, and look <a href="https://billbillbilly.github.io/greenSD/">here</a> for additional information.</p>
<p><a href="greenspace.gif" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/greenspace.gif" class="img-fluid" alt="Population weighted greenspace exposure"></a></p>
<p><a href="https://cran.r-project.org/package=paisaje">paisaje</a> v0.1.1: Provides functions for landscape analysis and data retrieval, which allow users to download environmental variables from global datasets (e.g., WorldClim, ESA WorldCover, Nighttime Lights), and to compute spatial and landscape metrics using a hexagonal grid system based on the H3 spatial index. See <a href="https://rmets.onlinelibrary.wiley.com/doi/10.1002/joc.5086">Fick and Hijmans (2017)</a> and <a href="https://zenodo.org/records/7254221">Zanaga et al.&nbsp;(2022)</a> for background and the <a href="https://cran.r-project.org/web/packages/paisaje/vignettes/paisaje.html">vignette</a> for examples.</p>
</section>
<section id="econometrics" class="level3">
<h3 class="anchored" data-anchor-id="econometrics">Econometrics</h3>
<p><a href="https://cran.r-project.org/package=BayesianDisaggregation">BayesianDisaggregation</a> v0.1.2: Implements a novel Bayesian disaggregation framework that combines Principal Component Analysis (PCA) and Singular Value Decomposition (SVD) dimension reduction of prior weight matrices with deterministic Bayesian updating rules. The method provides Markov Chain Monte Carlo (MCMC) free posterior estimation with built-in diagnostic metrics. Read the vignette in <a href="https://cran.r-project.org/web/packages/BayesianDisaggregation/vignettes/USERMANUAL-ENG.html">English</a> or <a href="https://cran.r-project.org/web/packages/BayesianDisaggregation/vignettes/MANUALUSUARIO-ESP.html">Spanish</a>.</p>
<p><a href="https://cran.r-project.org/package=pvars">pvars</a> v1.1.1: Implements panel cointegration rank tests and estimators for panel vector autoregressive models, and identification methods for panel structural vector autoregressive models. Functions allow accounting for cross-sectional dependence and for structural breaks in the deterministic terms of the VAR processes. Particularly noteworthy are the correlation-augmented inverse normal test on the cointegration rank by <a href="https://www.sciencedirect.com/science/article/pii/S2452306220300484">Arsova and Oersal (2021)</a>, the two-step estimator for pooled cointegrating vectors by <a href="https://www.tandfonline.com/doi/abs/10.1081/ETC-200067895">Breitung (2005)</a>, and the pooled identification based on independent component analysis by <a href="https://onlinelibrary.wiley.com/doi/10.1002/jae.3044">Herwartz and Wang (2024)</a>. See the <a href="https://cran.r-project.org/web/packages/pvars/vignettes/pvars_vignette.pdf">vignette</a> for a detailed introduction to the package and underlying theory.</p>
<p><a href="pvars.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/pvars.png" class="img-fluid" alt="Scree plot showing share of variance"></a></p>
</section>
<section id="finance" class="level3">
<h3 class="anchored" data-anchor-id="finance">Finance</h3>
<p><a href="https://cran.r-project.org/package=amsSim">amsSim</a> v0.1.0: Implements simulation and pricing routines for rare-event options using adaptive multilevel splitting and standard Monte Carlo under Black-Scholes and Heston models. Core routines are implemented in <code>C++</code> via <code>Rcpp</code> and <code>RcppArmadillo</code> with lightweight <code>R</code> wrappers. Look <a href="https://arxiv.org/html/2510.23461v1">here</a> for the theory and see the <a href="https://cran.r-project.org/web/packages/amsSim/readme/README.html">README</a> to get started.</p>
<p><a href="AMS.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/AMS.png" class="img-fluid" alt="Plot illustrating first 2 iterations of AMS algorithm"></a></p>
</section>
<section id="genomics" class="level3">
<h3 class="anchored" data-anchor-id="genomics">Genomics</h3>
<p><a href="https://cran.r-project.org/package=BTIME">BTIME</a> v1.0.0: Implements Bayesian Hierarchical beta-binomial models for modeling cell population to predictors/exposures. This package utilizes <code>runjags</code> to run Gibbs sampling with parallel chains. Options allow for different covariances/relationship structures among parameters of interest. There is an <a href="https://cran.r-project.org/web/packages/BTIME/vignettes/BICAM.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/BTIME/vignettes/BICAM_Cov.html">Covariance Structures</a>.</p>
<p><a href="BTIME.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/BTIME.png" class="img-fluid" alt="T cell hierarchy"></a></p>
</section>
<section id="logic" class="level3">
<h3 class="anchored" data-anchor-id="logic">Logic</h3>
<p><a href="https://cran.r-project.org/package=Pinference">Pinference</a> v0.2.5: Implements T. Hailperin’s procedure for calculating lower and upper probability bounds for a propositional-logic expression, given equality and inequality constraints on the probabilities for other expressions. Truth-valuation is included as a special case. Applications range from decision-making and probabilistic reasoning, to pedagogical for probability and logic courses. See <a href="https://www.tandfonline.com/doi/abs/10.1080/00029890.1965.11970533">Hailperin (1965)</a> background on logic and the <a href="https://cran.r-project.org/web/packages/Pinference/vignettes/inferP.html">vignette</a> for an analysis of the <strong>Monty Hall Problem</strong> and more.</p>
</section>
<section id="machine-learning" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning">Machine Learning</h3>
<p><a href="https://cran.r-project.org/package=bigPCAcpp">bigPCAcpp</a> v0.9.0: Implements high performance principal component analysis routines that operate directly on <code>bigmemory::big.matrix</code> objects. Functions avoid materializing large matrices in memory by streaming data through <code>BLAS</code> and <code>LAPACK</code> kernels and include helpers to derive scores, loadings, correlations, and diagnostics, and include utilities to stream results into <code>bigmemory</code> matrices for file-based workflows. Also implemented is the Scalable principal component analysis of <a href="https://dl.acm.org/doi/10.1145/2723372.2751520">Elgamal et al.&nbsp;(2015)</a>. There is an <a href="https://cran.r-project.org/web/packages/bigPCAcpp/vignettes/bigPCAcpp.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/bigPCAcpp/vignettes/bigPCA-benchmarks.html">Benchmarking</a>.</p>
<p><a href="bigPCAcpp.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/bigPCAcpp.png" class="img-fluid" alt="Plot showing benchmark times"></a></p>
<p><a href="https://cran.r-project.org/package=FuzzySpec">FuzzySpec</a> v1.0.0: Implements FVIBES, the Fuzzy Variable-Importance Based Eigenspace Separation algorithm. See the <a href="https://cran.r-project.org/web/packages/FuzzySpec/vignettes/FuzzySpec.html">vignette</a>.</p>
<p><a href="FuzzySpec.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/FuzzySpec.png" class="img-fluid" alt="Plot showing fuzzy spectral clustering of spiral data"></a></p>
<p><a href="https://cran.r-project.org/package=roclab">roclab</a> v0.1.4: Implements ROC (Receiver Operating Characteristic)–Optimizing Binary Classifiers, supporting both linear and kernel models. Scalability for large datasets is achieved through approximation-based options, which accelerate training and make fitting feasible on large data. Utilities are provided for model training, prediction, and cross-validation. See <a href="https://dl.acm.org/doi/10.1145/1046456.1046489">Hernàndez-Orallo et al.&nbsp;(2004)</a> background and the <a href="https://cran.r-project.org/web/packages/roclab/vignettes/roclab-intro.html">vignette</a> for examples.</p>
<p><a href="https://cran.r-project.org/package=rSDR">rSDR</a> v1.0.3.0: Implements a novel, sufficient dimension reduction method that is robust against outliers using alpha-distance covariance and manifold-learning. See <a href="https://www.tandfonline.com/doi/full/10.1080/10485252.2024.2313137">Huang et al.(2024)</a> for details and the <a href="https://cran.r-project.org/web/packages/rSDR/vignettes/rSDR_vignette.html">vignette</a> for examples.</p>
<p><a href="rSDR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/rSDR.png" class="img-fluid" alt="3D projectio plot"></a></p>
</section>
<section id="mathematics" class="level3">
<h3 class="anchored" data-anchor-id="mathematics">Mathematics</h3>
<p><a href="https://cran.r-project.org/package=SimplicialComplex">SimplicialComplex</a> v0.1.0: Implements simplicial complexes for Topological Data Analysis (TDA) and includes functions to compute faces, boundary operators, Betti numbers, and Euler characteristics. It also provides tools for studying persistent homology with the aim of helping readers understand the core concepts of computational topology. <a href="https://link.springer.com/article/10.1007/s00454-004-1146-y">Zomorodian and Carlsson (2005)</a> and <a href="https://www.frontiersin.org/journals/artificial-intelligence/articles/10.3389/frai.2021.667963/full">Chazal and Michel (2021)</a> for background and look <a href="https://github.com/TDA-R/SimplicialComplex">here</a> to access the <code>Shiny</code> App <a href="https://tf3q5u-0-0.shinyapps.io/simplicialcomplex/">playground</a>, which allows exploring the concepts underlying TDA.</p>
<p><a href="Simp.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/Simp.png" class="img-fluid" alt="Simple simplicial complex"></a></p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="medical-statistics" class="level3">
<h3 class="anchored" data-anchor-id="medical-statistics">Medical Statistics</h3>
<p><a href="https://cran.r-project.org/package=PERSUADE">PERSUADE</a> v0.1.2: Provides a standardized framework to support the selection and evaluation of parametric survival models for time-to-event data. Includes tools for visualizing survival data, checking proportional hazards assumptions (<a href="https://academic.oup.com/biomet/article-abstract/81/3/515/257037?redirectedFrom=fulltext&amp;login=false">Grambsch and Therneau (1994)</a>), comparing parametric (<a href="https://link.springer.com/article/10.1007/s40273-013-0064-3">Ishak et al.&nbsp;(2013)</a>), spline (<a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.1203">Royston and Parmar (2002)</a>) and cure models, examining hazard functions, and evaluating model extrapolation. Methods are consistent with recommendations in the NICE Decision Support Unit Technical Support <a href="https://sheffield.ac.uk/nice-dsu/tsds/survival-analysis">Documents 14 and 21</a>. See <a href="https://cran.r-project.org/web/packages/PERSUADE/readme/README.html">README</a> to get started.</p>
<p><a href="PERSUADE.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/PERSUADE.png" class="img-fluid" alt="Workflow diagram"></a></p>
<p><a href="https://cran.r-project.org/package=shinymrp">shinymrp</a> v0.9.1: Provides a dual interface, graphical and programmatic for multilevel regression and post stratification applications, offering tools for data cleaning, exploratory analysis, model building, and visualization. Users can apply the method to a variety of datasets including electronic health records and sample survey data. See <a href="https://projecteuclid.org/journals/statistical-science/volume-40/issue-2/On-the-Use-of-Auxiliary-Variables-in-Multilevel-Regression-and/10.1214/24-STS932.short">Si (2025)</a> for background. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/shinymrp/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/shinymrp/vignettes/workflow.html">Programmatic workflow demonstration</a>.</p>
<p><a href="shinymrp.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/shinymrp.png" class="img-fluid" alt="Model Comparison Plot"></a></p>
</section>
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=choicedata">choicedata</a> v0.1.0: Offers a set of objects tailored to simplify working with choice data. It enables the computation of choice probabilities and the likelihood of various types of choice models based on given data. Look <a href="https://loelschlaeger.de/choicedata/">here</a> for a detailed introduction.</p>
<p><a href="choicedata.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/choicedata.png" class="img-fluid" alt="Workflow diagram"></a></p>
<p><a href="https://cran.r-project.org/package=GPpenalty">GPpenalty</a> v1.0.0: Implements maximum likelihood estimation for Gaussian processes, supporting both isotropic and separable models with predictive capabilities. Includes penalized likelihood estimation following <a href="https://www.tandfonline.com/doi/abs/10.1198/004017004000000671">Li and Sudjianto (2005)</a>. Functions use decorrelated prediction error metrics to account for uncertainty, and cross validation techniques for tuning parameter selection. Designed specifically for small datasets. See <a href="https://cran.r-project.org/web/packages/GPpenalty/readme/README.html">README</a> for an example.</p>
<p><a href="GPpenalty.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/GPpenalty.png" class="img-fluid" alt="Plot comparing target function with estimation"></a></p>
<p><a href="https://cran.r-project.org/package=mda.biber">mda.biber</a> v1.0.1: Implements the factor analysis developed in <a href="https://link.springer.com/article/10.1007/BF00136979">Biber (1992)</a> most commonly used to describe language as it varies by genre, register, and use. Functions describe and plot MDA results, including dimension scores, dimension means, and factor loadings. See the <a href="https://cran.r-project.org/web/packages/mda.biber/vignettes/introduction.html">vignette</a> for an introduction.</p>
<p><a href="mda.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/mda.png" class="img-fluid" alt="Boxplots of contributing variables"></a></p>
<p><a href="https://cran.r-project.org/package=PanelSelect">PanelSelect</a> v1.0.0: Extends the Heckman selection framework to panel data with individual random effects. The first stage models participation via a panel Probit specification, while the second stage can take a panel linear, Probit, Poisson, or Poisson log-normal form. Model details are provided in <a href="https://papers.ssrn.com/sol3/papers.cfm?abstract_id=5475626">Bailey and Peng (2025)</a> and <a href="https://pubsonline.informs.org/doi/10.1287/mnsc.2019.01897">Peng and Van den Bulte (2024)</a>. See the <a href="https://cran.r-project.org/web/packages/PanelSelect/vignettes/vignette.html">vignette</a> for an introduction.</p>
<p><a href="https://cran.r-project.org/package=partialling.out">partialling.out</a> v0.2.0: Creates a data frame with the residuals of partial regressions of the main explanatory variable and other variables of interest. This method follows the Frisch-Waugh-Lovell theorem, as explained in <a href="https://www.tandfonline.com/doi/abs/10.3200/JECE.39.1.88-91">Lovell (2008)</a>. See the <a href="https://cran.r-project.org/web/packages/partialling.out/vignettes/partialling_out.html">vignette</a>.</p>
<p><a href="partial.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/partial.png" class="img-fluid" alt="Residual Plot"></a></p>
<p><a href="https://cran.r-project.org/package=projoint">projoint</a> v1.0.6: Provides tools for analyzing data generated from conjoint survey experiments, including functions to estimate marginal means and average marginal component effects, with corrections for measurement error and methods for profile-level and choice-level estimators, bias correction using intra-respondent reliability, and visualization utilities. For details on the methodology, see <a href="https://gking.harvard.edu/conjointE%3E">Clayton et al.&nbsp;(2025)</a>. There are seven vignettes including <a href="https://cran.r-project.org/web/packages/projoint/vignettes/analyze.html">Analyze and Visualize Important QOIs</a> and <a href="https://cran.r-project.org/web/packages/projoint/vignettes/explore.html">Explore and Compare Further</a>.</p>
<p><a href="projoint.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/projoint.png" class="img-fluid" alt="Plot of mean preferences"></a></p>
<p><a href="https://cran.r-project.org/package=RegCalReliab">RegCalReliab</a> v0.2.0: Implements regression calibration methods for correcting measurement error in regression models using external or internal reliability studies. Methods are described in <a href="https://www.taylorfrancis.com/books/mono/10.1201/9781420010138/measurement-error-nonlinear-models-ciprian-crainiceanu-raymond-carroll-leonard-stefanski-david-ruppert">Carroll et al.&nbsp;(2006)</a>. See the <a href="https://cran.r-project.org/web/packages/RegCalReliab/vignettes/regcal_example.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=RTMBdist">RTMBdist</a> v0.1.0: Extends the functionality of the <a href="https://kaskr.r-universe.dev/RTMB"><code>RTMB</code></a> package by providing a collection of non-standard probability distributions compatible with automatic differentiation. Automatic differentiation and Laplace approximation are described in <a href="https://www.jstatsoft.org/article/view/v070i05">Kristensen et al.&nbsp;(2016)</a>. See the vignettes, <a href="https://cran.r-project.org/web/packages/RTMBdist/vignettes/Examples.html">Examples</a> and <a href="https://cran.r-project.org/web/packages/RTMBdist/vignettes/distlist.html">distlist</a>.</p>
</section>
<section id="time-series" class="level3">
<h3 class="anchored" data-anchor-id="time-series">Time Series</h3>
<p><a href="https://cran.r-project.org/package=conformalForecast">conformalForecast</a> v0.1.0: Provides methods and tools for performing multistep-ahead time series forecasting using conformal prediction methods, including classical conformal prediction, adaptive conformal prediction, conformal PID (Proportional-Integral-Derivative) control, and autocorrelated multistep-ahead conformal prediction. The methods were described by <a href="https://arxiv.org/abs/2410.13115">Wang and Hyndman (2024)</a>. See the <a href="https://cran.r-project.org/web/packages/conformalForecast/vignettes/conformalForecast.html">vignette</a> for examples.</p>
<p><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/conformalF.png" class="img-fluid" alt="Time series plots for various methods and parameter settings"> <a href="https://cran.r-project.org/package=funbootband">funbootband</a> v0.2.0: Provides methods to compute simultaneous prediction and confidence bands for dense time series data. The implementation builds on the functional bootstrap approach proposed by <a href="https://www.sciencedirect.com/science/article/abs/pii/S0966636298000435">Lenhoff et al.&nbsp;(1999)</a> and extended by <a href="https://www.sciencedirect.com/science/article/abs/pii/S0021929023000751">Koska et al.&nbsp;(2023)</a> to support both independent and clustered (hierarchical) data. See the <a href="https://cran.r-project.org/web/packages/funbootband/vignettes/funbootband-intro.html">vignette</a>.</p>
<p><a href="funboot.png" class="lightbox" data-gallery="quarto-lightbox-gallery-20"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/funboot.png" class="img-fluid" alt="Time series with multiple confidence bands"></a></p>
<p><a href="https://cran.r-project.org/package=kardl">kardl</a> v0.1.1: Implements estimation procedures for Autoregressive Distributed Lag (ARDL) and Nonlinear ARDL (NARDL) models, which allow researchers to investigate both short and long-run relationships in time series data under mixed orders of integration. The package includes several cointegration testing approaches, such as the <a href="https://www.sciencedirect.com/science/article/abs/pii/S0304407601000495">Pesaran et al.&nbsp;(2001)</a> F and t bounds tests, the Banerjee error correction test, and the restricted ECM test, together with diagnostic tools, including Wald tests for asymmetry, ARCH tests, and stability procedures. See <a href="https://cran.r-project.org/web/packages/kardl/readme/README.html">README</a> to get started.</p>
<p><a href="kardl.png" class="lightbox" data-gallery="quarto-lightbox-gallery-21"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/kardl.png" class="img-fluid" alt="Plot showing lag criteria comparison"></a></p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=bakerrr">bakerrr</a> v0.2.0: Provides functions to launch, track, and control background-parallel jobs and includes utilities for job status, error handling, resource monitoring, and result collection. Designed for scalable workflows in interactive and automated settings (local or remote). Look <a href="https://anirbanshaw24.github.io/bakerrr/">here</a> for more information. There are four vignettes, including <a href="https://cran.r-project.org/web/packages/bakerrr/vignettes/log_file.html">Logging to File</a> and <a href="https://cran.r-project.org/web/packages/bakerrr/vignettes/multi_function.html">Orchestrating Multiple Functions in Parallel and in Background</a>.</p>
<p><a href="https://cran.r-project.org/package=localLLM">localLLM</a> v1.0.1: Provides <code>R</code> bindings to the <code>llama.cpp</code> library for running large language models. The package uses a lightweight architecture where the <code>C++</code> backend library is downloaded at runtime rather than bundled with the package. Package features include text generation, reproducible generation, and parallel inference. Look <a href="https://github.com/EddieYang211/localLLM">here</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=rixpress">rixpress</a> v0.10.1: Provides functions to streamline the creation of reproducible analytical pipelines using <code>default.nix</code> expressions generated via the <code>rix</code> package for reproducibility. Define derivations in <code>R</code>, <code>Python</code> or <code>Julia</code>, chain them into a composition of pure functions, and build the resulting pipeline using <code>Nix</code> as the underlying end-to-end build tool. Functions to plot the pipeline as a directed acyclic graph are included, as well as functions to load and inspect intermediary results for interactive analysis. There are twelve vignettes, including <a href="https://cran.r-project.org/web/packages/rixpress/vignettes/intro-concepts.html">introductory concepts</a> and <a href="https://cran.r-project.org/web/packages/rixpress/vignettes/core-functions.html">core functions</a>.</p>
<iframe width="400" height="250" src="https://www.youtube.com/embed/a1eNG9TFZ_o" title="Nix for Data Science: a 2-min intro to rixpress, a package for multilanguage reproducible pipelines" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="">
</iframe>
<p><a href="https://cran.r-project.org/package=summarytabl">summarytabl</a> v0.2.1: Provides functions to tabulate and summarize categorical, multiple response, ordinal, and continuous variables in <code>R</code> data frames, making it easy to create clear, structured summary tables. See the <a href="https://cran.r-project.org/web/packages/summarytabl/vignettes/summarytabl-intro.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=tabler">tabler</a> v0.1.0: Provides functions to build interactive dashboards combining the <code>Tabler UI Kit</code> with <code>Shiny</code>, making it easy to create professional-looking web applications. Dashboards are fully responsive and compatible with all modern browsers. Offers customizable layouts and components built with <code>HTML5</code> and <code>CSS3</code>. See <a href="https://cran.r-project.org/web/packages/tabler/readme/README.html">README</a> to get started.</p>
<iframe width="400" height="250" src="https://www.youtube.com/embed/_PWVmmis-AE" title="Tabler short demo using Shiny" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="">
</iframe>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=graphonmix">graphonmix</a> v0.0.1.0: Generates (U,W) mixture graphs where U is a line graph graphon and W is a dense graphon. Graphons are graph limits and graphon U can be written as a sequence of positive numbers adding to 1. Graphs are sampled from U and W and joined randomly to obtain the mixture graph. Given a mixture graph, U can be inferred. See <a href="https://arxiv.org/abs/2505.13864">Kandanaarachchi and Ong (2025)</a> for background and the vignettes <a href="https://cran.r-project.org/web/packages/graphonmix/vignettes/graphonmix.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/graphonmix/vignettes/linegraphons.html">Sparse graphs from line graphons</a>.</p>
<p><a href="graphon.png" class="lightbox" data-gallery="quarto-lightbox-gallery-22"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/graphon.png" class="img-fluid" alt="Line graphon U"></a></p>
<p><a href="https://cran.r-project.org/package=SimpleUpset">SimpleUpset</a> v0.1.3: Provides functions to create Upset plots using a combination of <code>ggplot2</code> and <code>patchwork</code>. See <a href="https://ieeexplore.ieee.org/document/6876017">Lex et al.&nbsp;(2014)</a> for background and the <a href="https://cran.r-project.org/web/packages/SimpleUpset/vignettes/introduction.html">vignette</a> for examples.</p>
<p><a href="SimpleU.png" class="lightbox" data-gallery="quarto-lightbox-gallery-23"><img src="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/SimpleU.png" class="img-fluid" alt="Example of an Upset Plot"></a></p>
</section>
</div>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/october-2025-top-40-new-cran-packages/</guid>
  <pubDate>Fri, 28 Nov 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/october-2025-top-40-new-cran-packages/greenspace.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>ROC Curves in Two Lines of Code</title>
  <dc:creator>Robert Horton</dc:creator>
  <link>https://rworks.dev/posts/HortonROCpost/</link>
  <description><![CDATA[ 





<p><em>Editor’s Note: This post first appeared on the Revolution Analytics/Microsoft Blog, “Revolutions” on August 2, 2016. As the Revolutions blog no longer exists, we are republishing it here with permission from the author.</em></p>
<p><a href="https://en.wikipedia.org/wiki/Receiver_operating_characteristic">ROC curves</a> are commonly used to characterize the sensitivity/specificity tradeoffs for a binary classifier. Most machine learning classifiers produce real-valued scores that correspond with the strength of the prediction that a given case is positive. Turning these real-valued scores into yes or no predictions requires setting a threshold; cases with scores above the threshold are classified as positive, and cases with scores below the threshold are predicted to be negative. Different threshold values give different levels of sensitivity and specificity. A high threshold is more conservative about labeling a case as positive; this makes it less likely to produce false positive results but more likely to miss cases that are in fact positive (lower rate of true positives). A low threshold produces positive labels more liberally, so it is less specific (more false positives) but also more sensitive (more true positives). The ROC curve plots true positive rate against false positive rate, giving a picture of the whole spectrum of such tradeoffs.</p>
<p>There are commonly used packages to plot these curves and to compute metrics from them, but it can still be worthwhile to contemplate how these curves are calculated to try to understand better what they show us. Here I present a simple function to compute an ROC curve from a set of outcomes and associated scores.</p>
<p>The calculation has two steps:</p>
<ol type="1">
<li>Sort the observed outcomes by their predicted scores with the highest scores first.</li>
<li>Calculate cumulative True Positive Rate (TPR) and True Negative Rate (TNR) for the ordered observed outcomes.</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">simple_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(labels, scores){</span>
<span id="cb1-2">  labels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> labels[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(scores, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decreasing=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)]</span>
<span id="cb1-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TPR=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(labels)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(labels), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FPR=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>labels)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>labels), labels)</span>
<span id="cb1-4">}</span></code></pre></div></div>
</div>
<p>The function takes two inputs: <code>labels</code> is a Boolean vector with the actual classification of each case, and <code>scores</code> is a vector of real-valued prediction scores assigned by some classifier.</p>
<p>Since this is a binary outcome, the labels vector is a series of TRUE and FALSE values (or ones and zeros if you prefer). You can think of this series of binary values as a sequence of instructions for <a href="https://en.wikipedia.org/wiki/Logo_(programming_language)">turtle graphics</a>, only in this case the turtle has a compass and takes instructions in terms of absolute plot directions (North or East) instead of relative left or right. The turtle starts at the origin (as turtles do) and it traces a path across the page dictated by the sequence of instructions. When it sees a one (TRUE) it takes a step Northward (in the positive y direction); when it sees a zero (FALSE) it takes a step to the East (the positive x direction). The sizes of the steps along each axis are scaled so that once the turtle has seen all of the ones it will be at 1.0 on the y-axis, and once it has seen all of the zeros it will be at 1.0 on the x-axis. The path across the page is determined by the order of the ones and zeros, and it always finishes in the upper right corner.</p>
<p>The progress of the turtle along the bits of the instruction string represents adjusting the classification threshold to be less and less stringent. Once the turtle has passed a bit, it has decided to classify that bit as positive. If the bit was in fact positive it is a true positive; otherwise it is a false positive. The y-axis shows the true positive rate (TPR), which is the number of true positives encountered so far divided by the total number of actual positives. The x-axis shows the false positive rate (the number of false positives encountered up to that point divided by total number of actual negatives). The vectorized implementation of this logic uses cumulative sums (the cumsum function) instead of walking through the values one at a time, though that is what the computer is doing at a lower level.</p>
<p>An ROC “curve” computed in this way is actually a step function. If you had very large numbers of positive and negative cases, these steps would be very small and the curve would appear smooth. (If you actually want to plot ROC curves for large numbers of cases, it could be problematic to plot every point; this one reason that production-grade ROC functions take more than two lines of code.)</p>
<p>As an example, we will simulate data about widgets. Here we have an input feature x that is linearly related to a latent outcome y which also includes some randomness. The value of y determines whether the widget exceeds tolerance requirements; if it does, it is a bad widget.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-2">sim_widget_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">noise=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>){</span>
<span id="cb2-3">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb2-4">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">122</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>noise)</span>
<span id="cb2-5">  bad_widget <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(x, y, bad_widget)</span>
<span id="cb2-7">}</span>
<span id="cb2-8">widget_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sim_widget_data</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-9"></span>
<span id="cb2-10">test_set_idx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(widget_data), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(widget_data)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb2-11"></span>
<span id="cb2-12">test_set <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> widget_data[test_set_idx,]</span>
<span id="cb2-13">training_set <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> widget_data[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>test_set_idx,]</span>
<span id="cb2-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb2-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-16">test_set <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>bad_widget)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bad widgets related to x"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-2-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/HortonROCpost/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<p>We reserve about 1/4 of cases for test set, and we’ll use the rest for training a predictive model. The plot shows the test set, since that is the data we’ll use to generate the ROC curves. If x is below about 20 all the points are red, and if it is above about 80 they are all black. In between is a region of varying uncertainty with more red at one end and more black at the other.</p>
<p>We use the training set to fit a logistic regression model using the x feature to predict whether a given widget is likely to be bad. This model will be used to generate scores for the test set, which will be used together with the actual labels of the test cases to calculate ROC curves. The values from the scores vector will not appear in the plot; they are only used to sort the labels. Two classifiers that put the labels in the same order will have exactly the same ROC curve regardless of the absolute values of the scores. This is shown by comparing the ROC curve you get using either the ‘response’ or the ‘link’ predictions from a logistic regression model. The ‘response’ scores have been mapped into the range between 0 and 1 by a sigmoid function and the ‘link’ scores have not. But either of these scores will put the points in the same order.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">fit_glm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(bad_widget <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, training_set, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">binomial</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>))</span>
<span id="cb3-2"></span>
<span id="cb3-3">glm_link_scores <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(fit_glm, test_set, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"link"</span>)</span>
<span id="cb3-4"></span>
<span id="cb3-5">glm_response_scores <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(fit_glm, test_set, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>)</span>
<span id="cb3-6"></span>
<span id="cb3-7">score_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link=</span>glm_link_scores, </span>
<span id="cb3-8">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">response=</span>glm_response_scores,</span>
<span id="cb3-9">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bad_widget=</span>test_set<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bad_widget,</span>
<span id="cb3-10">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb3-11"></span>
<span id="cb3-12">score_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>link, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>response, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>bad_widget)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_rug</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Both link and response scores put cases in the same order"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-3-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/HortonROCpost/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<p>Here the ROC curve for the response scores from the logistic regression model is calculated with the widely used <code>pROC</code> package and plotted as a yellow line. The <code>simple_roc</code> function was also used to calculate an ROC curve, but in this case it is calculated from the link scores. Since both sets of scores put the labels in the same order, and since both functions are doing essentially the same thing, we get the same curve. The points of the simple_roc curve are plotted as open circles, which land exactly on top of the yellow line. Each point represents a single case in the test set, and the outline colors of the circles show whether that case was a “bad widget” (red) or not (black). Red circles tell the turtle to go North, and black circles tell it to go East.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(pROC)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc</span>(test_set<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bad_widget, glm_response_scores, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;"</span>),</span>
<span id="cb4-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"yellow"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">asp=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The turtle finds its way"</span>)</span>
<span id="cb4-4"></span>
<span id="cb4-5">glm_simple_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simple_roc</span>(test_set<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bad_widget<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TRUE"</span>, glm_link_scores)</span>
<span id="cb4-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(glm_simple_roc, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> FPR, TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> labels))</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/roc_curve-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/HortonROCpost/index_files/figure-html/roc_curve-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<p>Note that the <code>pROC</code> package labels the x-axis “Specificity” with 1.0 on the left and 0 on the right (specificity is 1 minus the false positive rate). This means we had to do a similar subtraction to plot the simple_roc results the same way. Also, left to its own devices, roc decides how to label the cases and controls based on which group has the higher median score, which means it can flip a negative AUC to be positive (basically, a consistently wrong predictor can still be useful if you use reverse psychology). I specify direction=“&lt;” to prevent this since the simple_roc function is not that smart.</p>
<p>This brings up another limitation of this simple approach; by assuming that the rank order of the outcomes embodies predictive information from the model, it does not properly handle sequences of cases that all have the same score. The turtle assumes that the order of the labels has meaning, but in the situation of identical scores there is no meaningful order. These segments should properly be represented by a diagonal line, while our simple turtle will happily plot meaningless steps.</p>
<p>We’ll show an extreme case by creating an unbalanced dataset that is positive in only about 1% of cases. For prediction, we just always guess that the result will be negative (achieving 99% accuracy). Since all the scores are the same, we don’t really have any basis for sorting the outcomes; <code>pROC</code> handles this correctly and draws a diagonal line. The turtle assumes that the order of the cases means something when in fact it does not, and it takes a sort of random walk up to the top right corner.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-2">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span></span>
<span id="cb5-3">P <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb5-4">rare_success <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>), N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(P, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>P))</span>
<span id="cb5-5">guess_not <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, N)</span>
<span id="cb5-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">roc</span>(rare_success, guess_not), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">print.auc=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-7"></span>
<span id="cb5-8">simp_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simple_roc</span>(rare_success, guess_not)</span>
<span id="cb5-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(simp_roc, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> FPR, TPR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/wandering_along_diagonal-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/HortonROCpost/index_files/figure-html/wandering_along_diagonal-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">simp_roc2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simple_roc</span>(rare_success, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(guess_not)))</span></code></pre></div></div>
</details>
</div>
<p>If you repeat this simulation with larger value of N you will see that the turtle’s path tends to approximate the diagonal more closely, but the more unbalanced the outcomes are, the larger numbers of total cases you will likely need to keep the paths from deviating wildly from the diagonal.</p>
<p>For a less extreme example, you can usually generate diagonal segments in an ordinary ROC curve by rounding scores so that multiple points get identical ranks; this is left as an exercise for the reader.</p>
<p>Because ROC curves are so instructive and commonly used, they deserve some study and contemplation. For further information I recommend this <a href="https://kennis-research.shinyapps.io/ROC-Curves/">shiny app</a> showing continuous-valued ROC curves computed from probability distributions, and the excellent paper by Tom Fawcett entitled <a href="https://ccrma.stanford.edu/workshops/mir2009/references/ROCintro.pdf">An introduction to ROC analysis</a>.</p>
<p><em>Bob Horton started his career as a molecular biologist, studying genes involved in immune responses (MHC evolution and TCR repertoire), and developing genetic engineering techniques. Analyzing and simulating biological data led him to data science and his current interests, which include semantic searching of text data and decision modeling.</em></p>



 ]]></description>
  <category>ROC Curves</category>
  <guid>https://rworks.dev/posts/HortonROCpost/</guid>
  <pubDate>Wed, 12 Nov 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/HortonROCpost/turtle.png" medium="image" type="image/png" height="146" width="144"/>
</item>
<item>
  <title>An Introduction to Writing Your Own ggplot2 Geoms</title>
  <dc:creator>Isabella Velásquez</dc:creator>
  <link>https://rworks.dev/posts/ggplot2-extensions/</link>
  <description><![CDATA[ 
      <!-- user-provided yaml closeread style options -->
      <style>
        :root {
          --cr-narrative-background-color-sidebar: transparent;
--cr-section-background-color: transparent;
--cr-narrative-text-color-overlay: black;
--cr-narrative-background-color-overlay: transparent;
        }
      </style>
    





<p>If you use ggplot2, you are probably used to creating plots with <code>geom_line()</code> and <code>geom_point()</code>. You may also have ventured into to the broader ggplot2 ecosystem to use geoms like <code>geom_density_ridges()</code> from <a href="https://wilkelab.org/ggridges/">ggridges</a> or <code>geom_signif()</code> from <a href="https://const-ae.github.io/ggsignif/">ggsignif</a>. But have you ever wondered <em>how</em> these extensions were created? Where did the authors figure out how to create a new geom? And, if the plot of your dreams doesn’t exist, how would you make your own?</p>
<p>Enter the exciting world of creating your own ggplot2 extensions.</p>
<p>I had the pleasure of meeting <a href="https://github.com/EvaMaeRey">Gina Reynolds</a> when I first began my job at Posit (then RStudio) and she was contributing a blog post on <a href="https://evamaerey.github.io/ggplot_flipbook/ggplot_flipbook_xaringan.html#1">flipbookr</a>. Since then, we’ve kept in touch through the <a href="https://ggplot2-extenders.github.io/ggplot-extension-club/">ggextenders</a> extension club. Every few months, the club meets virtually to hear from a ggextender (someone who works with ggplot2 extensions). The speaker can talk about a custom geom they’ve created for the community or more general R visualization topics. Each presentation is insightful and interesting. I’ve had the opportunity to learn about cool packages like <a href="https://larmarange.github.io/ggstats/">ggstats</a>. <a href="https://docs.google.com/forms/d/e/1FAIpQLSe3M1KwUPrmTfEGuuQp0fZ0J7dZkk_82gb310JCvdouMTa_7Q/viewform">Join us sometime by filling out this questionnaire</a>!</p>
<p>However, I was never a “ggextender” myself (having just used and never developed extenders). I found the idea of creating an extension daunting. That is, until recently!</p>
<p>Gina held a focus group that worked through the <a href="https://evamaerey.github.io/easy-geom-recipes/">Easy geom recipes</a>, a series of tutorials on creating ggplot2 extensions. Following “recipes”, you methodically create three extensions. Each time, certain key knowledge points are reinforced and new variations are introduced.</p>
<p>So, say we want to create a new <code>geom_*()</code> that adds a point on the median of the x-axis and y-axis variables of a plot. We will call it <code>geom_medians()</code>. Let’s follow the recipe:</p>
<div class="cr-section cr-column-screen sidebar-left">
<div class="narrative-col">
<section id="step-0.-get-the-job-done-with-base-ggplot2." class="level2 narrative trigger">
<h2 class="narrative anchored" data-anchor-id="step-0.-get-the-job-done-with-base-ggplot2.">Step 0. Get the job done with ‘base’ ggplot2.</h2>
</section>
<div class="trigger new-trigger" data-highlight="1,2" data-focus-on="cr-step-0a">
<div class="narrative">
<p>First, clarify what needs to happen without getting into the extension architecture. Load the tidyverse package and the palmerpenguins package.</p>
</div>
</div>
<div class="trigger new-trigger" data-highlight="5,6,7" data-focus-on="cr-step-0a">
<div class="narrative">
<p>Calculate the median of the x variable (<code>bill_length_mm</code>) and y variable (<code>bill_depth_mm</code>) as you would normally.</p>
</div>
</div>
<div class="trigger new-trigger" data-focus-on="cr-step02">
<div class="narrative">
<p>Now, use base ggplot2 to get the job done.</p>
</div>
</div>
<div class="trigger new-trigger" data-highlight="7,8" data-focus-on="cr-step-0b">
<div class="narrative">
<p>This requires specifying <code>bill_depth_mm_median</code> and <code>bill_length_mm_median</code>, which we just created, within <code>aes()</code> in <code>geom_point()</code>.</p>
</div>
</div>
<div class="trigger new-trigger" data-focus-on="cr-step-0c">
<div class="narrative">
<p>This is the resulting plot.</p>
</div>
</div>
<section id="step-1-define-compute-and-test." class="level2 narrative trigger">
<h2 class="narrative anchored" data-anchor-id="step-1-define-compute-and-test.">Step 1: Define Compute and test.</h2>
</section>
<div class="trigger new-trigger" data-focus-on="cr-step-1a">
<div class="narrative">
<p>Define the compute that will transform your input data “under the hood” before rendering it.</p>
</div>
</div>
<div class="trigger new-trigger" data-focus-on="cr-step-1b">
<div class="narrative">
<p>Next, test the compute to make sure that the output matches what you expect. Note that the names <code>x</code> and <code>y</code> are required.</p>
</div>
</div>
<section id="step-2-define-new-stat.-test." class="level2 narrative trigger">
<h2 class="narrative anchored" data-anchor-id="step-2-define-new-stat.-test.">Step 2: Define new Stat. Test.</h2>
</section>
<div class="trigger new-trigger" data-focus-on="cr-step-2a">
<div class="narrative">
<p>Next, use the <code>ggplot2::ggproto()</code> function, which creates a new Stat function that does computation under the hood when building a plot. Don’t worry, you don’t have to write this yourself. This is provided as boilerplate code, all you have to do is edit the relevant code!</p>
</div>
</div>
<div class="trigger new-trigger" data-highlight="6" data-focus-on="cr-step-2b">
<div class="narrative">
<p>Test your new Stat by using it in a <code>geom_*()</code> function.</p>
</div>
</div>
<section id="step-3-define-user-facing-functions.-test." class="level2 narrative trigger">
<h2 class="narrative anchored" data-anchor-id="step-3-define-user-facing-functions.-test.">Step 3: Define user-facing functions. Test.</h2>
</section>
<div class="trigger new-trigger" data-focus-on="cr-step-3a">
<div class="narrative">
<p>Now, define the user-facing function. This is more boilerplate code that you edit depending on what you are creating.</p>
</div>
</div>
<div class="trigger new-trigger" data-highlight="1" data-focus-on="cr-step-3a">
<div class="narrative">
<p>The <code>stat_*()</code> function name derives from the Stat objects’s name, but is snake case.</p>
</div>
</div>
<div class="trigger new-trigger" data-highlight="3" data-focus-on="cr-step-3a">
<div class="narrative">
<p>“Point” is specified as the default for the geom argument in the function. This means that the <code>ggplot2::geom_point()</code> will be used in the layer unless otherwise specified by the user.</p>
</div>
</div>
<div class="trigger new-trigger" data-highlight="6" data-focus-on="cr-step-3a">
<div class="narrative">
<p><code>StatMedians</code> defines the new layer function, so summarizing the medians will be in play before the layer is rendered.</p>
</div>
</div>
<div class="trigger new-trigger" data-highlight="1" data-focus-on="cr-step-3a-1">
<div class="narrative">
<p>Alternatively, the new <code>make_constructor()</code> function, available in ggplot2 v4.0.0, will write much of the above boilerplate code for you.</p>
</div>
</div>
<div class="trigger new-trigger" data-focus-on="cr-step-3b">
<div class="narrative">
<p>And because users are more accustomed to using layers that have the ‘geom’ prefix, you might also define geom with almost identical properties using <code>make_constructor()</code>.The difference between <code>stat_medians</code> and <code>geom_medians</code> is that the <code>stat</code> is fixed in the former, and the <code>geom</code> is fixed in the latter.</p>
</div>
</div>
<div class="trigger new-trigger" data-highlight="6" data-focus-on="cr-step-3c">
<div class="narrative">
<p>Now, test your user-facing function by using it in a ggplot2 plot.</p>
</div>
</div>
</div>
<div class="sticky-col">
<div class="sticky-col-stack">
<div id="cr-step-0a" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute</span></span>
<span id="cb1-5">penguins_medians <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bill_length_mm_median =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-7">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bill_depth_mm_median =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div></div>
</div>
</div>
<div id="cr-step-0b" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot</span></span>
<span id="cb2-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_length_mm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> penguins_medians,</span>
<span id="cb2-7">             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_depth_mm_median,</span>
<span id="cb2-8">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_length_mm_median),</span>
<span id="cb2-9">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Created with base ggplot2"</span>)</span></code></pre></div></div>
</div>
</div>
<div id="cr-step-0c" class="scale-to-fill sticky">
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-3-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/ggplot2-extensions/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</div>
<div id="cr-step-1a" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define compute.</span></span>
<span id="cb3-2">compute_group_medians <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, scales){ </span>
<span id="cb3-3">  data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T),</span>
<span id="cb3-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T))</span>
<span id="cb3-6">}</span></code></pre></div></div>
</div>
</div>
<div id="cr-step-1b" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test compute. </span></span>
<span id="cb4-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_depth_mm,  </span>
<span id="cb4-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_length_mm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>  </span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compute_group_medians</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 2
      x     y
  &lt;dbl&gt; &lt;dbl&gt;
1  17.3  44.4</code></pre>
</div>
</div>
</div>
<div id="cr-step-2a" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">StatMedians <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb6-2">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggproto</span>(</span>
<span id="cb6-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">_class</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"StatMedians"</span>,</span>
<span id="cb6-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">_inherit</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>Stat,</span>
<span id="cb6-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compute_group =</span> compute_group_medians,</span>
<span id="cb6-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">required_aes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>)</span>
<span id="cb6-7">  )</span></code></pre></div></div>
</div>
</div>
<div id="cr-step-2b" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_depth_mm,</span>
<span id="cb7-4">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_length_mm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> StatMedians, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Testing StatMedians"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-7-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/ggplot2-extensions/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</div>
<div id="cr-step-3a" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">stat_medians <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb8-2">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, </span>
<span id="cb8-3">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"point"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, </span>
<span id="cb8-4">           ..., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) </span>
<span id="cb8-5">    {</span>
<span id="cb8-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> mapping, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> StatMedians, </span>
<span id="cb8-7">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> geom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> position, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> show.legend, </span>
<span id="cb8-8">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> inherit.aes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">params =</span> rlang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list2</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb8-9">                                                           ...))</span>
<span id="cb8-10">    }</span></code></pre></div></div>
</div>
</div>
<div id="cr-step-3a-1" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">stat_medians <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_constructor</span>(StatMedians, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"point"</span>)</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## check the new function's specification</span></span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(stat_medians)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>function (mapping = NULL, data = NULL, geom = "point", position = "identity", 
    ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) 
{
    layer(mapping = mapping, data = data, geom = geom, stat = "medians", 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = list2(na.rm = na.rm, ...))
}
&lt;environment: 0x11ff6daf8&gt;</code></pre>
</div>
</div>
</div>
<div id="cr-step-3b" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">geom_medians <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_constructor</span>(GeomPoint, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medians"</span>)</span>
<span id="cb11-2"></span>
<span id="cb11-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## check the new function's specification</span></span>
<span id="cb11-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(geom_medians)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>function (mapping = NULL, data = NULL, stat = "medians", position = "identity", 
    ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) 
{
    layer(mapping = mapping, data = data, geom = "point", stat = stat, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = list2(na.rm = na.rm, ...))
}
&lt;environment: 0x128266630&gt;</code></pre>
</div>
</div>
</div>
<div id="cr-step-3c" class="scale-to-fill sticky">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Test user-facing.</span></span>
<span id="cb13-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_length_mm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_medians</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Testing geom_medians()"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-11-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/ggplot2-extensions/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p>And then you’re done! You’ve created your first ggplot extension 🥳.</p>
<p>Going through the recipes is a great way to ease into your ggplot2 extension journey. They offer three well-crafted examples with a clear structure and sequence of steps. The boilerplate code looks daunting, but you can copy/paste it and edit it depending on what you are creating; if you want to go into more detail as to what it’s actually doing, the tutorials provide additional resources. A fun note is that the geom recipes website uses <a href="https://docs.r-wasm.org/webr/latest/">webR</a> and <a href="https://github.com/r-wasm/quarto-live">Quarto Live</a> to embed interactive code chunks directly in the tutorial. It makes for an immersive experience while going through the exercises.</p>
<p>Want to try your own hand at creating <code>geom_means()</code>? Go through the interactive tutorial in <a href="https://evamaerey.github.io/easy-geom-recipes/recipe1means.html#your-turn-write-geom_means">Easy geom recipes</a>!</p>
<section id="resources" class="level2">
<h2 class="anchored" data-anchor-id="resources">Resources</h2>
<p>It’s a delight going through Gina’s resources, from seeing the <a href="https://evamaerey.github.io/everyday_ggplot2_extension/community.html">adorable ggextenders hex</a> to reading all the touching notes about ggplot2, comparing it to art, food, poetry, and more. It’s a testament to how a tool can inspire so many. Here are some of my favorites quotations and metaphors:</p>
<ul>
<li>“ggplot2 lets users ‘speak their plots into existence’” — Thomas Lin Pedersen</li>
<li>“You are a composer of ‘graphical poems’” — Hadley Wickham</li>
</ul>
<p>Learn more about Gina’s work here:</p>
<ul>
<li><a href="https://ggplot2-extenders.github.io/ggplot-extension-club/">ggplot2 extenders club website</a>: See previous talks and sign up for future webinars</li>
<li><a href="https://evamaerey.github.io/everyday_ggplot2_extension/">Everyday ggplot2 extension</a>: Education materials for potential extenders</li>
<li><a href="https://evamaerey.github.io/ggplot2-extension-cookbook/">ggplot2 extension cookbook</a>: Guide that presents extension strategies in a consistent and accessible way</li>
<li><a href="https://evamaerey.github.io/easy-geom-recipes/">Easy geom recipes</a>: A series of tutorials on creating a tutorial</li>
</ul>
<p>There is a comprehensive <a href="https://ggplot2-extenders.github.io/ggplot-extension-club/additional-resources.html">list of resources on the ggplot2 extenders club website</a>.</p>
<p>Many thanks to Andrew Bray, James Goldie, and the QMD Lab for <a href="https://closeread.dev/">Closeread</a>, a Quarto extension for scrollytelling, which walked through the ggextender steps, and Gina, for both organizing the ggplot2 extenders club and reviewing this post!</p>


</section>

 ]]></description>
  <guid>https://rworks.dev/posts/ggplot2-extensions/</guid>
  <pubDate>Mon, 03 Nov 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/ggplot2-extensions/ggextenders-hex.png" medium="image" type="image/png" height="103" width="144"/>
</item>
<item>
  <title>September 2025 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/september-2025-top-40-new-cran-packages/</link>
  <description><![CDATA[ 





<div class="columns">
<div class="column" style="width:45%;">
<section id="computational-methods" class="level3">
<h3 class="anchored" data-anchor-id="computational-methods">Computational Methods</h3>
<p><a href="https://cran.r-project.org/package=fastfocal">fastfocal</a> v0.1.3: Provides a fast moving-window (“focal”) and buffer-based extraction for raster data using the <a href="https://CRAN.R-project.org/package=terra"><code>terra</code></a> package. Automatically selects between a <code>C++</code> backend and a Fast Fourier Transform (FFT) backend depending on problem size. The FFT backend supports sum and mean. Other statistics are handled by <code>terra</code>. There are two vignettes: <a href="https://cran.r-project.org/web/packages/fastfocal/vignettes/benchmark.html">Benchmarking</a> and <a href="https://cran.r-project.org/web/packages/fastfocal/vignettes/index.html">Fast Multi-scale Raster Extraction and Moving Window Analysis with FFT</a>.</p>
<p><a href="fastfocal.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/fastfocal.png" class="img-fluid" alt="FFT contour plots"></a></p>
<p><a href="https://cran.r-project.org/package=GTRT">GTRT</a> v0.1.0: Provides a collection of functions for testing randomness (or mutual independence) in linear and circular data as proposed in <a href="https://arxiv.org/abs/2506.21157">Gehlot and Laha (2025a)</a> and <a href="https://arxiv.org/abs/2506.23522">Gehlot and Laha (2025b)</a>. See the vignettes <a href="https://cran.r-project.org/web/packages/GTRT/vignettes/GTRT_circular.html">Randomness Tests for Circular Data</a> and <a href="https://cran.r-project.org/web/packages/GTRT/vignettes/GTRT_linear.html">Randomness Tests for Linear Data</a>.</p>
<p><a href="https://cran.r-project.org/package=rjuliabugs">rjuliabugs</a> v0.1.0: Implements an interface to the <a href="https://github.com/TuringLang/JuliaBUGS.jl"><code>JuliaBUGS.jl</code></a> package for Bayesian inference using the BUGS modeling language. <code>R</code> users can run models in Julia and return results as familiar <code>R</code> objects. Visualization and posterior analysis are supported via the <code>bayesplot</code> and <code>posterior</code> packages. See <a href="https://cran.r-project.org/web/packages/rjuliabugs/readme/README.html">README</a> for an example.</p>
</section>
<section id="data" class="level3">
<h3 class="anchored" data-anchor-id="data">Data</h3>
<p><a href="https://cran.r-project.org/package=BioWorldR">BioWorldR</a> v0.1.0: Provides a curated collection of biodiversity and species-related datasets (birds, plants, reptiles, turtles, mammals, bees, marine data and related biological measurements), together with small utilities to load and explore them. The package aims to simplify reproducible workflows by packaging commonly used example datasets and metadata so they can be easily inspected, visualized, and used for teaching, testing, and prototyping analyses. See the <a href="https://cran.r-project.org/web/packages/BioWorldR/vignettes/BioWorldR_vignette.html">vignette</a>.</p>
<p><a href="BioWorldR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/BioWorldR.png" class="img-fluid" alt="Barplotof brain weight for various species"></a></p>
<p><a href="https://cran.r-project.org/package=ecotourism">ecotourism</a> v0.1.0: This is a collection of data files for exploring sightings of wild things, relative to weather and tourism patterns in Australia. There are eight vignettes, including <a href="https://cran.r-project.org/web/packages/ecotourism/vignettes/glowworms.html">Glowworms</a> and <a href="https://cran.r-project.org/web/packages/ecotourism/vignettes/gouldian_finch.html">Gouldian Finch</a>.</p>
<p><a href="ecotourism.jpeg" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/ecotourism.jpeg" class="img-fluid" alt="Photo of a Gouldian Finch"></a></p>
<p><a href="https://cran.r-project.org/package=ineapir">ineapir</a> v0.2.5: Provides access to open statistical data and metadata disseminated by the National Statistics Institute of Spain (INE). The functions return data frames with the requested information thanks to calls to the <a href="https://www.ine.es/dyngs/DAB/index.htm?cid=1100">INE API</a>. See <a href="https://cran.r-project.org/web/packages/ineapir/readme/README.html">README</a> for information.</p>
<p><a href="ineapir.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/ineapir.png" class="img-fluid" alt="Cheat Sheet"></a></p>
</section>
<section id="decision-analysis" class="level3">
<h3 class="anchored" data-anchor-id="decision-analysis">Decision Analysis</h3>
<p><a href="https://cran.r-project.org/package=DUToolkit">DUToolkit</a> v1.0.2: Provides tools to help modelers and decision-makers effectively interpret and communicate decision risk when evaluating multiple policy options. It uses model outputs from uncertainty analysis for baseline scenarios and policy alternatives to generate visual representations of uncertainty and quantitative measures for assessing associated risks. See <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0332522">Wiggins et al.&nbsp;(2025)</a> and the <a href="https://dut.ihe.ca/">webpage</a> for more details. There are six vignettes, including <a href="https://cran.r-project.org/web/packages/DUToolkit/vignettes/Calculating_Risk.html">Calculating Risk</a> and <a href="https://cran.r-project.org/web/packages/DUToolkit/vignettes/DUToolkit.html">Introduction</a>.</p>
<p><a href="DUToolkit.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/DUToolkit.png" class="img-fluid" alt="Plot in which the trajectory of the outcome is summarized over time and plotted using the mean value for a given policy alternative"></a></p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://cran.r-project.org/package=ConSciR">ConSciR</a> v0.3.0: Provides data science tools for conservation science, including methods for environmental data analysis, humidity calculations, sustainability metrics, engineering calculations, and data visualization. See <a href="https://www.getty.edu/conservation/publications_resources/pdf_publications/tools_for_the_analysis_of_collection_environments.html">Cosaert &amp; Beltran et al.&nbsp;(2022)</a> for background and the <a href="https://cran.r-project.org/web/packages/ConSciR/vignettes/ConSciR.html">vignette</a> for examples.</p>
<p><a href="ConSciR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/ConSciR.png" class="img-fluid" alt="Plot of humidity and dewpoint"></a></p>
<p><a href="https://cran.r-project.org/package=geospatialsuite">geospatialsuite</a> v 0.1.0: Provides a toolkit for geospatio temporal analysis featuring 60+ vegetation indices, advanced raster visualization, universal spatial mapping, water quality analysis, CDL crop analysis, spatial interpolation, temporal analysis, and terrain analysis. Methods include vegetation indices calculations <a href="https://www.sciencedirect.com/science/article/pii/S0034425797001041">Rouse et al.&nbsp;(1974)</a>, water quality indices <a href="https://www.tandfonline.com/doi/abs/10.1080/01431169608948714">McFeeters (1996)</a>, and crop data layer analysis <a href="https://www.nass.usda.gov/Research_and_Science/Cropland/">USDA NASS (2024)</a>. There are seven vignettes, including <a href="https://cran.r-project.org/web/packages/geospatialsuite/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/geospatialsuite/vignettes/water-quality.html">Water Quality Assessment</a>.</p>
<p><a href="https://cran.r-project.org/package=streamsampler">streamsampler</a> v0.1.0: Provides tools to characterize daily stream discharge and water quality data and subsample water quality data. Stream sampling protocols are described in <a href="https://www.sciencedirect.com/science/article/pii/S0022169416305492?via%3Dihub">Lee et al.&nbsp;(2016)</a> and <a href="https://pubs.usgs.gov/publication/sir20195084">Lee et al.&nbsp;(2019)</a>. See the <a href="https://cran.r-project.org/web/packages/streamsampler/vignettes/streamsampler.html">vignette</a> for an introduction.</p>
<p><a href="streamsampler.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/streamsampler.png" class="img-fluid" alt="Time series of samples categorized by subgroup"></a></p>
</section>
<section id="epidemiology" class="level3">
<h3 class="anchored" data-anchor-id="epidemiology">Epidemiology</h3>
<p><a href="https://cran.r-project.org/package=epitraxr">epitraxr</a> v0.5.0: Implements a tool for generating disease surveillance reports from data exported from <a href="https://epi.utah.gov/utah-national-electronic-disease-surveillance-system-ut-nedss/">EpiTrax</a>, a central repository for epidemiological data used by public health officials. It provides functions to manipulate <code>EpiTrax</code> datasets, tailor reports to internal or public use, and export reports in <code>csv</code>, <code>xlsx</code>, or <code>pdf</code> formats. See the vignettes <a href="https://cran.r-project.org/web/packages/epitraxr/epitraxr.pdf">epitraxr</a> and <a href="https://cran.r-project.org/web/packages/epitraxr/vignettes/piped-mode.html">Piped Mode</a>.</p>
<p><a href="https://cran.r-project.org/package=mitey">mitey</a> v0.2.0: Provides methods to estimate serial intervals and time-varying case reproduction numbers from infectious disease outbreak data. Serial intervals measure the time between symptom onset in linked transmission pairs, while case reproduction numbers quantify how many secondary cases each infected individual generates over time. See <a href="https://academic.oup.com/aje/article-abstract/180/9/865/2739204?redirectedFrom=fulltext&amp;login=false">Vink et al.&nbsp;(2014)</a> and <a href="https://royalsocietypublishing.org/doi/10.1098/rspb.2006.3754">Wallinga &amp; Lipsitch (2007)</a> for background and the <a href="https://cran.r-project.org/web/packages/mitey/vignettes/quick_start_guide.html">vignette</a> for examples.</p>
<p><a href="mitey.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/mitey.png" class="img-fluid" alt="Fitted Serial Interval Distribution"></a></p>
<p><a href="https://cran.r-project.org/package=Rnaught">Rnaught</a> v0.1.0: Provides a collection of methods for estimating the basic reproduction number (R0) of infectious diseases, and includes a <code>shiny</code> application and datasets for Canadian national and provincial COVID-19 case counts from <a href="https://www.nature.com/articles/s41597-021-00955-2">Berry et al.&nbsp;(2021)</a>. See <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0083622">Fisman et al.&nbsp;(2013)</a>, <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0002185">Bettencourt &amp; Ribeiro (2008)</a>, and <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0002185">White &amp; Pagano (2008)</a> for the models implemented. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/Rnaught/vignettes/Rnaught.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/Rnaught/vignettes/seq_bayes_post.html">Sequential Bayes</a>.</p>
<p><a href="Rnaught.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/Rnaught.png" class="img-fluid" alt="Posterior distribution of R0"></a></p>
<p><a href="https://cran.r-project.org/package=vaxineR">vaxineR</a> v0.1.0: Provides tools to analyze vaccine coverage data and simulate potential disease outbreak scenarios and to calculate key epidemiological metrics, such as the effective reproduction number (Re), outbreak probabilities, and expected infection counts based on county-level vaccination rates, disease characteristics, and vaccine effectiveness. The package includes historical kindergarten vaccination data for Florida counties. See the <a href="https://cran.r-project.org/web/packages/vaxineR/vignettes/using_vaxineR.html">vignette</a>.</p>
<p><a href="vacineR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/vacineR.png" class="img-fluid" alt="Plots of Historical Trends"></a></p>
</section>
<section id="finance" class="level3">
<h3 class="anchored" data-anchor-id="finance">Finance</h3>
<p><a href="https://cran.r-project.org/package=PortfolioTesteR">PortfolioTesteR</a> v0.1.3: Provides functions to design, backtest, and analyze portfolio strategies using simple, English-like function chains. Includes technical indicators, flexible stock selection, portfolio construction methods (equal weighting, signal weighting, inverse volatility, hierarchical risk parity), and a compact backtesting engine for portfolio returns, drawdowns, and summary metrics. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/PortfolioTesteR/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/PortfolioTesteR/vignettes/Getting_Started_Advanced.html">ML Strategies</a>.</p>
<p><a href="portfolio.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/portfolio.png" class="img-fluid" alt="Portfolio momentum plots"></a></p>
<p><a href="https://cran.r-project.org/package=Rmfrac">Rmfrac</a> v0.1.1: Provides functions to simulate fractional and multifractional processes along with a <code>shiny</code>app for interactive exploration. Functions simulate Brownian and fractional Brownian motions, bridges and Gaussian Haar-based multifractional processes (GHBMP), and Hurst functions. See <a href="https://arxiv.org/abs/2503.07286">Ayache, Olenko and Samarakoon (2025)</a> <a href="doi:10.48550/arXiv.2503.07286" class="uri">doi:10.48550/arXiv.2503.07286</a> for background and <a href="https://cran.r-project.org/web/packages/Rmfrac/readme/README.html">README</a> to get started.</p>
<p><a href="Rmfrac.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/Rmfrac.png" class="img-fluid" alt="Plot Hurst functions"></a></p>
</section>
<section id="genomics" class="level3">
<h3 class="anchored" data-anchor-id="genomics">Genomics</h3>
<p><a href="https://cran.r-project.org/package=cellGeometry">cellGeometry</a> v0.5.4: Provides tools for the deconvolution of bulk RNA-Sequencing data into proportions of cells based on a reference single-cell RNA-Sequencing dataset using high-dimensional geometric methodology. See the vignettes <a href="https://cran.r-project.org/web/packages/cellGeometry/vignettes/intro.html">quickstart</a> and <a href="https://cran.r-project.org/web/packages/cellGeometry/vignettes/brain_atlas.html">Deconvolution with the brain atlas</a>’.</p>
<p><a href="cellGeometry.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/cellGeometry.png" class="img-fluid" alt="Plot showing cosine similarity of cell clusters"></a></p>
<p><a href="https://cran.r-project.org/package=MicrobTiSDA">MicrobTiSDA</a> v0.1.0: Provides a framework for microbiome time-series analysis and tools specifically designed for analyzing longitudinal microbiome data that integrate seven functional modules. See <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0102451">Fisher (2014)</a> for more details on inferences involving interspecies interactions and the <a href="https://cran.r-project.org/web/packages/MicrobTiSDA/vignettes/MicrobTiSDA.html">vignette</a> for examples.</p>
<p><a href="MicrobTiSDA.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/MicrobTiSDA.png" class="img-fluid" alt="Cluster plot of microbial features"></a></p>
</section>
<section id="machine-learning" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning">Machine Learning</h3>
<p><a href="https://cran.r-project.org/package=causalDT">causalDT</a> v1.0.0: Implements a novel machine learning method for estimating interpretable subgroups with heterogeneous treatment effects. CDT allows researchers to fit any machine learning model (or metalearner) to estimate heterogeneous treatment effects for each individual, and then “distills” these predicted heterogeneous treatment effects into interpretable subgroups by fitting an ordinary decision tree to predict the previously-estimated heterogeneous treatment effects. See <a href="https://arxiv.org/abs/2502.07275">Huang, Tang, and Kenney (2025)</a> for details and <a href="https://cran.r-project.org/web/packages/causalDT/readme/README.html">README</a> for an example.</p>
<p><a href="causalDT.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/causalDT.png" class="img-fluid" alt="Plot of causal treatment tree"></a></p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="machine-learning-continued" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning-continued">Machine Learning (continued)</h3>
<p><a href="https://cran.r-project.org/package=polarisR">polarisR</a> v0.1.4: Provides an interactive platform for exploring high-dimensional datasets, diagnosing the quality of embeddings and comparing different NLDR methods such as t-SNE and UMAP. It also implements a <code>shiny</code> application There is an <a href="https://cran.r-project.org/web/packages/polarisR/vignettes/polarisR-vignette.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/polarisR/vignettes/polarisR-vignette2.html">Tips, Requirements, and Support</a>.</p>
<p><a href="polarisR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/polarisR.png" class="img-fluid" alt="Image of Shiny app Screen"></a></p>
<p><a href="https://cran.r-project.org/package=SemanticDistance">SemanticDistance</a> v0.1.1: Implements tools to clean and format language transcripts based on a series of transformations such as lemmatize words, omit stopwords, and split strings across rows). Functions compute two distinct metrics of cosine semantic distance which reflect pairwise cosine distance between different elements or chunks of a language sample. Guided by user-specified options, the package tools can process stories, dialogues, word lists and more. There are five vignettes including <a href="https://cran.r-project.org/web/packages/SemanticDistance/vignettes/SemanticDistance_Dialogues.html">Dialogues</a> and <a href="https://cran.r-project.org/web/packages/SemanticDistance/vignettes/SemanticDistance_Monologues_Lists.html">Monologues</a>.</p>
</section>
<section id="mathematics" class="level3">
<h3 class="anchored" data-anchor-id="mathematics">Mathematics</h3>
<p><a href="https://cran.r-project.org/package=ricci">ricci</a> v0.1.1: Provides a compact interface for performing tensor calculations by allowing upper and lower index labeling of arrays. Ricci calculus conventions implicitly trigger contractions and diagonal subsetting. Features include explicit tensor operations, such as addition, subtraction and multiplication of tensors via the standard operators, raising and lowering indices, taking symmetric or antisymmetric tensor parts, the Kronecker product, the Levi Civita epsilon, the Christoffel symbols, the Riemann ans Ricci tensors. See <a href="https://cran.r-project.org/web/packages/ricci/readme/README.html">README</a> and the vignettes <a href="https://cran.r-project.org/web/packages/ricci/vignettes/ricci.html">ricci</a> and <a href="https://cran.r-project.org/web/packages/ricci/vignettes/tensor_fields.html">Tensor Fields</a>.</p>
</section>
<section id="medical-applications" class="level3">
<h3 class="anchored" data-anchor-id="medical-applications">Medical Applications</h3>
<p><a href="https://cran.r-project.org/package=diegr">diegr</a> v0.1.0: Provides tools to visualize high-density electroencephalography (HD-EEG) data through interactive plots and animations, enabling exploratory and communicative analysis of temporal-spatial brain signals. See the <a href="https://cran.r-project.org/web/packages/diegr/vignettes/diegr.html">vignette</a>.</p>
<p><a href="diegr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/diegr.png" class="img-fluid" alt="Topographic map of baseline signal"></a></p>
<p><a href="https://cran.r-project.org/package=Epoch">Epoch</a> v1.0.3: Provides tools for working with iEEG matrix data, including downloading curated iEEG data from <a href="https://osf.io/">The Open Science Framework</a> (OSF), making new objects, processing, and visualizing the data. See the <a href="https://cran.r-project.org/web/packages/Epoch/vignettes/Epoch.html">vignette</a>.</p>
<p><a href="Epoch.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/Epoch.png" class="img-fluid" alt="Plot of time series for each electrode"></a></p>
<p><a href="https://cran.r-project.org/package=optconerrf">optconerrf</a> v1.0.1: Provides tools to design and analyze confirmatory adaptive clinical trials using the optimal conditional error framework of <a href="https://academic.oup.com/biometrics/article-abstract/60/3/715/7289312?redirectedFrom=fulltext&amp;login=false">Brannath and Bauer (2004)</a> and the extensions of <a href="https://arxiv.org/abs/2402.00814">Brannath and Dreher (2024)</a>. There is an <a href="https://cran.r-project.org/web/packages/optconerrf/vignettes/introduction.html">Introduction</a> and a vignette, <a href="https://cran.r-project.org/web/packages/optconerrf/vignettes/constraints_interim_estimates.html">Using Constraints and Interim Estimates for the Optimal Conditional Error Function</a>.</p>
<p><a href="optconerrf.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/optconerrf.png" class="img-fluid" alt="Plot of second-stage information required to achieve the target condiitonal power"></a></p>
<p><a href="https://cran.r-project.org/package=TrialSimulator">TrialSimulator</a> v1.3.0: Implements tools to simulate phase II and/or phase III clinical trials including carrying out graphical testing procedure and combination test under group sequential design. It supports various types of endpoints and adaptive strategies. There are eleven vignettes including: <a href="https://cran.r-project.org/web/packages/TrialSimulator/vignettes/adaptiveDesign.html">An Example of Simulating a Trial with Adaptive Design</a> and <a href="https://cran.r-project.org/web/packages/TrialSimulator/vignettes/simulatePfsAndOs.html">Simulate Correlated Progression-Free Survival and Overall Survival as Endpoints</a>.</p>
<p><a href="TrialSimulator.png" class="lightbox" data-gallery="quarto-lightbox-gallery-20"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/TrialSimulator.png" class="img-fluid" alt="Diagram of model for simulating trial data"></a></p>
</section>
<section id="programming" class="level3">
<h3 class="anchored" data-anchor-id="programming">Programming</h3>
<p><a href="https://cran.r-project.org/package=kerasnip">kerasnip</a> v0.0.3: Provides a seamless bridge between the <code>keras</code> and the <code>tidymodels</code> frameworks that allows for the dynamic creation of <code>parsnip</code> model specifications for <code>keras</code> models. There are eight vignettes, including <a href="https://cran.r-project.org/web/packages/kerasnip/vignettes/applications.html">Transfer Learning with Keras Applications</a> and <a href="https://cran.r-project.org/web/packages/kerasnip/vignettes/workflows_functional.html">Tidymodels Workflow with Functional Keras Models</a>.</p>
</section>
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=eFCM">eFCM</a> v1.0: Implements the exponential Factor Copula Model (eFCM) of <a href="https://www.tandfonline.com/doi/full/10.1080/01621459.2019.1647842">Castro-Camilo &amp; Huser (2020)</a> for spatial extremes, with tools for dependence estimation, tail inference, and visualization. The package supports likelihood-based inference, Gaussian process modeling via <a href="https://en.wikipedia.org/wiki/Mat%C3%A9rn_covariance_function">Matérn covariance functions</a>, and bootstrap uncertainty quantification. See the <a href="https://cran.r-project.org/web/packages/eFCM/vignettes/prEurope.html">vignette</a> for an example.</p>
<p><a href="eFCM.png" class="lightbox" data-gallery="quarto-lightbox-gallery-21"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/eFCM.png" class="img-fluid" alt="Plot of conditional exceedance probability"></a></p>
<p><a href="https://cran.r-project.org/package=inphr">inphr</a> v0.0.1: Provides functions for performing null hypothesis testing on samples of persistence diagrams using the theory of permutations. Inputs can be either samples of persistence diagrams themselves or vectorizations. In the former case, they are embedded in a metric space using either the Bottleneck or Wasserstein distance. In the latter case, persistence data becomes functional data. See <a href="https://www.tandfonline.com/doi/full/10.1080/10485252.2017.1306627">Pini &amp; Vantini (2017)</a> and <a href="">Lovato et al.&nbsp;(2020)</a> for background and look <a href="https://www.sciencedirect.com/science/article/abs/pii/S0167947319302518?via%3Dihub">here</a> for an example.</p>
<p><a href="inphr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-22"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/inphr.png" class="img-fluid" alt="Functional data plot"></a></p>
<p><a href="https://cran.r-project.org/package=ordinalTables">ordinalTables</a> v1.0.0.3: Provides over 340 functions to fit a variety of models to two-way tables with ordered categories. Most models apply to tables with correlated ordered response categories. There is a particular focus on rater data and models for rescore tables. Functions are organized by the last name of the primary author of the source article suggesting the model. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/ordinalTables/vignettes/analysis_of_association.html">Goodman’s Analysis of Association</a> and <a href="https://cran.r-project.org/web/packages/ordinalTables/vignettes/tests_of_square_tables.html">Models to Fit Square Tables</a>.</p>
<p><a href="https://cran.r-project.org/package=SLGP">SLGP</a> v1.0.0: Provides tools for conditional and spatially dependent density estimation using spatial logistic Gaussian processes. Probability densities are represented through finite-rank Gaussian process priors transformed via a spatial logistic density transformation. Functions include density prediction, quantile and moment estimation, sampling methods, and preprocessing. See <a href="https://academic.oup.com/jrsssb/article/40/2/113/7027600?login=false">Leonard (1978)</a>, <a href="https://www.tandfonline.com/doi/abs/10.1080/01621459.1988.10478625">Lenk (1988)</a>, and other references provided for background. There is an <a href="https://cran.r-project.org/web/packages/SLGP/vignettes/IntroductionSLGP.html">introduction</a> and an additional <a href="https://cran.r-project.org/web/packages/SLGP/vignettes/SLGPdiscrete.html">vignette</a>.</p>
<p><a href="SLGP.png" class="lightbox" data-gallery="quarto-lightbox-gallery-23"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/SLGP.png" class="img-fluid" alt="Plot showing multiple densities of median value vs age of home"></a></p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=houba">houba</a> v0.1.0: Provides tools to manipulate data through memory-mapped files, as vectors, matrices, or arrays. Basic arithmetic functions are implemented. It also includes functions to write and read descriptor files for compatibility with the <code>bigmemory</code> package. See the <a href="https://cran.r-project.org/web/packages/houba/vignettes/houba.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=memshare">memshare</a> v1.0.2: Extends <code>R</code> with a mechanism for efficient parallel data access by utilizing <code>C++</code> shared memory. Large data objects can be accessed and manipulated directly from <code>R</code> without redundant copying, providing both speed and memory efficiency. See <a href="https://cran.r-project.org/web/packages/memshare/readme/README.html">README</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=plumber2">plumber2</a> v0.1.0: Provides tools to automatically create a web server from annotated <code>R</code> files or by building it up programmatically. Includes automatic <code>OpenAPI</code> documentation, input handling, asynchronous evaluation, and plugin support. There are ten vignettes, including <a href="https://cran.r-project.org/web/packages/plumber2/vignettes/plumber.html">Get Started</a> and <a href="https://cran.r-project.org/web/packages/plumber2/vignettes/programmatic-usage.html">Programmatic Usage</a>.</p>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=clockplot">clockplot</a> v0.8.3: Provides a novel visualization technique for plotting time stamped events on a 24-hour circular clock face. This is particularly useful for analyzing daily patterns, event clustering, and gaps in temporal data. See the vignettes <a href="https://cran.r-project.org/web/packages/clockplot/vignettes/clockplot.html">clockplot</a> and <a href="https://cran.r-project.org/web/packages/clockplot/vignettes/clockchart-construction.html">clockchart construction</a>.</p>
<p><a href="clockplot.png" class="lightbox" data-gallery="quarto-lightbox-gallery-24"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/clockplot.png" class="img-fluid" alt="Example of a Clock Plot"></a></p>
<p><a href="https://cran.r-project.org/package=ggDNAvis">ggDNAvis</a> v0.3.0: Use `ggplot2 to visualize a single DNA/RNA sequence split across multiple lines, multiple DNA/RNA sequences, each occupying a whole line, or base modifications such as DNA methylation called by modified bases models in <a href="https://nanoporetech.com/software/other/dorado">Dorado</a> or <a href="https://nanoporetech.com/software/other/guppy">Guppy</a>. See the <em>non-expert</em> <a href="https://ejade42.github.io/ggDNAvis/">user guide</a>.</p>
<p><a href="ggDNAvis.png" class="lightbox" data-gallery="quarto-lightbox-gallery-25"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/ggDNAvis.png" class="img-fluid" alt="Methylation Visualization"></a></p>
<p><a href="https://cran.r-project.org/package=isocubes">isocubes</a> v1.0.0: Provides functions for visualizing voxel data using fixed viewpoint isometric cubes for each data point. There are four vignettes, including <a href="https://cran.r-project.org/web/packages/isocubes/vignettes/animated-isosurface.html">Animated Isosurface</a> and <a href="https://cran.r-project.org/web/packages/isocubes/vignettes/heightmaps.html">Heightmaps</a>.</p>
<p><a href="isocubes.png" class="lightbox" data-gallery="quarto-lightbox-gallery-26"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/isocubes.png" class="img-fluid" alt="3D R"></a></p>
<p><a href="https://cran.r-project.org/package=radviz3d">radviz3d</a> v2.3.0: Extends traditional radial coordinate visualization techniques to three-dimensional space for enhanced exploration and analysis of high-dimensional datasets through interactive 3D plots. See <a href="https://www.tandfonline.com/doi/full/10.1080/10618600.2021.2020129">Zhu, Dai &amp; Maitra (2022)</a> for background and <a href="https://cran.r-project.org/web/packages/radviz3d/readme/README.html">README</a> for examples.</p>
<p><a href="radviz3d.png" class="lightbox" data-gallery="quarto-lightbox-gallery-27"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/radviz3d.png" class="img-fluid" alt="3D radial plot"></a></p>
<p><a href="https://cran.r-project.org/package=regress3d">regress3d</a> v1.0.0: Provides functions that use <a href="https://CRAN.R-project.org/package=plotly"><code>plotly</code></a> to plot regression surfaces and marginal effects in three dimensions. See <a href="https://cran.r-project.org/web/packages/regress3d/readme/README.html">README</a>.</p>
<p><a href="regress3d.gif" class="lightbox" data-gallery="quarto-lightbox-gallery-28"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/regress3d.gif" class="img-fluid" alt="3D plot of regression surface"></a></p>
<p><a href="https://cran.r-project.org/package=splineplot">splineplot</a> v0.1.1: Provides functions to create <code>ggplot2</code>visualizations of smooth effects from GAM (Generalized Additive Models) fitted with <code>mgcv</code>. Supports interaction terms and provides hazard ratio plots with histograms for survival analysis. See the <a href="https://cran.r-project.org/web/packages/splineplot/vignettes/getting-started.html">vignette</a>.</p>
<p><a href="splineplot.png" class="lightbox" data-gallery="quarto-lightbox-gallery-29"><img src="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/splineplot.png" class="img-fluid" alt="Spline plot with axes for odds ratio and percent of population"></a></p>
</section>
</div>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/september-2025-top-40-new-cran-packages/</guid>
  <pubDate>Tue, 28 Oct 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/september-2025-top-40-new-cran-packages/regress3d.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Be Mindful of the Time</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/5-state-ctmc-model/</link>
  <description><![CDATA[ 





<p>The following diagram illustrates the 5-state, absorbing Markov chain framework used to model the progression of patients through various health states as they undergo treatment for asthma. The states are defined as follows: <code>STW</code>= successfully treated week, <code>UTW</code>= unsuccessfully treated week, <code>Hex</code>= hospital-managed exacerbation, <code>Pex</code>= primary care-managed exacerbation, and <code>TF</code>= treatment failure. In the previously published post, <a href="https://rworks.dev/posts/simple-bayesian-model/"><em>A Simple Bayesian Multi-state Survival Model for a Clinical Trial</em></a>, I followed Welton et al.&nbsp;(2010) by using a discrete-time Markov model to compare the effectiveness of two asthma treatments. In this post, I fit a continuous-time Markov chain (CTMC) to the same data.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-1-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/5-state-ctmc-model/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
<section id="its-a-matter-of-time" class="level2">
<h2 class="anchored" data-anchor-id="its-a-matter-of-time">It’s A Matter of Time</h2>
<p>Before getting into the analysis itself, let’s review some of the salient differences between discrete-time and continuous-time Markov models. The choice between the two approaches hinges on how much the available data can tell you about time.</p>
<p>A discrete-time Markov chain (DTMC) models only the most primitive notion of time. The process starts in some state and then at the next time step, the process either stays in the same state or jumps to another state according to a matrix of transition probabilities that do not vary with time. The mathematical model assumes that the time interval between transitions is fixed and equal to one time unit. Hence, using a DTMC to model a real world process requires that the modeler choose a time step interval that is appropriate for the process being modeled. The time interval could be any regular interval that is consistent with the development of the real world process being modeled. For example, when modeling the progression of a disease, the time interval must be on a scale that is appropriate for the biology of disease progression and reflects the process with which data were collected.</p>
<p>A DTMC model is fully specified when the transition probability matrix <img src="https://latex.codecogs.com/png.latex?P"> is given or estimated. Each element <img src="https://latex.codecogs.com/png.latex?p_%7Bij%7D"> of <img src="https://latex.codecogs.com/png.latex?P"> gives the probability of transitioning from state <img src="https://latex.codecogs.com/png.latex?i"> to state <img src="https://latex.codecogs.com/png.latex?j"> in one time step. The transition probability matrix for multiple time steps may be computed by raising the one-step transition matrix to the appropriate power. For example, if the time step interval is one week, then the three-week transition probability matrix is given by <img src="https://latex.codecogs.com/png.latex?P%5E3">. So that while time does not generally explicitly enter into the process of estimating the <img src="https://latex.codecogs.com/png.latex?P"> matrix, the time interval is implicitly defined by the frequency of data collection. Developing a useful DTMC model requires verifying that model predictions compare well with the observed progress of the real world process being modeled. For a disease progression model this would mean comparing state transitions over time to the to actual transitions observed in a cohort of patients.</p>
<p>In contrast to a DTMC, a continuous-time Markov chain (CTMC) explicitly models the time between transitions as exponentially distributed random variables with rates determined by the generator matrix, <img src="https://latex.codecogs.com/png.latex?Q">. The elements <img src="https://latex.codecogs.com/png.latex?q_%7Bij%7D"> of <img src="https://latex.codecogs.com/png.latex?Q"> give the instantaneous rate of transitioning from state <img src="https://latex.codecogs.com/png.latex?i"> to state <img src="https://latex.codecogs.com/png.latex?j">. The time spent in state <img src="https://latex.codecogs.com/png.latex?i"> before transitioning to another state is also exponentially distributed. In a CTMC model the rates are fixed and do not vary with time, but the transition probabilities do vary with time. The time-dependent transition probability matrix <img src="https://latex.codecogs.com/png.latex?P(t)"> may be computed from <img src="https://latex.codecogs.com/png.latex?Q"> by solving the Kolmogorov differential equations. (See below.) For any time interval <img src="https://latex.codecogs.com/png.latex?t">, the probabilities of transitioning from state <img src="https://latex.codecogs.com/png.latex?i"> to state <img src="https://latex.codecogs.com/png.latex?j"> may be computed by matrix exponential: <img src="https://latex.codecogs.com/png.latex?P(t)%20=%20e%5E%7BQt%7D">.</p>
<p>Unlike a DTMC model, a CTMC model explicitly allows for states to change at random intervals determined by transition rates that are unique to each state. So while time interval are random, it may not be apparent that the overall time scale is set by the data used to estimate the <img src="https://latex.codecogs.com/png.latex?Q"> matrix. We will see how this plays out in the analysis below.</p>
<p>Both DTCMC and CTMC Markov models are fit by finding modeling parameters maximally consistent with the data. One would expect both models to produce similar results for time within the study period. However, this is not necessarily the case for times beyond the study period. The example below illustrates this kind of breakdown and raises important questions about long term outcomes. Beyond the scope of the data, differences in modeling assumptions become important.</p>
<p>Also note that the Appendix below provides some of the relevant background on CTMCs.</p>
</section>
<section id="preliminaries" class="level2">
<h2 class="anchored" data-anchor-id="preliminaries">Preliminaries</h2>
<p>These two sections of code load the required R packages and define a helper function to compute the transition probability matrix for a 5-state CTMC over a sequence of time points.</p>
<div class="cell">
<details class="code-fold">
<summary>R Packages</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dplyr'</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ggplot2'</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'stringr'</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tidyverse'</span>)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'matrixcalc'</span>)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'expm'</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for matrix exponentiation</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ctmcd'</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for continuous-time Markov chain models</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'diagram'</span>)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Helper Functions</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function computes the transition probability matrix for a 5-state CTMC. It results in a matrix with one row for each time step and 20 columns for the transitions probabilities for the four transient states.</span></span>
<span id="cb2-2">TP5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(Q, Time){</span>
<span id="cb2-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(Q)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(Q)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>){<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Matrix 'Q' must be 5 x 5."</span>)}</span>
<span id="cb2-4">TP <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb2-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (t <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)){</span>
<span id="cb2-6">  P <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expm</span>(Q <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Time[t])</span>
<span id="cb2-7">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-8">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb2-9">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb2-10">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span>
<span id="cb2-11">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span>
<span id="cb2-12">  </span>
<span id="cb2-13">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-14">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb2-15">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb2-16">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span>
<span id="cb2-17">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span>
<span id="cb2-18">  </span>
<span id="cb2-19">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-20">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb2-21">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb2-22">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span>
<span id="cb2-23">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span>
<span id="cb2-24">  </span>
<span id="cb2-25">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-26">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb2-27">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb2-28">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span>
<span id="cb2-29">  TP[t,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span>
<span id="cb2-30">  </span>
<span id="cb2-31">}</span>
<span id="cb2-32"></span>
<span id="cb2-33"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(TP,Time))</span>
<span id="cb2-34"></span>
<span id="cb2-35">}</span></code></pre></div></div>
</details>
</div>
</section>
<section id="the-data" class="level2">
<h2 class="anchored" data-anchor-id="the-data">The Data</h2>
<p>The data for the model comes from the clinical trial conducted by Kavuru et al.&nbsp;(2000). This data is published in Welton et al.&nbsp;(2010) as state tables that give the aggregate number of state transitions observed over the course of 12 weeks for the two treatments Seretide and Flucticasone. Data of this sort is sometimes described as “incomplete discrete data” because it does not provide the complete continuous-time history of each patient. The data are given with reference to only two time points, the beginning and end of the follow up period.</p>
<div class="cell">
<details class="code-fold">
<summary>Data Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">states <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STW"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sucessfully treated week"</span>,</span>
<span id="cb3-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UTW"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unsucessfully treated week"</span>,</span>
<span id="cb3-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hex"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hospital-managed exacerbation"</span>,</span>
<span id="cb3-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pex"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"primary care-managed exacerbation"</span>,</span>
<span id="cb3-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TF"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treatment failure"</span></span>
<span id="cb3-7">)</span>
<span id="cb3-8"></span>
<span id="cb3-9">treatments <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seretide"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fluticasone"</span>)</span>
<span id="cb3-10"></span>
<span id="cb3-11">s_state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(</span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-13">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">210</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb3-14">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">641</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>,</span>
<span id="cb3-15">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-16">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb3-17">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">81</span></span>
<span id="cb3-18">  ),</span>
<span id="cb3-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb3-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(states), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(states))</span>
<span id="cb3-21">)</span>
<span id="cb3-22"></span>
<span id="cb3-23">f_state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(</span>
<span id="cb3-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-25">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">66</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb3-26">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">752</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,</span>
<span id="cb3-27">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-28">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-29">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">156</span></span>
<span id="cb3-30">  ),</span>
<span id="cb3-31">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb3-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(states), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(states))</span>
<span id="cb3-33">)</span></code></pre></div></div>
</details>
</div>
<div class="columns">
<div class="column" style="width:45%;">
<div class="cell">
<details class="code-fold">
<summary>Seretide Transitions</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">s_state</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>    STW UTW Hex Pex TF
STW 210  60   0   1  1
UTW  88 641   0   4 13
Hex   0   0   0   0  0
Pex   1   0   0   0  1
TF    0   0   0   0 81</code></pre>
</div>
</div>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<div class="cell">
<details class="code-fold">
<summary>Flucticasone Transitions</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">f_state</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>    STW UTW Hex Pex  TF
STW  66  32   0   0   2
UTW  42 752   0   5  20
Hex   0   0   0   0   0
Pex   0   4   0   1   0
TF    0   0   0   0 156</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="the-likelihood-solution" class="level2">
<h2 class="anchored" data-anchor-id="the-likelihood-solution">The Likelihood Solution</h2>
<p>The model below uses the EM algorithm developed by Bladt and Sørensen (2005), further described in Inamura (2006), and implemented in the <a href="https://cran.r-project.org/package=ctmcd"><code>ctmcd</code></a> R package to calculate the Maximum Likelihood estimator for the generator matrix, <img src="https://latex.codecogs.com/png.latex?Q">. (See Israel et al.&nbsp;(2001) for its use in credit rating applications.) To understand the method, consider that if complete continuous-time data were available, it would be straightforward to compute the Likelihood function for the <img src="https://latex.codecogs.com/png.latex?Q"> matrix.</p>
<p><img src="https://latex.codecogs.com/png.latex?L(Q)%20=%20%5Cprod_%7Bi=1%7D%5E%7BI%7D%20%5Cprod_%7B%5Csubstack%7Bj=1%20%5C%5C%20j%20%5Cne%20i%7D%7D%5E%7BI%7D%20q_%7Bij%7D%5E%7BN_%7Bij%7D(T)%7D%20%5Cexp%5Cleft(%20-q_%7Bij%7D%20R_i(T)%20%5Cright)"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?N_%7Bij%7D(T)"> denotes the number of transitions from <img src="https://latex.codecogs.com/png.latex?i"> to <img src="https://latex.codecogs.com/png.latex?j"> within time <img src="https://latex.codecogs.com/png.latex?T"> and <img src="https://latex.codecogs.com/png.latex?R_i(T)"> gives the cumulative sojourn times in state <img src="https://latex.codecogs.com/png.latex?i"> before the process jumps to another state. In this case, a single off-diagonal element of <img src="https://latex.codecogs.com/png.latex?Q"> would look like:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Chat%7Bq%7D_%7Bij%7D%5E%7B%5Cmathrm%7BML%7D%7D%20=%20%5Cfrac%7BN_%7Bij%7D(T)%7D%7BR_i(T)%7D"></p>
<p>However, we don’t have complete data. The two data matrices represent data observed only at times 0 and <img src="https://latex.codecogs.com/png.latex?T">. Nevertheless, there are several methods for estimating <img src="https://latex.codecogs.com/png.latex?N_%7Bij%7D(T)">, <img src="https://latex.codecogs.com/png.latex?R_i(T)">, and <img src="https://latex.codecogs.com/png.latex?Q(T)"> which require developing numerical solutions to the two complicated conditional integral equations reproduced in the appendix below. In this post, use the EM algorithm solution implemented in the <code>gm()</code> function of Marius Pfeuffer’s <a href="https://cran.r-project.org/package=ctmcd"><code>ctmcd</code> R package</a>. Note that the <code>gmEM()</code>method called by <code>gm()</code> requires <code>gmguess</code> and initial guess for the <img src="https://latex.codecogs.com/png.latex?Q"> matrix an <code>te</code>, the <strong>time elapsed in the transition process</strong>.</p>
</section>
<section id="generating-matrix-q-and-transition-probability-matrix-p" class="level2">
<h2 class="anchored" data-anchor-id="generating-matrix-q-and-transition-probability-matrix-p">Generating Matrix (Q) and Transition Probability Matrix (P)</h2>
<p>This section calculates the Q matrix and some basic properties for the CTMC models of the Seretide and Flucticasone treatments using the EM algorithm described above. Note that in the code below, I set the <code>te</code> = 1. Even though the data were collected over a 12 week period, this information is not reflected in the state table. This means that the time scale, as discussed above, is one unit equals twelve weeks. The practical effect is that to estimate the time-dependent transition probabilities for week 1, t should be set to 1/12.</p>
<p>This code sets up the initial guess for the <img src="https://latex.codecogs.com/png.latex?Q"> matrix, which will be used for both the Seretide and Flucticasone calculations. The initial guess does not appear to be critical, but it must be a valid generator matrix.</p>
<div class="cell">
<details class="code-fold">
<summary>Initial Guess for Q Matrix</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Matrix to seed EM algorithm</span></span>
<span id="cb8-2">s0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(s0) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb8-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(s0) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(s0)</span>
<span id="cb8-5">s0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span></code></pre></div></div>
</details>
</div>
<p>Three sets of matrices are laid out here for each treatment. The first is the transition probability matrix, <img src="https://latex.codecogs.com/png.latex?P">, reported in Welton et al.&nbsp;(2010) which was derived from the state table data. These transitions are used to demonstrate the face value credibility of the CTMC model. The second pair of matrices is the generator matrices, <img src="https://latex.codecogs.com/png.latex?Q">, estimated by the EM algorithm. The third set show the estimated transition probability matrices, <img src="https://latex.codecogs.com/png.latex?P(t)">, computed from the estimated <img src="https://latex.codecogs.com/png.latex?Q"> matrix by matrix exponentiation for a time interval of one unit (12 weeks).</p>
</section>
<div class="columns">
<div class="column" style="width:45%;">
<section id="seretide-matrices" class="level2">
<h2 class="anchored" data-anchor-id="seretide-matrices">Seretide Matrices</h2>
<div class="cell">
<details class="code-fold">
<summary>P Matrix for Seretide from ESDMHC</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">ESDMHC_P_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(</span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb9-3">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.762</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.220</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.004</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.007</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.007</span>,</span>
<span id="cb9-4">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.119</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.855</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.007</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.019</span>,</span>
<span id="cb9-5">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>,</span>
<span id="cb9-6">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.286</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.144</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.144</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.142</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.284</span>,</span>
<span id="cb9-7">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb9-8">  ),</span>
<span id="cb9-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb9-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(states), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(states))</span>
<span id="cb9-11">)</span>
<span id="cb9-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(ESDMHC_P_s,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     STW  UTW  Hex  Pex   TF
STW 0.76 0.22 0.00 0.01 0.01
UTW 0.12 0.86 0.00 0.01 0.02
Hex 0.20 0.20 0.20 0.20 0.20
Pex 0.29 0.14 0.14 0.14 0.28
TF  0.00 0.00 0.00 0.00 1.00</code></pre>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Model Estimated Q Matrix</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">em_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gm</span>(s_state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">te=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EM"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gmguess=</span>s0)</span>
<span id="cb11-2"></span>
<span id="cb11-3"> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#t &lt;- 1  # time interval</span></span>
<span id="cb11-4">Q_s  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>em_s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par</span>
<span id="cb11-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(Q_s,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>      STW    UTW     Hex   Pex  TF
STW -0.28   0.27    0.00  0.01 0.0
UTW  0.12  -0.18    0.01  0.05 0.0
Hex 61.18 192.80 -260.13  6.15 0.0
Pex  3.81   0.00    0.00 -7.02 3.2
TF   0.00   0.00    0.00  0.00 0.0</code></pre>
</div>
<details class="code-fold">
<summary>Model Estimated Q Matrix</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#plot(em_s,col = c("white", "white"), cex = 1.5, main="Seretide EM estimate for Q")</span></span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Model Estimated P matrix</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb14-2">P_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expm</span>(Q_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t)</span>
<span id="cb14-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(P_s,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     STW  UTW Hex  Pex   TF
STW 0.77 0.22   0 0.00 0.00
UTW 0.12 0.86   0 0.01 0.02
Hex 0.28 0.69   0 0.00 0.02
Pex 0.43 0.11   0 0.00 0.46
TF  0.00 0.00   0 0.00 1.00</code></pre>
</div>
</div>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="fluticasone-matrices" class="level2">
<h2 class="anchored" data-anchor-id="fluticasone-matrices">Fluticasone Matrices</h2>
<div class="cell">
<details class="code-fold">
<summary>P Matrix for Flucticasone from ESDMHC</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">ESDMHC_P_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(</span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>( <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.638</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.314</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.010</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.009</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.029</span>,</span>
<span id="cb16-3">     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.052</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.914</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.007</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.026</span>,</span>
<span id="cb16-4">     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>,</span>
<span id="cb16-5">     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.101</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.500</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.199</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.100</span>,</span>
<span id="cb16-6">     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb16-7">  ),</span>
<span id="cb16-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb16-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(states), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(states))</span>
<span id="cb16-10">)</span>
<span id="cb16-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(ESDMHC_P_f,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     STW  UTW  Hex  Pex   TF
STW 0.64 0.31 0.01 0.01 0.03
UTW 0.05 0.91 0.00 0.01 0.03
Hex 0.20 0.20 0.20 0.20 0.20
Pex 0.10 0.50 0.10 0.20 0.10
TF  0.00 0.00 0.00 0.00 1.00</code></pre>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Model Estimated Q Matrix</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">em_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gm</span>(f_state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">te=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EM"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gmguess=</span>s0)</span>
<span id="cb18-2"></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#t &lt;- 1  # time interval</span></span>
<span id="cb18-4">Q_f  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>em_f<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par</span>
<span id="cb18-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(Q_f,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>      STW    UTW     Hex    Pex   TF
STW -0.43   0.41    0.00   0.00 0.02
UTW  0.07  -0.22    0.01   0.12 0.02
Hex 11.26 198.70 -221.70   9.77 1.97
Pex  0.00  15.20    0.05 -15.25 0.00
TF   0.00   0.00    0.00   0.00 0.00</code></pre>
</div>
<details class="code-fold">
<summary>Model Estimated Q Matrix</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#plot(em_f,col = c("white", "white"), cex = 1.5, main="Flucticasone EM estimate for Q")</span></span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Model Estimated P matrix</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb21-2">P_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expm</span>(Q_f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t)</span>
<span id="cb21-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(P_f,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     STW  UTW Hex  Pex   TF
STW 0.66 0.32   0 0.00 0.02
UTW 0.05 0.92   0 0.01 0.02
Hex 0.08 0.88   0 0.01 0.03
Pex 0.05 0.92   0 0.01 0.02
TF  0.00 0.00   0 0.00 1.00</code></pre>
</div>
</div>
</section>
</div>
</div>
<p>As it turns out, the continuous-time estimates are very credible. For both Seretide and Flucticasone, the transition probabilities estimated for the first two rows P[1,1] to P[1,5] and P[2,1] to P[2,5] closely match the corresponding transition probabilities reported in Welton et al.&nbsp;(2010). The remaining rows of the <img src="https://latex.codecogs.com/png.latex?P"> matrix are less important because they represent transitions from states that are rarely occupied.</p>
<section id="transition-probabilities-over-time" class="level2">
<h2 class="anchored" data-anchor-id="transition-probabilities-over-time">Transition Probabilities over Time</h2>
<p>This next bit of code plots out the transition probabilities over three time periods for both treatments. Note that the points on the curves at twelve weeks match the matrices computed above, and the values plotted for the first eleven weeks seem to be plausible estimates. Note that in the code below, the matrices <code>TP_s</code> and <code>TP_f</code> contain all of the transition probabilities, and that the code to plot these probabilities is also given. This may be useful for exploring the behavior of the model over time.</p>
<section id="seretide-probabilities-over-time" class="level3">
<h3 class="anchored" data-anchor-id="seretide-probabilities-over-time">Seretide Probabilities over time</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the Plot Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">Time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb23-2">TP_Mat_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TP5</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q =</span> Q_s, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Time =</span> Time)</span>
<span id="cb23-3">probs_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(TP_Mat_s[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb23-4">Weeks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)</span>
<span id="cb23-5">TP_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(Weeks, TP_Mat_s[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], probs_s)</span>
<span id="cb23-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(TP_s) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weeks"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time"</span>,</span>
<span id="cb23-7">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,1]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,2]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,3]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,4]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,5]"</span>,</span>
<span id="cb23-8">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,1]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,2]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,3]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,4]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,5]"</span>,</span>
<span id="cb23-9">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,1]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,2]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,3]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,4]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,5]"</span>,</span>
<span id="cb23-10">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,1]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,2]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,3]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,4]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,5]"</span>)</span>
<span id="cb23-11"></span>
<span id="cb23-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot the entire P matrix over time</span></span>
<span id="cb23-13">df_long_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> TP_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb23-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P["</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Selects all columns starting with "P["</span></span>
<span id="cb23-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TP"</span>,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New column for the original column names</span></span>
<span id="cb23-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New column for the values</span></span>
<span id="cb23-18">  )</span>
<span id="cb23-19"></span>
<span id="cb23-20">Probs_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_long_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Weeks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> TP)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> TP, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Weeks), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State Transition Probabilities over Time"</span>,</span>
<span id="cb23-25">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weeks x 12"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transition Probability"</span>)</span>
<span id="cb23-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Probs_s</span></span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show the Plot Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot only the first row of the P Matrix over time</span></span>
<span id="cb24-2">df_long_s1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> TP_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb24-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1"</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Selects all columns starting with "P["</span></span>
<span id="cb24-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TP"</span>,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New column for the original column names</span></span>
<span id="cb24-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New column for the values</span></span>
<span id="cb24-7">  )</span>
<span id="cb24-8"></span>
<span id="cb24-9">Probs_s1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_long_s1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Weeks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> TP)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> TP, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Weeks), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STW Transition Probabilities over Time"</span>,</span>
<span id="cb24-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weeks x 12"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transition Probability"</span>)</span>
<span id="cb24-15">Probs_s1</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-15-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/5-state-ctmc-model/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</section>
<section id="fluticasone-probabilities-over-time" class="level3">
<h3 class="anchored" data-anchor-id="fluticasone-probabilities-over-time">Fluticasone Probabilities over time</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the Plot Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">Time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb25-2">TP_Mat_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TP5</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q =</span> Q_f, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Time =</span> Time)</span>
<span id="cb25-3">probs_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(TP_Mat_f[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb25-4">Weeks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)</span>
<span id="cb25-5">TP_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(Weeks, TP_Mat_f[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], probs_f)</span>
<span id="cb25-6"></span>
<span id="cb25-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(TP_f) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weeks"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time"</span>,</span>
<span id="cb25-8">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,1]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,2]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,3]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,4]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1,5]"</span>,</span>
<span id="cb25-9">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,1]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,2]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,3]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,4]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[2,5]"</span>,</span>
<span id="cb25-10">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,1]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,2]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,3]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,4]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[3,5]"</span>,</span>
<span id="cb25-11">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,1]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,2]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,3]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,4]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[4,5]"</span>)</span>
<span id="cb25-12"></span>
<span id="cb25-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot the entire P Matrix over time</span></span>
<span id="cb25-14"></span>
<span id="cb25-15">df_long_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> TP_f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb25-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P["</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Selects all columns starting with "P["</span></span>
<span id="cb25-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TP"</span>,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New column for the original column names</span></span>
<span id="cb25-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New column for the values</span></span>
<span id="cb25-20">  )</span>
<span id="cb25-21"></span>
<span id="cb25-22">Probs_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_long_f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Weeks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> TP)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> TP, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Weeks), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"State Transition Probabilities over Time"</span>,</span>
<span id="cb25-27">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weeks x 12"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transition Probability"</span>)</span>
<span id="cb25-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Probs_f</span></span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show the Plot Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Plot only the first row of the P Matrix over time</span></span>
<span id="cb26-2">df_long_f1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> TP_f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb26-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P[1"</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Selects all columns starting with "P["</span></span>
<span id="cb26-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TP"</span>,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New column for the original column names</span></span>
<span id="cb26-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New column for the values</span></span>
<span id="cb26-7">  )</span>
<span id="cb26-8"></span>
<span id="cb26-9">Probs_f1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_long_f1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Weeks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> TP)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-11">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> TP, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Weeks), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STW Transition Probabilities over Time"</span>,</span>
<span id="cb26-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weeks x 12"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transition Probability"</span>)</span>
<span id="cb26-15">Probs_f1</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-17-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/5-state-ctmc-model/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="some-estimates-based-on-ctmc-theory" class="level2">
<h2 class="anchored" data-anchor-id="some-estimates-based-on-ctmc-theory">Some Estimates Based on CTMC Theory</h2>
<p>This next section presents some additional estimates based on the theory of continuous-time Markov chains. These estimates include the mean sojourn times in each state and the mean time to absorption. Note that all of these time estimates are in the units of weeks x 12 defined by the data.</p>
<section id="fundamental-matrix-for-absorbing-continuous-time-markov-chains" class="level3">
<h3 class="anchored" data-anchor-id="fundamental-matrix-for-absorbing-continuous-time-markov-chains">Fundamental Matrix for Absorbing Continuous Time Markov Chains</h3>
<p>Let V be the sub matrix of the generator matrix, Q, comprising the transient states. Then the matrix <img src="https://latex.codecogs.com/png.latex?F%20=%20-V%5E%7B-1%7D"> is called the fundamental matrix. For all transient states <img src="https://latex.codecogs.com/png.latex?i"> and <img src="https://latex.codecogs.com/png.latex?j">, <img src="https://latex.codecogs.com/png.latex?F_%7Bij%7D"> is the time that a process started in state <img src="https://latex.codecogs.com/png.latex?i"> spends in state <img src="https://latex.codecogs.com/png.latex?j">. Then, for each state i, <img src="https://latex.codecogs.com/png.latex?T_a%20=%20%5Csum_%7Bj%7DF_%7Bij%7D"> is the mean time to absorption.</p>
<div class="columns">
<div class="column" style="width:45%;">
<div class="cell">
<details class="code-fold">
<summary>Fundamental Matrix</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">V_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Q_s[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span>
<span id="cb27-2">F_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(V_s)</span>
<span id="cb27-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(F_s,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>      STW   UTW  Hex  Pex
STW 26.55 42.57 0.00 0.31
UTW 23.25 43.13 0.00 0.31
Hex 23.82 42.53 0.01 0.31
Pex 14.44 23.16 0.00 0.31</code></pre>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Mean Sojourn Times</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">Es <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(Q_s) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(Q_s))</span>
<span id="cb29-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Es &lt;- 12* Es # convert to weeks</span></span>
<span id="cb29-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(Es,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> STW  UTW  Hex  Pex   TF 
3.54 5.68 0.00 0.14  Inf </code></pre>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Time to Absorption</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">Ta_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(F_s)</span>
<span id="cb31-2"></span>
<span id="cb31-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(Ta_s,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>  STW   UTW   Hex   Pex 
69.43 66.69 66.66 37.91 </code></pre>
</div>
</div>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<div class="cell">
<details class="code-fold">
<summary>Fundamental Matrix</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">V_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Q_f[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span>
<span id="cb33-2">F_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(V_f)</span>
<span id="cb33-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(F_f,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>     STW   UTW  Hex  Pex
STW 7.58 34.29 0.00 0.26
UTW 5.50 35.89 0.00 0.27
Hex 5.56 35.49 0.01 0.27
Pex 5.50 35.89 0.00 0.34</code></pre>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Mean Sojourn Times</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1">Ef <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(Q_f) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(Q_f))</span>
<span id="cb35-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Ef &lt;- 12* Ef # convert to weeks</span></span>
<span id="cb35-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(Ef,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> STW  UTW  Hex  Pex   TF 
2.32 4.64 0.00 0.07  Inf </code></pre>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Time to Absorption</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1">Ta_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(F_f)</span>
<span id="cb37-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(Ta_f,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>  STW   UTW   Hex   Pex 
42.13 41.67 41.33 41.73 </code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="survival" class="level3">
<h3 class="anchored" data-anchor-id="survival">Survival</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the Plot Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generator matrix for Seretide: Q_s: see above</span></span>
<span id="cb39-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generator matrix for Flucticasone: Q_f: See above</span></span>
<span id="cb39-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial state: all patients start in Health</span></span>
<span id="cb39-4">p0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb39-5"></span>
<span id="cb39-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Time points</span></span>
<span id="cb39-7">time_points <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb39-8"></span>
<span id="cb39-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Survival probabilities for each cohort</span></span>
<span id="cb39-10">survival_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(time_points, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t) {</span>
<span id="cb39-11">  P_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expm</span>(Q_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t)</span>
<span id="cb39-12">  p_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> P_t</span>
<span id="cb39-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(p_t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Still in Health or Disease</span></span>
<span id="cb39-14">})</span>
<span id="cb39-15"></span>
<span id="cb39-16">survival_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(time_points, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t) {</span>
<span id="cb39-17">  P_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expm</span>(Q_f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t)</span>
<span id="cb39-18">  p_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> P_t</span>
<span id="cb39-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(p_t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Still in Health or Disease</span></span>
<span id="cb39-20">})</span>
<span id="cb39-21"></span>
<span id="cb39-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine into one data frame</span></span>
<span id="cb39-23">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb39-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(time_points, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb39-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">survival =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(survival_s, survival_f),</span>
<span id="cb39-26">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cohort =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seretide"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flucticasone"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(time_points))</span>
<span id="cb39-27">)</span>
<span id="cb39-28"></span>
<span id="cb39-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot survival curves</span></span>
<span id="cb39-30"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> survival, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> cohort)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Survival Curves for Two Treatment Cohorts"</span>,</span>
<span id="cb39-33">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weeks x 12"</span>,</span>
<span id="cb39-34">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Survival Probability"</span>,</span>
<span id="cb39-35">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cohort"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-24-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/5-state-ctmc-model/index_files/figure-html/unnamed-chunk-24-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</section>
<section id="conclusions" class="level3">
<h3 class="anchored" data-anchor-id="conclusions">Conclusions</h3>
<p>The estimated sojourn times seem reasonable, but the long range estimates are to be contemplated with a good deal of skepticism. A literature search did not produce any evidence that asthma would progress as modeled over extended time periods, and it is doubtful that the treatment regimens would remain unmodified over long periods of time. It is also noteworthy that Welton et al.&nbsp;did not offer any long range projections based on their discrete-time model. Nevertheless, the long term estimations presented are representative of the kinds of results that can be obtained by modeling in continuous-time.</p>
<p>The survival curves in the plot above initially appear to be consistent with those produced in the DTMC post referenced above. However, looking beyond the shape of the curves, one sees that <strong>the time scales differ by an order of magnitude</strong>. Both of these models proceed from reasonable assumptions and share many similar structural elements, but they lead to significantly different conclusions. As explained above, transition probabilities for DTMC do not vary over time. So, having obtained these probabilities, the internal evidence of the model would suggest that disease progression is over weeks. Nevertheless, the CTMC model, which replicates the estimated probabilities of the DTMCM, shows progression over a time scale that is dramatically different. This inconsistency urgently points out the need for expert opinion and external validation to assess the assumptions about time. Even without a change in the underlying dynamics of a system, a model may only be useful over a time scale that needs to be determined from external evidence.</p>
<p>Be careful with the time!</p>
</section>
</section>
<section id="appendix-continuous-time-markov-chains" class="level2">
<h2 class="anchored" data-anchor-id="appendix-continuous-time-markov-chains">Appendix: Continuous-Time Markov Chains</h2>
<p>As in the previous post, the progress of patients passing through various states of health as they undergo treatment for asthma is modeled by the following 5-state Markov chain. In the present analysis, I fit the model to a continuous-time Markov Chain (CTMC), so a little background on continuous-time Markov chains is in order. A Continuous-Time Markov Chain (CTMC) is a stochastic process <img src="https://latex.codecogs.com/png.latex?X(t),%20t%20%5Cge%200"> over a finite or countable state space, <img src="https://latex.codecogs.com/png.latex?S">, such that the process satisfies the Markov property: the future state depends only on the current state, not on the past history. <img src="https://latex.codecogs.com/png.latex?P(X(t%20+%20s)%20=j%20%7C%20X(s)%20=%20i,%20history)%20=%20P(X(t%20+%20s)%20=j%20%7C%20X(s)%20=%20i)"> Hence, just like a discrete-time Markov chain, a CTMC is <em>memoryless</em>. However, unlike a discrete-time Markov chain where the behavior of the process is determined by a matrix of transition probabilities, the dynamics of a CTMC are determined by its generator matrix <img src="https://latex.codecogs.com/png.latex?Q">, which has the following properties:</p>
<ul>
<li><p>For <img src="https://latex.codecogs.com/png.latex?%7Bi%20%5Cneq%20j%7D">, the off-diagonal entries <img src="https://latex.codecogs.com/png.latex?q_%7Bij%7D%20%5Cgeq%200"> represent the instantaneous transition rate from state <img src="https://latex.codecogs.com/png.latex?i"> to state <img src="https://latex.codecogs.com/png.latex?j">.</p></li>
<li><p>The diagonal entries are defined such that each row sums to zero: <img src="https://latex.codecogs.com/png.latex?q_%7Bii%7D%20=%20-%5Csum_%7Bj%20%5Cneq%20i%7D%20q_%7Bij%7D"></p></li>
<li><p>The time-dependent probability matrix for a CTMC may be derived from either the Kolmogorov forward equation: <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%7D%7Bdt%7D%20P(t)%20=%20P(t)%20Q"> or the Kolmogorov backward equation is: <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%7D%7Bdt%7D%20P(t)%20=%20Q%20P(t)"></p></li>
<li><p>The memoryless property dictates that the amount of time the process stays in a state <img src="https://latex.codecogs.com/png.latex?i"> before transitioning to another state <img src="https://latex.codecogs.com/png.latex?j%20%5Cne%20i"> is governed by an exponential distribution with rate: <img src="https://latex.codecogs.com/png.latex?q_%7Bi%7D=%5Csum%20_%7Bj%5Cne%20i%7Dq_%7Bij%7D">. Hence sojourn time in state <img src="https://latex.codecogs.com/png.latex?i"> is given by the exponential distribution: <img src="https://latex.codecogs.com/png.latex?P(T_%7Bi%7D%3Et)=e%5E%7B-q_%7Bi%7Dt%7D"> with mean sojourn time: <img src="https://latex.codecogs.com/png.latex?E%5BT_%7Bi%7D%5D=%5Cfrac%7B1%7D%7Bq_%7Bi%7D%7D=%5Cfrac%7B1%7D%7B%5Csum_%7Bj%5Cne%20i%7Dq_%7Bij%7D%7D"></p></li>
</ul>
</section>
<section id="appendix-the-conditional-em-equations" class="level2">
<h2 class="anchored" data-anchor-id="appendix-the-conditional-em-equations">Appendix: The Conditional EM Equations</h2>
<p>The integral equations required to estimate the likelihood function for the <img src="https://latex.codecogs.com/png.latex?Q"> matrix from incomplete data are: <img src="https://latex.codecogs.com/png.latex?E(R_i(T)%20%5Cmid%20Q,%20s(0),%20s(T))%20=%20%5Cfrac%7Bn_%7Bi,0%7Ds(T)%5C,%5Cmathbf%7Bu%7D_%7Bs(0)%7D%5ET%0A%5Cleft(%20%5Cint_0%5ET%20e%5E%7BQt%7D%20%5C,%5Cmathbf%7Bu%7D_i%20%5Cmathbf%7Bu%7D_i%5ET%5C,%20e%5E%7BQ(T%20-%20t)%7D%20%5C,dt%20%5Cright)%0A%5Cmathbf%7Bu%7D_%7Bs(T)%7D%7D%7B%5Cmathbf%7Bu%7D_%7Bs(0)%7D%5ET%5C,%20e%5E%7BQT%7D%5C,%20%5Cmathbf%7Bu%7D_%7Bs(T)%7D%7D"></p>
<p>and</p>
<p><img src="https://latex.codecogs.com/png.latex?E(N_%7Bij%7D(T)%20%5Cmid%20Q,%20s(0),%20s(T))%20=%0A%5Cfrac%7Bn_%7Bi,0%7Ds(T)%5C,%5Ceta_%7Bij%7D%5C,%5Cmathbf%7Bu%7D_%7Bs(0)%7D%5ET%0A%5Cleft(%20%5Cint_0%5ET%20e%5E%7BQt%7D%20%5C,%5Cmathbf%7Bu%7D_i%20%5Cmathbf%7Bu%7D_i%5ET%5C,%20e%5E%7BQ(T%20-%20t)%7D%20%5C,dt%20%5Cright)%0A%5Cmathbf%7Bu%7D_%7Bs(T)%7D%7D%7B%5Cmathbf%7Bu%7D_%7Bs(0)%7D%5ET%5C,%20e%5E%7BQT%7D%5C,%20%5Cmathbf%7Bu%7D_%7Bs(T)%7D%7D"></p>
<p>where</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?n"> refers to an element of the discrete-time absolute transition frequency matrix <img src="https://latex.codecogs.com/png.latex?N(T)"> which constitutes the data * * <img src="https://latex.codecogs.com/png.latex?uk"> denotes a unit vector with a 1 at position <img src="https://latex.codecogs.com/png.latex?k">.</li>
<li><img src="https://latex.codecogs.com/png.latex?l"> indicates the current iteration step of the EM algorithm.</li>
</ul>
<p>In order to initialize the EM algorithm it is necessary to provide an initial guess for the generator matrix, <img src="https://latex.codecogs.com/png.latex?Q">. See Pfeuffer (2017), Inamura (2006), and Bladt and Sørensen (2000), and the <a href="https://cran.r-project.org/web/packages/ctmcd/vignettes/RJ-2017-038.pdf">ctmcd package vignette</a> for more details.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><p>Bladt, M and M. Sørensen. <a href="https://doi.org/10.1111/j.1467-%209868.2005.00508.x">Statistical inference for discretely observed Markov jump processes</a> Journal of the Royal Statistical (2005). Look <a href="https://www.maphysto.dk/publications2/MPS-RR/2003/21.pdf">here</a> for the pdf.</p></li>
<li><p>Inamura, Yasunari. <a href="https://www.boj.or.jp/en/research/wps_rev/wps_2006/data/wp06e07.pdf">Estimating Continuous Time Transition Matrices From Discretely Observed Data</a>, Bank of Japan Working Paper Series (2006).</p></li>
<li><p>Israel, R.B. et al.&nbsp;<a href="https://onlinelibrary.wiley.com/doi/10.1111/1467-9965.00114">Finding generators for Markov chains via empirical transition matrices, with applications to credit ratings</a>. Mathematical Finance (2001)</p></li>
<li><p>Kavuru M, Melamed J, Gross G, Laforce C, House K, Prillaman B, Baitinger L, Woodring A, and Shah T. <a href="https://pubmed.ncbi.nlm.nih.gov/10856143/">Salmeterol and fluticasone propionate combined in a new powder inhalation device for the treatment of asthma: a randomized, double-blind, placebo-controlled trial</a>. (2000)</p></li>
<li><p>Pfeuffer, Marius. <a href="https://cran.r-project.org/web/packages/ctmcd/vignettes/RJ-2017-038.pdf">ctmcd: An R Package for Estimating the Parameters of a Continuous-Time Markov Chain from Discrete-Time Data</a>, The R Journal Vol. 9/2, December (2017)</p></li>
<li><p>Rickert, Joseph. <a href="https://rworks.dev/posts/simple-bayesian-model/">A Simple Bayesian Multi-state Survival Model for a Clinical Trial</a> (2025)</p></li>
<li><p>Welton NJ, Sutton AJ, Cooper NJ, Abrams KR, and Ades AE. <a href="https://onlinelibrary.wiley.com/doi/book/10.1002/9781119942986">Evidence Synthesis for Decision Making in Healthcare</a>. Cambridge University Press. (2010)</p></li>
</ul>


</section>

 ]]></description>
  <guid>https://rworks.dev/posts/5-state-ctmc-model/</guid>
  <pubDate>Thu, 23 Oct 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/5-state-ctmc-model/states.png" medium="image" type="image/png" height="141" width="144"/>
</item>
<item>
  <title>August 2025 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/august-2025-top-40-new-cran-packages/</link>
  <description><![CDATA[ 





<div class="columns">
<div class="column" style="width:45%;">
<section id="causal-inference" class="level3">
<h3 class="anchored" data-anchor-id="causal-inference">Causal Inference</h3>
<p><a href="https://cran.r-project.org/package=gadjid">gadjid</a> v0.1.0: Enables efficient <code>Rust</code> implementations of graph adjustment identification distances available in R. These distances (based on ancestor, optimal, and parent adjustment) count how often the respective adjustment identification strategy leads to causal inferences that are incorrect relative to a ground-truth graph when applied to a candidate graph instead. See <a href="https://arxiv.org/abs/2402.08616">Henckel, Würtzen, Weichwald (2024)</a> for background and <a href="https://cran.r-project.org/web/packages/gadjid/readme/README.html">README</a> to get started.</p>
</section>
<section id="data" class="level3">
<h3 class="anchored" data-anchor-id="data">Data</h3>
<p><a href="https://cran.r-project.org/package=datacommons">datacommons</a> v0.1.0: Implements a wrapper to the <a href="https://docs.datacommons.org/api/rest/v2/">Google Data Commons API V2</a> that provides programmatic access to statistical and demographic data from dozens of sources organized in a knowledge graph. See the <a href="https://cran.r-project.org/web/packages/datacommons/vignettes/analyzing-census-data.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=imfweo">imfweo</a> v0.1.0: Provides tools to download, process, and analyze data from the International Monetary Fund’s <a href="https://www.imf.org/en/Publications/SPROLLs/world-economic-outlook-databases">World Economic Outlook (WEO) database</a>, including functions for downloading complete WEO releases, accessing specific economic indicators for selected countries, and listing available data. See <a href="https://cran.r-project.org/web/packages/imfweo/readme/README.html">README</a>.</p>
<p><a href="https://cran.r-project.org/package=letsRept">letsRept</a> v1.0.1: Provides tools to retrieve and summarize taxonomic information and synonymy data for reptile species using data scraped from <a href="https://reptile-database.reptarium.cz/">The Reptile Database</a> website. See the <a href="https://cran.r-project.org/web/packages/letsRept/vignettes/letsRept.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=pubchem.bio">pubchem.bio</a> v1.0.1: Provides functions to parse the biologically relevant compounds from all <a href="https://pubchem.ncbi.nlm.nih.gov/search/help_search.html">PubChem</a> compounds, using biological database sources, pathway presence, and taxonomic relationships. See the <a href="https://cran.r-project.org/web/packages/pubchem.bio/vignettes/pubchem.bio.vignette.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=redlist">redlist</a> v0.2.0: Provides an interface to access data from the International Union for Conservation of Nature (IUCN) <a href="https://api.iucnredlist.org/api-docs/index.html">Red List</a>, allowing users to retrieve up-to-date information on species’ conservation status in support of biodiversity research and conservation efforts. There is an <a href="https://cran.r-project.org/web/packages/redlist/vignettes/get_data.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/redlist/vignettes/data_visualisation.html">Visualization</a>.</p>
<p><a href="redlist.jpeg" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/redlist.jpeg" class="img-fluid" alt="Plot of criticlly endangerd species by region"></a></p>
<p><a href="https://cran.r-project.org/package=verdadecu">verdadecu</a> v1.0.0: Provides access to data collected by the Ecuadorian Truth Commission, including datasets documenting human rights violations from 1984-2008, containing victim information, violation types, perpetrators, and geographic distribution. See the <a href="https://cran.r-project.org/web/packages/verdadecu/vignettes/verdadecu.html">vignette</a>.</p>
</section>
<section id="differential-privacy" class="level3">
<h3 class="anchored" data-anchor-id="differential-privacy">Differential Privacy</h3>
<p><a href="https://cran.r-project.org/package=GRANDpriv">GRANDpriv</a> v0.1.3: Implements a novel method for privatizing network data using differential privacy and includes functions for generating synthetic networks based on a latent space model, applying differential privacy to network latent positions, and evaluating the utility of privatized networks. See <a href="https://arxiv.org/abs/2507.00402">Liu, Bi, &amp; Li (2025)</a> for details and <a href="https://cran.r-project.org/web/packages/GRANDpriv/readme/README.html">README</a> for examples.</p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://cran.r-project.org/package=ecodive">ecodive</a> v2.0.0: Provides functions to compute alpha and beta diversity metrics using concurrent <code>C</code> threads. Metrics include <a href="https://en.wikipedia.org/wiki/UniFrac">UniFrac</a>, <a href="https://danielpfaith.wordpress.com/phylogenetic-diversity/">Faith’s phylogenetic diversity</a>, <a href="https://en.wikipedia.org/wiki/Bray%E2%80%93Curtis_dissimilarity">Bray-Curtis dissimilarity</a>, <a href="https://www.statology.org/shannon-diversity-index/">Shannon diversity index</a>. Functions also parse <a href="https://en.wikipedia.org/wiki/Newick_format">newick trees</a> into <em>phylo</em> objects and rarefy feature tables. There are six vignettes, including an <a href="https://cran.r-project.org/web/packages/ecodive/vignettes/ecodive.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/ecodive/vignettes/bdiv_guide.html">Selecting a Beta Diversity Metric</a>.</p>
<p><a href="ecodive.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/ecodive.png" class="img-fluid" alt="A phylogenetic tree"></a></p>
</section>
<section id="environmental-studies" class="level3">
<h3 class="anchored" data-anchor-id="environmental-studies">Environmental Studies</h3>
<p><a href="https://cran.r-project.org/package=ffaframework">ffaframework</a> v0.1.1: Implements the the FFA framework proposed by Vidrio- <a href="https://www.sciencedirect.com/science/article/abs/pii/S136481522400001X?via%3Dihub">Sahagún et al.&nbsp;(2024)</a> to support systematic and reproducible workflows for both stationary and nonstationary flood frequency analysis, with applications extending to other hydroclimate extremes, such as precipitation frequency analysis. There are six vignettes, including <a href="https://cran.r-project.org/web/packages/ffaframework/vignettes/change-points.html">Change Point Detection</a> and <a href="https://cran.r-project.org/web/packages/ffaframework/vignettes/non-stationary-ffa.html">Notstationary FFA</a>.</p>
<p><a href="ffaf.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/ffaf.png" class="img-fluid" alt="Plot illustrating L-distance model selection"></a></p>
</section>
<section id="epidemiology" class="level3">
<h3 class="anchored" data-anchor-id="epidemiology">Epidemiology</h3>
<p><a href="https://cran.r-project.org/package=mlspatial">mlspatial</a> v0.1.0: Provides tools for the integration, visualization, and modelling of spatial epidemiological data using the method described in <a href="https://zenodo.org/records/16529986">Azeez &amp; Noel (2025)</a>. Users can import and pre-process shapefiles and associated demographic or disease incidence data, generate richly annotated thematic maps, and apply predictive models to identify spatial patterns and risk factors. See the <a href="https://cran.r-project.org/web/packages/mlspatial/vignettes/mlspatial.html">vignette</a>.</p>
<p><a href="mlspatial.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/mlspatial.png" class="img-fluid" alt="Plot of predictions vs observations for multiple models"></a></p>
</section>
<section id="geology" class="level3">
<h3 class="anchored" data-anchor-id="geology">Geology</h3>
<p><a href="https://cran.r-project.org/package=stratcols">stratcols</a> v1.0.0: Provides tools to quantify stratigraphic disorder using the metrics defined by <a href="https://pubs.geoscienceworld.org/sepm/jsedres/article-abstract/86/3/148/145523/Identifying-Ordered-Strata-Evidence-Methods-and?redirectedFrom=fulltext">Burgess (2016)</a>, including a range of utility tools to construct and manipulate stratigraphic columns. There are three vignettes, including an <a href="https://cran.r-project.org/web/packages/stratcols/vignettes/stratcols.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/stratcols/vignettes/stratorder.html">Quantifying stratigraphic order</a>.</p>
</section>
<section id="genetics" class="level3">
<h3 class="anchored" data-anchor-id="genetics">Genetics</h3>
<p><a href="https://cran.r-project.org/package=tidypopgen">tidypopgen</a> v0.3.2: Provide a tidy grammar of population genetics, facilitating the manipulation and analysis of data on biallelic single nucleotide polymorphisms (SNPs). Functions scale to very large genetic datasets by storing genotypes on disk, and performing operations on them in chunks, without ever loading all data in memory. See <a href="https://www.biorxiv.org/content/10.1101/2025.06.06.658325v1">Carter et al.&nbsp;(2025)</a> for details. There are five vignettes, including an <a href="https://cran.r-project.org/web/packages/tidypopgen/vignettes/tidypopgen.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/tidypopgen/vignettes/a01_overview.html">The grammar of population genetics</a>.</p>
<p><a href="tidypopgen.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/tidypopgen.png" class="img-fluid" alt="PCA Plot"></a></p>
</section>
<section id="genomics" class="level3">
<h3 class="anchored" data-anchor-id="genomics">Genomics</h3>
<p><a href="https://cran.r-project.org/package=ibdfindr">ibdfindr</a> v0.3.1: Implements continuous-time hidden Markov models (HMMs) to infer identity-by-descent (IBD) segments shared by two individuals from their single-nucleotide polymorphism (SNP) genotypes. Provides posterior probabilities at each marker (forward-backward algorithm), prediction of IBD segments (Viterbi algorithm), and functions for visualizing results. Supports both autosomal data and X-chromosomal data. See <a href="https://cran.r-project.org/web/packages/ibdfindr/readme/README.html">README</a> for examples.</p>
<p><a href="ibdfindr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/ibdfindr.png" class="img-fluid" alt="Multiple IBD plots"></a></p>
<p><a href="https://cran.r-project.org/package=readyomics">readyomics</a> v0.1.2: Provides a flexible and streamlined pipeline for formatting, analyzing, and visualizing <em>omics</em> data (e.g., transcriptomics, proteomics, metabolomics) and includes tools for shaping input data, fitting linear or mixed-effect models, extracting key contrasts, and generating publication-quality plots. See the <a href="https://cran.r-project.org/web/packages/readyomics/vignettes/readyomics.html">vignette</a>.</p>
<p><a href="readyomics.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/readyomics.png" class="img-fluid" alt="Annotated distribution plots"></a></p>
</section>
<section id="health-technology-assessment" class="level3">
<h3 class="anchored" data-anchor-id="health-technology-assessment">Health Technology Assessment</h3>
<p><a href="https://cran.r-project.org/package=searchAnalyzeR">searchAnalyzeR</a> v0.1.0: Provides comprehensive analytics, reporting, and testing capabilities for systematic review search strategies, focusing on validating search performance, generating standardized <code>PRISMA</code>-compliant reports, and ensuring reproducibility in evidence synthesis. Features include precision-recall analysis, cross-database performance comparison, benchmark validation against gold standards, sensitivity analysis, temporal coverage assessment, automated report generation, and statistical comparison of search strategies. See the vignettes <a href="https://cran.r-project.org/web/packages/searchAnalyzeR/vignettes/Comparing_Search_Strategies_with_searchAnalyzeR.html">Comparing Search strategies</a> and <a href="https://cran.r-project.org/web/packages/searchAnalyzeR/vignettes/Real_PubMed_Search_Analysis_with_searchAnalyzeR.html">Real PubMed Search Analysis</a>.</p>
<p><a href="searchA.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/searchA.png" class="img-fluid" alt="Search Strategy Comparison"></a></p>
</section>
<section id="machine-learning" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning">Machine Learning</h3>
<p><a href="https://cran.r-project.org/package=LCCkNN">LCCkNN</a> v0.1.0: Implements the kK-NN algorithm, an adaptive k-nearest neighbor classifier that adjusts the neighborhood size based on local data curvature. The method estimates local Gaussian curvature by approximating the shape operator of the data manifold. This approach aims to improve classification performance, particularly in datasets with limited samples. Look <a href="https://github.com/Gabrielforest/LCCkNN">here</a> for examples.</p>
</section>
<section id="medical-statistics" class="level3">
<h3 class="anchored" data-anchor-id="medical-statistics">Medical Statistics</h3>
<p><a href="https://cran.r-project.org/package=E2E">E2E</a> v0.1.0: Provides a framework to build and evaluate diagnosis or prognosis models using stacking, voting, and bagging ensemble techniques with various base learners. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/E2E/vignettes/getting-started.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/E2E/vignettes/advanced-features.html">Advanced Features &amp; Customization</a>.</p>
<p><a href="https://cran.r-project.org/package=kfre">kfre</a> v0.0.2: Implements the Kidney Failure Risk Equation <a href="https://jamanetwork.com/journals/jama/fullarticle/897102">Tangri et al.&nbsp;(2011)</a> and <a href="https://jamanetwork.com/journals/jama/fullarticle/2481005">Tangri et al.&nbsp;(2016)</a> to compute two and five year kidney failure risk. Includes helper functions to classify chronic kidney disease (CKD) stages and end-stage renal disease (ESRD) outcomes, and evaluate and plot model performance. See the [vignette]https://cran.r-project.org/web/packages/kfre/vignettes/getting-started.html) to get started.</p>
<p><a href="https://cran.r-project.org/package=metaSVR">metaSVR</a> v0.1.0: Provides a hybrid modeling framework combining Support Vector Regression with metaheuristic optimization algorithms, including the [Archimedes Optimization Algorithm], <a href="https://link.springer.com/article/10.1007/s10489-020-01893-z">Hashim et al.&nbsp;(2021)</a>; Coot Bird Optimization, <a href="https://www.sciencedirect.com/science/article/abs/pii/S0957417421007806?via%3Dihub">Naruei &amp; Keynia (2021)</a>; as well as several others. Functions enable automatic tuning of SVR hyperparameters (cost, gamma, and epsilon) to enhance prediction performance. See <a href="https://www.sciencedirect.com/science/article/pii/S1877050920324364?via%3Dihub">Setiawan et al.&nbsp;(2021)</a> and <a href="https://onlinelibrary.wiley.com/doi/10.1155/2018/6076475">Liu et al.&nbsp;(2018)</a> for implementation details and <a href="https://cran.r-project.org/web/packages/metaSVR/readme/README.html">README</a> for an example.</p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=AdapDiscom">AdapDiscom</a> v1.0.0: Implements adaptive direct sparse regression for high-dimensional multimodal data with heterogeneous missing patterns and measurement errors. The method supports flexible block configurations and includes robust variants for heavy-tailed distributions and fast implementations for large-scale applications. See <a href="https://arxiv.org/abs/2508.00120">Diakité et al.&nbsp;(2025)</a> for background and the <a href="https://cran.r-project.org/web/packages/AdapDiscom/vignettes/AdapDiscom.html">vignette</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=allocation">allocation</a> v0.1.0: Implements several exact methods for allocating optimal sample sizes when designing stratified samples. These methods are discussed in <a href="https://www.tandfonline.com/doi/abs/10.1080/00031305.2012.733679">Wright (2012)</a> and <a href="https://www.sciencedirect.com/science/article/abs/pii/S0167715217301657?via%3Dihub">Wright (2017)</a>. See the <a href="https://cran.r-project.org/web/packages/allocation/vignettes/allocation.pdf">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=BSTFA">BSTFA</a> v0.1.0: Implements Bayesian spatio-temporal factor analysis models for multivariate data observed across space and time. The package provides MCMC tools for model fitting, spatial and temporal interpolation, and visualization of latent factors and loadings. See <a href="https://onlinelibrary.wiley.com/doi/10.1002/env.2609">Berrett et al.&nbsp;(2020)</a> and the <a href="https://cran.r-project.org/web/packages/BSTFA/vignettes/BSTFA-Vignette.pdf">vignette</a> for examples.</p>
<p><a href="BSTFA.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/BSTFA.png" class="img-fluid" alt="Plots comparing factor loadings using different bases"></a></p>
<p><a href="https://cran.r-project.org/package=calms">calms</a> v1.0-1: Provides a Shiny application to conduct comprehensive analysis of latent means including the examination of group equivalency, propensity score analysis, measurement in variance analysis, and assessment of latent mean differences of equivalent groups with invariant data. <a href="https://www.jstatsoft.org/article/view/v042i08">Ho et al.&nbsp;(2011)</a>, <a href="https://www.jstatsoft.org/article/view/v048i02">Rosseel (2012)</a>, and <a href="https://onlinelibrary.wiley.com/doi/10.1002/hrdq.21452">Hammack-Brown et al.&nbsp;(2021)</a> for background and the <a href="https://cran.r-project.org/web/packages/calms/vignettes/intro.html">vignette</a> for examples.</p>
<p><a href="calms.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/calms.png" class="img-fluid" alt="Shiny interface for latent means analysis"></a></p>
<p><a href="https://cran.r-project.org/package=CCI">CCI</a> v0.3.4: Implements a tool for performing computational testing for conditional independence between variables in a dataset by using permutations in combination with Monte Carlo cross-validation to generate null distributions and test statistics. See <a href="https://www.mdpi.com/1999-4893/17/8/323">Thorjussen et al.&nbsp;(2024)</a> for details and <a href="https://cran.r-project.org/web/packages/CCI/readme/README.html">README</a> for examples.</p>
<p><a href="https://cran.r-project.org/package=ExactVaRTest">ExactVaRTest</a> v0.1.3: Provides fast dynamic-programming algorithms in <code>C++</code>/<code>Rcpp</code> for the exact finite-sample distributions and p-values of <a href="https://www.jstor.org/stable/2527341?origin=crossref">Christoffersen (1998)</a> independence and conditional-coverage VaR back tests. For completeness, it also provides the exact unconditional-coverage test following <a href="https://www.pm-research.com/content/iijderiv/3/2/73">Kupiec (1995)</a> via a closed-form binomial enumeration. See the <a href="https://cran.r-project.org/web/packages/ExactVaRTest/vignettes/ExactVaRTest-intro.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=propertee">propertee</a> v1.0.1: Provides functions to facilitate direct adjustment for experiments and observational studies that are compatible with a range of study designs and covariance adjustment strategies. Functions use explicit specification of clusters, blocks, and treatment allocations to furnish probability of assignment-based weights targeting any of several average treatment effect parameters. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/propertee/vignettes/intro-to-propertee.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/propertee/vignettes/RDD.html">Regression Discontinuity Design</a>.</p>
<p><a href="https://cran.r-project.org/package=senseweight">senseweight</a> v0.0.1: Provides tools to conduct interpretable sensitivity analyses for weighted estimators to evaluate the sensitivity of the underlying weighting estimators to omitted moderators or confounders. See <a href="https://academic.oup.com/jrsssa/article/187/4/900/7626119?login=false">Huang (2024)</a> and <a href="https://www.cambridge.org/core/journals/political-analysis/article/sensitivity-analysis-for-survey-weights/0A13E3843155099F169CF195B8D7604F">Hartman and Huang (2024)</a> for background. There are two vignettes <a href="https://cran.r-project.org/web/packages/senseweight/vignettes/external-validity.html">External Validity</a> and <a href="https://cran.r-project.org/web/packages/senseweight/vignettes/survey.html">Survey</a>.</p>
<p><a href="senseweight.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/senseweight.png" class="img-fluid" alt="Contour plot to visualize sensitivity"></a></p>
<p><a href="https://cran.r-project.org/package=WData">WData</a> v0.1.0: Provides functions to estimate the density and cumulative distribution functions from biased samples of continuous distributions. Includes the estimators proposed by <a href="https://www.tandfonline.com/doi/abs/10.1080/03610928808829825">Bhattacharyya et al.&nbsp;(1988)</a> and <a href="https://academic.oup.com/biomet/article-abstract/78/3/511/255888?redirectedFrom=fulltext&amp;login=false">Jones (1991)</a> for density, and <a href="https://link.springer.com/article/10.1007/s00184-021-00824-3">Bose and Dutta (2022)</a> for distributions. See <a href="https://cran.r-project.org/web/packages/WData/readme/README.html">README</a> for examples.</p>
<p><a href="WData.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/WData.png" class="img-fluid" alt="Plots of various density estimators"></a></p>
</section>
<section id="surveys" class="level3">
<h3 class="anchored" data-anchor-id="surveys">Surveys</h3>
<p><a href="https://cran.r-project.org/package=CATAcode">CATAcode</a> v1.0.0: Provides tools to assist with data transformations and subjective decisions often required on “check-all-that-apply” surveys. Includes functions for exploring response patterns, data transformations, applying a set of decision rules for coding responses, and summarizing response frequencies. See the <a href="https://cran.r-project.org/web/packages/CATAcode/vignettes/CATAcode-overview.html">vignette</a>.</p>
</section>
<section id="time-series" class="level3">
<h3 class="anchored" data-anchor-id="time-series">Time Series</h3>
<p><a href="https://cran.r-project.org/package=dsp">dsp</a> v1.2.0: Provides efficient MCMC algorithms for dynamic shrinkage processes, which extend global-local shrinkage priors to the time series setting by allowing shrinkage to depend on its own past. These priors yield locally adaptive estimates, useful for time series and regression functions with irregular features. See <a href="https://academic.oup.com/jrsssb/article/81/4/781/7048377?login=false">Kowal et al.&nbsp;(2019)</a>, <a href="https://www.tandfonline.com/doi/full/10.1080/07350015.2024.2362269">Wu et al.&nbsp;(2024)</a>, and <a href="https://arxiv.org/abs/2408.11315">Cho and Matteson (2024)</a> for background on the methods implemented and <a href="https://cran.r-project.org/web/packages/dsp/readme/README.html">README</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=unfold">unfold</a> v1.0.0: Implements an algorithm that reframes time series forecasting as a geometric learning problem through variational mapping that expands future temporal dynamics from folded high-dimensional geometric distance spaces. It turns time series into a 4D block of pairwise distances between reframed windows and maps those distances to the next reframed window to produce predictive functions. Look <a href="https://rpubs.com/giancarlo_vercellino/unfold">here</a> for the details.</p>
<p><a href="unfold.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/unfold.png" class="img-fluid" alt="schmetic of unfold algorithm"></a></p>
</section>
<section id="toxicology" class="level3">
<h3 class="anchored" data-anchor-id="toxicology">Toxicology</h3>
<p><a href="https://cran.r-project.org/package=invitroTKstats">invitroTKstats</a> v0.0.13: Provides tools for processing and analyzing in vitro toxicokinetic measurements in a standardized and reproducible pipeline. Functions perform frequentist and Bayesian estimation on a variety of in vitro toxicokinetic measurements, including chemical fraction unbound in the presence of plasma (f_up), intrinsic hepatic clearance (Clint, uL/min/million hepatocytes), and membrane permeability for oral absorption (Caco2). See <a href="https://academic.oup.com/toxsci/article/172/2/235/5571377?login=false">Wambaugh et al.&nbsp;(2019)</a> for background. There are seven vignettes, including <a href="https://cran.r-project.org/web/packages/invitroTKstats/vignettes/Data-guide-and-L0_vignette.html">Data Guide</a> and <a href="https://cran.r-project.org/web/packages/invitroTKstats/vignettes/Clint_vignette.html">Clint</a>.</p>
<p><a href="invitro.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/invitro.png" class="img-fluid" alt="Plots comparing multiple compounds"></a></p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=CBTF">CBTF</a> v0.5.0: Provides simple runner (the mechanism responsible for executing the fuzzing process) for fuzz-testing functions in an R package’s public interface. Fuzz testing helps identify functions lacking sufficient argument validation, and uncovers problematic inputs that, while valid by function signature, may cause issues within the function body. See <a href="https://cran.r-project.org/web/packages/CBTF/readme/README.html">README</a> for examples.</p>
<p><a href="https://cran.r-project.org/package=multideploy">multideploy</a> v0.1.1: Provides tools to deploy file changes across multiple GitHub repositories using the <a href="https://docs.github.com/en/rest">GitHub Web API</a>, allow synchronizing common files, and continuous Integration workflows. See the <a href="https://cran.r-project.org/web/packages/multideploy/vignettes/multideploy.html">vignette</a>.</p>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=ggdiagram">ggdiagram</a> v0.1.1: Provides object oriented functions to create diagrams with geometric objects that have computed properties with information about themselves (e.g., their area) or about their relationships with other objects (e.g, the distance between their edges). See the <a href="https://cran.r-project.org/web/packages/ggdiagram/vignettes/ggdiagram.html">vignette</a>.</p>
<p><a href="ggdiagram.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/ggdiagram.png" class="img-fluid" alt="Plotted geometric objects"></a></p>
<p><a href="https://cran.r-project.org/package=ggdiceplot">ggdiceplot</a> v0.1.0: Provides <code>ggplot2</code> extensions, including <code>geom_dice()</code> for creating dice-based visualizations where each dot position represents a specific categorical variable. Look <a href="https://github.com/maflot/ggdiceplot">here</a> for examples.</p>
<p><a href="ggdiceplot.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/ggdiceplot.png" class="img-fluid" alt="Multiple dice plots"></a></p>
<p><a href="https://cran.r-project.org/package=gridify">gridify</a> v0.7.4: Implements a tool to create enriched figures and tables by providing a way to add text around them through predefined or custom layouts. Any input such as <code>ggplot</code>, <code>gt</code>, or <code>flextable</code> which is convertible to <code>grob</code>. Functions are based <code>grid</code> graphics. See <a href="https://www.taylorfrancis.com/books/mono/10.1201/9780429422768/graphics-third-edition-paul-murrell">Murrell (2018)</a> for details. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/gridify/vignettes/gridify.html">Get Started</a> and <a href="https://cran.r-project.org/web/packages/gridify/vignettes/multi_page_examples.html">Multi-Page and Multi-Figure Examples</a>.</p>
<p><a href="gridify.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/gridify.png" class="img-fluid" alt="Template with watermark"></a></p>
<p><a href="https://cran.r-project.org/package=terminalgraphics">terminalgraphics</a> v0.1.1: Defines a graphics device and functions for graphical output in terminal emulators that support graphical output. Currently supports the <a href="https://sw.kovidgoyal.net/kitty/graphics-protocol/">Terminal Graphics Protocol</a> and <a href="https://en.wikipedia.org/wiki/Sixel">Sixel</a>. Look <a href="https://codeberg.org/djvanderlaan/terminalgraphics">here</a> for examples.</p>
<p><a href="terminal.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/terminal.png" class="img-fluid" alt="Example of terminal plot"></a></p>
<p><a href="https://cran.r-project.org/package=TheseusPlot">TheseusPlot</a> v0.1.1: Provides tools for decomposing and visualizing differences in rate metrics between two groups into contributions from individual subgroups. A Theseus plot combines the stepwise progression of a waterfall plot with the comparative bars of a bar chart, offering an intuitive way to understand subgroup-level effects. See <a href="https://cran.r-project.org/web/packages/TheseusPlot/readme/README.html">README</a> for examples.</p>
<p><a href="Theseus.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/Theseus.png" class="img-fluid" alt="Example of Theseus Plot"></a></p>
</section>
</div>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/august-2025-top-40-new-cran-packages/</guid>
  <pubDate>Mon, 29 Sep 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/august-2025-top-40-new-cran-packages/tidypopgen.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>A Note on the Dirichlet Distribution</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/note-on-dirichlet/</link>
  <description><![CDATA[ 





<p>In 1839, the gifted mathematician <a href="https://en.wikipedia.org/wiki/Peter_Gustav_Lejeune_Dirichlet">Peter Gustav Lejeune Dirichlet</a> was attached to the Philosophy department at the University of Berlin working for less than full pay even though he had become a member of the Prussian Academy of Sciences in 1832. At that time, to become a “full professor” at the university it was required that a candidate deliver a <a href="https://en.wikipedia.org/wiki/Habilitation#:~:text=Habilitation%20is%20the%20highest%20university,their%20field%20in%20its%20entirety."><em>Habilitationsschrift</em></a> lecture in Latin. Apparently, Dirichlet’s facility with Latin wasn’t up to the task, so like many proficient “adjunct professors” today, Dirichlet took a side gig to support his family. He taught math at a military school. Anyway, I digress. It was about that time that Dirichlet began to work on a problem in celestial mechanics which involved this integral:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cquad%20I%20:=%20%5Ciiint_%7BS_s%7D%20%5Cfrac%7Bdx%5C,%20dy%5C,%20dz%7D%7B%5Crho%5Ep%7D"></p>
<p>Here <img src="https://latex.codecogs.com/png.latex?(x,y,z)%20%5Cin%20R%5E3"> which is attracted to a point <img src="https://latex.codecogs.com/png.latex?(a,b,c)"> where <img src="https://latex.codecogs.com/png.latex?1/%5Crho%5Ep"> is the force of attraction <img src="https://latex.codecogs.com/png.latex?(1%20%3C%20p%20%3C3)"> and <img src="https://latex.codecogs.com/png.latex?%5Crho%5E2%20:=%20(x%20-%20a)%5E2%20+%20(y%20-%20b)%5E2%20+%20(z%20-%20c)%5E2"> is the Euclidean norm.</p>
<p>After a supernaturally insightful series manipulations detailed by <a href="https://www.jstor.org/stable/1403524">Gupta and Richards</a>, Dirichlet arrived at the following integral</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cint_%7BS_s%7D%20%5Cleft(1%20-%20%5Csum_%7Bi=1%7D%5En%20x_i%5E2%20%5Cright)%5E%7B%5Calpha%20-%201%7D%20x_1%5E%7Bv_1%7D%20x_2%5E%7Bv_2%7D%20%5Ccdots%20x_n%5E%7Bv_n%7D%20%5C,%20dx_1%20dx_2%20%5Ccdots%20dx_n%20=%20%5Cfrac%7B%5CGamma(%5Calpha)%20%5CGamma%5Cleft(%5Cfrac%7B1%20+%20v_1%7D%7B2%7D%5Cright)%20%5Ccdots%20%5CGamma%5Cleft(%5Cfrac%7B1%20+%20v_n%7D%7B2%7D%5Cright)%7D%7B%5CGamma%5Cleft(%5Calpha%20+%20%5Cfrac%7Bn%20+%20v_1%20+%20%5Ccdots%20+%20v_n%7D%7B2%7D%5Cright)%7D"> which you will recognize as the Beta function, the normalizing constant for the <a href="https://en.wikipedia.org/wiki/Dirichlet_distribution">Dirichlet distribution</a>:</p>
<p><img src="https://latex.codecogs.com/png.latex?f(x_1,%20%5Cdots,%20x_K;%20%5Calpha_1,%20%5Cdots,%20%5Calpha_K)%20=%0A%5Cfrac%7B1%7D%7B%5Cmathrm%7BB%7D(%5Cboldsymbol%7B%5Calpha%7D)%7D%0A%5Cprod_%7Bi=1%7D%5E%7BK%7D%20x_i%5E%7B%5Calpha_i%20-%201%7D"></p>
<p>with mean <img src="https://latex.codecogs.com/png.latex?E%5BX_i%5D%20=%20%5Calpha_i%20/%20%5Calpha_0"> and variance <img src="https://latex.codecogs.com/png.latex?Var%5BX_i%5D%20=%20%5Cfrac%7B%5Calpha_i%20(%5Calpha_0%20-%20%5Calpha_i)%7D%7B%5Calpha_0%5E2%20(%5Calpha_0%20+%201)%7D"></p>
<p>where</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BB%7D(%5Cboldsymbol%7B%5Calpha%7D)%20=%0A%5Cfrac%7B%5Cprod_%7Bi=1%7D%5E%7BK%7D%20%5CGamma(%5Calpha_i)%7D%7B%5CGamma%5Cleft(%5Csum_%7Bi=1%7D%5E%7BK%7D%20%5Calpha_i%5Cright)%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20=%20%5Csum_%7Bi=1%7D%5E%7BK%7D%20%5Calpha_i"></p>
<p>The Dirichlet distribution is a multivariate generalization of the Beta distribution that is often used in Bayesian statistics as a prior distribution for categorical and multinomial distributions. I illustrated this use of the Dirichlet in a <a href="https://rworks.dev/posts/simple-bayesian-model/">previous post</a> while constructing a Bayesian model for a three-state Markov chain. The Dirichlet distribution is remarkable in that it brings together 18th and 19th century work in analysis as exemplified by the Gamma, Beta and digamma functions with early 20th ideas from geometry and topology (the simplex) and modern Bayesian statistics.</p>
<section id="the-2-simplex" class="level2">
<h2 class="anchored" data-anchor-id="the-2-simplex">The (2)-Simplex</h2>
<p>A simplex is a generalization of the notion of a triangle to multiple dimensions. Informally in K dimensions, a simplex is the simplest polygon that is the convex hull of its K vertices. The vectors that comprise the simplex must be non-negative and sum to 1. So, a simplex is a natural way to represent probabilities that sum to 1 in multidimensional spaces.</p>
<p>The support for the three dimensional Dirichlet distribution, the points on which the distribution is defined, is a (2)-simplex the triangular subset of a 2-dimensional plane intersecting the Euclidean axes at the points (1,0,0), (0,1,0), and (0,0,1). (Orient the triangle in the interactive plot below so that the reference plane is on top and the tip is pointing downward and you will see how the axes line up.)</p>
<div class="cell">
<details class="code-fold">
<summary>R packages used in this post</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gganimate)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magick)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MCMCpack)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for rdirichlet</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gtools)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for ddirichlet</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#library(patchwork)  # for combining plots</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(threejs)</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(extraDistr)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample from Dirichlet distribution over 3 categories</span></span>
<span id="cb2-4">n_samples <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span></span>
<span id="cb2-5">alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># uniform prior over the simplex</span></span>
<span id="cb2-6">samples <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rdirichlet</span>(n_samples, alpha)</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3D coordinates: each row is (x, y, z)</span></span>
<span id="cb2-9">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> samples[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-10">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> samples[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb2-11">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> samples[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualize using threejs scatterplot</span></span>
<span id="cb2-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scatterplot3js</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> z,</span>
<span id="cb2-15">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>,</span>
<span id="cb2-16">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span id="cb2-17">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,</span>
<span id="cb2-18">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2-simplex"</span>,</span>
<span id="cb2-19">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axisLabels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb2-20">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(1,0,0)"</span>,</span>
<span id="cb2-21">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(0,1,0)"</span>,</span>
<span id="cb2-22">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(0,0,1)"</span></span>
<span id="cb2-23">               ))</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div class="scatterplotThree html-widget html-fill-item" id="XIG7ifLcvz" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="XIG7ifLcvz">{"x":{"NROW":2000,"height":null,"width":null,"axis":true,"numticks":[6,6,6],"xticklabs":null,"yticklabs":null,"zticklabs":null,"color":["#4682B4"],"size":0.2,"stroke":"black","flipy":true,"grid":true,"renderer":"auto","signif":8,"bg":"black","cexsymbols":1,"xlim":{},"ylim":{},"zlim":{},"axisscale":[1,1,1],"pch":["@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"],"elementId":"XIG7ifLcvz","main":"2-simplex","axisLabels":["(1,0,0)","(0,1,0)","(0,0,1)"],"linealpha":1,"alpha":[1],"vertices":[[0.74852076,0.20431042,0.9315419,0.041116693,0.2442024,0.27461166,0.88001421,0.0077791566,0.86577691,0.52541355,0.20042349,0.70799311,0.80031811,0.17164132,0.95009035,0.47481857,0.13164111,0.58984222,0.26644663,0.65785121,0.90742467,0.8371965099999999,0.018829898,0.83459384,0.10061888,0.44860768,0.53626702,0.80436306,0.13813046,0.92078212,0.055991504,0.32270948,0.36725087,0.24578121,0.73859884,0.96729429,0.46347188,0.31024619,0.75610364,0.44413925,0.34774077,0.7743161200000001,0.10850749,0.60380455,0.69821414,0.04023093,0.73689047,0.76319759,0.95817174,0.049921579,0.98460256,0.25434343,0.47880733,0.71763688,0.01682253,0.4247364,0.43004035,0.046006264,0.3890124,0.42328694,0.14290015,0.12216051,0.25359313,0.22456714,0.77695684,0.98451193,0.61661812,0.33578213,0.93227092,0.25838985,0.72160455,0.9628267700000001,0.12846696,0.13939172,0.25649943,0.11287451,0.31543469,0.41603485,0.035380909,0.84116718,0.86201501,0.89789771,0.06450541899999999,0.9397406699999999,0.026270332,0.27419678,0.28979061,0.0077098344,0.76869591,0.76277192,1,0.01185203,0.98796969,0.39233771,0.12071678,0.49777294,0.61992166,0.29840963,0.89839634,0.090354332,0.86106178,0.93590995,0.00041821472,0.96235532,0.94798189,0.2736226,0.22876574,0.48821816,0.38478111,0.1959177,0.5650401,0.69610239,0.14383371,0.8198467699999999,0.6008483,0.098525297,0.6810414,0.033972619,0.34840654,0.3710981,0.18651237,0.14913155,0.32333106,0.62519307,0.3400201,0.94492462,0.40443572,0.57982782,0.96578673,0.15234393,0.63008616,0.76748795,0.03490844,0.18211366,0.20681687,0.16911248,0.51227371,0.6669593700000001,0.043816554,0.53880531,0.5699416899999999,0.38345844,0.34478496,0.7116290199999999,0.71090009,0.0049556525,0.69645005,0.51604402,0.11261864,0.61153798,0.42084146,0.40909033,0.81232282,0.091672276,0.14339526,0.22424604,0.40277987,0.37335328,0.75903532,0.66101179,0.011778324,0.6541044,0.80884763,0.21342613,1,0.6933184100000001,0.035928245,0.70990741,0.43290641,0.010534809,0.42826015,0.13200593,0.14226317,0.26283677,0.23825865,0.0066129739,0.2326999,0.7688406,0.21462073,0.96179299,0.36597145,0.1519686,0.50285777,0.32251849,0.66659233,0.97132085,0.2363489,0.025776717,0.24985754,0.16680181,0.050238984,0.20567839,0.15382016,0.6421200500000001,0.78089655,0.11921052,0.050250951,0.15882849,0.56297869,0.26305674,0.80720508,0.18729217,0.46274008,0.6356515,0.45316598,0.16296463,0.5996397,0.66333351,0.02061696,0.66517122,0.65705936,0.35611037,0.99228724,0.10383997,0.49804234,0.58854932,0.51578148,0.24807939,0.74585217,0.43025833,0.26752324,0.68095633,0.33174562,0.28986887,0.6061528,0.07151805999999999,0.74012269,0.79721617,0.25303391,0.74645371,0.98223918,0.58113836,0.22156592,0.78386757,0.45658666,0.27553956,0.71484486,0.34970587,0.52693999,0.85935468,0.085772268,0.72523162,0.79645844,0.62511289,0.24853531,0.85396064,0.51104055,0.38513614,0.87734214,0.31574493,0.53191065,0.83085239,0.41826994,0.0044213171,0.40777462,0.5703842099999999,0.22740237,0.77907646,0.89274708,0.007375258,0.87791336,0.24946039,0.52605547,0.75976709,0.78490137,0.17194955,0.93521614,0.020376285,0.91487109,0.9204611,0.44275604,0.13541901,0.56202428,0.44427072,0.47315105,0.8990337,0.66317747,0.090935382,0.73487501,0.74945107,0.24141116,0.96931547,0.01579312,0.67237349,0.67504035,0.47383494,0.28271242,0.73895458,0.23612272,0.43453837,0.65571673,0.092411751,0.34170284,0.42198169,0.17796003,0.83261231,0.99390983,0.30498955,0.51008555,0.79857985,0.51755397,0.25012559,0.74963028,0.35044525,0.42380917,0.75762802,0.29534096,0.44342635,0.72285696,0.83437242,0.15514403,0.96723352,0.39193961,0.58779789,0.96139998,0.22308885,0.070038037,0.28077189,0.9300947000000001,0.072801742,0.97968606,0.56778976,0.30161443,0.85024732,0.67002127,0.038358959,0.68938216,0.020824738,0.55734347,0.56571893,0.045986266,0.45740985,0.4912163,0.21120018,0.1559996,0.35446348,0.092470205,0.18622818,0.26758385,0.82411682,0.049082563,0.85176897,0.020142755,0.12238514,0.13294057,0.32515227,0.45669872,0.76539666,0.57832365,0.31234978,0.87128471,0.23381965,0.29557227,0.51539388,0.092214851,0.68771745,0.76553403,0.13548797,0.49477772,0.6164689800000001,0.07702647899999999,0.77657056,0.83884908,0.81981838,0.17678055,0.9743972400000001,0.1359205,0.50322322,0.62528501,0.60797425,0.14432339,0.73355598,0.15712103,0.5261717,0.6689586199999999,0.65235239,0.039331089,0.67294987,0.44373922,0.4301728,0.85581384,0.51843244,0.050423141,0.55210203,0.87594753,0.058095996,0.91175954,0.60684611,0.08596796800000001,0.67447226,0.20248347,0.35776407,0.54632213,0.1266028,0.41685409,0.53030722,0.53705977,0.056434183,0.57641547,0.37384578,0.32083958,0.6783753,0.5174084,0.37758308,0.87610884,0.030408259,0.53267282,0.55064664,0.23802901,0.10449481,0.32971393,0.21613499,0.65968889,0.85970986,0.53104457,0.11057966,0.62428299,0.45556409,0.31526995,0.75330789,0.37111247,0.51163285,0.86522637,0.74214409,0.072933888,0.79474772,0.16092756,0.11109391,0.26035015,0.44661191,0.31912831,0.74832599,0.14731028,0.2593443,0.39422009,0.61602726,0.29118071,0.8873801,0.039653937,0.35282467,0.38108149,0.30947219,0.48729735,0.78035497,0.9282872599999999,0.052143927,0.95738394,0.1331886,0.38570955,0.50585171,0.034737825,0.27721121,0.30112298,0.30104997,0.16179155,0.44868999,0.6380246000000001,0.29548054,0.9133119,0.28754834,0.5389997,0.81013058,0.09026920099999999,0.1099037,0.18959247,0.4416871,0.16380106,0.58916771,0.57900599,0.21409009,0.77434107,0.37082048,0.088904786,0.4449821,0.58739349,0.21645013,0.78494458,0.47339395,0.48901058,0.94346607,0.17267473,0.17873207,0.339112,0.050840595,0.7327617,0.76954292,0.011585499,0.06641693799999999,0.068913191,0.21851447,0.54712331,0.75022524,0.27779152,0.45020013,0.7123059,0.27024377,0.64135627,0.8947768,0.060573219,0.7219052,0.76834103,0.023628616,0.02174668,0.036394297,0.46661218,0.27948557,0.72863685,0.79917132,0.014863943,0.79321156,0.11697142,0.013227631,0.1198431,0.023222975,0.28680572,0.29931625,0.40364195,0.21110302,0.59869759,0.09803400900000001,0.53976201,0.62427851,0.53829813,0.17127001,0.69171785,0.30439502,0.55082642,0.83846822,0.68031133,0.0072471372,0.66860663,0.47779596,0.31611633,0.77603981,0.7784147,0.14669669,0.90374158,0.34193457,0.52550841,0.85028032,0.023790559,0.21921591,0.23272842,0.19930546,0.46757344,0.65228232,0.36302818,0.18097143,0.52877232,0.39459255,0.26773055,0.6460432,0.5908622100000001,0.040953919,0.61401443,0.8265898,0.08632169200000001,0.8911990400000001,0.0369969,0.034504301,0.062231652,0.51545749,0.30065578,0.79776485,0.0198033,0.15743446,0.16742586,0.18768721,0.03120802,0.20733745,0.28113984,0.47506271,0.74030247,0.17755095,0.21023851,0.37521337,0.1394423,0.71928525,0.8433984,0.089879524,0.5836179,0.6598174,0.059057511,0.80012932,0.8445598600000001,0.034206023,0.7349964,0.75538338,0.21358802,0.44604904,0.64496266,0.17788062,0.70456392,0.86662271,0.25210323,0.16354558,0.40223606,0.059382774,0.32007299,0.36797095,0.18733533,0.77354748,0.94446381,0.065139082,0.57831073,0.63018381,0.34473209,0.30777408,0.63672803,0.6833669999999999,0.033645999,0.69784125,0.56460107,0.020659678,0.5679946299999999,0.34013777,0.52896159,0.85194159,0.4072701,0.15437338,0.5459124,0.44844349,0.19014628,0.62199304,0.7299285599999999,0.027664311,0.73774663,0.036142627,0.8858477300000001,0.90715268,0.34238256,0.29653626,0.62325038,0.091445727,0.84193924,0.91798744,0.85236367,0.13167935,0.96163814,0.11956048,0.70117201,0.80582684,0.95206095,0.020136712,0.94899583,0.88819556,0.12089049,0.98620265,0.74083328,0.19404403,0.91377316,0.43998201,0.29334245,0.7161809099999999,0.47870628,0.5142380600000001,0.97375906,0.33570877,0.56389336,0.88228326,0.53851255,0.067620523,0.58895898,0.07201697,0.1442303,0.20572158,0.064884468,0.59933546,0.65082,0.21583127,0.36184213,0.56351665,0.23432807,0.56504203,0.78359769,0.034546126,0.48523945,0.50759867,0.8136385699999999,0.12492439,0.91679597,0.3254093,0.44734013,0.75635252,0.029665522,0.11994921,0.13989741,0.81433203,0.17206207,0.96430745,0.5449371,0.47494298,0.9999372,0.23416869,0.44746095,0.6666305,0.047965256,0.51951007,0.55485806,0.41157218,0.051616752,0.44806556,0.24418167,0.22918594,0.45964595,0.43963799,0.04506014,0.46918754,0.34742366,0.16661658,0.49914624,0.09278374,0.074205178,0.15660398,0.6269674,0.38529846,0.99165328,0.49368837,0.39048055,0.86556532,0.77650729,0.22325465,0.97791947,0.27895517,0.30738697,0.5715748,0.57532077,0.24074768,0.79719516,0.59213624,0.25533519,0.82824476,0.33796307,0.30136628,0.62369698,0.59132658,0.058680176,0.63208172,0.08631955099999999,0.49980259,0.57304616,0.19192925,0.1089507,0.28874749,0.74747887,0.087277305,0.8142501,0.34857478,0.55275251,0.88388422,0.35709655,0.21995665,0.56166125,0.50998344,0.21723855,0.70950434,0.2719237,0.38017128,0.63695826,0.31160242,0.53764237,0.83246751,0.03044697,0.6685511200000001,0.68567227,0.67220033,0.33869365,0.9898935800000001,0.23575537,0.40027472,0.6213159700000001,0.6426539100000001,0.048509756,0.67251854,0.37667287,0.43112511,0.79072158,0.12237483,0.14908724,0.26013263,0.09722380899999999,0.47290921,0.5570662,0.60233496,0.21950969,0.80269652,0.07276571699999999,0.64662514,0.70556011,0.30194405,0.43551474,0.72149909,0.63851186,0.37795526,0.99572572,0.22665511,0.54385634,0.75499554,0.78174534,0.15369828,0.91397686,0.09038678999999999,0.74764277,0.82326645,0.4699873,0.433159,0.88462621,0.30033718,0.21669536,0.502532,0.047610578,0.53387741,0.56878197,0.099980547,0.0061455463,0.096077019,0.49174135,0.41833718,0.89132215,0.4447924,0.31385656,0.74129718,0.41325707,0.35515888,0.7512768,0.49109774,0.13724661,0.6114406,0.47473354,0.37335336,0.82988614,0.18563158,0.57191355,0.74247411,0.1288399,0.40305129,0.51879772,0.46865553,0.20196208,0.65363361,0.82132011,0.19194176,0.99093778,0.2163186,0.13114076,0.33480757,0.66595437,0.15207665,0.79834978,0.5558289,0.24903346,0.78623354,0.12597561,0.50025005,0.6125388899999999,0.48625104,0.31609258,0.78434169,0.80581028,0.032158441,0.8169298699999999,0.45398387,0.13407276,0.5717425900000001,0.16896219,0.1483114,0.30523513,0.69003662,0.0086418893,0.67956845,0.044379301,0.76021817,0.79045714,0.10251971,0.53136212,0.62035063,0.55869876,0.45591745,0.99458712,0.24290298,0.082214645,0.31237909,0.6198313600000001,0.25111173,0.85131959,0.37254623,0.49552162,0.85063255,0.37154375,0.41438979,0.7690454799999999,0.1657826,0.36030632,0.51270941,0.23958562,0.30449546,0.52993618,0.30135235,0.36787973,0.65372485,0.14524162,0.62424118,0.75468785,0.05577713,0.13035046,0.17594183,0.17947924,0.82564236,0.9884815,0.92012783,0.022530678,0.9199304699999999,0.19832681,0.65528624,0.83780086,0.90413951,0.099740035,0.98089042,0.18229062,0.75345681,0.91953746,0.067345074,0.07654544200000001,0.13388017,0.90022969,0.040025452,0.91771742,0.13004387,0.4659354,0.5824551,0.40568781,0.18003916,0.56985188,0.16562738,0.013198542,0.16772435,0.11200624,0.47087417,0.56960034,0.71190256,0.092336139,0.78424479,0.28066393,0.44924716,0.7141875600000001,0.16422466,0.23666426,0.38834386,0.45087961,0.20163548,0.6358057,0.41864675,0.4449116,0.84574818,0.26701243,0.56296705,0.81371967,0.09178354499999999,0.6097575,0.68766047,0.11311414,0.32264118,0.42343006,0.13038319,0.34146107,0.4591309,0.075921564,0.90747705,0.9678094,0.29151508,0.09192199400000001,0.36988976,0.036746142,0.97156014,0.99289744,0.13712048,0.38039696,0.50444556,0.7354449199999999,0.14842018,0.86314266,0.28594013,0.087449843,0.35995742,0.075985552,0.8578081400000001,0.91852912,0.009054894900000001,0.73490656,0.73052853,0.27180869,0.32685036,0.58387366,0.33294323,0.57510032,0.8906936,0.30046121,0.20643068,0.49245675,0.54696213,0.086218761,0.61575537,0.89348214,0.11796914,0.98850601,0.55521701,0.052757971,0.59064228,0.58791072,0.26413175,0.83282291,0.76099501,0.10722346,0.8473744600000001,0.11926047,0.068100269,0.17660997,0.29116416,0.5940657499999999,0.86839602,0.31503573,0.65544897,0.95288249,0.10793767,0.49011739,0.5847112,0.31763656,0.015851192,0.32003873,0.44141663,0.57748929,0.99987757,0.022716227,0.17544905,0.18819062,0.20838917,0.7285825500000001,0.92052477,0.45645504,0.29040789,0.72948611,0.46730515,0.12361779,0.57447324,0.077389139,0.020853827,0.088443758,0.6033792,0.37505784,0.95825316,0.11994684,0.59582127,0.7015472,0.47135197,0.46355301,0.91616472,0.8208981,0.18510862,0.98373389,0.43856445,0.52257569,0.9425155200000001,0.48937939,0.18037816,0.65259738,0.13408112,0.42100939,0.54179897,0.036561371,0.61879235,0.64226039,0.02778399,0.77286828,0.78668339,0.90689166,0.02049151,0.9048713900000001,0.18128069,0.59038786,0.75654311,0.1884734,0.55059846,0.72409702,0.28522076,0.69225946,0.9600937,0.1723311,0.42370826,0.58214382,0.3867811,0.14128221,0.51273211,0.34543945,0.095912763,0.42695215,0.035856042,0.068088386,0.09447219699999999,0.0047071774,0.0038665678,0,0.79345724,0.15906898,0.93084473,0.68493042,0.093180153,0.75852457,0.34479143,0.63016651,0.95706536,0.44392399,0.52636269,0.9515550699999999,0.46750724,0.34115687,0.79078517,0.73266094,0.047007182,0.75965319,0.67234851,0.24534993,0.89730772,0.30807928,0.32180686,0.61457784,0.19840605,0.43057366,0.61463948,0.023573882,0.68143711,0.69170605,0.49511571,0.14446442,0.62256748,0.016841158,0.26872838,0.27507342,0.33041072,0.59126894,0.90426252,0.8193209299999999,0.20044395,0.99741569,0.14690382,0.063410968,0.19917103,0.28861448,0.079453858,0.35464721,0.044813894,0.12205832,0.15690886,0.81139105,0.11446008,0.90418718,0.76986286,0.14219712,0.89085077,0.41606681,0.183491,0.583501,0.5890976999999999,0.07506766300000001,0.64616707,0.14502787,0.32797913,0.46015755,0.38595952,0.51325558,0.8814579299999999,0.029328574,0.48148514,0.4987314,0.3919648,0.5685741600000001,0.94232708,0.15507304,0.08398238,0.22765158,0.1760057,0.58600337,0.74699324,0.21333078,0.23328286,0.43333803,0.095124628,0.050108944,0.13497072,0.6686917,0.00033438645,0.65029768,0.35538149,0.33484036,0.67410304,0.37128269,0.067204901,0.4238796,0.75062991,0.086650173,0.81672983,0.23676385,0.58247848,0.8033182800000001,0.15908389,0.40259386,0.5481236900000001,0.17407505,0.49865371,0.65831516,0.047428806,0.18553718,0.22254639,0.20561867,0.56679434,0.7570691899999999,0.61762966,0.069813123,0.66904158,0.84404334,0.10324206,0.92519449,0.44418809,0.37470269,0.80114937,0.59026769,0.33950613,0.91002399,0.10469509,0.14588851,0.23954614,0.36733932,0.34091747,0.69191484,0.39532324,0.16829485,0.54797887,0.35788253,0.55767746,0.89794197,0.67674562,0.066058861,0.72352175,0.095266462,0.48274044,0.56490564,0.5673199799999999,0.19893239,0.74777586,0.019227831,0.81289402,0.81802172,0.4287121,0.21727793,0.62951789,0.091658875,0.50381575,0.58229049,0.23193082,0.78228388,0.99705484,0.36825237,0.22400247,0.57666537,0.45319328,0.08092198,0.51816178,0.35719298,0.56829814,0.90781404,0.65978675,0.10811994,0.74860816,0.59446872,0.36660829,0.94108512,0.031953647,0.3275424,0.3483827,0.96746484,0.011937356,0.956018,0.065598031,0.58434064,0.63662611,0.026995044,0.76057328,0.77369214,0.07776583500000001,0.8597655400000001,0.92222667,0.36031379,0.1919966,0.53705243,0.12926531,0.36335068,0.47977627,0.028244995,0.747287,0.76172375,0.33906302,0.30748778,0.63086144,0.09728611700000001,0.79635388,0.8784518,0.013151403,0.9229893300000001,0.92141199,0.68271063,0.2211655,0.88348512,0.45423256,0.47327305,0.90896403,0.55140134,0.090636627,0.62451543,0.16267898,0.37243396,0.52170149,0.12409098,0.36771064,0.47901263,0.28844415,0.27670759,0.5504400699999999,0.30847838,0.67523093,0.96607792,0.22628918,0.55472467,0.7654323,0.29307106,0.4293573,0.70664503,0.06557513299999999,0.37849186,0.43210428,0.49281458,0.45332253,0.92713493,0.06716117100000001,0.67843163,0.73163944,0.713956,0.034757955,0.72906604,0.36956311,0.22380255,0.57775741,0.85285512,0.11040085,0.94098306,0.0043100529,0.13450069,0.12938668,0.12992673,0.0070254944,0.12643835,0.48980461,0.00964956,0.48340675,0.20203468,0.79379389,0.97905152,0.43207525,0.39461062,0.80899963,0.056100724,0.86297682,0.90408388,0.14074229,0.6554111500000001,0.78122311,0.10518244,0.11280792,0.20716229,0.63779737,0.30408147,0.9216327,0.25959702,0.10470341,0.35115857,0.100931,0.46854161,0.5563776,0.23123958,0.5348201,0.75053274,0.28851603,0.30396583,0.5775903999999999,0.39807022,0.56764105,0.94741193,0.18724398,0.09676918,0.27203236,0.046418245,0.94260805,0.9736590000000001,0.30863004,0.49284935,0.78504133,0.16053072,0.35527126,0.5025359700000001,0.16522107,0.38064736,0.53236417,0.12677472,0.52151504,0.63445132,0.58925132,0.093441,0.66457122,0.072714346,0.29772735,0.35889903,0.47613829,0.49321985,0.95035001,0.026202208,0.7909836099999999,0.80312242,0.1474386,0.052535403,0.18889335,0.088037396,0.2698082,0.34625108,0.009317744500000001,1,0.9941429000000001,0.31137081,0.22624408,0.52288262,0.67180638,0.20723923,0.85891304,0.72780969,0.23147456,0.93813435,0.33584179,0.46270923,0.78189343,0.26872856,0.49857747,0.75144204,0.16139916,0.33205272,0.48032479,0.082690917,0.13192528,0.20400756,0.06892435,0.24560032,0.30338183,0.059811791,0.029270385,0.07949722300000001,0.095104829,0.057150693,0.14194681,0.2239598,0.62245564,0.8304255699999999,0.069187211,0.33503202,0.39248606,0.33709579,0.66482545,0.98391941,0.31383555,0.64492101,0.94124176,0.84001568,0.011452817,0.83004107,0.3508131,0.4518359,0.78583322,0.7716661500000001,0.18307696,0.93323824,0.28245558,0.13257033,0.40135097,0.18289449,0.023601978,0.19506201,0.36425244,0.28231731,0.63065929,0.012897925,0.12238185,0.12580352,0.28161399,0.2186734,0.48606087,0.22654733,0.44662518,0.65829568,0.15140293,0.25265153,0.39160112,0.32500444,0.23793854,0.54792507,0.25474404,0.12433707,0.36588492,0.198982,0.43621236,0.62080832,0.58003729,0.14566014,0.70737522,0.56925442,0.074660976,0.62622393,0.04332596,0.53505285,0.56573076,0.18772335,0.14776162,0.32316254,0.34716461,0.25332501,0.58503116,0.16768986,0.29125613,0.44598992,0.51471976,0.12822493,0.625738,0.38482247,0.26653841,0.63523856,0.62172039,0.33733478,0.93883745,0.86793079,0.15125944,0.99641838,0.024685657,0.64925802,0.66083265,0.65673528,0.050830765,0.68868987,0.33348597,0.5237918499999999,0.84025591,0.12781172,0.40233683,0.5170755299999999,0.053284095,0.27172044,0.31393019,0.54491262,0.14121476,0.66837272,0.20160625,0.011380551,0.20134565,0.64534616,0.19204579,0.81776457,0.18599793,0.23775835,0.41087029,0.051316353,0.46736371,0.50635329,0.065843745,0.5395672,0.59238813,0.73785797,0.15451192,0.87157052,0.067026874,0.82611918,0.87822654,0.27276556,0.69623929,0.95178316,0.13303506,0.33601053,0.45632732,0.45364294,0.24536337,0.68196789,0.03042776,0.10914469,0.12991426,0.25725841,0.21198272,0.45543181,0.11301547,0.68188002,0.78021665,0.19509071,0.65494199,0.83427238,0.31920903,0.39699479,0.700232,0.41724807,0.2095669,0.6105691,0.0063858196,0.60118218,0.59505266,0.18071302,0.42836843,0.59502688,0.07369729999999999,0.3895541,0.45109166,0.77935255,0.16701285,0.92484803,0.19010937,0.2465018,0.42360485,0.16527571,0.82883417,0.97766657,0.24566931,0.50346812,0.73359484,0.19934335,0.55112303,0.73532148,0.0070597498,0.20077317,0.19793224,0.43947475,0.21210016,0.63497174,0.60595774,0.055382614,0.64321267,0.13634354,0.044608015,0.17009294,0.1350084,0.58768295,0.70829292,0.23567958,0.58082487,0.80060785,0.45073101,0.49164514,0.92376781,0.36645249,0.6391470299999999,0.98731604,0.061436449,0.16426536,0.21520695,0.41294731,0.34263476,0.73852977,0.36392984,0.64597722,0.99161747,0.0065770794,0.82884013,0.8214064599999999,0.08719228900000001,0.79836133,0.87050697,0.7950223199999999,0.17812653,0.95131843,0.24551668,0.61193541,0.8412007500000001,0.93294598,0.07848050299999999,0.98813517,0.06906620099999999,0.91314184,0.96668676,0.027090178,0.82492608,0.83771673,0.62254749,0.13664886,0.74028161,0.75082746,0.14928389,0.87914746,0.6359204000000001,0.19388632,0.81031175,0.027289333,0.48763118,0.50282917,0.63586728,0.031173815,0.64861365,0.16246107,0.38152018,0.53051358,0.23109746,0.12250304,0.3407788,0.15754486,0.65395246,0.796319,0.10769603,0.2669987,0.36281729,0.0091039164,0.57790003,0.5745995699999999,0.63975498,0.060030721,0.68110949,0.59620946,0.12977451,0.70751802,0.7957607,0.12864554,0.90288888,0.36373588,0.20849539,0.55681269,0.20104536,0.71047355,0.89530326,0.0059873272,0.78888686,0.7811344,0.19828921,0.29078686,0.47565404,0.2912118,0.047871545,0.32582946,0.00033907815,0.94554053,0.9311994099999999,0.78731088,0.09836600299999999,0.86448755,0.43966085,0.15405563,0.57749099,0.59231947,0.09577854399999999,0.66991455,0.5240526599999999,0.48561492,0.98997486,0.51844682,0.26435633,0.76464688,0.21114872,0.1569444,0.35535141,0.19088275,0.66829939,0.84339875,0.83087804,0.07615482899999999,0.88532134,0.66220296,0.16126636,0.80378534,0.17475927,0.032654029,0.1960442,0.66269646,0.29422582,0.9363590899999999,0.11751884,0.6999668100000001,0.80261921,0.86258021,0.12607157,0.96612707,0.15051201,0.09538181499999999,0.23448517,0.43706515,0.52257093,0.94103447,0.67938163,0.25729129,0.9160961,0.013453733,0.29209634,0.29495268,0.036214843,0.61080618,0.6339853600000001,0.21998894,0.7556294099999999,0.95881629,0.44959842,0.55509631,0.98568778,0.57890282,0.28624875,0.84592509,0.043711721,0.01435995,0.048831256,0.45718757,0.16270871,0.60334541,0.25524148,0.25005857,0.49127204,0.6666121699999999,0.29343224,0.9394264,0.7723446,0.15419101,0.9052097099999999,0.53870126,0.23784802,0.75825632,0.58865498,0.12598225,0.69631194,0.79692107,0.14037415,0.9156832,0.49110857,0.21472546,0.68842218,0.22530085,0.5157623,0.72575219,0.87264915,0.10568811,0.95579183,0.23161145,0.019996702,0.23945058,0.16155986,0.36893155,0.51712007,0.51118358,0.13087805,0.62489176,0.09264109099999999,0.56272492,0.64178063,0.42264584,0.13361193,0.54042711,0.28021837,0.31355781,0.57894902,0.16401337,0.5638258,0.7131525399999999,0.10497691,0.7561211,0.84605568,0.033926001,0.1177508,0.14190857,0.11075896,0.18854463,0.28789353,0.32859165,0.46551937,0.77754614,0.34154563,0.14712043,0.47398997,0.37965445,0.10096821,0.46566499,0.051857011,0.094403555,0.13637055,0.56773418,0.17322556,0.72264542,0.39335212,0.39566105,0.7719136,0.07757610400000001,0.46580786,0.53066487,0.35548958,0.38771882,0.72674127,0.11018152,0.36167047,0.45931581,0.56218754,0.10801159,0.65239735,0.069231775,0.33812463,0.39560228,0.011889527,0.29027026,0.29159835,0.57687738,0.078818376,0.63786019,0.41041431,0.4857879,0.87825025,0.49078841,0.48773212,0.95932382,0.4398097,0.051219027,0.47547512,0.5473218,0.28958948,0.81814698,0.48704391,0.41740031,0.88576599,0.06693563,0.5961038,0.64962925,0.54315819,0.27681659,0.80135804,0.0017966256,0.97748642,0.96437108,0.82332492,0.036682775,0.83867071,0.78153241,0.16621734,0.92620417,0.37285386,0.54923934,0.90430101,0.14710221,0.47125385,0.60453554,0.35503134,0.5967369300000001,0.93393789,0.049045569,0.9365761,0.97025365,0.074164247,0.62015868,0.68064424,0.67209508,0.34271147,0.99378143,0.37018124,0.18738419,0.54218645,0.49783447,0.50165942,0.98009787,0.016211845,0.23597666,0.24191675,0.34989675,0.58063413,0.91288476,0.96039474,0.050704587,0.98756936,0.20187656,0.23176085,0.42054736,0.35644669,0.37926015,0.7192805,0.055752679,0.34764873,0.39179146,0.25022583,0.76278054,0.99569394,0.5028338999999999,0.36636764,0.85061583,0.37967177,0.20299021,0.56703521,0.031866907,0.82426004,0.84175857,0.25027326,0.043524891,0.28120028,0.2576871,0.47499842,0.71714535,0.55426303,0.4316274,0.96608856,0.1227189,0.44329951,0.55275491,0.19476645,0.77379665,0.95202856,0.62843355,0.29001944,0.89844256,0.08502795,0.27472961,0.34817691,0.041382337,0.67403106,0.70188403,0.20837183,0.32845985,0.52300812,0.91705649,0.09531086800000001,0.98920927,0.66225061,0.08007781999999999,0.72317597,0.10030074,0.56411176,0.65070062,0.13677916,0.7998192200000001,0.9207821,0.062031318,0.86700079,0.91392115,0.6822762,0.0055644801,0.66886976,0.40975655,0.55175048,0.94313273,0.39951043,0.59360819,0.97462696,0.3303027,0.54646625,0.85964719,0.26939172,0.58490648,0.8378581000000001,0.30761289,0.34051572,0.63270481,0.41021349,0.2235544,0.61753815,0.42738566,0.06176431,0.47371768,0.1114108,0.40665842,0.5052192599999999,0.73776023,0.27531262,0.99148304,0.31336682,0.04689052,0.34667028,0.015436692,0.54131702,0.54449209,0.47555575,0.35807901,0.81552152,0.27887603,0.53406013,0.79668401,0.41120519,0.095170641,0.49097255,0.099850895,0.77501553,0.85977879,0.47442724,0.047234708,0.50560383,0.020247576,0.55231233,0.56015246,0.57018747,0.23647015,0.78789107,0.18612763,0.15144711,0.3252526,0.46629128,0.38165665,0.82982213,0.59770431,0.22479297,0.80338549,0.024193681,0.84483894,0.85464695,0.31927461,0.093832104,0.39912136,0.17613833,0.80687697,0.96654943,0.33423884,0.011223595,0.33178926,0.20010237,0.046023456,0.23428061,0.036066555,0.81262563,0.83433571,0.25223538,0.40111753,0.63838066,0.208865,0.035327147,0.23228274,0.57105044,0.41499153,0.96609185,0.27196225,0.70136194,0.95608122,0.8054969199999999,0.20327446,0.98661553,0.21374115,0.34399293,0.54372638,0.45536925,0.2028563,0.64143934,0.52896818,0.35117507,0.86125655,0.75562015,0.067900218,0.80301654,0.52442994,0.28047468,0.78655097,0.27465625,0.25409477,0.51439894,0.41920188,0.14492491,0.54827476,0.70671067,0.24784937,0.9336262,0.78845707,0.20636797,0.9729101100000001,0.66717523,0.066160154,0.71419868,0.10710334,0.63272816,0.7255655,0.2210282,0.60363662,0.80884326,0.23381018,0.24066002,0.4608323,0.49429713,0.41405494,0.88958459,0.6073571800000001,0.046217822,0.63548595,0.2656053,0.1225317,0.37478616,0.18097789,0.62671929,0.7923382,0.34957637,0.12809061,0.46299256,0.23329544,0.56073146,0.77829858,0.076603643,0.62294494,0.68581423,0.33788517,0.16320745,0.48636718,0.21598207,0.2035029,0.40636394,0.26411775,0.18318573,0.4335778,0.23477652,0.34591367,0.56634746,0.323421,0.31295974,0.62089528,0.0046338919,0.65435464,0.64615145,0.19110776,0.024468841,0.20401057,0.58552032,0.11499034,0.68230548,0.018205419,0.60977743,0.61522998,0.046304802,0.81562125,0.847393,0.28772242,0.48457366,0.7562327599999999,0.25687633,0.09522489000000001,0.3390632,0.24721876,0.010404212,0.24528905,0.81390304,0.19144665,0.98314253,0.1483121,0.7326194,0.86537898,0.23736147,0.65237572,0.87334574,0.64002781,0.17025518,0.79087999,0.37109103,0.1424631,0.49845569,0.25352909,0.59084905,0.82814223,0.17228884,0.2052196,0.36504592,0.65302868,0.018122965,0.65254671,0.053363521,0.48441395,0.52530754,0.15006858,0.13927621,0.27765517,0.33961996,0.6572036,0.978833,0.25489825,0.55892663,0.79777726,0.74996911,0.24026398,0.9686859,0.14341918,0.17995633,0.31152112,0.56142174,0.217735,0.76064737,0.054637568,0.85682423,0.8965309,0.32379054,0.63452462,0.94071592,0.43434129,0.09899008199999999,0.51754841,0.12149143,0.86417649,0.96966405,0.13769046,0.67726504,0.7999286799999999,0.097224163,0.88382309,0.96528657,0.039543206,0.22986064,0.25881456,0.004292106,0.24861646,0.24273665,0.45250026,0.51509173,0.94880281,0.46588643,0.39155593,0.83925786,0.57968233,0.25099209,0.81166712,0.1458153,0.51460079,0.64633112,0.27089348,0.5448666599999999,0.79955951,0.72456326,0.17705809,0.88087795,0.26355145,0.61919939,0.86617543,0.37527923,0.42646672,0.78472145,0.16133893,0.2596331,0.40832063,0.085301923,0.31923721,0.3926625,0.030472763,0.88317668,0.89891619,0.19071537,0.37368938,0.55055532,0.35245685,0.44710491,0.7827517899999999,0.027449985,0.17720171,0.194593,0.39768196,0.11170281,0.49408039,0.2945257,0.60906301,0.88660498,0.42869422,0.21088768,0.62315193,0.13573624,0.13385791,0.25815974,0.49059273,0.1313476,0.605083,0.38392277,0.51855854,0.88472059,0.21546441,0.022005356,0.22554654,0.12351947,0.48224958,0.59223793,0.11922396,0.46355269,0.56943395,0.8029847,0.16825131,0.94934828,0.13099532,0.051475251,0.17164891,0.26022961,0.11572001,0.36272584,0.019580622,0.81912152,0.82455578,0.017190959,0.68825432,0.69219348,0.82433139,0.043031795,0.8459691499999999,0.79947329,0.085429373,0.86361171,0.16991694,0.17132788,0.32904083,0.45259015,0.21352146,0.64929808,0.041712828,0.33087958,0.3613076,0.52279387,0.07951823,0.58530096,0.30681794,0.26821415,0.56009447,0.36912954,0.43477878,0.7869235999999999,0.38094719,0.10087192,0.46684226,0.24171402,0.75145263,0.97605896,0.71845335,0.30022773,0.99722387,0.61339815,0.26821371,0.86197486,0.16831138,0.047749838,0.204692,0.67483466,0.18863948,0.84341708,0.34111454,0.1780887,0.50433074,0.54475597,0.32971567,0.85548362,0.60030556,0.31664835,0.89720007,0.89556247,0.061380214,0.93433651,0.28533049,0.4533815,0.72288982,0.012347034,0.82430523,0.8225827999999999,0.21203649,0.7370983,0.93257612,0.053816694,0.52028962,0.56139425,0.41955128,0.20670299,0.60999188,0.6129643299999999,0.30130505,0.8944220899999999,0.54164225,0.37489822,0.89730397,0.42264714,0.06135077,0.46864096,0.16043655,0.36161046,0.5087409000000001,0.58945487,0.26446219,0.83467166,0.10825409,0.10070027,0.19815859,0.89585635,0.033302181,0.90673192,0.011350216,0.48119388,0.48073927,0.26990935,0.043619802,0.30062967,0.13541848,0.73030418,0.85038296,0.26305981,0.33730039,0.58564041,0.17973938,0.54775936,0.71267639,0.49044287,0.19528678,0.66845544,0.87349914,0.1415264,0.99223213,0.00053319276,0.98705013,0.97262802,0.54319225,0.24169447,0.76649972,0.13210394,0.30004941,0.4196851,0.53778698,0.1524522,0.67252008,0.71530202,0.2019927,0.89652982,0.07942904100000001,0.5105431499999999,0.57693142,0.2139579,0.61790895,0.8160601,0.082040513,0.5829447800000001,0.65142984,0.069374676,0.037202738,0.096793875,0.07696061799999999,0.11364208,0.18020176,0.73871683,0.19051212,0.90818041,0.17917885,0.73299102,0.8961417699999999,0.95789974,0.065651776,0.99996181,0.64857434,0.06415438399999999,0.69389029,0.8363380500000001,0.17308957,0.98699691,0.30448802,0.65333982,0.94040114,0.097011456,0.57549427,0.65876964,0.055681551,0.78508256,0.82628753,0.42435187,0.37601674,0.7829226500000001,0.27359965,0.44452805,0.70254339,0.62509163,0.36621877,0.97085166,0.25036093,0.62860752,0.86253357,0.35013043,0.4043525,0.7379888999999999,0.58385319,0.14790241,0.7133602,0.33266895,0.56299089,0.87839347,0.43645046,0.41710527,0.83565495,0.0057308452,0.2770551,0.27240555,0.15572658,0.39970016,0.54194311,0.18903104,0.57855247,0.75241686,0.51505936,0.051910176,0.5502579399999999,0.23127732,0.45160135,0.66789672,0.26428937,0.37425629,0.62356474,0.226157,0.46156953,0.67275771,0.15971342,0.063526663,0.21189922,0.22457141,0.3619412,0.57222124,0.56496962,0.34405731,0.88963505,0.55798212,0.13644806,0.67650644,0.22161946,0.67518142,0.88050123,0.21378268,0.085112409,0.28658392,0.39299475,0.43621085,0.81184567,0.11270995,0.7089498,0.80680813,0.22698194,0.45460708,0.6666531999999999,0.40461397,0.30507871,0.69301433,0.65847599,0.10354981,0.74277732,0.45977445,0.10283847,0.54641488,0.63554995,0.18000513,0.7961568,0.047931651,0.67800714,0.71228297,0.063106811,0.11281571,0.16573936,0.55331095,0.14823952,0.68362102,0.6219837499999999,0.057547313,0.66114353,0.18061013,0.53021674,0.69610618,0.25385037,0.50890845,0.7470551600000001,0.12482269,0.88834766,0.99695696,0.38698182,0.054780274,0.42699491,0.055972268,0.7708336,0.81241824,0.50280993,0.081074852,0.56716976,0.06824253499999999,0.13732226,0.19514224,0.46461056,0.50952402,0.95519627,0.0058030644,0.98247483,0.97327181,0.19944981,0.21369646,0.40021183,0.58867665,0.031611966,0.60258166,0.32948826,0.44706415,0.76009478,0.20417684,0.46191649,0.65145915,0.3052736,0.32081571,0.6108305000000001,0.42610736,0.24119303,0.65071139,0.026519363,0.21937074,0.23556922,0.57788259,0.32085502,0.8792999,0.8162025000000001,0.039778075,0.8347324699999999,0.0046237828,0.46709172,0.46010624,0.028636225,0.61292262,0.62862546,0.16882387,0.026420129,0.18400675,0.17037828,0.57913939,0.7346331,0.024697216,0.42031319,0.43340012,0.63967281,0.045357817,0.6664518699999999,0.34263184,0.22664766,0.55406541,0.034461114,0.16328081,0.18766701,0.20301421,0.30339964,0.49283671,0.71547581,0.28748964,0.9816373900000001,0.29031733,0.52342922,0.7973887200000001,0.16578417,0.22635175,0.37963456,0.061274381,0.55290483,0.60113901,0.58847517,0.35270918,0.92137545,0.8495994100000001,0.14014155,0.96732299,0.47436456,0.5449105,0.99995526,0.19266175,0.52602406,0.70380787,0.27111648,0.5719961099999999,0.82673069,0.49654774,0.4757693,0.95311047,0.3376753,0.2733881,0.59561886,0.09888111199999999,0.87172191,0.95489628,0.18353192,0.31180323,0.48200154,0.39024307,0.18641098,0.56097394,0.052653033,0.21057175,0.25256099,0.50555887,0.041124909,0.5301885200000001,0.041544548,0.39303084,0.4228857,0.59246239,0.14115318,0.71513246,0.70769899,0.12952498,0.8170507,0.15264696,0.54463258,0.68289297,0.78001617,0.19550149,0.95380336,0.57245884,0.043346366,0.59826991,0.07976259600000001,0.017588005,0.087536421,0.17420025,0.56203877,0.72140796,0.48836292,0.12140377,0.59300873,0.17376734,0.40834896,0.56829944,0.60266129,0.10829159,0.69252887,0.30234321,0.53944158,0.8251376499999999,0.23916744,0.43808393,0.66223709,0.37355499,0.15940456,0.51771229,0.79680449,0.19050081,0.96536644,0.84174159,0.026336162,0.8465263,0.21345509,0.34481118,0.5442576,0.49993315,0.49210127,0.97266889,0.058455537,0.17027153,0.21823853,0.009618313200000001,0.35189056,0.35057828,0.16868943,0.41383085,0.56874532,0.008009282100000001,0.50038887,0.4965187,0.019037582,0.68737657,0.6931398,0.060293724,0.36720299,0.41568897,0.003086515,0.29466486,0.28729606,0.55434365,0.044433164,0.58151208,0.55173265,0.28473575,0.81766831,0.7629599500000001,0.022685873,0.7653259100000001,0.07118569700000001,0.41459502,0.47349532,0.58427767,0.08397018000000001,0.6502650800000001,0.7662555599999999,0.16951626,0.9144387899999999,0.30144828,0.61289554,0.89722885,0.17133672,0.79002597,0.94508089,0.75770312,0.25855836,0.99447581,0.10380271,0.6419105000000001,0.73143761,0.61917971,0.12229737,0.72270805,0.08362589500000001,0.81654263,0.88505736,0.039981129,0.51962891,0.54711438,0.41959635,0.17763814,0.58116194,0.49954325,0.017455716,0.50075108,0.25963842,0.2994804,0.54469941,0.3213018,0.51258945,0.81712954,0.24741878,0.34646048,0.57933916,0.62020405,0.07324293599999999,0.67498384,0.36919397,0.2419612,0.59543353,0.24586929,0.20765886,0.43992176,0.37899441,0.63297741,0.99353654,0.12913579,0.58268177,0.69754194,0.11034811,0.84344417,0.93809514,0.26611971,0.64993105,0.89923452,0.71915468,0.29277895,0.9905145,0.74971334,0.079311744,0.80853698,0.48172826,0.4689532,0.93174674,0.016320747,0.12186214,0.12865757,0.33528202,0.3793475,0.69852702,0.24814994,0.6322929900000001,0.86401777,0.073875963,0.78817891,0.84727911,0.19174339,0.41019147,0.58783037,0.019382271,0.37386609,0.38202403,0.20540169,0.0011329803,0.19490252,0.28230037,0.1414951,0.41006441,0.1305472,0.59816775,0.71431617,0.30143268,0.11161633,0.39922055,0.16057035,0.060972477,0.21020557,0.041642765,0.052365727,0.084550634,0.18554027,0.5883174,0.75868051,0.07627800799999999,0.56173303,0.6246829699999999,0.26340934,0.57675913,0.82387348,0.10927631,0.15587861,0.25398173,0.19616999,0.558788,0.73981148,0.45480304,0.024918066,0.46411011,0.12857292,0.016069498,0.13409001,0.59292178,0.21699869,0.79093308,0.41172494,0.24298844,0.6383330699999999,0.15641778,0.52494856,0.6670510200000001,0.66006611,0.13610654,0.77668636,0.56637347,0.16027131,0.7084362400000001,0.17104651,0.73944241,0.89454318,0.3342054,0.10432876,0.42425112,0.10702136,0.39794374,0.49223956,0.50453945,0.07265445500000001,0.56050757,0.029224652,0.69675233,0.71248502,0.33123185,0.42229003,0.73719995,0.1763123,0.1984524,0.36228486,0.31135992,0.13507898,0.43230446,0.28371891,0.26789583,0.53703327,0.22492172,0.55477049,0.7641313199999999,0.53951695,0.025888791,0.54848984,0.44835382,0.45093813,0.8809869,0.84904896,0.073278548,0.9003563,0.19044651,0.19171388,0.36950808,0.33395127,0.64217776,0.95832387,0.41694199,0.12615904,0.52740667,0.1776966,0.66898591,0.83109674,0.95535338,0.015026898,0.94716148,0.40524824,0.50924211,0.89646382,0.24054813,0.17662005,0.40384684,0.07204654200000001,0.80464033,0.86183122,0.1186413,0.018975157,0.12719724,0.54389999,0.4189211,0.94326138,0.4569196,0.11814893,0.55881388,0.44551599,0.39087241,0.81852063,0.56222368,0.422359,0.96471956,0.55534895,0.15897415,0.69629204,0.55767786,0.38947861,0.92757859,0.10725113,0.25045402,0.34594299,0.1400162,0.51078935,0.63683446,0.96537026,0.052682729,0.99443379,0.5630339,0.21498872,0.75950655,0.23401655,0.2820754,0.50217938,0.037945245,0.62199281,0.64680252,0.36086394,0.43229878,0.7763209599999999,0.73140292,0.17356366,0.88414126,0.56317001,0.32554194,0.86946904,0.33210651,0.50263054,0.81787501,0.46121239,0.16166618,0.60627283,0.6234448500000001,0.20557747,0.80964194,0.32093498,0.082248826,0.38924894,0.62966571,0.39008937,0.99906973,0.71756627,0.17815029,0.87507326,0.44701636,0.55984672,0.98786456,0.30490881,0.52657021,0.81487694,0.47466896,0.45813303,0.91404641,0.43769613,0.37630838,0.79635209,0.031431923,0.36523015,0.38530966,0.42137002,0.43009285,0.83370811,0.078100825,0.66938889,0.7334279500000001,0.22012181,0.5063460400000001,0.71129799,0.19665676,0.37652718,0.5592248399999999,0.57300389,0.20646257,0.76085346,0.15424322,0.21154867,0.35356448,0.30623406,0.24639262,0.53784106,0.045135973,0.0049632179,0.040898548,0.56997412,0.23374038,0.78496911,0.07838508399999999,0.35000409,0.41641687,0.43425228,0.0015109304,0.42062067,0.007259462,0.66476912,0.65908298,0.13288912,0.07669094899999999,0.19856408,0.34357691,0.36493056,0.69237234,0.07987625700000001,0.33759264,0.40555509,0.16198392,0.69818114,0.84462874,0.001188108,0.87211309,0.85908936,0.45091837,0.23993103,0.67388836,0.19979728,0.67460099,0.85843692,0.10846515,0.64181411,0.73593282,0.23034579,0.6805658999999999,0.89444298,0.18913716,0.15949102,0.33620718,0.58933994,0.13030519,0.70128099,0.010069912,0.98995985,0.9849092,0.29763478,0.70188726,0.98188212,0.68761885,0.0045234973,0.67309635,0.39808756,0.08138341,0.46435915,0.1648243,0.40194317,0.55312969,0.83932065,0.15997002,0.97690025,0.066411821,0.0226869,0.079455761,0.63468251,0.17892438,0.79422898,0.7305623999999999,0.21066631,0.92017304,0.007961358700000001,0.090013101,0.08878607500000001,0.47389114,0.13718854,0.59444006,0.13982738,0.50038808,0.62631546,0.0018240246,0.43296183,0.42344325,0.60520164,0.18992653,0.77613004,0.032841432,0.7073067200000001,0.72653155,0.14352199,0.38362356,0.5139544,0.26971416,0.71194976,0.96438601,0.5443335,0.27478511,0.80049718,0.18051484,0.013729015,0.18291061,0.33504557,0.07099836,0.39196652,0.31457269,0.071428533,0.37223481,0.02324542,0.56624369,0.57694438,0.30177102,0.10903929,0.39699357,0.8952025300000001,0.073471628,0.94599423,0.8904736599999999,0.11233556,0.97994699,0.4066077,0.061978394,0.45347089,0.67593633,0.021983097,0.67893805,0.0093391727,0.17607407,0.17563954,0.18377521,0.19643048,0.36762471,0.23746664,0.10023609,0.32492937,0.30447931,0.54463085,0.83239626,0.14244235,0.54222183,0.67044985,0.19543062,0.06344535499999999,0.24698816,0.75116611,0.14329298,0.87352928,0.54249403,0.26813211,0.79207652,0.67740143,0.26595468,0.92275284,0.093128691,0.06572304399999999,0.14851711,0.48478813,0.088926325,0.55722421,0.48021119,0.19869773,0.66176919,0.5946373,0.35197158,0.92671035,0.16729759,0.29596027,0.45027696,0.20469645,0.29661516,0.48775315,0.24339011,0.31705939,0.54616391,0.092077229,0.38215646,0.46184071,0.7959081099999999,0.20624457,0.98012434,0.22328586,0.19245991,0.40258519,0.37691495,0.14463241,0.50634544,0.123951,0.39338608,0.50438191,0.15369449,0.66751095,0.80599725,0.36097704,0.5855736499999999,0.92870235,0.59202097,0.067406435,0.6414345299999999,0.24392045,0.47275214,0.7013581800000001,0.022546761,0.86066879,0.86875135,0.20647973,0.2952749,0.48817762,0.30066701,0.50335251,0.78763467,0.34049384,0.060926959,0.38732591,0.16837899,0.36674192,0.52165941,0.66328672,0.0047882184,0.64940018,0.3176509,0.48092435,0.78207709,0.1648177,0.42424113,0.5752749700000001,0.794318,0.076174679,0.84934143,0.59036785,0.26314193,0.83425904,0.68382822,0.29638906,0.95931598,0.01050611,0.070216955,0.07162545300000001,0.82050568,0.16952712,0.96786813,0.63828277,0.21942506,0.83800923,0.6507111,0.018418391,0.65055814,0.60314141,0.27590266,0.85951387,0.475016,0.10475596,0.56332772,0.21979265,0.5795148,0.78366298,0.36628618,0.01254236,0.36465549,0.41088286,0.20720823,0.60195826,0.4793661,0.10789302,0.57072764,0.78315906,0.13624884,0.89803385,0.22693322,0.46670802,0.67862684,0.37657448,0.51635146,0.87529233,0.13815856,0.28371251,0.40941712,0.079188671,0.73564454,0.80032041,0.68101783,0.07807389000000001,0.73966473,0.015367134,0.04547779,0.051834984,0.25546777,0.10515908,0.3475453,0.96652125,0.053460471,0.99633977,0.47146406,0.30320649,0.75697976,0.22256661,0.12557339,0.33542894,0.20277824,0.44542079,0.63369444,0.54241415,0.07805455,0.6031664,0.76118212,0.25620847,0.99556699,0.022025741,0.54514632,0.55478434,0.85428287,0.11479207,0.94675135,0.46065954,0.12498149,0.56928426,0.30017818,0.66277738,0.94553304,0.68535986,0.06326689200000001,0.72923028,0.11235014,0.64987299,0.74776432,0.70851986,0.097962038,0.78650296,0.17919897,0.64178239,0.8055509,0.13077959,0.19360801,0.31263747,0.69306094,0.1367816,0.80984606,0.34746732,0.55450011,0.88452989,0.19102213,0.062154822,0.24136518,0.39708649,0.37912039,0.75915851,0.33153546,0.51391878,0.82852695,0.12322968,0.14854734,0.26043802,0.17085155,0.48166246,0.63826121,0.20244578,0.09998483599999999,0.29019572,0.085821566,0.38973685,0.46321164,0.08788900400000001,0.14711331,0.22421443,0.84387998,0.07938261100000001,0.9013306,0.45801398,0.15156243,0.59308594,0.37516021,0.5363635,0.89378058,0.37071898,0.17985499,0.53523611,0.81293715,0.15679641,0.94776837,0.19641803,0.026904401,0.21165904,0.15526563,0.40147102,0.54324848,0.046963143,0.84401948,0.8762533,0.35055785,0.54145362,0.87461209,0.64176382,0.077991059,0.70093013,0.33535431,0.015201786,0.33683975,0.029155376,0.66075119,0.67665168,0.15030184,0.84906037,0.98301583,0.22935186,0.48957324,0.70372373,0.098690465,0.84488042,0.92804303,0.18734746,0.59884611,0.77091969,0.077873927,0.43242203,0.49779117,0.31860332,0.32076676,0.62390728,0.14510074,0.73844433,0.86800359,0.40520877,0.30378413,0.69231391,0.92800936,0.058363152,0.96328876,0.59181063,0.008366821300000001,0.58257485,0.3102863,0.32122833,0.6161763,0.29936363,0.5186853,0.80158354,0.48432887,0.11362127,0.5813050400000001,0.3760541,0.43303432,0.792009,0.2624796,0.33348022,0.58127396,0.16801905,0.015308452,0.17217543,0.33088594,0.42127944,0.7358553799999999,0.38028608,0.23844229,0.60285978,0.5508855,0.14258271,0.6756130299999999,0.20969147,0.082423624,0.27988426,0.0050346394,0.6228521,0.6152500400000001,0.27569624,0.34875936,0.60946698,0.23211035,0.30105687,0.51915943,0.54824613,0.020962086,0.55219082,0.16417641,0.79080585,0.9388050999999999,0.063576886,0.65001359,0.69987836,0.089417646,0.61928092,0.69479183,0.83807422,0.082878234,0.89908654,0.032230208,0.23325013,0.25498092,0.12647914,0.434301,0.5475179999999999,0.17113953,0.47676081,0.63367526,0.4355945,0.0059798188,0.42638191,0.39539538,0.40414559,0.78235447,0.070289844,0.40734799,0.46541368,0.46282526,0.24863736,0.69426199,0.16346871,0.48420157,0.633514,0.42140506,0.58102414,0.98368442,0.36768767,0.43884432,0.78954271,0.24737217,0.55011605,0.78161372,0.095708718,0.60506549,0.68686423,0.55213837,0.061439294,0.59623525,0.17224597,0.45335086,0.61150826,0.48730051,0.51176592,0.97976562,0.13187584,0.73758505,0.85412776,0.37324823,0.29393502,0.65105874,0.33288322,0.29591749,0.61328193,0.08600395199999999,0.3154369,0.38957837,0.2498172,0.2443236,0.48023353,0.40237881,0.0050292834,0.39273105,0.11183884,0.6051596299999999,0.70284063,0.19009967,0.16315035,0.34079027,0.72790234,0.16337644,0.87057389,0.60577212,0.22122752,0.80778756,0.9446092,0.06510247299999999,0.98632928,0.90013642,0.052326764,0.92984625,0.22395912,0.20619703,0.41689519,0.021725783,0.91689781,0.92380335,0.24712666,0.22754476,0.46091539,0.40993703,0.57945302,0.97083136,0.93687793,0.061144391,0.9747843899999999,0.66298254,0.33648954,0.97862743,0.03726922,0.60535724,0.62961035,0.0094731612,0.3958452,0.39410184,0.12265045,0.72929133,0.83680442,0.038638486,0.66464822,0.68986091,0.29065909,0.71575484,0.98879001,0.081694325,0.1469575,0.21795992,0.21517006,0.54611395,0.74592935,0.38526188,0.037129551,0.40776633,0.31943117,0.61761946,0.91962904,0.15089421,0.24420565,0.3827097,0.20482013,0.57220514,0.7616582200000001,0.31676591,0.38079291,0.68173067,0.20997661,0.76577328,0.9590348,0.1162908,0.36617049,0.46980196,0.38658111,0.17830674,0.5493170000000001,0.68262135,0.22090589,0.88313929,0.82002127,0.1387108,0.93677686,0.035874273,0.37865876,0.40302449,0.11477542,0.30608117,0.40861441,0.50499435,0.49894664,0.984453,0.15469643,0.4790793,0.6197875,0.49571588,0.38782278,0.8649214,0.14434894,0.68461425,0.81378613,0.94542001,0.0048640536,0.92728416,0.3254487,0.62235665,0.93026047,0.32329215,0.59545061,0.90140732,0.70041348,0.066374749,0.74714063,0.14563063,0.4320464,0.56413611,0.033256555,0.85265991,0.8713406,0.5419495,0.0031306571,0.52827618,0.21483728,0.43745702,0.63765707,0.096051396,0.52180846,0.60449044,0.78692237,0.23020061,0.99507533,0.016458811,0.92382433,0.92549822,0.34781885,0.41951428,0.75077513,0.54643043,0.28191389,0.809644,0.1526122,0.4488931,0.58774691,0.13338274,0.37798478,0.49836875,0.37719917,0.065955392,0.42846407,0.7072074,0.013550988,0.70135295,0.63675508,0.2259458,0.84298295,0.29186373,0.47734812,0.75313241,0.33436653,0.19190499,0.51141188,0.8739505,0.10403041,0.95542639,0.87685117,0.092068104,0.94639872,0.6370186799999999,0.1324604,0.75036998,0.59089459,0.33048624,0.90168052,0.13079988,0.71501582,0.83064702,0.45307555,0.13453417,0.57130657,0.53910391,0.07862664599999999,0.60047525,0.4126015,0.39260279,0.78782972,0.012218465,0.32648074,0.32789534,0.83123282,0.095611364,0.90499966,0.20168333,0.54184827,0.72841164,0.17821695,0.17913699,0.34497153,0.29571464,0.052407211,0.3347692,0.29730996,0.24983877,0.53247732,0.15542024,0.3038249,0.44639475,0.27401037,0.053613114,0.31459563,0.15733794,0.14286317,0.28837655,0.57784217,0.18124242,0.74056276,0.45556906,0.16047271,0.59953036,0.088548818,0.10166073,0.17970953,0.37594565,0.05138694,0.41275682,0.055869774,0.78587337,0.8272585,0.18345496,0.47324752,0.64231165,0.6129917499999999,0.37692372,0.96957201,0.5415907599999999,0.47819593,0.99987378,0.14433359,0.019201555,0.15272062,0.07249681299999999,0.5862886899999999,0.64535442,0.4676321,0.069413796,0.52094654,0.79211964,0.21110582,0.9812233299999999,0.29880825,0.28262875,0.56652765,0.067893288,0.17257103,0.22981604,0.16797903,0.54443431,0.69779306,0.54732442,0.32777939,0.85608911,0.00020295638,0.59491702,0.58274048,0.11534764,0.50743182,0.60920852,0.90079786,0.0043251249,0.8828106,0.40637853,0.40607925,0.7950902399999999,0.44963952,0.40443964,0.83605925,0.82722584,0.11026289,0.91560958,0.24191305,0.47426097,0.70088048,0.2134909,0.47537769,0.6740034,0.56283983,0.31219333,0.85588281,0.29936688,0.012378599,0.29859927,0.23958275,0.054239841,0.28131834,0.5997381899999999,0.31240194,0.89242282,0.58721457,0.13946603,0.70828899,0.014403718,0.6881939,0.68938895,0.12324237,0.34815414,0.45874874,0.25260998,0.010068426,0.25026405,0.54071547,0.24251773,0.76487876,0.25801982,0.71440643,0.95531149,0.7510582,0.065121391,0.79576391,0.027188654,0.73858259,0.75203626,0.73920104,0.27003878,0.9876624899999999,0.56857241,0.11360214,0.66423826,0.29822826,0.37234925,0.65508887,0.33118225,0.63322198,0.94670024,0.081533017,0.85173844,0.91796164,0.51165986,0.24272983,0.73647924,0.5505367,0.06137772,0.59459695,0.67372723,0.10532869,0.759562,0.12727148,0.26932838,0.38440709,0.009948561200000001,0.021138733,0.022319974,0.18376581,0.024992254,0.19730114,0.15456259,0.23082016,0.3730241,0.55914382,0.045452788,0.5872516,0.12771851,0.27033562,0.3858479,0.24095911,0.7169944,0.94108329,0.81482622,0.11413047,0.90724225,0.74367933,0.13646447,0.85937351,0.0041897749,0.77724048,0.7677943699999999,0.076889052,0.45601151,0.52025622,0.07464197,0.656711,0.71742734,0.19024965,0.41291228,0.5890625,0.016213464,0.35323486,0.35840783,0.10801035,0.20114529,0.29770511,0.09984667799999999,0.61176175,0.69759112,0.17450304,0.71554065,0.87420167,0.48538658,0.099195691,0.56801552,0.31710362,0.4286708,0.72962722,0.42380783,0.58812693,0.99310659,0.047966435,0.96029758,0.99275703,0.32553204,0.4343973,0.7436154,0.26560435,0.35679522,0.60751297,0.091136465,0.039061615,0.12006878,0.29010481,0.66773151,0.94053573,0.019934647,0.13375615,0.14403212,0.55177048,0.10726049,0.64139379,0.083798261,0.2666358,0.33892532,0.0058930275,0.78589683,0.77807112,0.57113455,0.30473311,0.8566390699999999,0.17594242,0.23940011,0.40259992,0.45356443,0.19872703,0.63555998,0.67673408,0.23861022,0.89493053,0.18669062,0.037145857,0.21225504,0.86442262,0.11551658,0.95745544,0.057818765,0.60458068,0.64907344,0.45738133,0.5404190800000001,0.97877037,0.24499348,0.34179138,0.57231255,0.14977166,0.84070234,0.97419054,0.091118455,0.30199835,0.38126403,0.67470645,0.26658505,0.92072541,0.13283708,0.59604542,0.71446253,0.25372335,0.71609894,0.95276229,0.39982941,0.30514961,0.68837354,0.53480761,0.045102547,0.56294049,0.16784988,0.25894302,0.41404622,0.11566387,0.60106286,0.70253711,0.43757301,0.46934913,0.88866165,0.15599491,0.2931337,0.43633949,0.010466203,0.43473784,0.43371733,0.78519937,0.15120361,0.91489964,0.43937993,0.57015791,0.99058879,0.29107775,0.13920037,0.41642755,0.26433594,0.050445011,0.30192216,0.20928445,0.15984575,0.35639804,0.080176206,0.10801556,0.17777843,0.056106549,0.62674452,0.6694059999999999,0.01465153,0.3860372,0.38945713,0.46339392,0.06340863099999999,0.51080753,0.32018587,0.69182649,0.99409272,0.17378692,0.2209243,0.38212277,0.16463698,0.041582156,0.19494666,0.9028377,0.080519253,0.96051379,0.051074881,0.22466962,0.26501247,0.14894611,0.53583614,0.67051008,0.64777281,0.28178284,0.90930278,0.062991031,0.22303481,0.27512188,0.33015009,0.6772638,0.98943698,0.46508537,0.29772206,0.7452503700000001,0.12358412,0.6145014600000001,0.72368645,0.39950574,0.44656612,0.82854424,0.082591649,0.17666531,0.24835654,0.16279632,0.10330593,0.25445335,0.023954605,0.38643379,0.39901159,0.031014441,0.011352776,0.033341146,0.31803122,0.45109609,0.75281886,0.2257015,0.088643102,0.30182759,0.22904172,0.44182279,0.65598093,0.42578243,0.21238968,0.62177692,0.14623268,0.61057782,0.74208993,0.18469529,0.0029195719,0.17628839,0.38016637,0.36453408,0.72800706,0.30659517,0.39491905,0.68574936,0.011386123,0.79357984,0.79111265,0.044437343,0.31220989,0.34544306,0.1190744,0.6494135599999999,0.75392908,0.29323509,0.22407122,0.50286628,0.89067536,0.13076578,0.998455,0.12325127,0.19329449,0.30491307,0.96108017,0.035170125,0.97281167,0.58668614,0.066614228,0.6353944500000001,0.033702794,0.62787384,0.64846758,0.002552311,0.69638783,0.68585942,0.81834046,0.16701745,0.96324289,0.17627006,0.19819297,0.36198554,0.5983777300000001,0.0094930664,0.5901601400000001,0.35282289,0.23430561,0.5717079900000001,0.5916585599999999,0.17814512,0.7510903799999999,0.60347255,0.25311538,0.83720204,0.92387877,0.092196324,0.99283285,0.81466775,0.11412025,0.90707606,0.1677168,0.028871333,0.18535178,0.009872233100000001,0.53212031,0.52987651,0.10229692,0.13622434,0.22758391,0.20430151,0.43751525,0.62734064,0.64421323,0.09433577,0.71957955,0.62669535,0.15146886,0.75908873,0.0067755958,0.98636139,0.97809052,0.26143696,0.065467468,0.31399158,0.16254191,0.78115888,0.92761194,0.30070484,0.37785873,0.66300084,0.18078254,0.5665897600000001,0.73241051,0.07298344399999999,0.11619068,0.17881744,0.25026727,0.2802597,0.51637721,0.20230918,0.50684593,0.6942550199999999,0.39111061,0.3067391,0.68136745,0.10301203,0.7616029600000001,0.84956683,0.37110035,0.62135949,0.97422173,0.43375685,0.37047317,0.78667623,0.39417715,0.5683526800000001,0.9442855,0.6358343400000001,0.12939962,0.74616307,0.17461778,0.32134713,0.48270537,0.25036741,0.5869675,0.82117292,0.47352659,0.52253886,0.9769051600000001,0.94764544,0.011616419,0.93618357,0.015961475,0.70132494,0.70396778,0.26961274,0.13677655,0.39288364,0.39713038,0.55714721,0.93606144,0.23088836,0.029790248,0.24846792,0.32101295,0.22379676,0.52994569,0.092458363,0.18808095,0.26941281,0.12231559,0.7823573700000001,0.88919285,0.20527987,0.19200089,0.38439919,0.29329419,0.2231803,0.50203939,0.048738707,0.93887137,0.97223172,0.47967131,0.4422688,0.90321185,0.63911545,0.038286251,0.65887784,0.022431787,0.5195031,0.52970905,0.55265685,0.085716931,0.62086425,0.77458034,0.19769346,0.95062845,0.44198747,0.54740488,0.97055247,0.12751468,0.025018505,0.14193833,0.60481566,0.38868398,0.97320441,0.77013961,0.053982318,0.80348678,0.23653523,0.3201442,0.54247869,0.30778135,0.63475359,0.9251795900000001,0.15195878,0.6040322,0.74122556,0.20933612,0.32709105,0.5225978100000001,0.015520425,0.35278204,0.35727555,0.42850588,0.41914961,0.82986309,0.10514657,0.77764239,0.86760293,0.08647542499999999,0.5930665899999999,0.66585223,0.074035823,0.0053916128,0.069780995,0.027574793,0.55835376,0.57336919,0.5365611300000001,0.012144748,0.5319253900000001,0.39814143,0.10082097,0.48372232,0.14602167,0.40664146,0.53928275,0.90105644,0,0.87876844,0.6635872,0.31017019,0.95307601,0.4770277,0.45866088,0.91689338,0.33038216,0.35965071,0.6741346,0.90366439,0.083444,0.96423338,0.20966134,0.22017975,0.41670762,0,0.80493208,0.79117888,0.20090678,0.7446299,0.9290992300000001,0.69108375,0.069443668,0.74100271,0.48612165,0.30789453,0.77606998,0.6552718199999999,0.21472176,0.8500654,0.46427559,0.022171593,0.47070898,0.044659013,0.6250967200000001,0.65649694,0.041092786,0.07459832,0.10609593,0.21750866,0.20585447,0.41020329,0.56579371,0.036311457,0.58471816,0.14431764,0.8358583000000001,0.96400783,0.11825038,0.11136994,0.21860137,0.25622055,0.14422664,0.38709798,0.4436433,0.48647178,0.9116492899999999,0.88341429,0.032706966,0.89388927,0.26001624,0.33306791,0.57843875,0.34801252,0.49781417,0.82875241,0.46730017,0.44500719,0.89375079,0.62471152,0.29457842,0.89930668,0.2468211,0.17143851,0.40487609,0.14922905,0.028944748,0.16722036,0.11341725,0.88667348,0.98406315,0.59294517,0.31639341,0.88969923,0.49809131,0.22447079,0.70497934,0.068357156,0.92487418,0.9776440199999999,0.060818373,0.31881951,0.36813928,0.15922206,0.77029749,0.91355279,0.3422905,0.32112197,0.64758425,0.7147481,0.19939779,0.8934064900000001,0.98573959,0.036817025,0.9987292,0.23588505,0.030989214,0.25457912,0.31957157,0.096006749,0.40157415,0.86055001,0.030618052,0.8693002399999999,0.015458175,0.2177166,0.22303429,0.33611814,0.67788579,0.99593147,0.50216649,0.4593677,0.94234902,0.0029419021,0.48036898,0.47164035,0.071697644,0.5826422999999999,0.64094502,0.12929185,0.063800678,0.18221618,0.22871357,0.026035118,0.24259595,0.19709778,0.050202727,0.23547395,0.19248308,0.26517607,0.44449401,0.24687009,0.43484458,0.66660355,0.0038403868,0.98207581,0.97094282,0.57350902,0.3916544,0.94532861,0.0262517,0.83847722,0.85035341,0.060950894,0.74248711,0.7891599,0.29825418,0.52382291,0.8055950200000001,0.31235645,0.52362313,0.8192826600000001,0.18044647,0.22559683,0.39332214,0.42690018,0.12467543,0.53573832,0.7057390100000001,0.12045718,0.8061124,0.060996944,0.67272304,0.71989853,0.22629964,0.6275305,0.83777113,0.29635159,0.15814424,0.44044024,0.12742312,0.7973104,0.90907709,0.09708217700000001,0.53328067,0.61690242,0.40118955,0.023257618,0.40966885,0.36419847,0.39544426,0.74299146,0.60026132,0.39533119,0.97532352,0.067173581,0.08543020799999999,0.14253782,0.67624625,0.33130205,0.98653434,0.5001783,0.18227785,0.66511799,0.050645541,0.0033444521,0.044715509,0.021343243,0.89082244,0.89752225,0.039546441,0.67790586,0.7039256699999999,0.12422196,0.84944266,0.9577155000000001,0.056531331,0.14489152,0.1911302,0.17459094,0.47303438,0.63337177,0.88291642,0.040097318,0.90074093,0.29233889,0.68704488,0.96192233,0.31034956,0.57405275,0.86740554,0.8716319300000001,0.00046310814,0.85025504,0.42135143,0.43284444,0.83642335,0.10489296,0.0071623854,0.10192431,0.018630991,0.058093578,0.067581894,0.11515684,0.43088442,0.5329750599999999,0.015701517,0.22142962,0.22696257,0.66772292,0.14067773,0.78876703,0.20816454,0.6145957399999999,0.80706405,0.17580541,0.57759608,0.73844383,0.07051012399999999,0.06643738,0.1269549,0.36017768,0.57675143,0.91915086,0.45015184,0.23466782,0.66790488,0.3456651,0.54049421,0.86884121,0.2598482,0.32623773,0.57148789,0.5493103499999999,0.41750378,0.94718077,0.74870553,0.26702389,0.99402617,0.085539272,0.55797461,0.6300685,0.12505395,0.29786411,0.4105722,0.069786287,0.12233877,0.18177707,0.056410257,0.31679959,0.36179206,0.8547412599999999,0.037547642,0.87046468,0.57075952,0.35658309,0.90777986,0.12331231,0.097151111,0.2094601,0.20545334,0.11021606,0.30332133,0.22453549,0.5764293,0.78526783,0.76583674,0.065786162,0.81097633,0.071569035,0.48670796,0.54551296,0.2494462,0.17007146,0.40610285,0.11277089,0.35452517,0.45476704,0.31533352,0.68993543,0.98743608,0.83448813,0.18038741,0.99242535,0.47133427,0.089283215,0.5443311199999999,0.32166041,0.059761768,0.36762361,0.79067917,0.054715428,0.8244397999999999,0.18099666,0.27563062,0.44356967,0.33600283,0.39761741,0.71738692,0.55589428,0.23908271,0.7764124,0.26175825,0.44090751,0.68728668,0.70124594,0.087324894,0.76877313,0.26589293,0.5581579800000001,0.8078397899999999,0.52869521,0.46710608,0.97615875,0.15710436,0.1282121,0.27359153,0.72506006,0.15682999,0.8612716500000001,0.45119495,0.20017708,0.63466737,0.84912012,0.054523368,0.8817941500000001,0.88505709,0.13648452,0.9986041,0.37861937,0.27506843,0.63760463,0.67278715,0.12291598,0.77610832,0.98750516,0.014350254,0.97814822,0.67969227,0.28561563,0.94454063,0.23320066,0.3247356,0.54375653,0.10253964,0.87229069,0.95906377,0.33015271,0.67804547,0.9902161,0.84247637,0.16204113,0.98206514,0.1594395,0.3029167,0.44945015,0.80635115,0.066827649,0.85190437,0.32043948,0.68821463,0.99075426,0.13425627,0.13322462,0.25607333,0.46964682,0.5001565,0.95084923,0.53845155,0.3843647,0.9035666,0.33676487,0.01408549,0.33711971,0.2041752,0.36926967,0.55941809,0.66107284,0.12227425,0.76393605,0.82648707,0.15115614,0.9555072999999999,0.050687447,0.79239478,0.82863426,0.25960153,0.45985646,0.70398775,0.59308638,0.09778726,0.67266526,0.026624564,0.4592614,0.47399079,0.035716803,0.052344547,0.078694466,0.048352918,0.85575215,0.88927753,0.119023,0.7558677,0.85963473,0.35142216,0.25248115,0.58838512,0.5272065,0.21819869,0.72741726,0.50669177,0.047997311,0.53813141,0.41543982,0.46009372,0.85767299,0.7099014300000001,0.13478001,0.82443996,0.44005159,0.059058284,0.48350117,0.0019546695,0.24547673,0.23731591,0.042603283,0.3123044,0.343731,0.65742176,0.033026052,0.67167783,0.2777719,0.29234971,0.55547099,0.19093262,0.40678559,0.58364849,0.73180143,0.27062561,0.98095929,0.16923708,0.32440315,0.48044313,0.54942356,0.20901051,0.74016581,0.062488635,0.15796141,0.20998039,0.53283545,0.17601605,0.69105382,0.030375332,0.91869749,0.9341082000000001,0.15851908,0.83560504,0.9777400000000001,0.12627034,0.1284099,0.24342665,0.40940118,0.28128253,0.67408797,0.67557024,0.062045021,0.71837685,0.19501717,0.43604983,0.61674281,0.47472325,0.22402648,0.68152806,0.16940498,0.64830518,0.80238706,0.073510969,0.60716399,0.66709147,0.50730935,0.05141687,0.54213666,0.70182408,0.14505825,0.82669727,0.098503701,0.8288124100000001,0.91189645,0.71727107,0.23084508,0.92713192,0.28826511,0.37460748,0.6475218700000001,0.37365928,0.54860656,0.90446545,0.53333707,0.13958275,0.65535331,0.017209138,0.26727848,0.27399536,0.54917056,0.026656372,0.55875803,0.10726539,0.51405417,0.60782909,0.0729233,0.44338275,0.50380529,0.35469137,0.37357009,0.71189932,0.17663378,0.21339751,0.37744855,0.44683591,0.53072594,0.958757,0.72128594,0.24465171,0.94480136,0.43785596,0.020872921,0.44340417,0.76413953,0.15251577,0.89546617,0.1596034,0.34561525,0.49203016,0.45274738,0.25854909,0.69418534,0.56655727,0.42613885,0.9727417900000001,0.88454813,0.071580963,0.9336248700000001,0.45464721,0.28000516,0.71737146,0.1787703,0.26782672,0.43362471,0.11556935,0.65075967,0.75181505,0.45056103,0.3921382,0.82474584,0.43470854,0.10036302,0.51927397,0.28952808,0.010084777,0.2866325,0.48361573,0.11379694,0.58077735,0.21615344,0.14461246,0.34802832,0.08745114800000001,0.6851080899999999,0.75825109,0.23169618,0.14171463,0.36045398,0.29153862,0.097849779,0.37580185,0.06105429,0.018409738,0.06993123,0.6056699,0.38423319,0.96962395,0.20401193,0.54513694,0.73397165,0.15602765,0.49898398,0.6408725,0.41122162,0.58845297,0.9810372000000001,0.61737642,0.25465276,0.8524201,0.044873753,0.43845797,0.47129321,0.14612751,0.80894966,0.93905774,0.13262357,0.26195106,0.38234819,0.054366451,0.51119258,0.55289817,0.33426179,0.5427871399999999,0.85989059,0.29895727,0.6295646,0.9113358,0.5242982,0.0038053946,0.51156575,0.37465849,0.48881528,0.84605005,0.31098175,0.47255276,0.76719346,0.42175882,0.12487211,0.53087116,0.6734857,0.2883954,0.94119074,0.097438315,0.52673873,0.61075404,0.042782575,0.64372189,0.67315233,0.019015794,0.025571242,0.035651672,0.24748849,0.057846499,0.29268592,0.50156877,0.048994545,0.53407762,0.24144239,0.14884152,0.37713097,0.065873162,0.38402987,0.43789944,0.035839646,0.80317491,0.82472351,0.16739091,0.83092802,0.98182946,0.25058824,0.41745037,0.65298453,0.43252358,0.14973642,0.56617222,0.28614187,0.5002095600000001,0.77020983,0.32738713,0.098405256,0.4116527,0.58857455,0.27932873,0.84857392,0.79690907,0.074227715,0.84995858,0.28622052,0.31177022,0.5830833,0.034909613,0.025281165,0.051013688,0.17154884,0.47707624,0.63439167,0.35871097,0.47310164,0.81473635,0.041337113,0.10010198,0.13167297,0.08720449600000001,0.31947035,0.39476752,0.25806471,0.60254197,0.8442245900000001,0.62321028,0.11423127,0.7186636199999999,0.59247776,0.31842261,0.89125489,0.25158914,0.15078091,0.38904887,0.19839534,0.23424841,0.41959074,0.62476003,0.12380661,0.72970218,0.26940489,0.33836467,0.59294552,0.32952096,0.43785868,0.7509818700000001,0.23615176,0.36974635,0.59137807,0.35126824,0.46210488,0.79648303,0.29328348,0.10052594,0.38017858,0.7532553400000001,0.26226097,0.99377454,0.055569014,0.58306137,0.62547994,0.22929156,0.41072973,0.62533772,0.18921151,0.33340435,0.50905358,0.36148416,0.26760691,0.61331948,0.0095392025,0.15938119,0.15925306,0.028650076,0.00360531,0.023316347,0.72542406,0.19656808,0.90110764,0.041499496,0.92526373,0.95158504,0.37600861,0.32733367,0.6869565,0.58887228,0.082950987,0.65377674,0.088685663,0.057260961,0.13573558,0.98295197,0.0095950667,0.96894081,0.06583612,0.60776343,0.66012978,0.61097278,0.20423133,0.79602374,0.22575076,0.34202093,0.55359283,0.38525474,0.029173403,0.39985531,0.09374998900000001,0.9090833699999999,0.9869603300000001,0.24690294,0.26963559,0.50250999,0.17292792,0.35037225,0.50987626,0.39873378,0.2486622,0.6311776099999999,0.39051608,0.45039166,0.82349284,0.34613455,0.59064108,0.91912159,0.059978719,0.53857371,0.58562604,0.066407867,0.89298703,0.9440465099999999,0.76392165,0.057877157,0.80123344,0.47737743,0.039689558,0.50101312,0.00094004016,0.065130303,0.057152711,0.065391739,0.31732054,0.37115341,0.22843325,0.60220265,0.81471023,0.73196209,0.07253582,0.78432633,0.32397985,0.60059337,0.90719353,0.5698774599999999,0.11193435,0.6638664399999999,0.49250756,0.060754031,0.53683771,0.14192769,0.74618704,0.87257113,0.083106376,0.3642546,0.43522287,0.80379415,0.07057912099999999,0.85311344,0.47422131,0.29992332,0.7564331,0.6688094999999999,0.33770658,0.98557413,0.72379024,0.22221481,0.92497747,0.13898166,0.49680636,0.62192445,0.3109817,0.26376218,0.55977161,0.23183883,0.049013743,0.26850129,0.055399489,0.68110416,0.72271306,0.49059594,0.48397781,0.95540461,0.49257351,0.08381532999999999,0.55981276,0.45745063,0.051821006,0.49344368,0.29815676,0.07298141499999999,0.35761322,0.16865831,0.6972571400000001,0.85028289,0.78639125,0.21039045,0.97487206,0.29026055,0.7011496,0.9738881,0.42595473,0.45353391,0.86150993,0.5891569800000001,0.16268537,0.73326874,0.22520629,0.5161281,0.72602247,0.37400952,0.24600621,0.60419375,0.066544861,0.42718116,0.48142925,0.26177251,0.25191301,0.49954524,0.50928208,0.38410789,0.87458912,0.34873057,0.36601871,0.69852802,0.11128185,0.5873611,0.68461033,0.17787405,0.63499242,0.79750083,0.94247281,0.0090361213,0.92852685,0.20504529,0.62692459,0.81624065,0.68249279,0.11448351,0.77728801,0.18431789,0.22431496,0.39586074,0.39405454,0.34497891,0.72225537,0.086400216,0.37405254,0.44819993,0.16765219,0.40575284,0.55969892,0.5516110400000001,0.33804882,0.8705121300000001,0.020995402,0.94000241,0.9460373,0.071593857,0.08174644,0.14323072,0.41236435,0.59470725,0.98837571,0.9814036,0.018077726,0.97584323,0.24639257,0.52553848,0.75623269,0.07415432399999999,0.63954054,0.69988927,0.041103775,0.71619985,0.74350208,0.093287908,0.47280662,0.55308871,0.20905745,0.27490851,0.47048296,0.22034845,0.42762832,0.6333195,0.12256166,0.22367784,0.3344182,0.17364262,0.028103875,0.19042435,0.09467558299999999,0.23474878,0.31795793,0.28384686,0.4948805,0.76265587,0.6505972,0.35965691,0.98944742,0.06887926799999999,0.76105004,0.81540798,0.65354702,0.024161014,0.65905557,0.053065407,0.26518252,0.3072198,0.25293465,0.20520065,0.44443672,0.68507311,0.13857528,0.8037626,0.29697323,0.5288347,0.80931265,0.70667815,0.097828216,0.78455653,0.15496861,0.8398121,0.97842342,0.0060204213,0.48641591,0.48067897,0.40103123,0.29311284,0.67759907,0.49804169,0.15992737,0.6408101899999999,0.23019701,0.44225743,0.6575503,0.28929876,0.058671645,0.33467503,0.26075854,0.07306771300000001,0.32087398,0.6019088,0.052326708,0.63618992,0.17925405,0.2325842,0.39908956,0.57364547,0.046812594,0.60288186,0.28080217,0.22518596,0.49173137,0.32065915,0.38646785,0.6912019700000001,0.50646871,0.28716823,0.77551473,0.28080085,0.70929667,0.97266706,0.030980321,0.8330923,0.84965993,0.3772286,0.0071972142,0.37012007,0.61604329,0.36635661,0.96207894,0.32833546,0.61825997,0.92903316,0.095029474,0.20580405,0.28955143,0.052706174,0.12313461,0.16574939,0.046782915,0.72242015,0.75527369,0.24472026,0.49934417,0.72856343,0.56193306,0.40665025,0.94882762,0.032201295,0.47284607,0.49297765,0.33972595,0.14741599,0.47249181,0.43843377,0.30840303,0.72961825,0.28130727,0.2788307,0.54552178,0.13085665,0.26702146,0.38564551,0.58557915,0.29278457,0.85899205,0.9009694,0.12124683,0.9991347,0.47042153,0.50003633,0.95149268,0.12274842,0.3426502,0.45279451,0.064399636,0.65550931,0.70614818,0.049149394,0.81171467,0.84631302,0.008929295,0.83386016,0.82870973,0.83842997,0.018795215,0.83577393,0.074583966,0.34499703,0.40769978,0.41507171,0.39033809,0.7880122000000001,0.076822882,0.06690096299999999,0.13363144,0.61108549,0.28567744,0.87704689,0.18389612,0.10411728,0.27603575,0.018838452,0.8181923800000001,0.8229019400000001,0.2608427,0.17345532,0.42068633,0.49469195,0.24004191,0.71710112,0.24892528,0.72373356,0.95562233,0.060978973,0.28737424,0.33705829,0.58514546,0.14167504,0.70844613,0.082819568,0.29929046,0.37040221,0.44304244,0.11736674,0.54437237,0.27770332,0.72016861,0.98041767,0.85957291,0.074160238,0.91159485,0.52269672,0.14225863,0.64753439,0.092572695,0.52880867,0.60801938,0.99802296,0.012409493,0.9865767600000001,0.19980124,0.49629425,0.68130302,0.8203939099999999,0.1411813,0.9395980900000001,0.53907977,0.30602007,0.8263541599999999,0.07244395300000001,0.7074757,0.7656949,0.062014989,0.67912093,0.72725692,0.29879466,0.5202950200000001,0.80262246,0.084498857,0.38826701,0.460449,0.64681114,0.17728898,0.8045470300000001,0.33957053,0.23137609,0.5557484499999999,0.12713636,0.56487913,0.67788723,0.098805505,0.072694466,0.16103263,0.51585824,0.13053129,0.62915027,0.63849638,0.34010336,0.95810673,0.48675782,0.5009154300000001,0.9684519,0.14360607,0.50597114,0.63558269,0.14949497,0.32280042,0.45941142,0.074191413,0.84207443,0.90113191,0.24207286,0.31577364,0.54358953,0.018010941,0.34859053,0.35556387,0.012070463,0.39236942,0.39320633,0.1134888,0.24249104,0.34417428,0.12699327,0.28432803,0.39903446,0.41640049,0.29199659,0.69162382,0.049425829,0.6262383,0.66232479,0.0092195687,0.32367236,0.32215244,0.30541029,0.047119625,0.33906331,0.8348664,0.11227742,0.92513435,0.11968746,0.66427812,0.76929985,0.40545229,0.34226734,0.73078463,0.631844,0.16095,0.77357745,0.43950442,0.18640758,0.60947681,0.77650297,0.21033296,0.96507823,0.38907566,0.5395541700000001,0.91065248,0.2743325,0.060518616,0.32177305,0.18615005,0.39684665,0.56906543,0.34732261,0.31644255,0.64789049,0.21083975,0.65657665,0.85140395,0.01959958,0.84303905,0.84833518,0.26315395,0.61401034,0.86062901,0.42010276,0.52461871,0.92636643,0.053259549,0.53917458,0.57960679,0.85329024,0.08493139299999999,0.91610903,0.08596688199999999,0.054017081,0.12983586,0.59772384,0.12449478,0.70376406,0.079596901,0.56774538,0.63392394,0.2079646,0.41572013,0.60929535,0.19910732,0.11206186,0.29890629,0.34344663,0.10164297,0.43068253,0.2788258,0.56365681,0.8260372,0.29243453,0.34183485,0.6190695899999999,0.6511976,0.17962585,0.8111878,0.21448612,0.6363832,0.83493336,0.18078159,0.72673327,0.89150322,0.42112208,0.17830856,0.58333031,0.20387121,0.019792174,0.21193236,0.10053829,0.31692205,0.40536534,0.0048280714,0.023179741,0.019305601,0.042293413,0.6317533,0.66078054,0.56491403,0.19628082,0.7427726,0.6637429,0.16553546,0.80954278,0.22389578,0.25809569,0.46839125,0.062165549,0.37853472,0.42878954,0.36564182,0.20913987,0.55932967,0.5029046,0.26005391,0.74506869,0.49859234,0.41472062,0.89447528,0.44728618,0.4574258,0.88638077,0.025954102,0.25349948,0.26891761,0.61316027,0.35967061,0.95259795,0.19571171,0.083492863,0.26718103,0.6002132100000001,0.20058261,0.78180429,0.52128024,0.49601033,0.9975722,0.1975632,0.15938271,0.34439646,0.013853357,0.39931169,0.40185865,0.11815303,0.54557288,0.64986191,0.43543374,0.33894997,0.75701089,0.48783025,0.51671761,0.98520648,0.044518076,0.77299309,0.80328496,0.54481208,0.44241612,0.9675004699999999,0.54635768,0.15262962,0.68113566,0.10958498,0.42152003,0.51818562,0.21802503,0.21442914,0.4192302,0.60386778,0.37855373,0.96220723,0.33134633,0.42838804,0.7433707000000001,0.5076931099999999,0.51037005,0.99845892,0.031269374,0.52665684,0.54551801,0.47479831,0.045356771,0.50410359]],"xticklab":["1.58e-04","0.20","0.39","0.59","0.78","0.98"],"yticklab":["1.87e-04","0.20","0.39","0.59","0.79","0.98"],"zticklab":["0.99","0.79","0.59","0.40","0.20","1.26e-04"],"xtick":[0,0.2,0.4,0.6,0.7999999999999999,1],"ytick":[0,0.2,0.4,0.6,0.7999999999999999,1],"ztick":[0,0.2,0.4,0.6,0.8,1],"axislength":[1,1,1]},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<p>When <img src="https://latex.codecogs.com/png.latex?%5Calpha_1%20=%20%5Calpha_2%20=%20%5Calpha_3%20=%20constant">, the Dirichlet density is symmetric about the middle of the simplex, <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Calpha%7D%20=%20(1/3,%201/3,%201/3)"> . In the special case when <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Calpha%7D%20=%20(1,%201,%201)">, the density is uniform over the simplex. When all the <img src="https://latex.codecogs.com/png.latex?%5Calpha_i%20%3C%201"> the density is concentrated at the vertices of the simplex, and when <img src="https://latex.codecogs.com/png.latex?%5Calpha_i%20%3E%201">, the density is concentrated in the center of the simplex with most of the mass concentrated on a few values.</p>
</section>
<section id="how-the-symetric-dirichlet-distribution-changes-as-alpha-changes" class="level2">
<h2 class="anchored" data-anchor-id="how-the-symetric-dirichlet-distribution-changes-as-alpha-changes">How the symetric Dirichlet distribution changes as <img src="https://latex.codecogs.com/png.latex?%5Calpha"> changes</h2>
<p>The following animation, which projects the above plot onto two dimensions, shows how the Dirichlet distribution changes as the common value of <img src="https://latex.codecogs.com/png.latex?%5Calpha_i">, called the concentration parameter, moves systematically from (1,1,1), the uniform distribution, to (0.1,0.1,0.1).</p>
<div class="cell">
<details class="code-fold">
<summary>Code for helper functions</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function generates the data frame for the animation</span></span>
<span id="cb3-2">generate_dirichlet_animation_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(alpha1_values, alpha2_values, alpha3_values, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_samples =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>) {</span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MCMCpack)</span>
<span id="cb3-4">  </span>
<span id="cb3-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Triangle vertices</span></span>
<span id="cb3-6">  v1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-7">  v2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-8">  v3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-9">  </span>
<span id="cb3-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Projection function</span></span>
<span id="cb3-11">  project_to_triangle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x1, x2, x3) {</span>
<span id="cb3-12">    x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v1[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x3 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v3[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-13">    y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v1[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x3 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v3[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb3-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y)</span>
<span id="cb3-15">  }</span>
<span id="cb3-16">  </span>
<span id="cb3-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate animation data</span></span>
<span id="cb3-18">  animation_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(alpha1_values), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i) {</span>
<span id="cb3-19">    alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(alpha1_values[i], alpha2_values[i], alpha3_values[i])</span>
<span id="cb3-20">    samples <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rdirichlet</span>(n_samples, alpha)</span>
<span id="cb3-21">    projected <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project_to_triangle</span>(samples[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], samples[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], samples[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])</span>
<span id="cb3-22">    projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alpha1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-23">    projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alpha2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb3-24">    projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alpha3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb3-25">    projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>frame <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> i</span>
<span id="cb3-26">    projected</span>
<span id="cb3-27">  }))</span>
<span id="cb3-28">  </span>
<span id="cb3-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(animation_data)</span>
<span id="cb3-30">}</span>
<span id="cb3-31"></span>
<span id="cb3-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function creates the animated plot</span></span>
<span id="cb3-33">plot_dirichlet_evolution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(animation_data) {</span>
<span id="cb3-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb3-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gganimate)</span>
<span id="cb3-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for arrow units</span></span>
<span id="cb3-37">  </span>
<span id="cb3-38">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Triangle vertices</span></span>
<span id="cb3-39">  v1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-40">  v2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-41">  v3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-42">  </span>
<span id="cb3-43">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Label positions</span></span>
<span id="cb3-44">  label_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(v1[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], v2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], v3[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]),</span>
<span id="cb3-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(v1[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], v2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], v3[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]),</span>
<span id="cb3-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(1,0,0)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(0,1,0)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(0,0,1)"</span>),</span>
<span id="cb3-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.08</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb3-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.08</span>)</span>
<span id="cb3-50">  )</span>
<span id="cb3-51">  </span>
<span id="cb3-52">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Triangle outline</span></span>
<span id="cb3-53">  triangle_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(v1[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], v2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], v3[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], v1[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]),</span>
<span id="cb3-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(v1[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], v2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], v3[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], v1[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb3-56">  )</span>
<span id="cb3-57">  </span>
<span id="cb3-58">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build plot</span></span>
<span id="cb3-59">  p_animation <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(animation_data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-60">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-61">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density_2d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-62">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> triangle_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-63">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"segment"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb3-64">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>)),</span>
<span id="cb3-65">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkgreen"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-66">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-67">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> label_df,</span>
<span id="cb3-68">              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label),</span>
<span id="cb3-69">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_x =</span> label_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nudge_x,</span>
<span id="cb3-70">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_y =</span> label_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nudge_y,</span>
<span id="cb3-71">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-72">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb3-73">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirichlet Prior Evolution"</span>,</span>
<span id="cb3-74">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb3-75">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Projected X"</span>,</span>
<span id="cb3-76">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Projected Y"</span>,</span>
<span id="cb3-77">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transition from uniform to concentrated distribution"</span></span>
<span id="cb3-78">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-79">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-80">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb3-81">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb3-82">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb3-83">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb3-84">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.caption =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb3-85">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-86">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transition_states</span>(frame,</span>
<span id="cb3-87">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">transition_length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb3-88">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state_length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-89">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ease_aes</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sine-in-out'</span>)</span>
<span id="cb3-90">  </span>
<span id="cb3-91">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(p_animation)</span>
<span id="cb3-92">}</span>
<span id="cb3-93"></span>
<span id="cb3-94"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| message: false</span></span>
<span id="cb3-95"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb3-96"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb3-97"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-summary: "Animation Code"</span></span>
<span id="cb3-98"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| </span></span>
<span id="cb3-99"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb3-100"></span>
<span id="cb3-101"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parameters</span></span>
<span id="cb3-102">alpha1_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># starts high, ends low</span></span>
<span id="cb3-103">alpha2_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># starts low, ends high</span></span>
<span id="cb3-104">alpha3_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># starts low, ends high</span></span>
<span id="cb3-105"></span>
<span id="cb3-106">animation_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_dirichlet_animation_data</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha1 =</span> alpha1_values, </span>
<span id="cb3-107">                                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha2 =</span> alpha2_values, </span>
<span id="cb3-108">                                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha3 =</span> alpha3_values, </span>
<span id="cb3-109">                                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_samples =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>)</span>
<span id="cb3-110"></span>
<span id="cb3-111"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_dirichlet_evolution</span>(animation_data)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-3-1.gif" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/note-on-dirichlet/index_files/figure-html/unnamed-chunk-3-1.gif" class="img-fluid figure-img"></a></p>
</figure>
</div>
</div>
</div>
<p>These next two plots, the first and last frames of the animation, clearly show how the density moves from being uniformly distributed over the simplex to being concentrated at the vertices of the simplex. When modeling the development of a multi-state Markov chain, as I was doing in the post I alluded to above, it is common practice to select a uniform Dirichlet prior with <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Calpha%7D%20=%20(1,1,1)">. However, if you believe that the process is likely to start off uniformly distributed among the states, then a prior with <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Calpha%7D%20=%20(0.1,0.1,0.1)"> might be appropriate. If you had reason to believe that the process would start off concentrated on particular states, then you might explore using an asymmetric distribution by setting different values for the <img src="https://latex.codecogs.com/png.latex?%5Calpha_i">. The code driving these animations might be helpful.</p>
<div class="columns">
<div class="column" style="width:45%;">
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(animation_data, frame <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Initial Frame: Uniform Prior: alpha = (1,1,1)"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-4-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/note-on-dirichlet/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(animation_data, frame <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(animation_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>frame)), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Final Frame: Concetrated Prior: alpha = (.1,.1,.1)"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-5-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/note-on-dirichlet/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</div>
</div>
<p>This concentration of density as <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Calpha%7D"> increases is very apparent in this next simulation as <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Calpha%7D"> moves from (0.1, 0.1, 0.1) to (10.0, 10.0, 10.0). Here we see the distribution concentrating on the mean, <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Calpha%7D"> = (1/3, 1/3, 1/3).</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Define alpha trajectories</span></span>
<span id="cb6-2">alpha1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb6-3">alpha2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb6-4">alpha3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb6-5"></span>
<span id="cb6-6">animation_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_dirichlet_animation_data</span>(alpha1, alpha2, alpha3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_samples =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>)</span>
<span id="cb6-7"></span>
<span id="cb6-8">anim2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_dirichlet_evolution</span>(animation_data)</span>
<span id="cb6-9">anim2</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-6-1.gif" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/note-on-dirichlet/index_files/figure-html/unnamed-chunk-6-1.gif" class="img-fluid figure-img"></a></p>
</figure>
</div>
</div>
</div>
<p>Note that the animation <img src="https://latex.codecogs.com/png.latex?%5Calpha"> passes through (0.5, 0.5, 0.5) which is the <a href="https://en.wikipedia.org/wiki/Jeffreys_prior">Jeffreys prior</a> for the Dirichlet distribution.</p>
</section>
<section id="variance-and-differential-entropy" class="level2">
<h2 class="anchored" data-anchor-id="variance-and-differential-entropy">Variance and Differential Entropy</h2>
<p>The Wikipedia article for the <a href="https://en.wikipedia.org/wiki/Dirichlet_distribution">Dirichlet distribution</a> prominently displays the distribution’s differential entropy:</p>
<p><img src="https://latex.codecogs.com/png.latex?H(%5Cboldsymbol%7B%5Calpha%7D)%20=%20%5Clog%20B(%5Cboldsymbol%7B%5Calpha%7D)%20+%20(%5Calpha_0%20-%20K)%20%5Cpsi(%5Calpha_0)%20-%20%5Csum_%7Bi=1%7D%5E%7BK%7D%20(%5Calpha_i%20-%201)%20%5Cpsi(%5Calpha_i)"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Calpha%7D"> and <img src="https://latex.codecogs.com/png.latex?B(%5Cboldsymbol%7B%5Calpha%7D)"> are defined above and <img src="https://latex.codecogs.com/png.latex?%5Cpsi(%5Ccdot)"> is the digamma function. (This equation triggered my mention of the digamma function above.) But please be advised that differential entropy defined as: <img src="https://latex.codecogs.com/png.latex?h(X)%20=%20-%5Cint%20p(x)%20%5Clog%20p(x)%20%5C,%20dx"> for continuous distributions is not the same as the Shannon entropy <img src="https://latex.codecogs.com/png.latex?H(X)%20=%20-%5Csum%20p(x)%20%5Clog%20p(x)"> for discrete distributions and does not have a similar interpretation. Among other things, differential entropy can be negative, in not invariant under a change of variables, and probably doesn’t conform to any intuition you may have developed about maximum entropy. The plot on the left below shows the behavior of the differential entropy for the symmetric Dirichlet distribution we have been considering as <img src="https://latex.codecogs.com/png.latex?%5Calpha"> moves from (0.1, 0.1, 0.1) to (5.0, 5.0, 5.0). Note that the entropy keeps increasing beyond the point <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%201"> which corresponds to the uniform distribution over the simplex.</p>
<div class="columns">
<div class="column" style="width:45%;">
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the number of dimensions for the symmetric Dirichlet distribution</span></span>
<span id="cb7-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a function to calculate the entropy of a symmetric Dirichlet distribution</span></span>
<span id="cb7-5">dirichlet_entropy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(alpha, n) {</span>
<span id="cb7-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This formula is for a symmetric Dirichlet distribution</span></span>
<span id="cb7-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># where all parameters a_i are equal to alpha.</span></span>
<span id="cb7-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The formula uses the digamma function (psi) and the log-gamma function (lgamma).</span></span>
<span id="cb7-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># H(alpha) = log(Beta(alpha)) + (n * alpha - n) * psi(n * alpha) - n * (alpha - 1) * psi(alpha)</span></span>
<span id="cb7-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># log(Beta(alpha)) = n * lgamma(alpha) - lgamma(n * alpha)</span></span>
<span id="cb7-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The final formula is simplified for symmetric case.</span></span>
<span id="cb7-12">  </span>
<span id="cb7-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># lgamma(x) is the log-gamma function</span></span>
<span id="cb7-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># digamma(x) is the psi function, the first derivative of lgamma(x)</span></span>
<span id="cb7-15">  </span>
<span id="cb7-16">  entropy_val <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lgamma</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> alpha) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lgamma</span>(alpha) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-17">                 (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">digamma</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> alpha) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span></span>
<span id="cb7-18">                 n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">digamma</span>(alpha)</span>
<span id="cb7-19">                 </span>
<span id="cb7-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(entropy_val)</span>
<span id="cb7-21">}</span>
<span id="cb7-22"></span>
<span id="cb7-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a sequence of alpha values from 0.1 to 5</span></span>
<span id="cb7-24">alpha_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb7-25"></span>
<span id="cb7-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate the entropy for each alpha value</span></span>
<span id="cb7-27">entropy_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(alpha_values, dirichlet_entropy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n)</span>
<span id="cb7-28"></span>
<span id="cb7-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a data frame for plotting</span></span>
<span id="cb7-30">plot_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> alpha_values, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">entropy =</span> entropy_values)</span>
<span id="cb7-31"></span>
<span id="cb7-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot the entropy values</span></span>
<span id="cb7-33"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(plot_data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> alpha, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> entropy)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb7-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Entropy of a Symmetric Dirichlet Distribution"</span>,</span>
<span id="cb7-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"for n ="</span>, n, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dimensions"</span>),</span>
<span id="cb7-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alpha (Concentration Parameter)"</span>,</span>
<span id="cb7-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Entropy"</span></span>
<span id="cb7-40">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(entropy_values) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>,</span>
<span id="cb7-43">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alpha = 1 (Uniform Distribution)"</span>,</span>
<span id="cb7-44">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb7-47">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-7-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/note-on-dirichlet/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">k <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb8-2"></span>
<span id="cb8-3">mean_dir <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(k))</span>
<span id="cb8-4">var_dir <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(k))</span>
<span id="cb8-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(k)) {</span>
<span id="cb8-6">  alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> k[i]</span>
<span id="cb8-7">  sum_alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb8-8">  mean_dir[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sum_alpha</span>
<span id="cb8-9">  var_dir[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (sum_alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (sum_alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (sum_alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb8-10">}</span>
<span id="cb8-11">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> mean_dir, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variance =</span> var_dir)</span>
<span id="cb8-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> variance)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb8-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variance of Dirichlet Distribution vs Alpha Parameter"</span>,</span>
<span id="cb8-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alpha Parameter (k)"</span>,</span>
<span id="cb8-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variance"</span></span>
<span id="cb8-18">  ) </span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-8-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/note-on-dirichlet/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</div>
</div>
<p>The plot on the right shows the variance of the Dirichlet distribution as a function increasing <img src="https://latex.codecogs.com/png.latex?%5Calpha">. We see that the variance decreases towards zero as <img src="https://latex.codecogs.com/png.latex?%5Calpha"> increases. This is reflected in the second animation above which shows the distribution concentrating on the mean. Uncertainty is going to zero but differential entropy is shooting off towards to infinity. If you are nevertheless intrigued by differential entropy, you may want to have a look at the references I have included below.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><p>Bela A. Frigyik, Amol Kapila, and Maya R. Gupta, <a href="https://web.archive.org/web/20150219021331/https://www.ee.washington.edu/techsite/papers/documents/UWEETR-2010-0006.pdf">Introduction to the Dirichlet Distribution and Related Processes</a></p></li>
<li><p>Thomas M. Cover and Joy A. Thomas, <em>Elements of Information Theory</em>, Wiley-Interscience Edition: 2nd Edition, (2006)</p></li>
<li><p>Rameshwar Gupta and Donald St.&nbsp;P. Richards, <a href="https://en.wikipedia.org/wiki/Peter_Gustav_Lejeune_Dirichlets">The History of the Dirchet and Liouville Distributions</a> international Statistical Review (2001)</p></li>
<li><p>Jiayu Lin, <a href="https://qspace.library.queensu.ca/server/api/core/bitstreams/0a2eb9d4-145a-4e76-84eb-1aa16d6670aa/content">On the Dirichlet Distribution</a></p></li>
</ul>


</section>

 ]]></description>
  <guid>https://rworks.dev/posts/note-on-dirichlet/</guid>
  <pubDate>Mon, 22 Sep 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/note-on-dirichlet/anim2.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>July 2025 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/july-2025-top-40-new-cran-packages/</link>
  <description><![CDATA[ 





<div class="columns">
<div class="column" style="width:45%;">
<section id="causal-inference" class="level3">
<h3 class="anchored" data-anchor-id="causal-inference">Causal Inference</h3>
<p><a href="https://cran.r-project.org/package=ciflyr">ciflyr</a> v0.1.1: Provides a framework for specifying flexible linear-time reachability-based algorithms for graphical causal inference. Rule tables are used to encode and customize the ‘reachability’ algorithm for typical causal and probabilistic reasoning tasks, such as finding d-connected nodes or more advanced applications. See <a href="https://arxiv.org/abs/2506.15758">Wienöbst, Weichwald and Henckel (2025)</a> for the details.</p>
</section>
<section id="computational-methods" class="level3">
<h3 class="anchored" data-anchor-id="computational-methods">Computational Methods</h3>
<p><a href="https://cran.r-project.org/package=hmde">hmde</a> v1.2.1: Implements a wrapper for <code>Stan</code> with a built-in hierarchical Bayesian longitudinal model for repeat observation data. Model choice selects the differential equation that is fit to the observations. See <a href="https://besjournals.onlinelibrary.wiley.com/doi/10.1111/2041-210X.14463">O’Brien et al.&nbsp;(2024)</a> for the theory. There are six vignettes, including <a href="https://cran.r-project.org/web/packages/hmde/vignettes/constant-growth.html">Constant Growth</a> and <a href="https://cran.r-project.org/web/packages/hmde/vignettes/hmde_for_mathematicians.html">hmde for Mathematicians</a>.</p>
<p><a href="hmde.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/hmde.png" class="img-fluid" alt="Canham growth curve"></a></p>
<p><a href="https://cran.r-project.org/package=ironseed">ironseed</a> v0.2.0: Implements a procedure for seeding R’s built in random number generators using a variable-length sequence of values. The algorithm accumulates entropy into a 256-bit hash digest or “ironseed” and generates a variable-length sequence of output seeds. See the <a href="https://cran.r-project.org/web/packages/ironseed/readme/README.html">vignette</a> for an overview.</p>
<p><a href="https://cran.r-project.org/package=RBaM">RBaM</a> v1.0.1: Implements an interface to the <code>BaM</code> (Bayesian Modeling) engine, a <code>Fortran</code> - based executable for Bayesian estimation with a particular focus on uncertainty quantification. See <a href="https://hal.science/hal-02606929v1">Renard (2017)</a> for technical details and the <a href="https://cran.r-project.org/web/packages/RBaM/readme/README.html">vignette</a> for examples.</p>
<p><a href="RBaM.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/RBaM.png" class="img-fluid" alt="Posterior Distribution Curves"></a></p>
</section>
<section id="data" class="level3">
<h3 class="anchored" data-anchor-id="data">Data</h3>
<p><a href="https://cran.r-project.org/package=ArgentinAPI">ArgentinAPI</a> v0.1.0: Provides functions to access data from the <a href="https://argentinadatos.com/">ArgentinaDatos API</a> and the <a href="https://restcountries.com/">REST Countries API</a> related to Argentina’s exchange rates, inflation, political figures, holidays, and general country-level indicators. Additionally, the package includes curated datasets related to Argentina, covering topics such as economic indicators, biodiversity, agriculture, human rights, genetic data, and consumer prices. See the <a href="https://cran.r-project.org/web/packages/ArgentinAPI/vignettes/ArgentinAPI_vignette.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=bcRP">bcRP</a> v1.0.1: Enables search and access for more than ten thousand BANCO CENTRAL DE RESERVA DEL PERÚ Gerencia Central de Estudios Económicos (<a href="https://estadisticas.bcrp.gob.pe/estadisticas/series/ayuda/bcrpdata">BCRPDATA</a>) data sets. See <a href="https://cran.r-project.org/web/packages/bcRP/readme/README.html">README</a> for an example.</p>
<p><a href="https://cran.r-project.org/package=ChileDataAPI">ChileDataAPI</a> v0.1.0: Provides functions to access data from the <a href="https://restcountries.com/">FINDIC API</a> and the <a href="https://findic.cl">REST Countries API</a>, related to Chile’s financial indicators, international country information, and more. Additionally, the package includes curated datasets related to Chile, covering topics such as human rights violations during the Pinochet regime, electoral data, census samples, health surveys, seismic events, territorial codes, and environmental measurements. See the <a href="https://cran.r-project.org/web/packages/ChileDataAPI/vignettes/ChileDataAPI_vignette.html">vignette</a>.</p>
<p><a href="Chile.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/Chile.png" class="img-fluid" alt="Ploot of Blood pressuredata by Age and Sex"></a></p>
<p><a href="https://cran.r-project.org/package=galaxias">galaxias</a> v0.1.0: Provides tools to access the <a href="https://dwc.tdwg.org/">Darwin Core</a> data standard for biodiversity information. For details, see <a href="https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0029715">Wieczorek et al.&nbsp;(2012)</a>. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/galaxias/vignettes/quick_start_guide.html">Quick Start Guide</a> and <a href="https://cran.r-project.org/web/packages/galaxias/vignettes/events-example.html">Standardize an Event Dataset</a>.</p>
<p><a href="https://cran.r-project.org/package=getRad">getRad</a> v0.2.0: Provides functions to load polar volume and vertical profile data from several sources in Europe and the US for aeroecological research. See the <a href="https://cran.r-project.org/web/packages/getRad/vignettes/supported_sources.html">vignette</a> and <a href="https://aloftdata.github.io/getRad/">GitHub page</a> for examples.</p>
<p><a href="getRad.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/getRad.png" class="img-fluid" alt="Polar volume plot"></a></p>
<p><a href="https://cran.r-project.org/package=MexicoDataAPI">MexicoDataAPI</a> v0.1.0: Provides functions to access data from the <a href="https://datahelpdesk.worldbank.org/knowledgebase/articles/889392">World Bank API</a> and the <a href="https://restcountries.com/">REST Countries API</a>, related to Mexico’s economic indicators, population statistics, literacy rates, and international geopolitical information. Additionally, the package includes curated datasets related to Mexico, including air quality monitoring stations, pollution zones, income surveys, postal abbreviations, election studies, forest productivity, and demographic data by state. See the <a href="https://cran.r-project.org/web/packages/MexicoDataAPI/vignettes/MexicoDataAPI_vignette.html">vignette</a>.</p>
<p><a href="Mexico.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/Mexico.png" class="img-fluid" alt="Plot of Average Household Data by Education Level"></a></p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://cran.r-project.org/package=commecometrics">commecometrics</a> v1.0.0: Provides a framework for modeling relationships between functional traits and environmental variables at the community level, including tools for trait binning, likelihood-based environmental estimation, model evaluation, fossil projection into modern ecometric space, and visualization. For more details, see <a href="https://link.springer.com/chapter/10.1007/978-3-319-94265-0_17">Vermillion et al.&nbsp;(2018)</a>, <a href="https://royalsocietypublishing.org/doi/10.1098/rspb.2010.2233">Polly et al.&nbsp;(2011)</a>, and <a href="https://www.cambridge.org/core/journals/the-paleontological-society-papers/article/abs/measuring-earthlife-transitions-ecometric-analysis-of-functional-traits-from-late-cenozoic-vertebrates/C0068D2B30ACCF76F415B2EB75F8BEB2">Polly &amp; Head (2015)</a>. There is an <a href="https://cran.r-project.org/web/packages/commecometrics/vignettes/introduction_to_commecometrics.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/commecometrics/vignettes/fossil_commecometrics.html">Fossil Commecometrics</a>.</p>
<p><a href="https://cran.r-project.org/package=ConFluxPro">ConFluxPro</a> v1.3.1: Provides tools to model soil gas fluxes with the Flux-Gradient Method, including functions for data handling, a forward and an inverse model for flux modeling, and methods for calibration and uncertainty estimation. For more details, see <a href="https://joss.theoj.org/papers/10.21105/joss.08094">Gartiser et al.&nbsp;(2025a)</a> and <a href="https://bsssjournals.onlinelibrary.wiley.com/doi/10.1111/ejss.70126">Gartiser et al.&nbsp;(2025b)</a>. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/ConFluxPro/vignettes/flux.html">Flux</a> and <a href="https://cran.r-project.org/web/packages/ConFluxPro/vignettes/uncertainty.html">Uncertainty Estimation</a>.</p>
<p><a href="ConFlux.svg" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/ConFlux.svg" class="img-fluid" alt="Concentration Plots"></a></p>
</section>
<section id="epidemilogy" class="level3">
<h3 class="anchored" data-anchor-id="epidemilogy">Epidemilogy</h3>
<p><a href="https://cran.r-project.org/package=Pv3Rs">Pv3Rs</a> v0.0.2: Provides functions to plot malaria parasite genetic data on two or more episodes and compute the per-person posterior probabilities that each Plasmodium vivax (Pv) recurrence is a recrudescence, relapse, or reinfection (3Rs) using per-person P. vivax genetic data on two or more episodes. It uses the statistical model described in <a href="https://www.medrxiv.org/content/10.1101/2022.11.23.22282669v1">Taylor, Foo and White (2022</a>. See the <a href="https://cran.r-project.org/web/packages/Pv3Rs/vignettes/demonstrate-usage.html">vignette</a> for examples.</p>
<p><a href="Pv3Rs.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/Pv3Rs.png" class="img-fluid" alt="Simplex plot of per-recurrence posterior probabilities"></a></p>
<p><a href="https://cran.r-project.org/package=rtestim">rtestim</a> v1.0.0: Use trend filtering, a type of regularized nonparametric regression, to estimate the instantaneous reproduction number, Rt, which indicates how many new infections will result from each new infection today. See <a href="https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1012324">Liu et al.(2024)</a> for background. There is an <a href="https://cran.r-project.org/web/packages/rtestim/vignettes/rtestim.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/rtestim/vignettes/delay-distributions.html">Delay Distributions</a>.</p>
<p><a href="rtestim.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/rtestim.png" class="img-fluid" alt="Plot of variants in circulation"></a></p>
</section>
<section id="machine-learning" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning">Machine Learning</h3>
<p><a href="https://cran.r-project.org/package=flowcluster">flowcluster</a> v0.2.1: Provides functions for clustering origin-destination pairs flows. Includes functions for creating distance matrices between OD pairs and passing distance matrices to a clustering algorithm. See <a href="https://onlinelibrary.wiley.com/doi/10.1111/gean.12100">Tao &amp; Thill (2016)</a> for details on the spatial clustering of flows, <a href="https://www.sciencedirect.com/science/article/pii/S2667091725000378?via%3Dihub">Mahfouz et al.&nbsp;(2025)</a> for an extended example, and <a href="https://cran.r-project.org/web/packages/flowcluster/readme/README.html">README</a> to get started.</p>
<p><a href="flow.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/flow.png" class="img-fluid" alt="Plot of clustered flows"></a></p>
<p><a href="https://cran.r-project.org/package=mrIML">mrIML</a> v2.1.0: Provides machine learning techniques to automatically fit multiple response models to multivariate data and interpret the results. See <a href="https://onlinelibrary.wiley.com/doi/10.1111/1755-0998.13495">Fountain-Jones (2021)</a> and <a href="https://www.authorea.com/users/729149/articles/1226458-advancing-ecological-community-analysis-with-mriml-2-0-unravelling-taxa-associations-through-interpretable-machine-learning?commit=9dfb71ae58318bc0d38e5121f061c63dd9861ae9">Fountain-Jones et al.&nbsp;(2024)</a> and the <a href="https://cran.r-project.org/web/packages/mrIML/vignettes/mrIML.html">vignette</a> for examples.</p>
<p><a href="mrIML.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/mrIML.png" class="img-fluid" alt="Multiple Importance Plots"></a></p>
<p><a href="https://cran.r-project.org/package=Silhouette">Silhouette</a> v0.94: Provides tools to quantify clustering quality by measuring both cohesion within clusters and separation between clusters. See <a href="https://www.tandfonline.com/doi/full/10.1080/10618600.2022.2050249">Raymaekers &amp; Rousseeuw, (2022)</a>, <a href="https://www.sciencedirect.com/science/article/abs/pii/S0165011406002892?via%3Dihub">Campello &amp; Hruschka, 2006</a>, and <a href="https://link.springer.com/article/10.1007/s00357-008-9005-9">Schepers et al.</a> for background and the <a href="https://cran.r-project.org/web/packages/Silhouette/vignettes/Silhouette.html">vignette</a> for examples.</p>
<p><a href="Silhouette.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/Silhouette.png" class="img-fluid" alt="Clusters Silhouette Plot"></a></p>
<p><a href="https://cran.r-project.org/package=survdnn">survdnn</a> v0.6.0: Implements deep learning models for right-censored survival data using the <code>torch</code> back-end that include Cox partial likelihood, L2-penalized Cox, time-dependent Cox, and accelerated failure time loss models. Also provides a formula-based interface, built-in support for cross-validation, hyperparameter tuning, survival curve plotting, and evaluation metrics such as the C-index, Brier score, and integrated Brier score. See <a href="https://www.jmlr.org/papers/v20/18-424.html">Kvamme et al.&nbsp;(2019)</a> for details and look <a href="https://github.com/ielbadisy/survdnn">here</a> for examples.</p>
<p><a href="survdnn.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/survdnn.png" class="img-fluid" alt="Survival Curves"></a></p>
</section>
<section id="mathematics" class="level3">
<h3 class="anchored" data-anchor-id="mathematics">Mathematics</h3>
<p><a href="https://cran.r-project.org/package=boostmath">boostmath</a> v1.0.2: Provides <code>R</code> bindings for the various functions and statistical distributions provided by the <a href="https://www.boost.org/doc/libs/latest/libs/math/doc/html/index.html"><code>Boost</code></a> Math library. There are four vignettes, including <a href="https://cran.r-project.org/web/packages/boostmath/vignettes/quadrature-differentiation.html">Quadrature and Differention</a> and <a href="https://cran.r-project.org/web/packages/boostmath/vignettes/special-functions.html">Special Functions</a>.</p>
<p><a href="https://cran.r-project.org/package=inteq">inteq</a> v1.0: Implements Matthew Thomas’s <em>Python</em> library <code>inteq</code>. Solves Fredholm integral equations of the first kind, <img src="https://latex.codecogs.com/png.latex?f(s)%20=%20%5Cint_a%5Eb%20K(s,%20y)%20g(y)%20dy">, using methods described by <a href="https://dl.acm.org/doi/10.1145/321150.321157">Twomey (1963)</a>. Solves Volterra integral equations of the first kind, <img src="https://latex.codecogs.com/png.latex?f(s)%20=%20%5Cint_0%5Es%20K(s,y)%20g(t)%20dt">, using methods from <a href="https://arxiv.org/abs/2106.08496">Betto and Thomas (2021)</a>. Solves Voltera integral equations of the second kind, <img src="https://latex.codecogs.com/png.latex?g(s)%20=%20f(s)%20+%20%5Cint_a%5Es%20K(s,y)%20g(y)%20dy">, using methods from <a href="https://epubs.siam.org/doi/10.1137/0706034">Linz (1969)</a>. See the <a href="https://cran.r-project.org/web/packages/inteq/vignettes/documentation.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=topolow">topolow</a> v2.0.0: Implements the Topolow algorithm to embed objects into a low-dimensional Euclidean space from a matrix of pairwise dissimilarities, even when the data do not satisfy metric or Euclidean axioms. The algorithm is particularly well-suited for sparse, incomplete, and censored (thresholded) datasets such as antigenic relationships. It employs a likelihood-based approach to determine the optimal number of dimensions for the embedding, avoiding distortions common in methods with fixed low dimensions. See <a href="https://academic.oup.com/bioinformatics/article/41/7/btaf372/8173949?login=false">Arhami &amp; Rohani (2025a)</a> for methods and mathematical proofs, <a href="https://arxiv.org/abs/2508.01733">Arhami and Rohani (2025b)</a> for embedding details, and the <a href="https://cran.r-project.org/web/packages/topolow/vignettes/Comprehensive_Evaluation.html">vignette</a> for an introduction.</p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="medical-statistics" class="level3">
<h3 class="anchored" data-anchor-id="medical-statistics">Medical Statistics</h3>
<p><a href="https://cran.r-project.org/package=GHRexplore">GHRexplore</a> v0.1.1: Provides commonly used visualizations of temporal and spatio-temporal health data including case counts, incidence rates, and covariates including time series, heatmaps, seasonality plots, maps and more. See the <a href="https://cran.r-project.org/web/packages/GHRexplore/vignettes/GHRexplore.html">vignette</a>.</p>
<p><a href="GHRexplore.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/GHRexplore.png" class="img-fluid" alt="Time series plot}"></a></p>
<p><a href="https://cran.r-project.org/package=MDCcure">MDCcure</a> v0.1.0: Provides functions to compute martingale difference correlation, martingale difference divergence, and partial extensions to assess conditional mean dependence. Also implements a novel hypothesis test for evaluating covariate effects on the cure rate in mixture cure models. See <a href="https://www.tandfonline.com/doi/full/10.1080/01621459.2014.887012">Shao &amp; Zhang (2014)</a> for background on the martingale methods and the <a href="https://cran.r-project.org/web/packages/MDCcure/vignettes/MDCcure.html">vignette</a> for information on the covariate effects test.</p>
</section>
<section id="pharma" class="level3">
<h3 class="anchored" data-anchor-id="pharma">Pharma</h3>
<p><a href="https://cran.r-project.org/package=junco">junco</a> v0.1.1: Provides general and reusable tooling for use alongside <code>rtables</code>, <code>rlistings</code>, and <code>tern</code> for creating table and listing outputs. Major features include: extended statistical analyses beyond what <code>tern</code> supports for safety and efficacy tables, a robust production-grade RTF exporter for both tables and listings, structural support for spanning column headers and risk difference columns in tables, and robust font-aware automatic column width algorithms for both listings and tables. See the vignettes <a href="https://cran.r-project.org/web/packages/junco/vignettes/auto_colwidths.html">Automatic Column Widths</a> and <a href="https://cran.r-project.org/web/packages/junco/vignettes/junco.html">Tabulation</a>.</p>
<p><a href="https://cran.r-project.org/package=verifyr2">verifyr2</a> v1.0.0: Provides Extendable <code>R6</code> file comparison classes, and a <code>shiny</code> App for integrating comparison functions into file comparison applications. The idea originates from the pharma drug development processes, which require different versions of files and datasets to be compared and reviewed. See <a href="https://cran.r-project.org/web/packages/verifyr2/readme/README.html">README</a>.</p>
</section>
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=GCEstim">GCEstim</a> v0.1.0: Provides functions for regression estimation and inference using the Generalized Maximum Entropy (GME) and Generalized Cross Entropy (GCE) framework, a flexible method for solving ill-posed inverse problems and parameter estimation under uncertainty. See <a href="https://link.springer.com/chapter/10.1007/978-3-031-97589-9_21">Macedo et al.&nbsp;(2025)</a>, <a href="https://link.springer.com/chapter/10.1007/978-3-030-26036-1_2">Macedo &amp; Costa (2019)</a>, and <a href="https://www.tandfonline.com/doi/full/10.1080/03610918.2022.2057540">Macedo (2022)</a> for background. There are seven vignettes, including <a href="https://cran.r-project.org/web/packages/GCEstim/vignettes/V1_Quick_start.html">Quick Start</a> and <a href="https://cran.r-project.org/web/packages/GCEstim/vignettes/V2_GME_framework.html">Generalized Maximum Entropy framework</a>.</p>
<p><a href="GCEstim.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/GCEstim.png" class="img-fluid" alt="Prediction Error vs Supports"></a></p>
<p><a href="https://cran.r-project.org/package=gilmour">gilmour</a> v0.1.1: Implements the method described in <a href="https://www.jstor.org/stable/2348411?origin=crossref">Gilmour (1996)</a> for selecting a subset of regressors from a set of k candidate variables in multiple linear regression. All possible regression models are compared using Mallows’s Cp statistic, and the submodel with the minimum adjusted value Cp (ModelMin) is identified. The final model is selected by applying a sequence of hypothesis tests on submodels nested within ModelMin. See the <a href="https://cran.r-project.org/web/packages/gilmour/vignettes/gilmour-spec.html">vignette</a> for an overview and example.</p>
<p><a href="Gilmour.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/Gilmour.png" class="img-fluid" alt="Diagram of model selection process"></a></p>
<p><a href="https://cran.r-project.org/package=hbsaems">hbsaems</a> v0.1.1: Implements Hierarchical Bayesian Small Area Estimation models using the <code>brms</code> package to facilitate a principled Bayesian workflow, enabling users to conduct prior predictive checks, model fitting, posterior predictive checks, model comparison, and sensitivity analysis in a coherent and reproducible manner. See <a href="https://onlinelibrary.wiley.com/doi/book/10.1002/9781118735855">Rao and Molina (2015)</a> for background on the methods implemented. There are seven vignettes, including <a href="https://cran.r-project.org/web/packages/hbsaems/vignettes/hbsaems-modelling.html">Hierarchical Bayesian Small Area Modeling</a> and <a href="https://cran.r-project.org/web/packages/hbsaems/vignettes/hbsaems-binlogitnorm-model.html">Logit-normal model</a> and a <a href="https://cran.r-project.org/web/packages/hbsaems/vignettes/hbsaems-run_sae_app.html"><code>shiny</code>dashboard</a>.</p>
<p><a href="https://cran.r-project.org/package=ROCnGO">ROCnGO</a> v0.1.0: Provides tools for analyzing classifier performance by using receiver operating characteristic (ROC) curves. Performance may be assessed on a single or multiple classifier. Different metrics allow the evaluation of local performance when working within restricted ranges of sensitivity and specificity. For details on the different implementations, see <a href="https://journals.sagepub.com/doi/10.1177/0272989X8900900307">McClish (1989)</a>, <a href="https://link.springer.com/article/10.1007/s11634-017-0295-9">Vivo et al.&nbsp;(2018)</a>, <a href="https://pubs.rsna.org/doi/10.1148/radiology.201.3.8939225">Jiang et al.&nbsp;(1996)</a>, <a href="https://www.mdpi.com/2227-7390/9/21/2826">Franco &amp; Vivo (2021)</a>, and <a href="https://bmcmedinformdecismak.biomedcentral.com/articles/10.1186/s12911-019-1014-6">Carrington et al.&nbsp;(2020)</a>. There is an <a href="https://cran.r-project.org/web/packages/ROCnGO/vignettes/ROCnGO.html">Introduction</a> and another vignette on <a href="https://cran.r-project.org/web/packages/ROCnGO/vignettes/selecting-condition.html">Selecting condition of interest</a>.</p>
<p><a href="https://cran.r-project.org/package=smoothemplik">smoothemplik</a> v0.0.15: Uses <code>Rcpp</code> to implement Kernel-based non-parametric methods for density and regression estimation and numerical routines for empirical likelihood maximization. See <a href="https://onlinelibrary.wiley.com/doi/abs/10.1111/j.1468-0262.2004.00550.x">Kitamura, Tripathi &amp; Ahn (2004)</a> and <a href="https://onlinelibrary.wiley.com/doi/10.1002/cjs.11183">Owen (2013)</a> for background and the two vignettes <a href="https://cran.r-project.org/web/packages/smoothemplik/vignettes/choice-of-SEL-weights.html">Choosing Weights</a> and <a href="https://cran.r-project.org/web/packages/smoothemplik/vignettes/non-parametric-rcpp.html">Using Rcpp</a>.</p>
<p><a href="smooth.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/smooth.png" class="img-fluid" alt="3 D plots showing differences between kernels"></a></p>
</section>
<section id="time-series" class="level3">
<h3 class="anchored" data-anchor-id="time-series">Time Series</h3>
<p><a href="https://cran.r-project.org/package=calcal">calcal</a> v1.0.0: Implements the algorithms described in <a href="https://www.cambridge.org/core/books/calendrical-calculations/B897CA3260110348F1F7D906B8D9480D">Reingold and Dershowitz</a> that allow conversion between many different calendar systems and calculate cultural and religious holidays. See the <a href="https://cran.r-project.org/web/packages/calcal/vignettes/new_calendar.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=mantis">mantis</a> v0.4.3: Provides tools to generate interactive html reports that enable quick visual review of multiple related time series. The reports are self-contained and shareable without a web server. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/mantis/vignettes/alert-rules.html">Get Started</a> and <a href="https://cran.r-project.org/web/packages/mantis/vignettes/mantis.html">Specifying Alerting Rules</a>.</p>
<p><a href="mantis.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/mantis.png" class="img-fluid" alt="Multiple Selected TIme Series"></a></p>
<p><a href="https://cran.r-project.org/package=REPS">REPS</a> v1.0.0: Provides functions to compute price indices using various Hedonic and multilateral methods, including Laspeyres, Paasche, Fisher, and HMTS (Hedonic Multilateral Time series re-estimation with splicing). Functions support index construction workflows across a wide range of domains, including but not limited to real estate, where quality-adjusted price comparisons over time are essential. The HMTS method implemented here is described in <a href="https://journals.sagepub.com/doi/10.1177/0282423X241246617">Ishaak, Ouwehand &amp; Remøy (2024)</a>. See the vignettes <a href="https://cran.r-project.org/web/packages/REPS/vignettes/calculate_price_index.html">Getting Started with Price Index Calculation</a> and <a href="https://cran.r-project.org/web/packages/REPS/vignettes/calculate_regression_diagnostics.html">Regression Diagnostics by Period</a>.</p>
<p><a href="REPS.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/REPS.png" class="img-fluid" alt="Regression Diagnostic Plots"></a></p>
<p><a href="https://cran.r-project.org/package=tEDM">tEDM</a> v1.0: Implements methods to infer causation from time series data through empirical dynamic modeling (EDM). Methods include convergent cross mapping from <a href="https://www.science.org/doi/10.1126/science.1227079">Sugihara et al.</a>, partial cross mapping as outlined in <a href="https://www.nature.com/articles/s41467-020-16238-0">Leng et al.&nbsp;(2020)</a>, and cross mapping cardinality as described in <a href="https://www.sciencedirect.com/science/article/pii/S2667325823000122?via%3Dihub">Tao et al.&nbsp;(2023)</a>. See the <a href="https://cran.r-project.org/web/packages/tEDM/vignettes/tEDM.html">vignette</a> for examples.</p>
<p><a href="tEDM.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/tEDM.png" class="img-fluid" alt="Diagram illustrating causal inference "></a></p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=geeLite">geeLite</a> v1.0.2: Simplifies the creation, management, and updating of local databases using data extracted from <a href="https://earthengine.google.com/">Google Earth Engine</a> (GEE) by providing tools that leverage <code>SQLite</code> to store, aggregate, and process spatio-temporal data. Utilities support real-time monitoring and analysis of geospatial features, making it suitable for researchers and practitioners in geospatial science. See <a href="https://hdl.handle.net/10986/43165">Kurbucz &amp; Andrée (2025)</a> for details and the <a href="https://cran.r-project.org/web/packages/geeLite/vignettes/geeLite.html">vignette</a> for examples.</p>
<p><a href="geeLite.png" class="lightbox" data-gallery="quarto-lightbox-gallery-20"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/geeLite.png" class="img-fluid" alt="Gootle Earth Map"></a></p>
<p><a href="https://cran.r-project.org/package=otel">otel</a> v0.1.0: Implements a collection of tools based on the <a href="https://opentelemetry.io/docs/specs/otel/">OpenTelemetry API</a> to instrument software to generate, collect, and export telemetry data (metrics, logs, and traces) to analyze the software and understand its performance and behavior. See <a href="https://cran.r-project.org/web/packages/otel/readme/README.html">README</a> for an example.</p>
<p><a href="https://cran.r-project.org/package=qryflow">qryflow</a> v0.1.0: Provides functions to execute multi-step <code>SQL</code> workflows by leveraging specially formatted comments to define and control execution. This enables users to mix queries, commands, and metadata within a single script. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/qryflow/vignettes/qryflow.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/qryflow/vignettes/advanced-qryflow.html">Advanced Usage</a>.</p>
<p><a href="https://cran.r-project.org/package=staninside">staninside</a> v0.0.4: Provides infrastructure and functions for integrating <a href="https://www.jstatsoft.org/article/view/v076i01"><code>Stan</code></a> code into standalone <code>R</code> packages, which, in turn, use the <a href="https://github.com/stan-dev/cmdstan"><code>CmdStan</code></a> engine. see the <a href="https://cran.r-project.org/web/packages/staninside/vignettes/using-staninside.html">vignette</a>.</p>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=ggchord">ggchord</a> v0.2.0: Extends <code>ggplot2</code> to visualize pairwise <a href="https://blast.ncbi.nlm.nih.gov/Blast.cgi">BLAST</a> alignment results as chord diagrams, intuitively displaying homologous regions between query and subject sequences. See the <a href="https://cran.r-project.org/web/packages/ggchord/vignettes/ggchord_vignette.html">vignette</a>.</p>
<p><a href="ggchord.png" class="lightbox" data-gallery="quarto-lightbox-gallery-21"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/ggchord.png" class="img-fluid" alt="Chord Plot of BLAST alignments"></a></p>
<p><a href="https://cran.r-project.org/package=moveEZ">moveEZ</a> v1.1.0: Provides functions to create animated biplots that enable dynamic visualization of temporal or sequential changes in multivariate data by animating a single biplot across the levels of a time variable. See the <a href="https://cran.r-project.org/web/packages/moveEZ/vignettes/moveEZ.html">vignette</a>.</p>
<p><a href="moveEZ.png" class="lightbox" data-gallery="quarto-lightbox-gallery-22"><img src="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/moveEZ.png" class="img-fluid" alt="Multiple biplots"></a></p>
</section>
</div>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/july-2025-top-40-new-cran-packages/</guid>
  <pubDate>Wed, 27 Aug 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/july-2025-top-40-new-cran-packages/Pv3RS.png" medium="image" type="image/png"/>
</item>
<item>
  <title>ANES 2024 is Out! How to Analyze the Data with R</title>
  <dc:creator>Isabella Velásquez</dc:creator>
  <link>https://rworks.dev/posts/anes2024/</link>
  <description><![CDATA[ 





<p>Last fall, my co-authors <a href="https://www.linkedin.com/in/stephanie-zimmer/">Stephanie Zimmer</a>, <a href="https://www.linkedin.com/in/rebecca-powell-1416226b/">Rebecca Powell</a>, and I released <a href="https://tidy-survey-r.github.io/tidy-survey-book/">Exploring Complex Survey Data Analysis Using R</a>. Earlier this August, we had the joy of teaching a <a href="https://tidy-survey-r.github.io/user-2025-survey-data-tutorial/">workshop</a> based on the book at <a href="https://user2025.r-project.org/">useR! 2025</a>.</p>
<p>One of our favorite examples in both the book and workshop comes from the <a href="https://electionstudies.org/">American National Election Studies (ANES)</a>, a long-running project from Stanford University and the University of Michigan supported by the National Science Foundation. Since 1948, ANES has fielded surveys almost every two years, making it one of the richest resources for studying public opinion and voting behavior in U.S. elections. The data cover everything from party affiliation to trust in government, and are carefully designed to represent the voting population.</p>
<p>Just a couple of days after returning from useR!, I was thrilled to see that the brand-new ANES 2024 dataset had been released!</p>
<center>
<blockquote class="bluesky-embed blockquote" data-bluesky-uri="at://did:plc:l5kkuquemh5xnycs2axptlam/app.bsky.feed.post/3lw7tc5qqic2z" data-bluesky-cid="bafyreibnu557kdchflh56tq6ecqchxuiw455j3i3mqfjit67tlrutfw7vq" data-bluesky-embed-color-mode="system">
<p lang="en">
The full release of the ANES 2024 Time Series #Data is now available. More details here: electionstudies.org/anes-announc…<br><br><a href="https://bsky.app/profile/did:plc:l5kkuquemh5xnycs2axptlam/post/3lw7tc5qqic2z?ref_src=embed">[image or embed]</a>
</p>
— American National Election Studies (<a href="https://bsky.app/profile/did:plc:l5kkuquemh5xnycs2axptlam?ref_src=embed"><span class="citation" data-cites="electionstudies.bsky.social">@electionstudies.bsky.social</span></a>) <a href="https://bsky.app/profile/did:plc:l5kkuquemh5xnycs2axptlam/post/3lw7tc5qqic2z?ref_src=embed">August 12, 2025 at 12:24 PM</a>
</blockquote>
<script async="" src="https://embed.bsky.app/static/embed.js" charset="utf-8"></script>
</center>
<p>Survey data like ANES are not as straightforward as a simple random sample. Some groups are intentionally oversampled, others are harder to reach, and not every respondent has the same probability of being included. To correct for this, surveys provide “weights”, or numbers that tell us how much each response should “count” when estimating results for the whole population.</p>
<p>If we ignore these weights and just calculate raw percentages, we risk drawing the wrong conclusions. That’s where the <a href="https://r-survey.r-forge.r-project.org/survey/">{survey}</a> and <a href="http://gdfe.co/srvyr/">{srvyr}</a> packages come in: they handle weights, strata, and clusters behind the scenes so our estimates properly reflect the survey design.</p>
<p>So, let’s walk through how you can get started analyzing this dataset in R with the {survey} and {srvyr} packages. For more depth, the <a href="https://tidy-survey-r.github.io/tidy-survey-book/c04-getting-started.html">Getting Started</a> chapter of our book is a handy companion.</p>
<section id="download-the-data" class="level2">
<h2 class="anchored" data-anchor-id="download-the-data">Download the data</h2>
<p>Head to the <a href="https://electionstudies.org/data-center/2024-time-series-study/">Election Studies website</a> and grab the CSV version of the 2024 Time Series study. The download comes as a Zip file that includes both the dataset and documentation.</p>
</section>
<section id="load-packages-and-data" class="level2">
<h2 class="anchored" data-anchor-id="load-packages-and-data">Load packages and data</h2>
<p>We’ll use the tidyverse for wrangling, and {survey} plus {srvyr} for analysis:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(survey)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(srvyr)</span>
<span id="cb1-4"></span>
<span id="cb1-5">anes_2024 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anes_timeseries_2024_csv_20250808.csv"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="review-the-documentation" class="level2">
<h2 class="anchored" data-anchor-id="review-the-documentation">Review the documentation</h2>
<p><a href="https://tidy-survey-r.github.io/tidy-survey-book/c03-survey-data-documentation.html">Reviewing the survey documentation</a> is a critical part of survey analysis. The <a href="https://electionstudies.org/wp-content/uploads/2025/08/anes_timeseries_2024_userguidecodebook_20250808.pdf">ANES materials</a> include details about the sample, weighting, and codebook.</p>
<section id="sample" class="level3">
<h3 class="anchored" data-anchor-id="sample">Sample</h3>
<p>The 2024 sample was designed to represent U.S. citizens age 18 or older. Respondents were recruited from both in-person and web samples, with careful notes on eligibility and exclusions.</p>
</section>
<section id="codebook" class="level3">
<h3 class="anchored" data-anchor-id="codebook">Codebook</h3>
<p>The codebook decodes variable names into something human-readable.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="codebook.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/anes2024/codebook.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3); border-radius: 5px;;width:40.0%"></a></p>
</figure>
</div>
<p>Here are two variables we’ll use:</p>
<ul>
<li><code>V240002b</code>: MODE OF INTERVIEW: POST-ELECTION: This variable captures the mode in which the respondent completed the majority of the post-election survey.
<ul>
<li>-7. Insuﬃcient parital, interview deleted</li>
<li>-6. No post interview</li>
<li><ol type="1">
<li>Face-to-face (FTF)</li>
</ol></li>
<li><ol start="2" type="1">
<li>Internet (WEB)</li>
</ol></li>
<li><ol start="3" type="1">
<li>Paper (PAPI)</li>
</ol></li>
<li><ol start="4" type="1">
<li>Phone</li>
</ol></li>
<li><ol start="5" type="1">
<li>Video</li>
</ol></li>
</ul></li>
<li><code>V241229</code>: PRE: HOW OFTEN TRUST GOVERNMENT IN WASHINGTON TO DO WHAT IS RIGHT [REVISED]:
<ul>
<li>-9. Refused</li>
<li>-8. Don’t know</li>
<li>-1. Inapplicable</li>
<li><ol type="1">
<li>Always</li>
</ol></li>
<li><ol start="2" type="1">
<li>Most of the time</li>
</ol></li>
<li><ol start="3" type="1">
<li>About half the time</li>
</ol></li>
<li><ol start="4" type="1">
<li>Some of the time</li>
</ol></li>
<li><ol start="5" type="1">
<li>Never</li>
</ol></li>
</ul></li>
</ul>
<p>Let’s recode <code>V241229</code> for easier analysis:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">anes_2024_code <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-2">  anes_2024 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TrustGovernment =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(</span>
<span id="cb2-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb2-6">        V241229 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Always"</span>,</span>
<span id="cb2-7">        V241229 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Most of the time"</span>,</span>
<span id="cb2-8">        V241229 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"About half the time"</span>,</span>
<span id="cb2-9">        V241229 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Some of the time"</span>,</span>
<span id="cb2-10">        V241229 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Never"</span>,</span>
<span id="cb2-11">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.default =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb2-12">      ),</span>
<span id="cb2-13">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Always"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Most of the time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"About half the time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Some of the time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Never"</span>)</span>
<span id="cb2-14">    )</span>
<span id="cb2-15">  )</span></code></pre></div></div>
</div>
<p>See more on <a href="https://tidy-survey-r.github.io/tidy-survey-book/c03-survey-data-documentation.html#codebooks">codebooks</a>.</p>
</section>
</section>
<section id="create-a-design-object" class="level2">
<h2 class="anchored" data-anchor-id="create-a-design-object">Create a design object</h2>
<p>The design object is the backbone of survey analysis. Survey data use weights, strata, and clusters to reflect the population. That means we need a survey design object before running any analyses, and the design object tells R which weights to use, how respondents were sampled, and what adjustments are needed. Without it, any estimates we calculate could be misleading.</p>
<p>To build the design object, we need three key ingredients:</p>
<ol type="1">
<li>Weights: to make sure results reflect the population.</li>
<li>Strata: groups used in sampling to ensure coverage.</li>
<li>Clusters (PSUs): primary sampling units, like households or addresses.</li>
</ol>
<section id="finding-the-right-weight" class="level3">
<h3 class="anchored" data-anchor-id="finding-the-right-weight">Finding the right weight</h3>
<p>The ANES documentation lists which weight variables go with which sample:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="weights.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/anes2024/weights.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3); border-radius: 5px;;width:40.0%"></a></p>
</figure>
</div>
<p>Since we’re looking at the post-election fresh in-person sample, we’ll use weight variable <code>V240101b</code>.</p>
<p>One important note: ANES weights add up to the sample size, not the population size. Let’s confirm:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">anes_2024_code <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(V240101b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sum =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(V240101b))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 1
    sum
  &lt;dbl&gt;
1   925</code></pre>
</div>
</div>
<p>This matches the <code>925</code> respondents shown in the documentation’s sample table.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="sample.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/anes2024/sample.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3); border-radius: 5px;;width:40.0%"></a></p>
</figure>
</div>
</section>
<section id="adjusting-for-the-population" class="level3">
<h3 class="anchored" data-anchor-id="adjusting-for-the-population">Adjusting for the population</h3>
<p>If we want to make inferences about the entire U.S. population, we need to adjust the weights. The target population for this sample is about 232.5 million U.S. citizens aged 18 or older, but that’s just a rough number. Per the documentation, for something more precise, we can pull population counts from the 2023 American Community Survey 1-year estimates.</p>
<p>Thank you, Stephanie, for writing up the code for the population estimates!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidycensus)</span>
<span id="cb5-2"></span>
<span id="cb5-3">varlist_2023 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load_variables</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acs1"</span>)</span>
<span id="cb5-4"></span>
<span id="cb5-5">citizen_pop_18_plus <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_acs</span>(</span>
<span id="cb5-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geography =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>,</span>
<span id="cb5-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S2901_C01_001"</span>,</span>
<span id="cb5-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023</span>,</span>
<span id="cb5-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">survey =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acs1"</span></span>
<span id="cb5-11">  )</span>
<span id="cb5-12"></span>
<span id="cb5-13">pums_vars_2023 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pums_variables <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023</span>, survey <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acs1"</span>)</span>
<span id="cb5-16"></span>
<span id="cb5-17">pums_vars_2023 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(var_code <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGEP"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TYPEHUGQ"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CIT"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(var_code, var_label, val_min, val_max, val_label) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb5-21"></span>
<span id="cb5-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find those who are 18+ and in group quarters</span></span>
<span id="cb5-23">get_citizen_pop_18_plus_gq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(state) {</span>
<span id="cb5-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_pums</span>(</span>
<span id="cb5-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGEP"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TYPEHUGQ"</span>),</span>
<span id="cb5-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state =</span> state,</span>
<span id="cb5-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023</span>,</span>
<span id="cb5-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">survey =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acs1"</span>,</span>
<span id="cb5-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables_filter =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-30">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TYPEHUGQ =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> ,</span>
<span id="cb5-31">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CIT =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb5-32">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AGEP =</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb5-33">    )</span>
<span id="cb5-34">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(PWGTP), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> STATE)</span>
<span id="cb5-36">}</span>
<span id="cb5-37"></span>
<span id="cb5-38">citizen_pop_18_plus_gq_l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(state.abb, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DC"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(get_citizen_pop_18_plus_gq)</span>
<span id="cb5-40"></span>
<span id="cb5-41">citizen_pop_18_plus_gq_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-42">  citizen_pop_18_plus_gq_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list_rbind</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate_gq =</span> estimate)</span>
<span id="cb5-45"></span>
<span id="cb5-46">state_pops <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> citizen_pop_18_plus <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(GEOID, NAME, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate_tot =</span> estimate) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">full_join</span>(citizen_pop_18_plus_gq_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GEOID"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STATE"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(NAME <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Puerto Rico"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate_scope =</span> estimate_tot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> estimate_gq)</span>
<span id="cb5-51"></span>
<span id="cb5-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total without AK and HI</span></span>
<span id="cb5-53">targetpop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-54">  state_pops <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>GEOID <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"02"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"15"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-56">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TargetPop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(estimate_scope))</span>
<span id="cb5-57"></span>
<span id="cb5-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total with AK and HI</span></span>
<span id="cb5-59">state_pops <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-60">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TargetPop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(estimate_scope))</span></code></pre></div></div>
</div>
<p>The estimated target population (<code>targetpop</code>) for 2024 is 232,449,541. Now we can rescale the ANES weights so they add up to the population size:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">anes_adjwgt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> anes_2024_code <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Weight =</span> V240101b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(V240101b, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> targetpop)</span></code></pre></div></div>
</div>
</section>
<section id="strata-and-clusters" class="level3">
<h3 class="anchored" data-anchor-id="strata-and-clusters">Strata and clusters</h3>
<p>The documentation also tells us which strata and PSU (cluster) variables pair with each weight.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="weight-strata-cluster.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/anes2024/weight-strata-cluster.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3); border-radius: 5px;;width:40.0%"></a></p>
</figure>
</div>
<p>For V240101b, those are:</p>
<ul>
<li>Strata: <code>V240101d</code></li>
<li>PSU: <code>V240101c</code></li>
</ul>
<p>More on <a href="https://tidy-survey-r.github.io/tidy-survey-book/c10-sample-designs-replicate-weights.html">sampling design can be found in this chapter</a>.</p>
</section>
<section id="putting-it-all-together" class="level3">
<h3 class="anchored" data-anchor-id="putting-it-all-together">Putting it all together</h3>
<p>Finally, we can build the design object. We’ll also filter to respondents who actually completed the in-person post-election interview (<code>V240002b == 1</code>):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">survey.lonely.psu =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adjust"</span>)</span>
<span id="cb7-2"></span>
<span id="cb7-3">anes_des <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> anes_adjwgt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(V240002b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_survey_design</span>(</span>
<span id="cb7-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> Weight,</span>
<span id="cb7-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strata =</span> V240101d,</span>
<span id="cb7-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ids =</span> V240101c,</span>
<span id="cb7-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nest =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb7-10">  )</span>
<span id="cb7-11"></span>
<span id="cb7-12">anes_des</span></code></pre></div></div>
</div>
<p>And there we go! <code>anes_des</code> is our fully specified survey design object. From here on, we use it so every estimate we calculate properly reflects the survey’s design.</p>
</section>
</section>
<section id="analyze-the-data" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="analyze-the-data">Analyze the data</h2>
<p>Now for the fun part! Let’s see how people answered the Trust Government question:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">(</span>
<span id="cb8-2">  trustgov <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> anes_des <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(TrustGovernment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(TrustGovernment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">survey_prop</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vartype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ci"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V241229"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Answer =</span> TrustGovernment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Variable, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>())</span>
<span id="cb8-9">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 5
  Variable Answer                    p   p_low  p_upp
  &lt;chr&gt;    &lt;fct&gt;                 &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;
1 V241229  Always              0.00774 0.00288 0.0206
2 V241229  Most of the time    0.174   0.122   0.242 
3 V241229  About half the time 0.283   0.225   0.351 
4 V241229  Some of the time    0.402   0.341   0.465 
5 V241229  Never               0.133   0.0857  0.201 </code></pre>
</div>
</div>
<p>And a quick plot:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-9-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/anes2024/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="2100"></a></p>
</figure>
</div>
</div>
</div>
<p>The results show just how varied public trust is, with only a small fraction of respondents saying they always trust the government.</p>
<p>We can also look at the data by subgroups:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">anes_des <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb10-2">  anes_des <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Gender =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(</span>
<span id="cb10-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(V241550 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Male"</span>, V241550 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Female"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.default =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb10-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Male"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Female"</span>)</span>
<span id="cb10-6">  ))</span>
<span id="cb10-7"></span>
<span id="cb10-8">(</span>
<span id="cb10-9">  trustgov_gender <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> anes_des <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(Gender, TrustGovernment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Gender, TrustGovernment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">survey_prop</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vartype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ci"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Variable =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V241229"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Answer =</span> TrustGovernment) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Variable, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>())</span>
<span id="cb10-16">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 10 × 6
# Groups:   Gender [2]
   Variable Gender Answer                    p    p_low  p_upp
   &lt;chr&gt;    &lt;fct&gt;  &lt;fct&gt;                 &lt;dbl&gt;    &lt;dbl&gt;  &lt;dbl&gt;
 1 V241229  Male   Always              0.0123  0.00379  0.0393
 2 V241229  Male   Most of the time    0.194   0.109    0.322 
 3 V241229  Male   About half the time 0.249   0.178    0.336 
 4 V241229  Male   Some of the time    0.423   0.321    0.533 
 5 V241229  Male   Never               0.121   0.0659   0.212 
 6 V241229  Female Always              0.00336 0.000744 0.0150
 7 V241229  Female Most of the time    0.153   0.0984   0.230 
 8 V241229  Female About half the time 0.320   0.208    0.457 
 9 V241229  Female Some of the time    0.381   0.285    0.487 
10 V241229  Female Never               0.142   0.0736   0.257 </code></pre>
</div>
</div>
<div class="page-columns page-full">
<div class="cell page-columns page-full">
<div class="cell-output-display page-columns page-full">
<div class="page-columns page-full">
<figure class="figure page-columns page-full">
<p class="page-columns page-full"><a href="index_files/figure-html/unnamed-chunk-11-1.png" class="lightbox page-columns page-full" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/anes2024/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img column-page" width="2100"></a></p>
</figure>
</div>
</div>
</div>
</div>
</section>
<section id="wrap-up" class="level2">
<h2 class="anchored" data-anchor-id="wrap-up">Wrap up</h2>
<p>The ANES 2024 release is packed with insights, and what we’ve covered here is just the beginning. With hundreds of variables across attitudes, demographics, and behaviors, you’ll definitely find questions worth exploring.</p>
<p>If you try out the dataset, I’d love to hear what you discover. Feel free to share your own analyses or visualizations!</p>
<p>Find me on Bluesky: <a href="https://bsky.app/profile/ivelasq3.bsky.social"><span class="citation" data-cites="ivelasq3">@ivelasq3</span></a></p>
<p>Check out more resources:</p>
<ul>
<li><a href="https://tidy-survey-r.github.io/tidy-survey-book/">Exploring Complex Survey Data Analysis Using R</a> free online book</li>
<li><a href="https://r-survey.r-forge.r-project.org/survey/">{survey}</a> package documentation</li>
<li><a href="http://gdfe.co/srvyr/">{srvyr}</a> package documentation
<ul>
<li><a href="https://github.com/gergness/srvyr/blob/main/cheatsheet/srvyr.pdf">{srvyr} cheatsheet</a> for quick function references</li>
</ul></li>
</ul>


</section>

 ]]></description>
  <guid>https://rworks.dev/posts/anes2024/</guid>
  <pubDate>Wed, 20 Aug 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/anes2024/image.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Health Technology notes: Copilot tips and guidance</title>
  <dc:creator>Vidisha Vachharajani</dc:creator>
  <link>https://rworks.dev/posts/healthcare-part2-copilot-supplement/</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">1. Introduction</h2>
<section id="the-use-of-ai-agents-and-the-case-for-prompt-choices" class="level3">
<h3 class="anchored" data-anchor-id="the-use-of-ai-agents-and-the-case-for-prompt-choices">1.1. The use of AI agents and the case for “prompt” choices</h3>
<p>We <a href="https://rworks.dev/posts/healthcare-part1/">introduced</a> the use of the AI pair programmer, Copilot, while working with <code>tidyverse</code>. In this post, we walk the reader through the fundamental driver of successfully using AI agents – prompting. Prompting is the method of instructing an LLM to perform a task by carefully designing an input text. Our prompting will be the “regular” or “zero-shot” prompting, the most commonly used prompting <a href="https://towardsdatascience.com/recap-of-all-types-of-llm-agents/">technique</a> where you simply ask the agent a question and receive an answer. Typically, while using this, iteration is recommended to get to a more exact answer, especially with coding outputs. This technique is flexible and easy to use, but in my other more complex coding and analytics workflows, I have also used <a href="https://www.datacamp.com/tutorial/few-shot-prompting">few-shot</a> prompting, where you provide the LLM with examples and/or context in your prompt. In this post, we will focus on iterative zero-shot prompting.</p>
</section>
<section id="a-recap-of-getting-started-with-copilot-in-rstudio" class="level3">
<h3 class="anchored" data-anchor-id="a-recap-of-getting-started-with-copilot-in-rstudio">1.2. A recap of getting started with Copilot in RStudio</h3>
<blockquote class="blockquote">
<p><em><strong>Setting up Copilot in RStudio:</strong> Adding Copilot as a pair programmer to your RStudio is simple, and has a seamless interface.</em> <em>Take a look at the Posit documentation <a href="https://docs.posit.co/ide/user/ide/guide/tools/copilot.html"><strong>here</strong></a></em><strong>.</strong> <em>Make sure to enable it! See below.</em></p>
</blockquote>
<p><a href="Copilot_start.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/healthcare-part2-copilot-supplement/Copilot_start.png" class="img-fluid" width="538"></a></p>
</section>
</section>
<section id="using-copilot-to-get-data-summaries" class="level2">
<h2 class="anchored" data-anchor-id="using-copilot-to-get-data-summaries">2. Using Copilot to get data summaries</h2>
<p>After cleaning our data like we do in this <a href="https://github.com/VidishaVac/healthcare-analytics/blob/main/cleanDiabetic_data.R">snippet</a>, we will now use Copilot for our first task – to get some useful summaries of the fields we think are likely to impact patient care outcomes. We do this in section 2 of our analytics post. Note that to give the right guidance to Copilot for these summaries, we would need to know what fields we are summarizing, and their underlying <code>class</code> within your R workspace, all of which we will specify in our prompt.</p>
<p>These are the fields we will summarize.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 19%">
<col style="width: 19%">
<col style="width: 61%">
</colgroup>
<thead>
<tr class="header">
<th>Field name</th>
<th>Field type</th>
<th>Field description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>primary_diag</code></td>
<td>initial admission covariate</td>
<td>primary diagnosis upon admission</td>
</tr>
<tr class="even">
<td><code>a1c</code></td>
<td>initial admission covariate</td>
<td>whether a patient was given the A1c test or not</td>
</tr>
<tr class="odd">
<td><code>diabetesMed</code></td>
<td>intermediate response</td>
<td>whether a patient was given any diabetes medication</td>
</tr>
<tr class="even">
<td><code>acarbose:troglitazone</code></td>
<td>intermediate response</td>
<td>individual 23 diabetes medications that were prescribed or not, and if prescribed, were held steady, increased or decreased</td>
</tr>
<tr class="odd">
<td><code>readmitted</code></td>
<td>final response</td>
<td>whether the patient was readmitted early (&lt;30 days)</td>
</tr>
</tbody>
</table>
<p>Table 1: Important data fields and their descriptions</p>
<blockquote class="blockquote">
<p><em><strong>Notes:</strong> Take a look at the clip where I show the prompt I wrote to get to the summary code. Note the following -</em></p>
<ol type="1">
<li><em>The ghost text auto-complete that is typical of Copilot once you begin materializing the initial line of the code.</em></li>
<li><em>The specifics of the prompt - the field names and that they are <code>factor</code> fields, etc.</em></li>
</ol>
<p><em>Note that I went back and modified the prompt since I forgot to ask it to include frequency percentages. I also went back and added <code>arrange()</code> myself in order to enable sorting for a neater look. Right after the video, see the final code that was used in the analysis. Note that this does not get us to the final kable outputs and table formatting, which were done manually.</em></p>
</blockquote>
<div class="quarto-video"><video id="video_shortcode_videojs_video1" class="video-js vjs-default-skin vjs-big-play-centered vjs-fluid" controls="" preload="auto" data-setup="{}" title=""><source src="summaryMovie.mov"></video></div>
<p>Click on “Show the code” to see the Copilot code output from the above interaction.</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- ---------- ---------- ---------- #</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Final code output after iterating with Copilot </span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- ---------- ---------- ---------- #</span></span>
<span id="cb1-4">summary_a1c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(a1c) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span>
<span id="cb1-8">summary_primary_diag <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(primary_diag) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span>
<span id="cb1-12">summary_readmitted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(readmitted) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span>
<span id="cb1-16">summary_change <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(change) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span>
<span id="cb1-20">summary_diabetesMed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(diabetesMed) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span></code></pre></div></div>
</details>
</div>
<p>Click on “Show the code” to see the final code used to clean the data and generate the summaries.</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --------------------------------------------- #</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load necessary libraries</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --------------------------------------------- #</span></span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(kableExtra)</span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gridExtra)</span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)</span>
<span id="cb2-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lattice)</span>
<span id="cb2-11"></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --------------------------------------------- #</span></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read in the data</span></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --------------------------------------------- #</span></span>
<span id="cb2-15"></span>
<span id="cb2-16">D <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://raw.githubusercontent.com/VidishaVac/healthcare-analytics/refs/heads/main/dataset_diabetes/diabetic_data.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>)</span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># -------------------------------------------------------- #</span></span>
<span id="cb2-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clean and re-define HbA1c and primary diag</span></span>
<span id="cb2-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># -------------------------------------------------------- #</span></span>
<span id="cb2-21"></span>
<span id="cb2-22">D <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a1c=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(A1Cresult<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"None"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"not measured"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"measured"</span>), </span>
<span id="cb2-24">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">primary_diag =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb2-25">    diag_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">390</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">459</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">785</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Circulatory"</span>,</span>
<span id="cb2-26">    diag_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">460</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">519</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">786</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Respiratory"</span>,</span>
<span id="cb2-27">    diag_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">520</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">579</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">787</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Digestive"</span>,</span>
<span id="cb2-28">    diag_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">580</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">629</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">788</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Genitourinary"</span>,</span>
<span id="cb2-29">    diag_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">630</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">679</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pregnancy"</span>,</span>
<span id="cb2-30">    diag_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">680</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">709</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">782</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Skin"</span>,</span>
<span id="cb2-31">    diag_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">710</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">739</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Musculoskeletal"</span>,</span>
<span id="cb2-32">    diag_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">740</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">759</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congenital"</span>,</span>
<span id="cb2-33">    diag_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">999</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Injury"</span>,</span>
<span id="cb2-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^250"</span>, diag_1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diabetes"</span>,</span>
<span id="cb2-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(diag_1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Missing"</span>,</span>
<span id="cb2-36">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span></span>
<span id="cb2-37">  ))</span>
<span id="cb2-38"></span>
<span id="cb2-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove columns not used, patients with a discharge disposition of "expired" or "hospice"</span></span>
<span id="cb2-40">D <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>discharge_disposition_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'11'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'13'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'14'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'19'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'20'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'21'</span>))</span>
<span id="cb2-42"></span>
<span id="cb2-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Summarize</span></span>
<span id="cb2-44">summary_a1c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(a1c) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span>
<span id="cb2-48">summary_primary_diag <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(primary_diag) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span>
<span id="cb2-52">summary_readmitted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(readmitted) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span>
<span id="cb2-56">summary_change <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(change) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-58">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span>
<span id="cb2-60">summary_diabetesMed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(diabetesMed) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-62">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent))</span>
<span id="cb2-64"></span>
<span id="cb2-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># summary_primary_diag %&gt;% </span></span>
<span id="cb2-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   kable(digits = 1, format.args = list(big.mark = ","), </span></span>
<span id="cb2-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#         caption = "Breakdown of the primary patient diagnosis upon admission for a diabetic encounter") %&gt;%</span></span>
<span id="cb2-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#           row_spec(1, background = "yellow", font_size = "larger")</span></span></code></pre></div></div>
</details>
</div>
<p>We show an example summary for patient primary diagnosis on admission – notice how circulatory diagnoses account for the most common reason for hospital admissions in diabetic encounters. This is a vital insight and an important theme that will run through our analyses.</p>
<p>To make the summary data easier to consume, we use a circular bar plot. While I did not use Copilot for the actual bar plot, I used GPT-4o (directly in a browser) to achieve this clean 2-column look for the plot and its notes to the right. See here for the full conversation.</p>
<p>Finally, see “Plot notes” for details on the design.</p>
<div class="columns">
<div class="column" style="width:65%;">
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/Chunk21-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/healthcare-part2-copilot-supplement/index_files/figure-html/Chunk21-1.png" class="img-fluid figure-img" width="960"></a></p>
</figure>
</div>
</div>
</div>
</div><div class="column" style="width:35%;">
<div class="callout callout-style-default callout-note callout-titled" title="Plot notes">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Plot notes
</div>
</div>
<div class="callout-body-container callout-body">
<p><span style="font-size: 0.8em;"><em>The circular bar plot offers a visually engaging and effective way to convey categorical comparisons, combining aesthetic appeal with immediate interpretability. One of the strengths of the design here is its ability to simultaneously emphasize both absolute and relative differences through clear axis labeling and proportional arc length. For instance, circulatory disease accounts for 30% of the sample — roughly 30,000 encounters — while diabetes as a primary admission diagnosis represents just 9%, or fewer than 10,000. These contrasts are instantly evident not only in the numbers but also in the visual impact of the plot. The minimalist style, paired with concise explanatory text above the chart, makes each visual a self-contained and accessible summary — both analytically rich and reader-friendly.</em></span></p>
</div>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> summary_primary_diag</span>
<span id="cb3-2">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>percent[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(df)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)))</span>
<span id="cb3-4"></span>
<span id="cb3-5"></span>
<span id="cb3-6">p0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(primary_diag,count), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>count, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>percent)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span>(</span>
<span id="cb3-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Percent"</span>,</span>
<span id="cb3-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>( <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#6C5B7B"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C06C84"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F67280"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F8B195"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>primary_diag, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>count<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>, </span>
<span id="cb3-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,</span>
<span id="cb3-14">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>),</span>
<span id="cb3-16">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40000</span>),</span>
<span id="cb3-17">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10,000"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"20,000"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30,000"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb3-22">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove axis ticks and text</span></span>
<span id="cb3-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use gray text for the region names</span></span>
<span id="cb3-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray12"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb3-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb3-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>),</span>
<span id="cb3-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-31">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add labels</span></span>
<span id="cb3-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb3-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Patient primary diagnoses"</span>,</span>
<span id="cb3-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(</span>
<span id="cb3-35">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Primary diagnoses of admitted patients vary considerably."</span>,</span>
<span id="cb3-36">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Even for diabetic encounters, the most common admitting diagnosis"</span>,</span>
<span id="cb3-37">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"is circulatory or heart disease. We will call this out in our analysis,"</span>,</span>
<span id="cb3-38">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"comparing patient outcomes for these high-risk but non-diabetic"</span>,</span>
<span id="cb3-39">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"admissions with diabetic admissions (which are only 9% of the"</span>,</span>
<span id="cb3-40">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"admitting or primary diagnoses)."</span>,</span>
<span id="cb3-41">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb3-42">    )) </span></code></pre></div></div>
</details>
</div>
</section>
<section id="prompting-pitfalls-what-you-ask-is-what-you-get" class="level2">
<h2 class="anchored" data-anchor-id="prompting-pitfalls-what-you-ask-is-what-you-get">3. Prompting pitfalls: What You Ask Is What You Get</h2>
<p>Copilot is powerful and easy to use, but in this section, we’ll dive a little more into the importance of iteration – how effective prompting often requires fine-tuning, and how insufficient or non-specific guidance can lead you astray.</p>
<p>Consider the 23 medication prescriptions, each categorized into one of four levels: 𝘕𝘰, 𝘚𝘵𝘦𝘢𝘥𝘺, 𝘜𝘱, or 𝘋𝘰𝘸𝘯., where the medications include well-known drugs like insulin and metformin. I wanted a strong visual for looking at all 23 in 1 plot, and I started with my own code for a bar plot. Next, I wanted to explore alternate plot types that could reveal more nuanced insights.</p>
<p>🖼️ In the image below, you can follow my conversation with Copilot: I began with a vague prompt (“Can you fix this blank plot?”) Then refined it a little (“Can you use a different kind of plot?”) Finally, I gave it the specific guidance it needed (which variables to use and how to specify its aesthetics)</p>
<p>🔍 Only when I provided clear, well-scoped instructions did Copilot return a useful, meaningful visualization.</p>
<p>💡 Takeaway: The quality of your output depends on the quality of your prompt. Copilot is powerful, but only if you guide it with precision.</p>
<p><a href="convo.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/healthcare-part2-copilot-supplement/convo.png" class="img-fluid" width="785"></a></p>
<p>Click on “Show the code” to see the actual back-and-forth conversation with Copilot that was the basis of the final visualization.</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prompt 1</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Consider "meds" and "p0" above. </span></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Can you recommend a plot that is not geom_col </span></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># or geom_bar but that will capture how they are </span></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># being prescribed, using "freq"?</span></span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output 1</span></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-13"></span>
<span id="cb4-14">p1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>meds, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>medication, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion of Medication Prescriptions"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion (%)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication"</span>)</span>
<span id="cb4-19"></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prompt 2</span></span>
<span id="cb4-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-23"></span>
<span id="cb4-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This plot above is blank, can you fix this?</span></span>
<span id="cb4-25"></span>
<span id="cb4-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output 2</span></span>
<span id="cb4-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-29"></span>
<span id="cb4-30">p1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>meds, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>medication, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion of Medication Prescriptions"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion (%)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb4-36"></span>
<span id="cb4-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prompt 3</span></span>
<span id="cb4-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-40"></span>
<span id="cb4-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># It is still blank, can you use a different type of plot?</span></span>
<span id="cb4-42"></span>
<span id="cb4-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output 3</span></span>
<span id="cb4-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-46"></span>
<span id="cb4-47">p2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>meds, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>medication, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication Prescription Frequency"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Frequency (%)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb4-53"></span>
<span id="cb4-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prompt 4</span></span>
<span id="cb4-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-57"></span>
<span id="cb4-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Do you think geom_tile needs "medication" and "value" to be on the axes and "freq"</span></span>
<span id="cb4-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to be the fill?</span></span>
<span id="cb4-60"></span>
<span id="cb4-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output 4</span></span>
<span id="cb4-63"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-64"></span>
<span id="cb4-65">p3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>meds, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>medication, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>freq)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-66">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-67">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-68">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication Prescription Frequency"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-69">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb4-71"></span>
<span id="cb4-72"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Final tweak -- Prompt 5</span></span>
<span id="cb4-74"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-75"></span>
<span id="cb4-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># It seems discrete values have been supplied to continuous scale, can you fix?</span></span>
<span id="cb4-77"></span>
<span id="cb4-78"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-79"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output 5</span></span>
<span id="cb4-80"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- # </span></span>
<span id="cb4-81"></span>
<span id="cb4-82">p4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>meds, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>medication, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>freq)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-83">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-84">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-85">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication Prescription Frequency"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-86">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-87">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div></div>
</details>
</div>
<p>Let’s see the final code used for the visualization outputs, generated from the Copilot conversation above. See plot design notes below for the motivation behind the original bar plot and the crucial difference between the specifications of the 2 plots, and how it relates back to the Copilot iteration above. Click on “Show the code” to see the underlying plot generating code.</p>
<section id="plot-1-original-barplot-with-flipped-coordinates" class="level3">
<h3 class="anchored" data-anchor-id="plot-1-original-barplot-with-flipped-coordinates">Plot 1: Original barplot with flipped coordinates</h3>
<div class="columns">
<div class="column" style="width:50%;">
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reshaping wide to get a better view of medications</span></span>
<span id="cb5-2">meds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(metformin<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>metformin.pioglitazone) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medication"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(medication, value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct =</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(n))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># My original barplot with flipped coordinates</span></span>
<span id="cb5-8">p0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>meds, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>medication, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>pct, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diabetes medications bar plot"</span>,</span>
<span id="cb5-10">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication name"</span>,</span>
<span id="cb5-11">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Percent prescribed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication value"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span>
<span id="cb5-13"></span>
<span id="cb5-14">p0</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/Chunk3-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/healthcare-part2-copilot-supplement/index_files/figure-html/Chunk3-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</div><div class="column" style="width:50%;">
<div class="callout callout-style-default callout-note callout-titled" title="Plot notes">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Plot notes
</div>
</div>
<div class="callout-body-container callout-body">
<p><em><strong>Plot notes:</strong> The original barplot on the left is a slightly different use of <code>geom_col()</code>. While it is normally used for a bar chart use case, in this plot, it has a quite useful “heatmap” look. By using <code>coord_flip()</code>, we are able to clearly see the full name of each medication. In my mind, this is a slightly better look than having the medication names be on the x-axis at a 45-degree angle. We enhanced this further by also using <code>geom_tile()</code>.</em></p>
</div>
</div>
</div>
</div>
</section>
<section id="plot-2-heatmap-with-improved-palette-and-labels" class="level3">
<h3 class="anchored" data-anchor-id="plot-2-heatmap-with-improved-palette-and-labels">Plot 2: Heatmap with improved palette and labels</h3>
<div class="columns">
<div class="column" style="width:50%;">
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Heatmap with improved color palette and labels</span></span>
<span id="cb6-2">p1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>meds, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>medication, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>pct)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diabetes medication heatmap"</span>,</span>
<span id="cb6-4">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication value"</span>,</span>
<span id="cb6-5">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medication name"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Percent prescribed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colors =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette.colors</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>), </span>
<span id="cb6-8">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(pct, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) </span>
<span id="cb6-10"></span>
<span id="cb6-11">p1</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><a href="index_files/figure-html/Chunk4-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/healthcare-part2-copilot-supplement/index_files/figure-html/Chunk4-1.png" class="img-fluid figure-img" width="672"></a></p>
</figure>
</div>
</div>
</div>
</div><div class="column" style="width:50%;">
<div class="callout callout-style-default callout-note callout-titled" title="Plot notes">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Plot notes
</div>
</div>
<div class="callout-body-container callout-body">
<p><span style="font-size: 0.95em;"><em><strong>Plot notes</strong>: Upon switching to a <code>geom_tile()</code></em></span> <em>heatmap plot, the more nuanced prescription variations become much more evident. However, the default color parameters from Copilot output 4 does not offer this as clearly. I adjust these color parameters to make them more robust under color vision deficiencies, making them easily distinguishable for all viewers, and surfacing the actual variations across medications prescribed. I also add data labels to the final plot.</em></p>
</div>
</div>
</div>
</div>
<p>Finally, notice the very important difference in the <code>aes()</code> specifications between the 2 plots, which is where the Copilot output 3 from the conversation goes awry, and needs a fine-tuned prompt – for the left hand side <code>geom_col()</code>, the <code>fill</code> parameter is the <code>value</code> or the levels of the medication, while for the right hand side <code>geom_tile()</code>, its the actual percent. The legend to the right of each plot shows this.</p>
</section>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">4. What’s next</h2>
<p>We will continue showcasing the use of AI agents in our healthcare analytics workflow while using <code>tidyverse</code>, to demonstrate how health technology can benefit stakeholders in this industry, especially given the richness of the data sitting in our health systems. Effectively mining this data using the right tools in order to extract insights that impact care delivery decisions is not just our end goal, but also crucial for the advancement of the use of technology in healthcare.</p>
</section>
<section id="about-the-author" class="level2">
<h2 class="anchored" data-anchor-id="about-the-author">About the Author</h2>
<p><em>Vidisha writes: “I am a statistician and data science professional. For practitioners like me, ‘tidyverse’ is a household word. I’ve used it for complex data wrangling, visualization, and advanced analytics work, especially where I needed clarity on how to leverage my data so that it allows for story-telling that informs business decisions, recasting my work into actionable insights. I recently began exploring GPT-4o mini and GitHub Copilot to help me speed up my workflows, especially within Healthcare Analytics. I am excited to share this post series to help readers understand the role of RStudio and AI in Healthcare Analytics!”</em></p>


</section>

 ]]></description>
  <guid>https://rworks.dev/posts/healthcare-part2-copilot-supplement/</guid>
  <pubDate>Wed, 30 Jul 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/healthcare-part2-copilot-supplement/Copilot_start.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>June 2025 Top 40 New CRAN Packages</title>
  <dc:creator>Joseph Rickert</dc:creator>
  <link>https://rworks.dev/posts/june-2025-top-40-new-cran-packages/</link>
  <description><![CDATA[ 





<p>In June, one hundred twenty-three new packages made it to CRAN. Here are my Top 40 picks in twenty-one categories: AI, Chess, Computational Methods, Data, Decision Analysis, Ecology, Epidemiology, Finance, Genomics, Linguistics, Machine Learning, Mathematics, Medical Statistics, Music Theory, Networks, Programming, Statistics, Time Series, Utilities, and Visualization.</p>
<div class="columns">
<div class="column" style="width:45%;">
<p><a href=".png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/.png" class="img-fluid" alt=""></a></p>
<section id="ai" class="level3">
<h3 class="anchored" data-anchor-id="ai">AI</h3>
<p><a href="https://cran.r-project.org/package=statlingua">statlingua</a> v0.1.0: Provides functions to transform complex statistical output into straightforward, understandable, and context-aware natural language descriptions using Large Language Models (LLMs). It works with <a href="https://openai.com/">OpenAI</a>, <a href="https://aistudio.google.com/">Google AI Studio</a>, and <a href="https://www.anthropic.com/">Anthropic</a>. See the <a href="https://cran.r-project.org/web/packages/statlingua/vignettes/statlingua.html">vignette</a> for examples.</p>
<p><a href="https://cran.r-project.org/package=vitals">vitals</a> v0.1.0: Provides a port of <code>Inspect</code>, a widely adopted <code>Python</code> framework for large language model evaluation that supports prompt engineering, tool usage, multi-turn dialog, and model graded evaluations. It is specifically aimed at <code>ellmer</code> users who want to measure the effectiveness of their LLM based products. There are two vignettes: <a href="https://cran.r-project.org/web/packages/vitals/vignettes/vitals.html">Getting Started</a> and <a href="https://cran.r-project.org/web/packages/vitals/vignettes/writing-evals.html">Writing Evals</a>.</p>
</section>
<section id="chess" class="level3">
<h3 class="anchored" data-anchor-id="chess">Chess</h3>
<p><a href="https://cran.r-project.org/package=chess2plyrs">chess2plyrs</a> v0.3.0: Implements a chess program based on the <a href="https://www.chessprogramming.org/Minimax">Minimax</a> chess engine which allows users to create games, read and write <a href="https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation#:~:text=Forsyth%E2%80%93Edwards%20Notation%20(FEN),Scottish%20newspaper%20journalist%20David%20Forsyth.">FEN</a> and more. See the <a href="https://cran.r-project.org/web/packages/chess2plyrs/vignettes/chess2plyrs.html">vignette</a> for examples.</p>
<p><a href="chess.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/chess.png" class="img-fluid" alt="Plot of chess board"></a></p>
</section>
<section id="computational-methods" class="level3">
<h3 class="anchored" data-anchor-id="computational-methods">Computational Methods</h3>
<p><a href="https://cran.r-project.org/package=tvdenoising">tvdenoising</a> v1.0.0: Implements total variation denoising which can be used to approximate a given sequence of noisy observations by a piecewise constant sequence with adaptively-chosen break points. See <a href="https://www.tandfonline.com/doi/full/10.1080/10618600.2012.681238">Johnson (2013)</a> for background and <a href="https://cran.r-project.org/web/packages/tvdenoising/readme/README.html">README</a> for an example.</p>
<p><a href="tvdenoising.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/tvdenoising.png" class="img-fluid" alt="Plot of function with approximating points"></a></p>
<p><a href="https://cran.r-project.org/package=wideRhino">wideRhino</a> v1.0.2: Provides functions to construct a canonical Variate Analysis biplot via the Generalized Singular Value Decomposition for cases when the number of samples is less than the number of variables. For more information on biplots, see <a href="https://onlinelibrary.wiley.com/doi/book/10.1002/9780470973196">Gower et al.&nbsp;(2011)</a>, for a discussion of the generalized singular value decomposition, see <a href="https://epubs.siam.org/doi/10.1137/18M1234412">Edelman &amp; Wang (2020)</a>, and see <a href="https://cran.r-project.org/web/packages/wideRhino/readme/README.html">README</a> for examples.</p>
<p><a href="wideRhino.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/wideRhino.png" class="img-fluid" alt="CVA biplot"></a></p>
</section>
<section id="data" class="level3">
<h3 class="anchored" data-anchor-id="data">Data</h3>
<p><a href="https://cran.r-project.org/package=avilistr">avilistr</a> v0.0.1: Provides easy access to the <a href="https://www.avilist.org/checklist/v2025/">AviList Global Avian Checklist</a>, the first unified global bird taxonomy that harmonizes differences between International Ornithological Committee, Clements, and BirdLife checklists. See the <a href="https://cran.r-project.org/web/packages/avilistr/vignettes/getting-started.html">vignette</a> to get started.</p>
<p><a href="avilistr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/avilistr.png" class="img-fluid" alt="Barplot of top 10 species-rich bird orders"></a></p>
<p><a href="https://cran.r-project.org/package=ecoteach">ecoteach</a> v0.1.0: A collection of curated educational datasets for teaching ecology and agriculture concepts. Includes data on wildlife monitoring, plant treatments, and ecological observations with documentation and examples for educational use. All datasets are derived from published scientific studies. See the <a href="https://cran.r-project.org/web/packages/ecoteach/vignettes/introduction-to-ecoteach.html">vignette</a>.</p>
<p><a href="https://cran.r-project.org/package=jpinfect">jpinfect</a> v0.1.2: Provides functions to download and post process the infectious disease case data from the <a href="https://id-info.jihs.go.jp/">Japan Institute for Health Security</a>. See the <a href="https://cran.r-project.org/web/packages/jpinfect/vignettes/jpinfect.html">vignette</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=LBDiscover">LBDiscover</a> v0.1.0: Provides a suite of tools for literature-based discovery in biomedical research, including functions for retrieving scientific articles from PubMed and other NCBI databases, extracting biomedical entities (diseases, drugs, genes, etc.), building co-occurrence networks, and applying various discovery models. See <a href="https://cran.r-project.org/web/packages/LBDiscover/readme/README.html">README</a> to get started.</p>
<p><a href="https://cran.r-project.org/package=Rdatasets">Rdatasets</a> v0.0.1: Provides functions to search, download, and view documentation for thousands of datasets from R packages included in the <a href="https://vincentarelbundock.github.io/Rdatasets/">Rdatasets archive</a>. Datasets are available in both CSV and Parquet formats. See <a href="https://cran.r-project.org/web/packages/Rdatasets/readme/README.html">README</a> to get started.</p>
</section>
<section id="decision-analysis" class="level3">
<h3 class="anchored" data-anchor-id="decision-analysis">Decision Analysis</h3>
<p><a href="https://cran.r-project.org/package=RMCDA">RMCDA</a> v0.3: Implements methods including AHP, TOPSIS, PROMETHEE, VIKOR, Stratified MCDM, and the Stratified Best–Worst Method to support multiple criteria decision making. <a href="https://www.sciencedirect.com/science/article/pii/S2665963825000223?via%3Dihub">Najafi &amp; Mirzaei (2025)</a> contains references for all of these acronyms and provides details of the package. See the <a href="https://cran.r-project.org/web/packages/RMCDA/vignettes/RMCDA_guidelines.html">vignette</a> for examples.</p>
</section>
<section id="ecology" class="level3">
<h3 class="anchored" data-anchor-id="ecology">Ecology</h3>
<p><a href="https://cran.r-project.org/package=climodr">climodr</a> v1.0.0: Provides tools to automate workflows for predictive climate mapping using climate station data, and tools to create reproducible climate models. See <a href="https://www.sciencedirect.com/science/article/abs/pii/S0304380019303230?via%3Dihub">Meyer (2019)</a> and <a href="https://www.nature.com/articles/s41467-022-29838-9">Meyer (2022)</a> for background on the methods implemented and the <a href="https://cran.r-project.org/web/packages/climodr/vignettes/climodr.html">vignette</a> and <a href="https://envima.github.io/climodr/">website</a> for details.</p>
<p><a href="climodr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/climodr.png" class="img-fluid" alt="Sample climate map"></a></p>
<p><a href="https://cran.r-project.org/package=movedesign">movedesign</a> v0.3.1: Implements a toolbox and <code>shiny</code> application to help researchers design movement ecology studies, focusing on two key objectives: estimating home range areas, and estimating fine-scale movement behavior, specifically speed and distance traveled. See <a href="https://besjournals.onlinelibrary.wiley.com/doi/10.1111/2041-210X.14153">Silva et al.&nbsp;(2023)</a> for background. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/movedesign/vignettes/movedesign.html">Get started</a> and <a href="https://cran.r-project.org/web/packages/movedesign/vignettes/tutorial_pop.html">Tutorial</a>.</p>
<p><a href="movedesign.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/movedesign.png" class="img-fluid" alt="Sample plots"></a></p>
</section>
<section id="epidemiology" class="level3">
<h3 class="anchored" data-anchor-id="epidemiology">Epidemiology</h3>
<p><a href="https://cran.r-project.org/package=infectiousR">infectiousR</a> v0.1.0: Provides functions to access real-time infectious disease data from the <a href="https://disease.sh/">disease.sh API</a>, including global COVID-19 data, vaccination coverage, influenza-like illness data from CDC, and more. It also includes curated datasets on a variety of infectious diseases. See the <a href="https://cran.r-project.org/web/packages/infectiousR/vignettes/infectiousR_vignette.html">vignette</a> to get started.</p>
<p><a href="infectiousR.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/infectiousR.png" class="img-fluid" alt="Covid-19 data from Latin America"></a></p>
<p><a href="https://cran.r-project.org/package=rifttable">rifttable</a> v0.7.1: Provides functions to automate the production of reproducible, presentation-ready tables for epidemiologists. Users specify the design of the table, with rows and/or columns defined by exposures, effect modifiers, and estimands. See <a href="https://link.springer.com/article/10.1007/s10654-017-0314-3">Rothman (2017)</a> for background. There are six vignettes, including <a href="https://cran.r-project.org/web/packages/rifttable/vignettes/rifttable.html">Get started</a> and <a href="https://cran.r-project.org/web/packages/rifttable/vignettes/estimators_survival.html">Survival outcomes</a>.</p>
</section>
<section id="finance" class="level3">
<h3 class="anchored" data-anchor-id="finance">Finance</h3>
<p><a href="https://cran.r-project.org/package=fEGarch">fEGarch</a> v1.01: Provides functions to implement and fit a variety of short-memory and long-memory models from a very broad family of exponential generalized autoregressive conditional heteroskedasticity EGARCH models. Includes MEGARCH, modified EGARCH, FIEGARCH, fractionally integrated EGARCH, and FIMLog-GARCH, fractionally integrated modulus Log-GARCH. Look <a href="https://econpapers.repec.org/paper/pdnciepap/156.htm">here</a> for background and see <a href="https://cran.r-project.org/web/packages/fEGarch/readme/README.html">README</a> for examples.</p>
<p><a href="fEGarch.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/fEGarch.png" class="img-fluid" alt="Plots of various Garch models"></a></p>
</section>
<section id="genomics" class="level3">
<h3 class="anchored" data-anchor-id="genomics">Genomics</h3>
<p><a href="https://cran.r-project.org/package=multiDEGGs">multiDEGGs</a> 1.0.0: Provides functions to perform multi-omic differential network analysis by revealing differential interactions between molecular entities (genes, proteins, transcription factors, or other biomolecules) across the omic datasets provided. A comprehensive visualization from differential networks is constructed for each omic dataset, where links represent statistically significant differential interactions between entities. See <a href="https://academic.oup.com/bioinformatics/article/39/4/btad192/7135827?login=false">Sciacca et al.&nbsp;(2023)</a> for information on the methods implemented, and the <a href="https://cran.r-project.org/web/packages/multiDEGGs/vignettes/multiDEGGs_vignette.html">vignette</a> for examples.</p>
<p><a href="multiDEGGS.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/multiDEGGS.png" class="img-fluid" alt="Differential regression plot"></a></p>
<p><a href="https://cran.r-project.org/package=rsynthbio">rsynthbio</a> v2.0.0: Implements a wrapper to the <a href="https://app.synthesize.bio/">Synthesize Bio API</a>, which enables users to generate realistic gene expression data based on specified biological conditions. Researchers can access AI-generated transcriptomic data for various modalities, including bulk RNA-seq, single-cell RNA-seq, and microarray data. See the <a href="https://cran.r-project.org/web/packages/rsynthbio/vignettes/getting-started.html">vignette</a> to get started.</p>
</section>
<section id="linguistics" class="level3">
<h3 class="anchored" data-anchor-id="linguistics">Linguistics</h3>
<p><a href="https://cran.r-project.org/package=tidynorm">tidynorm</a> v0.3.0: Implements tidy speaker vowel normalization and includes generic functions for defining new normalization methods for points, format tracks, and Discrete Cosine Transform coefficients, as well as convenience functions implementing established normalization methods. See <a href="https://pubs.aip.org/asa/jasa/article-abstract/49/2B/606/747097/Classification-of-Russian-Vowels-Spoken-by?redirectedFrom=fulltext">Johnson (2020)</a>, <a href="https://sites.ualberta.ca/~tnearey/Nearey1978_compressed.pdf">Lobanov (1971)</a>, and <a href="https://www.latl.leeds.ac.uk/article/evaluation-of-a-technique-for-improving-the-mapping-of-multiple-speakers-vowel-spaces-in-the-f1-f2-plane/">Watt &amp; Fabricius (2002)</a> for the theory. There are five vignettes, including <a href="https://cran.r-project.org/web/packages/tidynorm/vignettes/norm-methods.html">Normalization Methods</a> and <a href="https://cran.r-project.org/web/packages/tidynorm/vignettes/normalization-overview.html">Normalization Overview</a>.</p>
<p><a href="tidynorm.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/tidynorm.png" class="img-fluid" alt="Plot of vowel space for different speakers"></a></p>
</section>
<section id="machine-learning" class="level3">
<h3 class="anchored" data-anchor-id="machine-learning">Machine Learning</h3>
<p><a href="https://cran.r-project.org/package=midr">midr</a> v0.5.0: Implements <em>Maximum Interpretation Decomposition</em>, a functional decomposition technique, to provide a model-agnostic method for interpreting and explaining black-box predictive models by creating a globally interpretable surrogate model. See <a href="https://arxiv.org/abs/2506.08338">Asashiba et al.&nbsp;(2025)</a> for details and <a href="https://cran.r-project.org/web/packages/midr/readme/README.html">README</a> for examples.</p>
<p><a href="midr.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/midr.png" class="img-fluid" alt="ICE curves of interactions"></a></p>
</section>
<section id="mathematics" class="level3">
<h3 class="anchored" data-anchor-id="mathematics">Mathematics</h3>
<p><a href="https://cran.r-project.org/package=polarzonoid">polarzonoid</a> v0.1-2: Implements some applications of the polar zonoid, a straightforward generalization of the polar zonohedron in dimension 3 and a root solver for trigonometric polynomials. There are five vignettes, including a <a href="https://cran.r-project.org/web/packages/polarzonoid/vignettes/polarzonoid-guide.html">User Guide</a> and <a href="https://cran.r-project.org/web/packages/polarzonoid/vignettes/rotations.html">Real Projective Spaces and 3x3 Rotation Matrices</a>.</p>
<p><a href="polarzonoid.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/polarzonoid.png" class="img-fluid" alt="When 𝑛=1, ∂𝑍1 is homeomorphic to 𝕊2and it has cusps at 2 “poles” corresponding to the 2 points in 𝐴0."></a></p>
</section>
</div><div class="column" style="width:10%;">

</div><div class="column" style="width:45%;">
<section id="medical-statistics" class="level3">
<h3 class="anchored" data-anchor-id="medical-statistics">Medical Statistics</h3>
<p><a href="https://cran.r-project.org/package=bbssr">bbssr</a> v1.0.2: Provides comprehensive tools for blinded sample size re-estimation in two-arm clinical trials with binary endpoints that allow for adaptive sample size adjustments during trials while maintaining statistical integrity and study blinding. Implements five exact statistical tests: Pearson chi-squared, Fisher exact, Fisher mid-p, Z-pooled exact unconditional, and Boschloo exact unconditional tests. See <a href="https://academic.oup.com/biometrics/article-abstract/59/2/441/7286196?redirectedFrom=fulltext&amp;login=false">Mehrotra et al.&nbsp;(2003)</a> and <a href="https://link.springer.com/chapter/10.1007/978-3-030-49528-2_21">Kieser (2020)</a>. There are three vignettes, including an <a href="https://cran.r-project.org/web/packages/bbssr/vignettes/bbssr-introduction.html">Introduction</a> and <a href="https://cran.r-project.org/web/packages/bbssr/vignettes/bbssr-statistical-methods.html">Statistical Methods</a>.</p>
<p><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/bbssr.png" class="img-fluid" alt="Power comparison plot"> <a href="https://cran.r-project.org/package=causens">causens</a> v0.0.3: Implements methods to perform causal sensitivity analysis to adjust for possible unmeasured confounders when working with observational data. Methods include those developed in <a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.1657">Brumback et al.&nbsp;(2004)</a>, <a href="https://academic.oup.com/aje/article-abstract/174/3/345/247053?redirectedFrom=fulltext&amp;login=false">Li et al.&nbsp;(2011)</a>, the Bayesian and Monte Carlo approaches of <a href="https://onlinelibrary.wiley.com/doi/10.1002/sim.7298">McCandless et al.&nbsp;(2017)</a>. See the <a href="https://cran.r-project.org/web/packages/causens/vignettes/causens-vignette.html">vignette</a> for examples.</p>
<p><a href="https://cran.r-project.org/package=door">door</a> v0.0.2: Provides functions for the design, analysis, and interpretation of clinical trials and other research studies based on the patient centric benefit risk evaluation. See <a href="https://www.taylorfrancis.com/books/edit/10.1201/9781003390855/handbook-generalized-pairwise-comparisons-marc-buyse-everardo-saad-johan-verbeeck-micka%C3%ABl-de-backer-geert-molenberghs-vaiva-deltuvaite-thomas">Hamasaki &amp; Evans (2025)</a> for details of the statistical methods and the <a href="https://methods.bsc.gwu.edu/">Shiny App</a> for documentation and examples.</p>
<p><a href="door.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/door.png" class="img-fluid" alt="Sample size page from door shiny app."></a></p>
</section>
<section id="music-theory" class="level3">
<h3 class="anchored" data-anchor-id="music-theory">Music Theory</h3>
<p><a href="https://cran.r-project.org/package=musicMCT">musicMCT</a> v0.2.0: Provides functions to analyze musical scales à la <em>Modal Color Theory</em> of <a href="https://read.dukeupress.edu/journal-of-music-theory/article-abstract/69/1/1/401399/Modal-Color-Theory?redirectedFrom=fulltext">Sherrill (2025)</a>, work with conventional music pitch theory and the <em>continuous geometries</em> of <a href="https://www.science.org/doi/10.1126/science.1153021">Callender et al.&nbsp;(2008)</a>, and identify structural properties of scales and calculate derived values. There is an <a href="https://cran.r-project.org/web/packages/musicMCT/vignettes/musicMCT.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/musicMCT/vignettes/visualizing_higher_dimensions.html">Visualizing Higher Dimensions</a>.</p>
<p><a href="musicMCT.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/musicMCT.png" class="img-fluid" alt="Plot scale evenness"></a></p>
</section>
<section id="networks" class="level3">
<h3 class="anchored" data-anchor-id="networks">Networks</h3>
<p><a href="https://cran.r-project.org/package=INetTool">INetTool</a> v0.1.1: Implements methods to model complex systems as a consensus network where nodes can represent either statistical units or observed variables and edges represent distance metrics or correlation between units. See <a href="https://link.springer.com/article/10.1007/s00180-024-01536-8">Policastro et al.&nbsp;(2024)</a> for a description of the method and the <a href="https://cran.r-project.org/web/packages/INetTool/vignettes/INet.html">vignette</a> for an example.</p>
<p><a href="INet.png" class="lightbox" data-gallery="quarto-lightbox-gallery-16"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/INet.png" class="img-fluid" alt="Plot of concensus network"></a></p>
</section>
<section id="programming" class="level3">
<h3 class="anchored" data-anchor-id="programming">Programming</h3>
<p><a href="https://cran.r-project.org/package=putior">putior</a> v0.1.0: Provides tools for extracting and processing structured annotations from <code>R</code> and <code>Python</code> source files to facilitate workflow visualization. Functions scan source files for annotations that define nodes, connections, and metadata within a data processing workflow, which are used to generate visual representations of data flows and processing steps across polyglot software environments. See <a href="https://academic.oup.com/comjnl/article-abstract/27/2/97/343244?redirectedFrom=fulltext&amp;login=false">Knuth (1984)</a> for background and the <a href="https://cran.r-project.org/web/packages/putior/vignettes/getting-started.html">vignette</a> and <a href="https://cran.r-project.org/web/packages/putior/readme/README.html">README</a> for more information.</p>
<p><a href="https://cran.r-project.org/package=quickr">quickr</a> v0.1.0: Provides compiled <code>R</code> functions annotated with type and shape declarations for fast performance with robust runtime type checking. Supports both just-in-time (JIT) and ahead-of-time (AOT) compilation. Compilation is performed by lowering <code>R</code> code to <code>FORTRAN</code>. See <a href="https://cran.r-project.org/web/packages/quickr/readme/README.html">README</a> for examples.</p>
</section>
<section id="statistics" class="level3">
<h3 class="anchored" data-anchor-id="statistics">Statistics</h3>
<p><a href="https://cran.r-project.org/package=aamatch">aamatch</a> v0.3.7: Implements a simple version of multivariate matching using a propensity score, near-exact matching, near-fine balance, and robust Mahalanobis distance matching. You specify the variables, and the program does everything else. See <a href="https://www.annualreviews.org/content/journals/10.1146/annurev-statistics-031219-041058">Rosenbaum (2020)</a> for details.</p>
<p><a href="https://cran.r-project.org/package=bayesmsm">bayesmsm</a> v1.0.0: Implements Bayesian marginal structural models for causal effect estimation with time-varying treatment and confounding and includes an extension for informative right censoring. <a href="https://academic.oup.com/biometrics/article-abstract/71/2/279/7511423?redirectedFrom=fulltext&amp;login=false">See Saarela (2015)</a> for methodological details. There are two vignettes containing examples <a href="https://cran.r-project.org/web/packages/bayesmsm/vignettes/bayesmsm-censoring.html">with right censoring</a> and <a href="https://cran.r-project.org/web/packages/bayesmsm/vignettes/bayesmsm-nocensoring.html">without right censoring</a>.</p>
<p><a href="bayesmsm.png" class="lightbox" data-gallery="quarto-lightbox-gallery-17"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/bayesmsm.png" class="img-fluid" alt="Distribution of estimated treatment effects"></a></p>
<p><a href="https://cran.r-project.org/package=BCD">BCD</a> v0.1.1: Implements bivariate binomial, geometric, and Poisson distributions based on conditional specifications and includes tools for data generation and goodness-of-fit testing for these three distribution families. For methodological details, see <a href="https://www.tandfonline.com/doi/full/10.1080/03610926.2024.2315294">Ghosh et al.(2025)</a>, <a href="https://www.tandfonline.com/doi/full/10.1080/03610918.2021.2004419">Ghosh et al.&nbsp;(2023)</a>, and <a href="https://www.tandfonline.com/doi/full/10.1080/02664763.2020.1793307">Ghosh et al.&nbsp;(202)</a>. There are three vignettes: <a href="https://cran.r-project.org/web/packages/BCD/vignettes/bbcd.html">Bivariate Binomial Conditionals</a>, <a href="https://cran.r-project.org/web/packages/BCD/vignettes/bgcd.html">Bivariate Geometric Conditionals</a>, and <a href="https://cran.r-project.org/web/packages/BCD/vignettes/bpcd.html">Bivariate Poisson Conditionals</a>.</p>
<p><a href="https://cran.r-project.org/package=lognGPD">lognGPD</a> v0.1.0: Provides functions to estimate a lognormal, generalized Pareto mixture model via the Expectation-Maximization algorithm and includes functions for random number simulation and density evaluation. For details, see <a href="doi:10.48550/arXiv.2505.22507">Bee &amp; Santi (2025)</a>.</p>
<p><a href="https://cran.r-project.org/package=QuantilePeer">QuantilePeer</a> v0.0.1: Provides functions to simulate and estimate peer effect models, including the quantile-based specification (<a href="https://arxiv.org/abs/2506.12920">Houndetoungan (2025)</a>), and the models with Constant Elasticity of Substitution (CES)-based social norm (<a href="https://www.econometricsociety.org/publications/econometrica/2024/03/01/Toward-a-General-Theory-of-Peer-Effects">Boucher et al.&nbsp;(2024)</a>). See the <a href="https://cran.r-project.org/web/packages/QuantilePeer/vignettes/introduction_to_QuantilePeer.pdf">vignette</a> for details.</p>
<p><a href="https://cran.r-project.org/package=riskdiff">riskdiff</a> v0.2.1: Provides functions to calculate risk differences (or prevalence differences for cross-sectional data) using generalized linear models with automatic link function selection. See <a href="https://www.tandfonline.com/doi/full/10.1080/00273171.2011.568786">Austin (2011)</a> for background on propensity score matching and <a href="https://www.jstatsoft.org/article/view/v086i09">Donoghoe &amp; Marschner (2018)</a> for package details. There are two vignettes: <a href="https://cran.r-project.org/web/packages/riskdiff/vignettes/riskdiff-intro.html">Getting Started</a>, <a href="https://cran.r-project.org/web/packages/riskdiff/vignettes/iptw-analysis.html">Causal Inference</a>.</p>
<p><a href="riskdiff.png" class="lightbox" data-gallery="quarto-lightbox-gallery-18"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/riskdiff.png" class="img-fluid" alt="Propensity score plot"></a></p>
<p><a href="https://cran.r-project.org/package=survextrap">survextrap</a> v1.0: Provides functions for survival analysis using Bayesian models for individual-level right-censored data. Hazard functions are modeled with M-splines. Priors can be customized and calibrated to substantive beliefs. Posterior distributions are estimated using <code>Stan</code>. See <a href="https://bmcmedresmethodol.biomedcentral.com/articles/10.1186/s12874-023-02094-1">Jackson (2023)</a> for details. There are three vignettes, including <a href="https://cran.r-project.org/web/packages/survextrap/vignettes/examples.html">Examples</a> and <a href="https://cran.r-project.org/web/packages/survextrap/vignettes/priors.html">Priors</a>.</p>
<p><a href="survextrap.png" class="lightbox" data-gallery="quarto-lightbox-gallery-19"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/survextrap.png" class="img-fluid" alt="Survival curves showing spline knots"></a></p>
<p><a href="https://cran.r-project.org/package=unsum">unsum</a> v0.2.0: Reconstructs all possible raw data that could have led to reported summary statistics. Provides a wrapper for the <code>Rust</code> implementation of the <code>CLOSURE</code> algorithm. See the <a href="https://cran.r-project.org/web/packages/unsum/vignettes/unsum.html">vignette</a>.</p>
<p><a href="unsum.png" class="lightbox" data-gallery="quarto-lightbox-gallery-20"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/unsum.png" class="img-fluid" alt="Plot of mean sapple found by CLOSURE"></a></p>
</section>
<section id="time-series" class="level3">
<h3 class="anchored" data-anchor-id="time-series">Time Series</h3>
<p><a href="https://cran.r-project.org/package=gseries">gseries</a> v3.0.2: Provides functions to improve the coherence of time series data. The methods used are described in <a href="https://link.springer.com/book/10.1007/0-387-35439-5">Dagum &amp; Cholette (2006)</a>. There are four vignettes, including <a href="https://cran.r-project.org/web/packages/gseries/vignettes/benchmarking-cookbook.html">Benchmarking Cookbook</a> and <a href="https://cran.r-project.org/web/packages/gseries/vignettes/benchmarking-demo-script.html">A Beginners Benchmarking Script</a>.</p>
</section>
<section id="utilities" class="level3">
<h3 class="anchored" data-anchor-id="utilities">Utilities</h3>
<p><a href="https://cran.r-project.org/package=blocking">blocking</a> v1.0.1: Provides blocking methods for record linkage and deduplication using approximate nearest neighbor algorithms, functions to generate shingles from character strings and similarity vectors for record comparison, and evaluation metrics for assessing blocking performance, including false positive and false negative rates. For background and details, see <a href="https://dl.acm.org/doi/10.1145/3377455">Papadakis et al.&nbsp;(2020)</a>, <a href="https://link.springer.com/chapter/10.1007/978-3-319-11257-2_20">Steorts et al.&nbsp;(2014)</a>, <a href="https://www150.statcan.gc.ca/n1/en/catalogue/12-001-X202100200002">Dasylva and Goussanou (2021)</a>, and <a href="https://link.springer.com/article/10.1007/s42081-022-00153-3">Dasylva and Goussanou (2022)</a>. There are three vignettes, including blocking records for <a href="https://cran.r-project.org/web/packages/blocking/vignettes/v1-deduplication.html">deduplication</a> and <a href="https://cran.r-project.org/web/packages/blocking/vignettes/v2-reclin.html">record linkage</a>.</p>
<p><a href="blocking.png" class="lightbox" data-gallery="quarto-lightbox-gallery-21"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/blocking.png" class="img-fluid" alt="Plot of distribution of cluster distances"></a></p>
<p><a href="https://cran.r-project.org/package=flir">flir</a> v0.5.0: Provides functions to find and fix <a href="https://english.stackexchange.com/questions/623031/etymology-of-the-verb-lint-in-the-context-of-programming-where-it-means-to-app">lints</a> (code patterns that are not optimal because they are inefficient) in R code. There are three vignettes including <a href="https://cran.r-project.org/web/packages/flir/vignettes/adding_rules.html">Adding new rules</a> and <a href="https://cran.r-project.org/web/packages/flir/vignettes/automatic_fixes.html">Automatic fixes</a>.</p>
<p><a href="flir.gif" class="lightbox" data-gallery="quarto-lightbox-gallery-22"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/flir.gif" class="img-fluid" alt="Gif showing reviewing of fixes"></a></p>
</section>
<section id="visualization" class="level3">
<h3 class="anchored" data-anchor-id="visualization">Visualization</h3>
<p><a href="https://cran.r-project.org/package=fractalforest">fractalforest</a> v1.0.1: Provides functions to create and visualize fractal trees and fractal forests, based on the Lindenmayer system (L-system). For more details, see <a href="https://www.sciencedirect.com/science/article/abs/pii/0022519368900799?via%3Dihub">Lindenmayer (1968a)</a> and <a href="https://www.sciencedirect.com/science/article/abs/pii/0022519368900805?via%3Dihub">Lindenmayer (1968b)</a>. There is an <a href="https://cran.r-project.org/web/packages/fractalforest/vignettes/intro-to-fractal-forest.html">Introduction</a> and a vignette on <a href="https://cran.r-project.org/web/packages/fractalforest/vignettes/customizing-fractal-trees.html">customizing fractal trees</a>.</p>
<p><a href="fractalforest.png" class="lightbox" data-gallery="quarto-lightbox-gallery-23"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/fractalforest.png" class="img-fluid" alt="Fractal based rendering of trees"></a></p>
<p><a href="https://cran.r-project.org/package=ggtime">ggtime</a> v0.1.0: Extends <code>ggplot2</code> by implementing a grammar of temporal graphics and helper functions for visualizing temporal patterns in time series graphics, time plots, season plots, and seasonal sub-series plots. See <a href="https://cran.r-project.org/web/packages/ggtime/readme/README.html">README</a> for examples.</p>
<p><a href="ggtime.png" class="lightbox" data-gallery="quarto-lightbox-gallery-24"><img src="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/ggtime.png" class="img-fluid" alt="Multi year seasonal plots"></a></p>
</section>
</div>
</div>



 ]]></description>
  <category>Top 40</category>
  <guid>https://rworks.dev/posts/june-2025-top-40-new-cran-packages/</guid>
  <pubDate>Wed, 30 Jul 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rworks.dev/posts/june-2025-top-40-new-cran-packages/polarzonoid.png" medium="image" type="image/png" height="96" width="144"/>
</item>
</channel>
</rss>
