add number at risk to the Kaplan-Meier plot in survival analysis
Does someone know how to add the number at risk to the Kaplan-Meier survival curve based on the synax in the below? I can do it using Stata, R, etc., kinda not difficult
at all. But I have to covert everything from Stata to SPSS syntax in order for discussing my project with my supervisor now. I’ll appreciate any help.
KM SurivivalTime BY age
Erasmus MC, Rotterdam
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
For a list of commands to manage subscriptions, send the command
Re: add number at risk to the Kaplan-Meier plot in survival analysis
I don't have KM on my machine, but with this simple a model you can calculate the surviving N yourself to add into the plot. Below is one way to do that.
The resulting graph you will see is a bit crowded with labels. If you turned it into a discrete table (I don't remember exactly what KM spits out) - it would produce fewer stats.
SET SEED 10.
LOOP #i = 1 TO 200.
COMPUTE age = TRUNC(RV.UNIFORM(1,4)).
COMPUTE SurvTime = RV.UNIFORM(1,15).
END INPUT PROGRAM.
DATASET NAME Sim.
VALUE LABELS age 1 '30s' 2 '40s' 3 '50s'.
*Censoring cases above 5.
DO IF SurvTime > 5.
COMPUTE Vital = 0.
COMPUTE CensTime = 5.
COMPUTE Vital = 1.
COMPUTE CensTime = SurvTime.
*KM CensTime BY age
*Manually calculate remaining cases.
SORT CASES BY age SurvTime.
AGGREGATE OUTFILE=* MODE=ADDVARIABLES
/TotAgeN = N.
*Calculate N within so have the remaining number.
DO IF ($casenum = 1) OR (age <> LAG(age) ).
COMPUTE RemN = TotAgeN.
ELSE IF Vital = 1.
COMPUTE RemN = LAG(RemN) - 1.
ELSE IF Vital = 0.
COMPUTE RemN = LAG(RemN).
Once you have the data though you can do some more interesting things though to visualize the remaining N besides just a label. Here are examples of making the lines smaller throughout time, and another example of using the size of point markers superimposed on the lines.