---
title: "Simulate from Existing Data"
author: "Lisa DeBruine"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Simulate from Existing Data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  out.width = "100%"
)
ggplot2::theme_set(ggplot2::theme_bw())
set.seed(8675309)
```

```{r, warning=FALSE, message=FALSE}
library(ggplot2)
library(dplyr)
library(tidyr)
library(faux)
```

The `sim_df()` function produces a data table with the same distributions and correlations as an existing data table. It simulates all numeric variables from a continuous normal distribution (for now).

For example, here is the relationship between speed and distance in the built-in dataset `cars`. 

```{r plot-cars-orig, fig.cap="Original cars dataset"}
cars %>%
  ggplot(aes(speed, dist)) + 
  geom_point() +
  geom_smooth(method = "lm", formula = "y~x")
```

You can create a new sample with the same parameters and 500 rows with the code `sim_df(cars, 500)`.

```{r plot-cars-sim, fig.cap="Simulated cars dataset"}
sim_df(cars, 500) %>%
  ggplot(aes(speed, dist)) + 
    geom_point() +
    geom_smooth(method = "lm", formula = "y~x")
```

## Between-subject variables

You can also optionally add between-subject variables. For example, here is the relationship between horsepower (`hp`) and weight (`wt`) for automatic (`am = 0`) versus manual (`am = 1`) transmission in the built-in dataset `mtcars`.

```{r plot-mtcars-orig, fig.cap="Original mtcars dataset"}
mtcars %>%
  mutate(transmission = factor(am, labels = c("automatic", "manual"))) %>%
  ggplot(aes(hp, wt, color = transmission)) +
  geom_point() +
  geom_smooth(method = "lm", formula = "y~x")
```

And here is a new sample with 50 observations of each.

```{r plot-iris-sim, fig.cap="Simulated iris dataset"}
sim_df(mtcars, 50 , between = "am") %>%
  mutate(transmission = factor(am, labels = c("automatic", "manual"))) %>%
  ggplot(aes(hp, wt, color = transmission)) +
  geom_point() +
  geom_smooth(method = "lm", formula = "y~x")
```

## Empirical

Set `empirical = TRUE` to return a data frame with *exactly* the same means, SDs, and correlations as the original dataset.

```{r}
exact_mtcars <- sim_df(mtcars, 50, between = "am", empirical = TRUE)
```


## Rounding

For now, the function only creates new variables sampled from a continuous normal distribution. I hope to add in other sampling distributions in the future. So you'd need to do any rounding or truncating yourself.

```{r plot-iris-sim-round, fig.cap="Simulated iris dataset (rounded)"}
sim_df(mtcars, 50, between = "am") %>%
  mutate(hp = round(hp),
         transmission = factor(am, labels = c("automatic", "manual"))) %>%
  ggplot(aes(hp, wt, color = transmission)) +
  geom_point() +
  geom_smooth(method = "lm", formula = "y~x")
```

## Missing data

As of faux 0.0.1.8, if you want to simulate missing data, set `missing = TRUE` and `sim_df` will simulate missing data with the same joint probabilities as your data. In the dataset below, in condition B1 30% of A1 values are missing and 60% of A2 values are missing. This is correlated so that there is a 100% chance that A2 is missing if A1 is. There is no missing data for condition B2.

```{r}
data <- sim_design(2, 2, n = 10, plot = FALSE)
data$A1[1:3] <- NA
data$A2[1:6] <- NA
data
```

The simulated data will have the same pattern of missingness (sampled from the joint distribution, so it won't be exact).

```{r}
simdat <- sim_df(data, between = "B", n = 1000, 
                 missing = TRUE)
```

```{r, echo = FALSE, results = 'asis'}
simdat %>%
  mutate(A1 = ifelse(is.na(A1), "NA", "not NA"),
         A2 = ifelse(is.na(A2), "NA", "not NA")) %>%
  count(B, A1, A2) %>%
  group_by(B) %>%
  mutate(n = round(n/sum(n), 2)) %>%
  knitr::kable()
```


