---
title: "Using detectnorm in meta-analysis"
author: "Rong Wei Sun & Shu Fai Cheung"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Using detectnorm in meta-analysis}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width  =  6,
  fig.height =  6,
  fig.align = "center"
)
```

## Introduction
The goal of `detectnorm` is to speculate the skewness and kurtosis based on the Beta and truncated normal distribution. When conducting a meta-analysis for two independent groups, we generally retrieved very limited statistics from primary studies, and it is hard to access the raw data. However, the non-normality of raw data could influence the meta-analytic results greatly (Sun & Cheung, 2020). This package allows meta-analysis researchers to speculate the skewness and kurtosis of the raw data, even without the raw data. Instead of normal distribution, if the researcher believes the population distribution is non-normal, then beta-distribution could be a good choice. If the researcher believes the population distribution is normal but truncated by the measuring ability of the instrument, then truncated normal distribution is a good option. 
The package provides not only the skewness and kurtosis estimates but also the figures to visualize them. Now, this package could work directly with the standardized mean difference for two independent groups. It also works for one group of data. 

```{r setup}
library(detectnorm)
```

## Example

This is a basic example which shows you how to solve a common problem:

For one study if you assumed non-normal distribution in population: 
```{r beta_example}
library(detectnorm)
# Situations using beta distributions
set.seed(32411)
#Using Fleishman's method to generate non-normal data
dat1 <- rnonnorm(n = 1000, mean = 0, sd = 1, skew = 2, kurt = 5)$dat
hist(dat1)
psych::describe(dat1)
#Suppose we don't know about the raw data
result <- desbeta(vmean = mean(dat1), vsd = sd(dat1),lo = min(dat1), hi = max(dat1), showFigure = TRUE, rawdata = dat1)
result
```

For one study if you assumed normal distribution in population with truncated by the measurements: 
```{r trun_example}
library(detectnorm)
#Truncated normal distribution
set.seed(34120)
dat2 <- truncnorm::rtruncnorm(n = 1000, a = 0, b = 14, mean = 4, sd = 3)
psych::describe(dat2)
destrunc(vmean=mean(dat2), vsd=sd(dat2), lo=0, hi=14, rawdata = dat2, showFigure = TRUE)
```

For one meta-analysis of two independent groups. It is very similar to the requirements for package `metafor` only you need to add one or two columns for each group about the possible minimum and maximum of the data. It will add information including:

- `g1_alpha`, `g1_beta`, `g2_alpha`, and `g2_beta` are the shape parameters of beta distributions; 

- `g1_mean`, `g1_sd`, `g2_mean`, `g2_sd` are the means and standard deviations of the standard beta distribution (ranged from 0 to 1); 

- `g1_skewness`, `g1_kurtosis`, `g2_skewness`, and `g2_kurtosis` are the skewness and kurtosis of the two groups. 

In this example, it has contained the empirical skewness `skew1` and `skew2`, which are calculated from raw data. 

```{r betameta_example}
library(detectnorm)
# examine the meta-analysis dataset by simulating extremely non-normal distribution
# population mean1 = 1, mean2 = 1.5, sd1 = sd2=1, skewness1 = 4, kurtosis2 = 2, skewness2=-4, kurtosis2=2
data("beta_mdat")
beta1 <- detectnorm(m1i = m1,sd1i = sd1,n1i = n1, hi1i = hi1,lo1i = lo1,m2i = m2,sd2i = sd2,n2i = n2, hi2i = hi2,lo2i=lo2,distri = "beta", data = beta_mdat)
head(beta1)
#compare the sample skewness and estimated skewness using beta distribution
mean(beta1$skew1)#sample skewness calculated from the sample in group 1
mean(beta1$g1_skewness) #estimated using beta in group 1
mean(beta1$skew2) #sample skewness calculated from the sample in group 2
mean(beta1$g2_skewness)#estimated using beta in group 2
```

```{r trunmeta_example}
library(detectnorm)
data("trun_mdat")
head(trun_mdat)
trun1 <- detectnorm(m1i = m1,sd1i = sd1,n1i = n1, hi1i = 4,lo1i = 0,m2i = m2,sd2i = sd2,n2i = n2, hi2i = 4,lo2i= 0,distri = "truncnorm", data = trun_mdat)
mean(trun1$skew1)#sample skewness calculated from the sample in group 1
mean(trun1$g1_skewness)#estimated using truncnorm in group 1
mean(trun1$skew2)#sample skewness calculated from the sample in group 2
mean(trun1$g2_skewness)#estimated using truncnorm in group 2
```

