-
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 #141 from dbosak01/main
One Sample T-Test
- Loading branch information
Showing
7 changed files
with
197 additions
and
1 deletion.
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,49 @@ | ||
--- | ||
title: "R vs SAS One Sample T-Test" | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
library(procs) | ||
``` | ||
|
||
# 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](../R/ttest_1Sample.html#normal) | [Yes](../SAS/ttest_1Sample.html#normal) | [Yes](#normal) | In Base R, use `mu` parameter on `t.test()` function to set null hypothesis value | | ||
| One sample t-test, lognormal data | [Maybe](../R/ttest_1Sample.html#lognormal) | [Yes](../SAS/ttest_1Sample.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 | 29 | 29 | 29 | Yes | | | ||
| t value | 2.364306 | 2.364306 | 2.364306 | Yes | | | ||
| p value | 0.0249741 | 0.0249741 | 0.0249741 | 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 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 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. | ||
|
||
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> |
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,75 @@ | ||
--- | ||
title: "One Sample t-test" | ||
output: html_document | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
# **One Sample t-test in R** | ||
|
||
The One Sample t-test is used to compare a single sample against an expected hypothesis value. In the One Sample t-test, the mean of the sample is compared against the hypothesis value. In R, a One Sample t-test can be performed using the Base R `t.test()` from the **stats** package or the `proc_ttest()` function from the **procs** package. | ||
|
||
### Data Used | ||
|
||
The following data was used in this example. | ||
|
||
```{r eval=TRUE, echo = TRUE} | ||
# Create sample data | ||
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 | ||
) | ||
``` | ||
|
||
## Normal Data {#normal} | ||
|
||
By default, the R one sample t-test functions assume normality in the data and use a classic Student's t-test. | ||
|
||
|
||
### Base R | ||
|
||
#### Code | ||
|
||
The following code was used to test the comparison in Base R. Note that the baseline null hypothesis goes in the "mu" parameter. | ||
|
||
```{r eval=TRUE, echo = TRUE} | ||
# Perform t-test | ||
t.test(read$score, mu = 30) | ||
``` | ||
|
||
### Procs Package | ||
|
||
#### Code | ||
|
||
The following code from the **procs** package was used to perform a one sample t-test. Note that the null hypothesis value goes in the "options" parameter. | ||
|
||
```{r eval=TRUE, echo = TRUE, message=FALSE, warning=FALSE} | ||
library(procs) | ||
# Perform t-test | ||
proc_ttest(read, var = score, | ||
options = c("h0" = 30)) | ||
``` | ||
|
||
Viewer Output: | ||
|
||
```{r, echo=FALSE, fig.align='center', out.width="50%"} | ||
knitr::include_graphics("../images/ttest/onesample_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: "One Sample t-test" | ||
output: html_document | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
# **One Sample t-test in SAS** | ||
|
||
In SAS, a one sample t-test is usually performed using PROC TTEST. The one sample t-test compares the mean of the sample to a provided null hypothesis, called "h0". The h0 value is provided as an option. By default, the h0 value is zero (0). Running the procedure produces a set of results that suggest whether or not the null hypothesis should be rejected. | ||
|
||
### Data Used | ||
|
||
The following data was used in this example. | ||
|
||
``` | ||
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 | ||
; | ||
``` | ||
|
||
## Normal Data {#normal} | ||
|
||
By default, SAS PROC TTEST t-test assumes normality in the data and uses a classic Student's t-test. | ||
|
||
### Code | ||
|
||
The following code was used to test the comparison of a reading scores against a baseline hypothesis value of 30: | ||
|
||
``` | ||
proc ttest data=read h0=30; | ||
var score; | ||
run; | ||
``` | ||
|
||
Output: | ||
|
||
```{r, echo=FALSE, fig.align='center', out.width="50%"} | ||
knitr::include_graphics("../images/ttest/onesample_test1.png") | ||
``` | ||
|
||
## Lognormal Data {#lognormal} | ||
|
||
The SAS one sample t-test also supports lognormal analysis for a one sample t-test. | ||
|
||
|
||
### Code | ||
|
||
Using the same data as above, we will set the "DIST" option to "lognormal" to perform this analysis: | ||
|
||
``` | ||
proc ttest data=read h0=30 dist=lognormal; | ||
var score; | ||
run; | ||
``` | ||
|
||
Output: | ||
|
||
```{r, echo=FALSE, fig.align='center', out.width="60%"} | ||
knitr::include_graphics("../images/ttest/onesample_test2.png") | ||
``` | ||
|
||
As can be seen in the figure above, the lognormal variation of the one sample TTEST provides results for geometric mean, coefficient of variation, and 95% confidence limits for the coefficient of variation. |
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.