plot icon indicating copy to clipboard operation
plot copied to clipboard

Kaplan-Meier curve transform

Open fgregg opened this issue 1 year ago • 1 comments

It would be great to have support of kaplan-meier curve, perhaps in the Plot.map family.

As the curve is basically a cumulative sum with some transformations, it shouldn't be too hard.

This code gives a pretty good approxmation:

const caseLength = [{ case_length: 1 }, { case_length: 11 }, { case_length: null }]

Plot.plot({
  y: { transform: (d) => 100 - d * 100 },
  marks: [
    Plot.ruleY([1]), // we want a rule at 0% but it will get flipped by the transform
    Plot.line(
      caseLength,
      Plot.normalizeY(
        (arr) => arr.length,
        Plot.mapY("cumsum", {
          x: "case_length",
          y: (d) => (d.case_length === null ? 0 : 1),
          curve: "step-before"
        })
      )
    )
  ]
})

If interested, I can work on something that extends Plot.map

fgregg avatar Aug 22 '24 01:08 fgregg

It should be possible to simplify a little:

Plot.plot({
  y: { percent: true },
  marks: [
    Plot.ruleY([0]),
    Plot.line(
      caseLength,
      Plot.normalizeY(
        (arr) => arr.length,
        Plot.mapY("cumsum", {
          sort: "case_length",
          reverse: true,
          x: "case_length",
          y: (d) => (d.case_length === null ? 0 : 1),
          curve: "step-after"
        })
      )
    )
  ]
})

I think this is more for an example (or an essay explaining how it's used), rather than as a built-in transform?

Fil avatar Aug 22 '24 14:08 Fil