Here is my file for Kaplan-Meier / log-rank tests in R. To use it: 0. Save km.r in an easy to remember location, e.g. the desktop. 1. Open R. 2. At the prompt (">") type "source(file.choose())" (without quotation marks) and click your way to the file saved in step 0. Alternatively, type “source(url()” This makes the km() function and a lung cancer data set available to you. 3. If at the prompt you type: a) "?lung" you will get the data set documentation; same as "help(lung)"; b) "str(lung)", short for "structure", you will get the variable names, types (i.e. numerical, logical, factor, etc.) and the first few values. c) "summary(lung)", you will get summary statistics for each variable; d) "args(km)" you will get the function arguments, their name, position, and, possibly, their default values. e) "km" you will see the function's code, which you are not expected to understand at this point. 4. To get the K-M survival estimates at 0.5, 1, and 2 years for males and females, and to plot the curves in blue and pink respectively, type km(lung$time/365.25, lung$status==1, factor(lung$sex), tit="Lung Cancer", shorttit="death", timev=c(0.5, 1, 2), colv=c("blue", "pink")) 5. To equate 1="M" and 2="F" for sex, in order to get more informative labels in the legend and text output, type km(lung$time/365.25, lung$status==1, factor(c("M","F")[lung$sex]), tit="Lung Cancer", shorttit="death", timev=c(0.5, 1, 2), colv=c("pink", "blue")) 6. km() produces pair-wise comparisons automatically if there are more than two groups. To compare survival between those who gained weight (negative wt.loss), those who lost up to 25 pounds, and those who lost more than 25 pounds, type: km(lung$time/365.25, lung$status==1, cut(lung$wt.loss, c(-Inf, 0, 25, Inf)), tit="Lung Cancer vs. Weight Loss", shorttit="death", timev=c(0.5, 1, 2), colv=c("green", "orange", "red")) km(lung$time/365.25, lung$status==1, cut(lung$wt.loss, c(-Inf, 0, 25, Inf)), tit="Lung Cancer vs. Weight Losss", shorttit="death", timev=c(0.5, 1, 2), colv=c("green", "orange", "red")) 7. If you wish to save your text output, you can highlight the text portion from the R console window and save to your editor. To save the survival graph, you have two choices: a) Use the jpeg=TRUE and fname="filename.jpg" arguments to km. b) Right click on the R graphics window; you can print or save the graph in a variety of formats (bitmap, metafile, etc.) If you are not providing a path explicitly in the fname argument, e.g. fname="c:/myfiles/shapiro/km.jpg" (note "/", not "\"), you may want to type "getwd()" (get working directory) to see where R is saving your file. 8. To end your R session (not recommended if you are concurrently running an Rcmdr session), type "q("yes")" at the prompt; the inner set of double quotes are required. This will save your workspace; otherwise, type "q("no")", but you will lose your work. Notice: The first three arguments are the survival time, the censoring indicator, and the variable defining the groups to be compared. These arguments are passed by position, so order matters; if they are passed as named argument, order does not matter (e.g. "km(censor=lung$status==1), ...") a) The survival time is transformed to years by dividing by 365.25, and this is done on the fly. b) The censoring indicator should assume the value 0 (if numerical) or FALSE (if logical) for events, i.e. non-censored observations. c) The group variable should be a factor. If you type "str(lung)" you will see sex is a numerical variable; it is converted to factor on the fly. d) The reason the colors are reversed in the 2nd call to km is that once "F" and "M" are assigned to sex, their levels are in alphabetical order, so the curve for females (and its color) will precede the one for males. There is no documentation for km, so typing ?km will not produce anything helpful. If you have any questions, please contact me: Alejandro Muñoz del Río 608 263-2284