---
title: "TestDimorph examples"
author: "Bassam A. Abulnoor, MennattAllah H. Attia, Iain R. Konigsberg, and Lyle W.  Konigsberg"
date: "August 22, 2022"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Examples for different package functions}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
bibliography: bibliography.bibtex
---

```{r global-options, include=FALSE}
knitr::opts_chunk$set(comment=NULL)
knitr::opts_chunk$set(fig.width=6, fig.height=4)
suppressPackageStartupMessages(library(TestDimorph))
```

<br> <br>

<h4>Table 1 Description of the main functions of the TestDimorph R package.</h4>

| Function     | Description                                                                                                                                                                                                                                                            | Notes                                                                                                                                                                                                                                                                              |
|-------------|------------------------------|-----------------------------|
| aov_ss       | Calculates sex specific one-way ANOVA from summary statistics and performs pairwise comparisons                                                                                                                                                                        | Uses the summary statistics                                                                                                                                                                                                                                                        |
| D_index      | Dissimilarity index [@RN7899] for statistical computation and visualization of the area of non-overlap in the trait distribution between the sexes.                                                                                                                    | Provides a table and a graphical representation of the selected traits and their corresponding dissimilarity indices. Also provides confidence intervals via a bias-corrected parametric bootstrap.                                                                                |
| extract_sum  | Extract summary statistics needed for the other functions from uploaded raw data directly without need to go to a third-party package.                                                                                                                                 | Can also run the aov_ss, multivariate, t_greene, univariate, or van_vark functions after extracting the summary statistics.                                                                                                                                                        |
| Hedges_g     | Calculates Hedges' [-@RN11055] for effect size between the sexes for a single trait. The confidence interval is found using a method described in @RN8020.                                                                                                             | Can also find the confidence interval using a bias-corrected parametric bootstrap.                                                                                                                                                                                                 |
| MI_index     | Mixture Index is the mixture intersection measure of sexual dimorphism [@RN7880]. @RN7880 also define a normal intersection NI measure which is the overlap coefficient of two normal distributions, equivalent to Inman and Bradley's [-@RN7898] overlap coefficient. | Can produce confidence intervals using a bias-corrected parametric bootstrap.                                                                                                                                                                                                      |
| multivariate | An extension of the univariate analysis of sexual dimorphism between different samples. MANOVA test is used to analyze the interaction effects and main effects.                                                                                                       | Type of MANOVA test employed can be "I", "II" or "III" sum of squares and cross products. The test statistics can be Wilks' lambda, Pillai's trace, Hotelling-Lawley's trace or Roy's largest root. If univariate argument is TRUE, the function conducts ANOVAs on each variable. |
| raw_gen      | Raw data generation from summary statistics using univariate or multivariate normal distributions (with truncation as an option).                                                                                                                                      |                                                                                                                                                                                                                                                                                    |
| t_greene     | Relethford and Hodges' (1985) and Greene's (1989) t-test of sexual dimorphism.                                                                                                                                                                                         | A plot of p-values for differences in sexual dimorphism across all pairs of samples can be produced with plot=TRUE                                                                                                                                                                 |
| univariate   | Univariate analysis of sexual dimorphism using two-way ANOVA.                                                                                                                                                                                                          | Type of sums of squares can type type "I", "II", or "III."                                                                                                                                                                                                                         |
| van_vark     | Provides testing for differences in sexual dimorphism between samples using van Vark et al.'s [-@RN7879] method.                                                                                                                                                       |                                                                                                                                                                                                                                                                                    |

<h4>This is Table 2. The R script extracts summary statistics for body mass from the 1999-2000 NHANES raw data and stores it in NHANES_univariate. The "populations" are self-reported race, coded as "Black" = Non-Hispanic Black, "Mex.Am" = Mexican American, or "White" = Non-Hispanic White, and the data are restricted to ages 20-40 years (inclusive). The output are an ANOVA (type II sums of squares by default) with eta-squared values and a table of pairwise comparisons. For more information on the NHANES studies see <https://www.cdc.gov/nchs/nhanes/index.htm></h4>

```{r Table 2}
Table.02=function () 
{
library(TestDimorph)
options(width=100) # This option just for output from Rmarkdown
NHANES_univariate<<-extract_sum(NHANES_1999,test='uni',run=FALSE) # BMXWT (Body mass)
univariate(NHANES_univariate,es_anova = "eta2",pairwise = TRUE)
}

Table.02()
```

<br>

<h4>This is Table 3. The R script extracts summary statistics for body mass, standing height, and upper arm length from the 1999-2000 NHANES raw data and stores it in NHANES_multivariate. The "populations" are self-reported race, coded as "Black" = Non-Hispanic Black, "Mex.Am" = Mexican American, or "White" = Non-Hispanic White, and the data are restricted to ages 20-40 years (inclusive). The output is a MANOVA table (type II sums of squares and cross products by default) using Wilk's lambda (the default).</h4>

<br>

```{r Table 3}
Table.03=function()
{
library(TestDimorph)
NHANES_multivariate<<-extract_sum(NHANES_1999,test='multi',run=FALSE)
multivariate(NHANES_multivariate)
}

Table.03()
```

<br>

<h4>This is Table 4. The R script uses NHANES_univariate and does an ANOVA with type III sums of squares. It then applies t_greene to do Relethford and Hodges' [-@RN8925] and Greene's [-@RN8546] t-test for all pairs, adjusts the p-values using the false discovery rate, and finally returns a "corrplot" view (<https://cran.r-project.org/package=corrplot>) of the p-values.</h4>

<br>

```{r Table 4}
Table.04=function()
{
library(TestDimorph)
print(univariate(NHANES_univariate, type_anova='III'))
t_greene(NHANES_univariate,plot = TRUE,padjust ="fdr")
}

Table.04()
```

<br>

<h4>This is Table 5. The R script extracts summary statistics on eight variables from four samples in the W.W. Howells'[-@RN8613; -@RN9221; -@RN11068; -@RN10726] dataset. The full data set can be found in <https://rdrr.io/github/geanes/bioanth/man/howell.html>. The script then runs van Vark et al.'s [-@RN7879] analysis.</h4>

<br>

```{r Table 5}
Table.05=function()
{
library(TestDimorph)
to_van_Vark=extract_sum(Howells,test='van',run=F)
van_vark(to_van_Vark)
}

Table.05()
```

<br>

<h4>This is Table 6. The R script writes summary statistics on femur head diameters for males and females from four samples [@RN11497; @RN11494; @RN11496; @RN11495] to the object df. It then uses aov_ss to do ANOVA within males and within females across the four samples with each ANOVA followed by post-hoc pairwise comparisons.</h4>

<br>

```{r Table 6}
Table.06=function () 
{
# Comparisons of femur head diameter in four populations
library(TestDimorph)
df <- data.frame(
  Pop = c("Turkish", "Bulgarian", "Greek", "Portuguese"),
  m = c(150.00, 82.00, 36.00, 34.00),
  f = c(150.00, 58.00, 34.00, 24.00),
  M.mu = c(49.39, 48.33, 46.99, 45.20),
  F.mu = c(42.91, 42.89, 42.44, 40.90),
  M.sdev = c(3.01, 2.53, 2.47, 2.00),
  F.sdev = c(2.90, 2.84, 2.26, 2.90)
)
print(aov_ss(x = df, CI=0.95),digits=6)
}

Table.06()
```

<br>

<h4>This is Table 7. The R script uses the maximum width of the patella from @RN11059. Calculated values are the mixture index (MI) and normal intersection (NI) [@RN7880], the D index [@RN7899], and Hedges' g [@RN11055]. For each statistic the bias-corrected parametric bootstrap [@RN11056; @RN11057] is run 1,000 starting from a set.seed value of 42.</h4>

<br>

```{r Table 7,fig.width=6, fig.height=3}
Table.07=function (i.which=13) 
{
library(TestDimorph)
print(MI_index(Cremains_measurements[i.which,],B=1000,rand=F,verbose=F,plot=T))
print(MI_index(Cremains_measurements[i.which,],index_type='NI',
   B=1000,rand=F,plot=T,verbose=F))
print(D_index(Cremains_measurements[i.which,],B=1000,rand=F,verbose=F,plot=T))
print(Hedges_g(Cremains_measurements[i.which,],B=1000,rand=F,verbose=F))
}

Table.07()
```

<br> <br>

<h3>References</h3>
