Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mmrm and wilcoxs #122

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
637 changes: 588 additions & 49 deletions Comp/r-sas_mmrm.qmd

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions R/anova_cache/html/__packages

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
79 changes: 79 additions & 0 deletions R/nonpara_wilcoxon_ranksum.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: "Wilcoxon Rank Sum (Mann Whitney-U) in R"
---

```{r}
#| echo: FALSE
#| include: FALSE
library(tidyverse)
```

# Overview

Wilcoxon rank sum test, or equivalently, Mann-Whitney U-test is a rank based non-paramatric method. The aim is to examine the differences between two groups. To be more specific, it tests whether the median difference between pairs is equal to zero.

It is the non-parametric equivalent to two-sample t-test, where the two groups are not paired.

## Available R package

The `stats` package implements various classic statistical tests, including wilcoxon rank sum test.

```{r}
#| eval: false
#| echo: true
# x, y are two unpaired vectors. Do not necessary need to be of the same length.
stats::wilcox.test(x, y, paired = F)
```


## Example: Birth Weight

*Data source: Table 30.4, Kirkwood BR. and Sterne JAC. Essentials of medical statistics. Second Edition. ISBN 978-0-86542-871-3*

Comparison of birth weights (kg) of children born to 15 non-smokers with those of children born to 14 heavy smokers.

```{r}
#| eval: true
#| echo: true
# bw_ns: non smokers
# bw_s: smokers
bw_ns <- c(3.99, 3.89, 3.6, 3.73, 3.31,
3.7, 4.08, 3.61, 3.83, 3.41,
4.13, 3.36, 3.54, 3.51, 2.71)
bw_s <- c(3.18, 2.74, 2.9, 3.27, 3.65,
3.42, 3.23, 2.86, 3.6, 3.65,
3.69, 3.53, 2.38, 2.34)
```

Can visualize the data on two histograms. Red lines indicate the location of medians.

```{r}
#| eval: true
#| echo: true
par(mfrow =c(1,2))
hist(bw_ns, main = 'Birthweight: non-smokers')
abline(v = median(bw_ns), col = 'red', lwd = 2)
hist(bw_s, main = 'Birthweight: smokers')
abline(v = median(bw_s), col = 'red', lwd = 2)
```


It is possible to see that for non-smokers, the median birthweight is higher than those of smokers. Now we can formally test it with wilcoxon rank sum test.

The default test is two-sided with confidence level of 0.95, and does continuity correction.

```{r}
#| eval: true
#| echo: true
# default is two sided
stats::wilcox.test(bw_ns, bw_s, paired = F)
```
We can also carry out a one-sided test, by specifying `alternative = greater` (if the first item is greater than the second).

```{r}
#| eval: true
#| echo: true
# default is two sided
stats::wilcox.test(bw_ns, bw_s, paired = F, alternative = 'greater')
```

Binary file added data/cached_mmrm_results.rda
Binary file not shown.
6 changes: 3 additions & 3 deletions data/stat_method_tbl.csv
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Generalized Linear Models,Logistic Regression,[R](R/logistic_regr),,
Generalized Linear Models,Poisson/Negative Binomial Regression,,,
Generalized Linear Models,Categorical Repeated Measures,,,
Generalized Linear Models,Categorical Multiple Imputation,,,
Non-parametric Analysis,Wilcoxon signed rank,,,
Non-parametric Analysis,Wilcoxon signed rank,[R](R/nonpara_wilcoxon_ranksum),,
Non-parametric Analysis,Mann-Whitney U/Wilcoxon rank sum,,,
Non-parametric Analysis,Kolmogorov-Smirnov test,,,
Non-parametric Analysis,Kruskall-Wallis test,[R](R/kruskal_wallis),[SAS](SAS/kruskal_wallis),[R vs SAS](Comp/r-sas_kruskalwallis)
Non-parametric Analysis,Friedman test,,,
Non-parametric Analysis,Jonckheere test,,,
Categorical Data Analysis,Binomial test,,,
Categorical Data Analysis,McNemar's test,[R](R/mcnemar),[SAS](SAS/mcnemar),[R vs SAS](Comp/r-sas_mcnemar)
Categorical Data Analysis,Chi-Square Association/Fishers exact,[R](R/association),,
Categorical Data Analysis,Chi-Square Association/Fishers exact,[R](R/association),,[R vs SAS](Comp/r-sas_chi-sq)
Categorical Data Analysis,Cochran Mantel Haenszel,[R](R/cmh),[SAS](SAS/cmh),[R vs SAS](Comp/r-sas_cmh)
Categorical Data Analysis,Confidence Intervals for proportions,,,
Linear Mixed Models,MMRM,[R](R/mmrm),[SAS](SAS/mmrm),[R vs SAS](Comp/r-sas_mmrm)
Expand All @@ -44,4 +44,4 @@ Multivariate methods,Canonical correlation,,,
Multivariate methods,PLS,,,
Other Methods,Nearest neighbour,,,
Other Methods,Causal inference,,,
Other Methods,Machine learning,,,
Other Methods,Machine learning,,,
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/mmrm/review-treatment-bcva-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/mmrm/review-treatment-bcva-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/mmrm/review-treatment-fev-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 135 additions & 2 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,29 @@
}
]
},
"Bioconductor": {
"Version": "3.16"
},
"Packages": {
"BiocManager": {
"Package": "BiocManager",
"Version": "1.30.22",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"utils"
],
"Hash": "d57e43105a1aa9cb54fdb4629725acb1"
},
"BiocVersion": {
"Package": "BiocVersion",
"Version": "3.16.0",
"Source": "Bioconductor",
"Requirements": [
"R"
],
"Hash": "44c5824508b9a10e52dbb505c34fa880"
},
"DBI": {
"Package": "DBI",
"Version": "1.1.3",
Expand Down Expand Up @@ -127,6 +149,20 @@
],
"Hash": "1e035db628cefb315c571202d70202fe"
},
"Rdpack": {
"Package": "Rdpack",
"Version": "2.5",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"methods",
"rbibutils",
"tools",
"utils"
],
"Hash": "2a4dbc2502d3fd9f1c5d24b14a223583"
},
"SparseM": {
"Package": "SparseM",
"Version": "1.81",
Expand All @@ -141,6 +177,22 @@
],
"Hash": "2042cd9759cc89a453c4aefef0ce9aae"
},
"TMB": {
"Package": "TMB",
"Version": "1.9.6",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"Matrix",
"R",
"RcppEigen",
"graphics",
"methods",
"stats",
"utils"
],
"Hash": "b062cb79db56803311ea22b90ed6f57a"
},
"V8": {
"Package": "V8",
"Version": "4.2.2",
Expand Down Expand Up @@ -398,6 +450,18 @@
],
"Hash": "f61dbaec772ccd2e17705c1e872e9e7c"
},
"checkmate": {
"Package": "checkmate",
"Version": "2.3.0",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"backports",
"utils"
],
"Hash": "ed4275b13c6ab74b89a31def0b6bf835"
},
"cli": {
"Package": "cli",
"Version": "3.6.0",
Expand Down Expand Up @@ -546,6 +610,28 @@
],
"Hash": "6b9602c7ebbe87101a9c8edb6e8b6d21"
},
"details": {
"Package": "details",
"Version": "0.3.0",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"clipr",
"desc",
"grid",
"htmltools",
"httr",
"knitr",
"magrittr",
"png",
"stats",
"utils",
"withr",
"xml2"
],
"Hash": "64c0eb2b740ab1ac553d928b3a75d72a"
},
"diffobj": {
"Package": "diffobj",
"Version": "0.3.5",
Expand Down Expand Up @@ -574,7 +660,7 @@
},
"dplyr": {
"Package": "dplyr",
"Version": "1.1.2",
"Version": "1.1.3",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand All @@ -593,7 +679,7 @@
"utils",
"vctrs"
],
"Hash": "dea6970ff715ca541c387de363ff405e"
"Hash": "e85ffbebaad5f70e1a2e2ef4302b4949"
},
"dtplyr": {
"Package": "dtplyr",
Expand Down Expand Up @@ -1426,6 +1512,17 @@
],
"Hash": "c6b2fdb18cf68ab613bd564363e1ba0d"
},
"microbenchmark": {
"Package": "microbenchmark",
"Version": "1.4.10",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"graphics",
"stats"
],
"Hash": "db81b552e393ed092872cf7023469bc2"
},
"mime": {
"Package": "mime",
"Version": "0.12",
Expand All @@ -1446,6 +1543,30 @@
],
"Hash": "587ce77fd3c7bada7eadb2d18b62930d"
},
"mmrm": {
"Package": "mmrm",
"Version": "0.2.2",
"Source": "Bioconductor",
"Repository": "CRAN",
"Requirements": [
"R",
"Rcpp",
"RcppEigen",
"Rdpack",
"TMB",
"checkmate",
"lifecycle",
"methods",
"nlme",
"numDeriv",
"parallel",
"stats",
"stringr",
"testthat",
"utils"
],
"Hash": "cb4cddcde5482069a9afdcb4e246b5c3"
},
"modelr": {
"Package": "modelr",
"Version": "0.1.10",
Expand Down Expand Up @@ -1745,6 +1866,18 @@
],
"Hash": "5e3c5dc0b071b21fa128676560dbe94d"
},
"rbibutils": {
"Package": "rbibutils",
"Version": "2.2.16",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"tools",
"utils"
],
"Hash": "8c06968e0a5b0209c5f34239b1302336"
},
"reactR": {
"Package": "reactR",
"Version": "0.4.4",
Expand Down
Loading