Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kaplan-Meier curve transform #2144

Open
fgregg opened this issue Aug 22, 2024 · 1 comment
Open

Kaplan-Meier curve transform #2144

fgregg opened this issue Aug 22, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@fgregg
Copy link

fgregg commented Aug 22, 2024

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 fgregg added the enhancement New feature or request label Aug 22, 2024
@Fil
Copy link
Contributor

Fil commented Aug 22, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants