-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from dbosak01/main
Added Paired T-Test
- Loading branch information
Showing
9 changed files
with
319 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
title: "R vs SAS Paired T-Test" | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
library(procs) | ||
``` | ||
|
||
# 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](../R/ttest_Paired.html#normal) | [Yes](../SAS/ttest_Paired.html#normal) | [Yes](#normal) | In Base R, use `paired = TRUE` on `t.test()` function | | ||
| Paired t-test, lognormal data | [Maybe](../R/ttest_Paired.html#lognormal) | [Yes](../SAS/ttest_Paired.html#lognormal) | [NA](#lognormal) | May be supported by **envstats** package | | ||
|
||
## Comparison Results | ||
|
||
### Normal Data {#normal} | ||
|
||
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.089648 | Yes | | | ||
| p value | 0.2992 | 0.2992 | 0.2992 | Yes | | | ||
|
||
### Lognormal Data {#lognormal} | ||
|
||
Since there is currently no known support for lognormal t-test in R, this comparison is not applicable. | ||
|
||
# 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 t-test, it does not appear to be supported in the **stats** or **procs** package. It may be supported in the **envstats** package. More exploration is needed to determine whether this package will produce the expected results, and whether the results will match SAS. | ||
|
||
# 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
title: "Paired t-test" | ||
output: html_document | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
# **Paired t-test in R** | ||
|
||
The Paired t-test is used when two samples are naturally correlated. In the Paired t-test, the difference of the means between the two samples is compared to a given number that represents the null hypothesis. For a Paired t-test, the number of observations in each sample must be equal. | ||
|
||
In R, a Paired t-test can be performed using the Base R `t.test()` from the **stats** package or the `proc_ttest()` function from the **procs** package. | ||
|
||
## Normal Data {#normal} | ||
|
||
By default, the R paired t-test functions assume normality in the data and use a classic Student's t-test. | ||
|
||
### Data Used | ||
|
||
The following data was used in this example. | ||
|
||
```{r eval=TRUE, echo = TRUE} | ||
# Create sample data | ||
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 | ||
) | ||
``` | ||
|
||
### Base R | ||
|
||
#### Code | ||
|
||
The following code was used to test the comparison in Base R. | ||
|
||
```{r eval=TRUE, echo = TRUE} | ||
# Perform t-test | ||
t.test(pressure$SBPbefore, pressure$SBPafter, paired = TRUE) | ||
``` | ||
|
||
### Procs Package | ||
|
||
#### Code | ||
|
||
The following code from the **procs** package was used to perform a paired t-test. | ||
|
||
```{r eval=TRUE, echo = TRUE, message=FALSE, warning=FALSE} | ||
library(procs) | ||
# Perform t-test | ||
proc_ttest(pressure, | ||
paired = "SBPbefore*SBPafter") | ||
``` | ||
|
||
Viewer Output: | ||
|
||
```{r, echo=FALSE, fig.align='center', out.width="50%"} | ||
knitr::include_graphics("../images/ttest/paired_rtest1.png") | ||
``` | ||
|
||
## Lognormal Data {#lognormal} | ||
|
||
The Base R `t.test()` function does not have an option for lognormal data. Likewise, the **procs** `proc_ttest()` function also does not have an option for lognormal data. | ||
|
||
One possibility may be the `tTestLnormAltPower()` function from the **EnvStats** package. This package has not been evaluated yet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: "Paired t-test" | ||
output: html_document | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
# **Paired t-test in SAS** | ||
|
||
The Paired t-test is used when two samples are naturally correlated. In the Paired t-test, the difference of the means between the two samples is compared to a given number that represents the null hypothesis. For a Paired t-test, the number of observations in each sample must be equal. | ||
|
||
In SAS, a Paired t-test is typically performed using PROC TTEST. | ||
|
||
## Normal Data {#normal} | ||
|
||
By default, SAS PROC TTEST t-test assumes normality in the data and uses a classic Student's t-test. | ||
|
||
### Data Used | ||
|
||
The following data was used in this example. | ||
|
||
``` | ||
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 | ||
; | ||
``` | ||
|
||
### Code | ||
|
||
The following code was used to test the comparison of two paired samples of Systolic Blood Pressure before and after a procedure. | ||
|
||
``` | ||
proc ttest data=pressure; | ||
paired SBPbefore*SBPafter; | ||
run; | ||
``` | ||
|
||
Output: | ||
|
||
```{r, echo=FALSE, fig.align='center', out.width="50%"} | ||
knitr::include_graphics("../images/ttest/paired_test1.png") | ||
``` | ||
|
||
## Lognormal Data {#lognormal} | ||
|
||
The SAS paired t-test also supports analysis of lognormal data. Here is the data used for the lognormal analysis. | ||
|
||
### Data | ||
|
||
``` | ||
data auc; | ||
input TestAUC RefAUC @@; | ||
datalines; | ||
103.4 90.11 59.92 77.71 68.17 77.71 94.54 97.51 | ||
69.48 58.21 72.17 101.3 74.37 79.84 84.44 96.06 | ||
96.74 89.30 94.26 97.22 48.52 61.62 95.68 85.80 | ||
; | ||
``` | ||
|
||
### Code | ||
|
||
For cases when the data is lognormal, SAS offers the "DIST" option to chose between a normal and lognormal distribution. The procedure also offers the TOST option to specify the equivalence bounds. | ||
|
||
``` | ||
proc ttest data=auc dist=lognormal tost(0.8, 1.25); | ||
paired TestAUC*RefAUC; | ||
run; | ||
``` | ||
|
||
Output: | ||
|
||
```{r, echo=FALSE, fig.align='center', out.width="70%"} | ||
knitr::include_graphics("../images/ttest/paired_test2.png") | ||
``` | ||
|
||
As can be seen in the figure above, the lognormal variation of the TTEST procedure offers additional results for geometric mean, coefficient of variation, and TOST equivalence analysis. The output also includes multiple p-values. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.