library(tibble) # for example data
library(stats) # t.test()
library(procs) # proc_ttest()R vs SAS One Sample T-Test
Summary
Goal
The goal of this comparison is to evaluate whether one sample 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 One Sample t-test and Lognormal One Sample t-test Sample Data
We use a simulated sample data containing one numeric variable, score. The same data are used for the R and SAS examples.
R:
read <- tibble::tribble(
~score, ~count,
40, 2, 47, 2, 52, 2, 26, 1, 19, 2,
25, 2, 35, 4, 39, 1, 26, 1, 48, 1,
14, 2, 22, 1, 42, 1, 34, 2, 33, 2,
18, 1, 15, 1, 29, 1, 41, 2, 44, 1,
51, 1, 43, 1, 27, 2, 46, 2, 28, 1,
49, 1, 31, 1, 28, 1, 54, 1, 45, 1
)SAS:
data read;
input score count @@;
datalines;
40 2 47 2 52 2 26 1 19 2
25 2 35 4 39 1 26 1 48 1
14 2 22 1 42 1 34 2 33 2
18 1 15 1 29 1 41 2 44 1
51 1 43 1 27 2 46 2 28 1
49 1 31 1 28 1 54 1 45 1
;
run;One Sample t-test Comparison
The following table shows the types of One Sample 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 |
|---|---|---|---|---|
| One sample t-test, normal data | Yes | Yes | Yes | In Base R, use mu parameter on t.test() function to set null hypothesis value |
| One sample t-test, lognormal data | Yes | Yes | Yes | Apply natural log transformation, perform stats::t.test() on the log-transformed data, and exponentiate the mean and 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 | 43 | 43 | 43 | Yes | |
| t value | 2.8727 | 2.8727 | 2.87 | Yes | |
| p value | 0.006297 | 0.006297 | 0.0063 | Yes | |
| Mean | 34.8636 | 34.8636 | 34.8636 | Yes | |
| Lower 95% CL Mean | 31.44930 | 31.44930 | 31.44930 | Yes | |
| Upper 95% CL Mean | 38.27797 | 38.27797 | 38.2780 | Yes |
Lognormal Data
Lognormal one sample t-tests can be performed in R by applying a natural log transformation to the data, performing stats::t.test() on the log-transformed values, and exponentiating the resulting mean and confidence limits. Comparison of the example data produced equivalent results to 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 | 43 | 43 | 43 | Yes | Calculated on log-transformed data |
| t value | 1.6373 | 1.6373 | 1.64 | Yes | Calculated on log-transformed data |
| p value | 0.1089 | 0.1089 | 0.1089 | Yes | Calculated on log-transformed data |
| Geometric Mean | 32.84336 | 32.84336 | 32.8434 | Yes | Back-transformed mean |
| Lower 95% CL Mean | 29.3770 | 29.3770 | 29.3770 | Yes | Back-transformed lower confidence limit |
| Upper 95% CL Mean | 36.7187 | 36.7187 | 36.7187 | Yes | Back-transformed upper confidence limit |
Summary and Recommendation
For normal data, the R one sample 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 one sample t-test, R does not provide a dedicated lognormal option in stats::t.test() or procs::proc_ttest(). However, lognormal data can be analyzed by applying a natural log transformation to the data, performing the t-test on the log-transformed values, and exponentiating the resulting mean and confidence limits. Comparison of the example data showed equivalent results to SAS PROC TTEST using the DIST=LOGNORMAL option.
Also note that neither t.test() or proc_ttest() supports the “freq” option that is utilized on examples from the SAS documentation. Therefore, this option has been removed from this comparison. In R, the “freq” functionality could be performed by manipulating the data prior to sending to the t-test function.
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 One Sample analysis documentation: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/statug/statug_ttest_syntax09.htm