version 2.0.0.0 (16.7 KB) by
Giuseppe Cardillo

Comparing survival curves of two groups using the log rank test

Comparing survival curves of two groups using the log rank test.

Comparison of two survival curves can be done using a statistical hypothesis test called the log rank test. It is used to test the null hypothesis that there is no difference between the population survival curves (i.e. the probability of an event occurring at any time point is the same for each population). This function uses the Kaplan-Meier procedure to estimate the survival function (KMPLOT), so if it misses, logrank will try to download it from FEX.

Created by Giuseppe Cardillo

giuseppe.cardillo-edta@poste.it

To cite this file, this would be an appropriate format: Cardillo G. (2008). LogRank: Comparing survival curves of two groups using the log rank test http://www.mathworks.com/matlabcentral/fileexchange/22317

Giuseppe Cardillo (2020). Logrank (https://github.com/dnafinder/logrank), GitHub. Retrieved .

Created with
R2014b

Compatible with any release

**Inspired:**
MatSurv

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Zhengyang SunFor Ms.Keller's problem,you could download 'kmplot.m' file from the link and unzip it to the file where you put 'logrank.m' and data there.

Hellen KellerI got an error like this:

Incorrect use checkfilename>validateFilename (line 122)

Function UNZIP cannot read URL

''https://it.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/22293/versions/10/download/zip''.

Error checkfilename (line 49)

[fullfilename, fid] = validateFilename( ...

Error parseUnArchiveInputs (line 81)

[archiveFilename, url] = checkfilename(archiveFilename, validExtensions, ...

Error unzip (line 57)

[zipFilename, outputDir, url, urlFilename] = parseUnArchiveInputs( ...

Error logrank (line 75)

Filename=unzip('https://it.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/22293/versions/10/download/zip','prova');

DecasbarThanks for your contribution Giuseppe.

Jordan MandelThis seems to be the Gehan-Breslow-Wilcoxon test, and not the Log-rank (Mantel-Cox) test. Graphpad Prism puts out the results for both kinds of survival, and the output of your program is consistent with Gehan-Breslow-Wilcoxon.

Haoyu ZhongGiuseppe CardilloThank you Jacob.

For the first question, kmplot shows confidence intervals and median time. If I add these lines on Logrank it became too difficult to understand.

For the second question, you can use "text" or "gtext" matlab function to add on to the plot whatever you want.

Feel free to modify the code.

Jacob ScottOne more thought - could you make it so that the hazard ratios and p-value are reported in the figure?

thanks so much for this

Jacob ScottI've found that the plots that come out of kmplot are much prettier (and contain more information) than the plots that are generated by logrank. Is there a way that I'm missing to plot two survival curves on the same axis using kmplot? or to make the plots from logrank more similar to those of kmplot?

Thanks, great tool.

Fernando PenaGiuseppe CardilloThank you Jan.

1) You are right. There is a bug in kmplot not in logrank. Now I have just upload the new version of kmplot on FEX. If you need, write me in private and I'll send you the file.

2) they are both correct! I explain. If you are doing a test of a drug against placebo, you will use a 1-tailed test (because, before to do the experiments you are thinking that drug is better of placebo). But if you are testing two different drugs or two different protocols you can only ask to logrank test: Are they different? And so you have to use a 2-tailed test. Of course, 1-tailed test are more powerful but the conditions to apply them are not always respected. Anyway, I'll clarify this in the logrank help and output

Jan liphardtVery useful, but I had two questions. (1) LogRank gives an error at line 116, at [table1 table12 t1 T1 xcg1 ycg1 lambda1]=kmplot(x1,0.05,cflag,0);

This is fixed by deleting the last arg to kmplot, giving kmplot(x1,0.05,flag);

2) The p-value for LogRank is twice what it should be? 0.03058 vs. 0.01529 in the file header. Perhaps, mismatch between old documentation and changed function, or extra factor of 2 in p = 2*(1-0.5*erfc(-z/realsqrt(2))); %p-value

Removing Yates' gives 0.019250, which is correct. I tend to think 0.03058 is correct, with the Yates' correction. Thanks for the super-useful code!

Giuseppe Cardillothe cyclist is right. He did a perfect explanation

the cyclistThe logrank calculation here is correct. This code applies the Yates correction in the calculation of the z-score. (Line 207 of my version of the code.) I get 0.0193 from this code when I remove the Yates correction.

NitinI also am having trouble with this function.. the p-values are always slightly off (usually a little higher than what I get using other programs)

AbhijitI'm sorry, but using your software I do not get the p-value for the log-rank test that is consistent with the results of either R or Stata (both of which match). For the test data supplied with the function, I get a p-value of 0.0193, whereas you're getting a p-value of 0.01529. The KM curves are the same, but there is apparently something wrong in your logrank computation.

Giuseppe CardilloI have just uploaded a new version of KMPLOT and LOGRANK that compute hazard rate and hazard ratio

sunbbIs there a way to retrieve the Hazard ratio from the output of Logrank or Kmplot function?

Giuseppe CardilloYes it is. The 0.05 put in that lines is a dummy variable. Infact, in that lines logrank invokes kmplot to compute needed tables (i didnt want to duplicate code). If you look inside kmplot it is commented that a piece of code id jumped whan invoked by logrank and in that piece of code alpha is used to compute the confidence interval of survival curve (that is useless in logrank).

the cyclistI notice that in the call to kmplot [lines 96-97] in current version, the hard-coded value 0.05 is used, even if alpha is specified differently as an input. Is that correct?

Giuseppe CardilloDear Sven, thank you for your email.

T1 and T2 are cleared because they don't appear in the output. As everybody can see in the figure (without downloading the function) logrank always prints all needed output parameters. Logrank recalls another function that is kmplot: this function can give back T1 (if you want). Anyway this possibility is hidden because usually you are only interested to the plot.

SvenExcellent submission - thank you. One note: the outputs to the function [T1,T2] are actually cleared during computation, so an error is thrown if output is requested. To conform a little bit to the MATLAB stats package, I suggest the following changes:

Line one should change to:

function [h,p] = logrank(varargin)

and the end of the function should set h as follows:

h = p<alpha;

The help section could then say:

% Displays:

% Kaplan-Meier plot

% Log-rank statistics

% Outputs:

% H : statistical significance (true if P<ALPHA)

% P : P-value for log-rank test

Giuseppe CardilloThank you for your comment. If you type on matlab "help logrank" you'll find the reference.

J ReyBeautiful, thanks for sharing this. If we use your software for a journal purpose, what reference should we use?

J ReyGiuseppe CardilloAt the moment, there is not on FEX the new uploaded file (this takes 1-2 days). If you contact me in private, I'll send you the file as an attach

the cyclistThanks for the really quick reply. I appreciate it.

However, the program still crashes for me, with the same error, when I try the syntax you suggest. Maybe the version on the File Exchange is not quite the same as yours?

Giuseppe CardilloYou are partially right. If you use only the first column, the routine crashes because the informations in the second column are mandatory. If you want no censored data:

>> x1(:,2)=0; x2(:,2)=0; logrank(x1,x2);

Anyway, a bug was present but I fixed it and upload the new version.

the cyclistIt seems to me that this function does not work for the case when none of the data are censored. (That shouldn't be a problem, should it?) For example, using only the first columns of your sample x1 and x2:

>> logrank(x1(:,1),x2(:,1))

causes a crash for me.

En BlThanks.

Giuseppe CardilloI have corrected the bug and uploaded the new version. Anyway, with all these censored data, it is difficult to assess differences....

En BlHere is the data:

x1 =

3010 1

1733 1

2797 1

334 0

3678 1

3314 1

4378 1

3253 1

1672 0

3709 1

1642 1

3040 1

2827 1

and x2 =

1955 1

1808 1

2037 1

3136 1

938 0

3497 1

2858 1

3832 1

3256 0

2933 0

5054 1

927 0

3795 0

4155 1

3304 0

sorry for the late reply.

Giuseppe CardilloPerhaps, if you email me the data I can understand the error. I think that your input data matrix are in a wrong format.

En BlI get this error:

Matrix dimensions must agree.

Error in ==> logrank at 157

K=find((table12(:,1)<table(c(I),1) & table12(:,1)>=table(J,1)),1,'last');

I debugged it and J=[] .