library(tibble) # for example data
library(stats) # t.test()
library(procs) # proc_ttest()R vs SAS Paired T-Test
Summary
Goal
The goal of this comparison is to evaluate whether paired t-tests produce equivalent results in R and SAS for both normal and lognormal data. For normal data, results from stats::t.test(), procs::proc_ttest(), and SAS PROC TTEST are compared directly. For lognormal data, results from a log-transformation / back-transformation approach in R are compared with SAS PROC TTEST using the DIST=LOGNORMAL option.
Scope
Prerequisites
R packages
Data
Normal Paired t-test and Lognormal Paired t-test Sample Data
We use a simulated paired dataset containing systolic blood pressure measurements before and after treatment. The same data are used in the R and SAS examples to compare the log-transformation / back-transformation approach in R with SAS PROC TTEST using the DIST=LOGNORMAL option.
R:
pressure <- tibble::tribble(
~SBPbefore, ~SBPafter,
120, 128,
124, 131,
130, 131,
118, 127,
140, 132,
128, 125,
140, 141,
135, 137,
126, 118,
130, 132,
126, 129,
127, 135
)SAS:
data pressure;
input SBPbefore SBPafter @@;
datalines;
120 128 124 131 130 131 118 127
140 132 128 125 140 141 135 137
126 118 130 132 126 129 127 135
;
run;Paired t-test Comparison
The following table shows the types of Paired t-test analysis, the capabilities of each language, and whether or not the results from each language match.
| Analysis | Supported in R | Supported in SAS | Results Match | Notes |
|---|---|---|---|---|
| Paired t-test, normal data | Yes | Yes | Yes | In Base R, use paired = TRUE on t.test() function |
| Paired t-test, lognormal data | Yes | Yes | Yes | Apply a natural log transformation to both paired measurements, perform the paired t-test on the log-transformed data, and exponentiate the mean difference and the confidence limits. |
Comparison Results
Normal Data
Here is a table of comparison values between t.test(), proc_ttest(), and SAS PROC TTEST:
| Statistic | t.test() | proc_ttest() | PROC TTEST | Match | Notes |
|---|---|---|---|---|---|
| Degrees of Freedom | 11 | 11 | 11 | Yes | |
| t value | -1.089648 | -1.089648 | -1.09 | Yes | |
| p value | 0.2992 | 0.2992 | 0.2992 | Yes | |
| Mean Difference | -1.8333 | -1.8333 | -1.8333 | Yes | |
| Lower 95% CL Mean Difference | -5.5365 | -5.5365 | -5.5365 | Yes | |
| Upper 95% CL Mean Difference | 1.8698 | 1.8698 | 1.8698 | Yes |
Lognormal Data
For lognormal paired data, a paired t-test can be performed in R by applying a natural log transformation to both paired measurements and conducting the analysis on the log-transformed values. The resulting mean difference and confidence limits can then be exponentiated to obtain a geometric mean ratio and corresponding confidence limits. The table below compares results from stats::t.test(), procs::proc_ttest(), and SAS PROC TTEST with the DIST=LOGNORMAL option.
Here is a table of comparison values between t.test(), proc_ttest(), and SAS PROC TTEST:
| Statistic | t.test() | proc_ttest() | PROC TTEST | Match | Notes |
|---|---|---|---|---|---|
| Degrees of Freedom | 11 | 11 | 11 | Yes | |
| t value | -1.094185 | -1.094185 | -1.09 | Yes | |
| p value | 0.2973 | 0.2973 | 0.2973 | Yes | |
| Geometric Mean Ratio | 0.9856 | 0.9856 | 0.9856 | Yes | Back-transformed mean difference |
| Lower 95% CL Mean Ratio | 0.9572 | 0.9572 | 0.9572 | Yes | Back-transformed lower confidence limit |
| Upper 95% CL Mean Ratio | 1.0148 | 1.0148 | 1.0148 | Yes | Back-transformed upper confidence limit |
Summary and Recommendation
For normal data, the R paired t-test capabilities are comparable to SAS. Comparison between SAS and R show identical results for the datasets tried. The procs package proc_ttest() function is very similar to SAS in the syntax and output produced. proc_ttest() also supports by groups, where t.test() does not.
For the lognormal version of the paired t-test, equivalent results can be obtained in R by applying a natural log transformation to the paired measurements, performing the paired t-test on the transformed data, and exponentiating the resulting estimates and confidence limits. Comparison with SAS PROC TTEST using the DIST=LOGNORMAL option showed that the results matched after back-transformation. Therefore, a separate package is not required for this analysis.
References
R t.test() documentation: https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/t.test
R proc_ttest() documentation: https://procs.r-sassy.org/reference/proc_ttest.html
SAS PROC TTEST Paired analysis documentation: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/statug/statug_ttest_syntax08.htm