{"spec_id":"ice-basic","library":"makie","language":"julia","code":"# anyplot.ai\n# ice-basic: Individual Conditional Expectation (ICE) Plot\n# Library: makie 0.21.9 | Julia 1.11.9\n# Quality: 93/100 | Created: 2026-08-17\n\nusing CairoMakie\nusing Colors\nusing Random\nusing Statistics\n\nRandom.seed!(42)\n\n# --- Theme tokens -----------------------------------------------------------\nconst THEME    = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG  = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst INK      = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nconst IMPRINT_PALETTE = [\n    colorant\"#009E73\", colorant\"#C475FD\", colorant\"#4467A3\", colorant\"#BD8233\",\n    colorant\"#AE3030\", colorant\"#2ABCCD\", colorant\"#954477\", colorant\"#99B314\",\n]\nconst BRAND          = IMPRINT_PALETTE[1]  # ALWAYS first series\nconst ANYPLOT_NEUTRAL = INK                # baseline / reference line\nconst ANYPLOT_MUTED   = THEME == \"light\" ? colorant\"#6B6A63\" : colorant\"#A8A79F\"  # confidence-band fill\nconst RESPONDER_COLOR     = BRAND                 # responders — primary story, brand green\nconst NONRESPONDER_COLOR  = IMPRINT_PALETTE[3]    # non-responders — blue, CVD-safe contrast to green\n\n# --- Data ---------------------------------------------------------------\n# ICE curves from a gradient-boosted risk model: predicted relapse-risk\n# score as a function of drug dosage, one curve per simulated patient.\nn_patients  = 80\nn_grid      = 60\ndosage_grid = range(0, 100; length = n_grid)\n\n# ~65% of patients respond to dosage (risk falls with increasing dose);\n# the remainder show little to no response — the subgroup split an ICE\n# plot is designed to reveal that a partial-dependence average would hide.\nresponder = rand(n_patients) .< 0.65\n\nrisk_curves = Matrix{Float64}(undef, n_grid, n_patients)\nfor i in 1:n_patients\n    baseline = 55 + randn() * 8\n    if responder[i]\n        slope     = -0.34 + randn() * 0.07\n        curvature = 0.0014 + randn() * 0.0004\n        risk_curves[:, i] = baseline .+ slope .* dosage_grid .+\n                            curvature .* dosage_grid .^ 2 .+ randn(n_grid) .* 1.4\n    else\n        slope = 0.04 + randn() * 0.05\n        risk_curves[:, i] = baseline .+ slope .* dosage_grid .+ randn(n_grid) .* 1.4\n    end\nend\n\npdp_curve = vec(mean(risk_curves; dims = 2))\n\n# Interquartile spread at each grid point, to anchor the eye against the\n# dense haze of 80 overlapping semi-transparent lines.\nband_lo = [quantile(risk_curves[j, :], 0.25) for j in 1:n_grid]\nband_hi = [quantile(risk_curves[j, :], 0.75) for j in 1:n_grid]\n\nfirst_responder    = findfirst(responder)\nfirst_nonresponder = findfirst(!, responder)\n\n# Observed dosages actually recorded per patient, for the x-axis rug.\nobserved_dosage = clamp.(45 .+ randn(n_patients) .* 22, 0, 100)\n\n# --- Plot -----------------------------------------------------------------\nfig = Figure(\n    resolution      = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title              = \"ice-basic · julia · makie · anyplot.ai\",\n    titlesize          = 20,\n    titlecolor         = INK,\n    xlabel             = \"Drug Dosage (mg)\",\n    ylabel             = \"Predicted Relapse Risk (%)\",\n    xlabelsize         = 14,\n    ylabelsize         = 14,\n    xlabelcolor        = INK,\n    ylabelcolor        = INK,\n    xticklabelsize     = 12,\n    yticklabelsize     = 12,\n    xticklabelcolor    = INK_SOFT,\n    yticklabelcolor    = INK_SOFT,\n    xtickcolor         = INK_SOFT,\n    ytickcolor         = INK_SOFT,\n    backgroundcolor    = PAGE_BG,\n    topspinevisible    = false,\n    rightspinevisible  = false,\n    leftspinecolor     = INK_SOFT,\n    bottomspinecolor   = INK_SOFT,\n    xgridvisible       = false,\n    ygridcolor         = RGBAf(INK.r, INK.g, INK.b, 0.15),\n    xminorgridvisible  = false,\n    yminorgridvisible  = false,\n)\n\nband!(\n    ax, dosage_grid, band_lo, band_hi;\n    color = (ANYPLOT_MUTED, 0.15),\n    label = \"Interquartile spread (25th–75th pct)\",\n)\n\nfor i in 1:n_patients\n    group_color = responder[i] ? RESPONDER_COLOR : NONRESPONDER_COLOR\n    label = if i == first_responder\n        \"Responders (ICE)\"\n    elseif i == first_nonresponder\n        \"Non-responders (ICE)\"\n    else\n        nothing\n    end\n    lines!(\n        ax, dosage_grid, risk_curves[:, i];\n        color     = (group_color, 0.16),\n        linewidth = 1.3,\n        label     = label,\n    )\nend\n\nlines!(\n    ax, dosage_grid, pdp_curve;\n    color     = ANYPLOT_NEUTRAL,\n    linewidth = 5,\n    label     = \"Population average (PDP)\",\n)\n\n# Rug plot: distribution of the dosages actually observed in the cohort.\nvlines!(\n    ax, observed_dosage;\n    ymin      = 0.0,\n    ymax      = 0.035,\n    color     = INK_SOFT,\n    linewidth = 1.3,\n    alpha     = 0.5,\n)\n\naxislegend(\n    ax;\n    position        = :rt,\n    backgroundcolor = THEME == \"light\" ? colorant\"#FFFDF6\" : colorant\"#242420\",\n    labelcolor      = INK_SOFT,\n    framevisible    = false,\n)\n\n# --- Save -------------------------------------------------------------------\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}