sim_panel: Simulate a panel of one or two categorical variables and a...

View source: R/sim_panel.R

sim_panelR Documentation

Simulate a panel of one or two categorical variables and a response variable corresponding to a specified distribution or design

Description

Simulate a panel of one or two categorical variables and a response variable corresponding to a specified distribution or design

Usage

sim_panel(nx = 2, nfacet = 3, ntimes = 500, sim_dist = sim_varall)

Arguments

nx

number of x categories

nfacet

number of facet categories

ntimes

number of observations to be simulated for each categories

sim_dist

type of distribution to be simulated

Value

the simulated data for the data structure considered

Author(s)

Sayani07

Examples

library(dplyr)
library(distributional)
library(tidyr)
library(ggplot2)
sim_varx_normal <- function(nx, nfacet, mean, sd, w) {
  rep(dist_normal((mean + seq(0, nx - 1, by = 1) * w), sd), nfacet)
}
sim_varx_normal(2, 3, 0, 1, 1)
sim_varx_normal(2, 3, 0, 1, -2)

sim_panel_data <- sim_panel(
  nx = 2, nfacet = 3,
  ntimes = 50,
  sim_dist = sim_varx_normal(2, 3, 0, 1, 10)
) %>% unnest(data)

sim_panel_data %>%
  ggplot() +
  geom_boxplot(aes(x = as.factor(id_x), y = sim_data)) +
  facet_wrap(~id_facet)
compute_quantiles(sim_panel_data) %>%
  unnest(c(sim_data_quantile)) %>%
  ggplot() +
  geom_boxplot(aes(x = as.factor(id_x), y = sim_data_quantile)) +
  facet_wrap(~id_facet)


sim_varf_normal <- function(nx, nfacet, mean, sd, w) {
  rep(dist_normal((mean + seq(0, nfacet - 1, by = 1) * w), sd), each = nx)
}
sim_varf_normal(2, 3, 0, 1, 1)
sim_varf_normal(2, 3, 0, 1, 2)
sim_panel_data <- sim_panel(
  nx = 2, nfacet = 3,
  ntimes = 50,
  sim_dist = sim_varf_normal(2, 3, 0, 1, 10)
) %>% unnest(data)

sim_panel_data %>%
  ggplot() +
  geom_boxplot(aes(x = as.factor(id_x), y = sim_data)) +
  facet_wrap(~id_facet)
compute_quantiles(sim_panel_data) %>%
  unnest(c(sim_data_quantile)) %>%
  ggplot() +
  geom_boxplot(aes(x = as.factor(id_x), y = sim_data_quantile)) +
  facet_wrap(~id_facet)



sim_varall_normal <- function(nx, nfacet, mean, sd, w) {
  dist_normal((mean + seq(0,
    (nx *
      nfacet - 1),
    by = 1
  ) * w), sd)
}
sim_varall_normal(2, 3, 0, 1, 1)
sim_varall_normal(2, 3, 0, 1, 2)
sim_panel_data <- sim_panel(
  nx = 2, nfacet = 3,
  ntimes = 5,
  sim_dist = sim_varall_normal(2, 3, 0, 1, 10)
) %>% unnest(data)

sim_panel_data %>%
  ggplot() +
  geom_boxplot(aes(x = as.factor(id_x), y = sim_data)) +
  facet_wrap(~id_facet)
compute_quantiles(sim_panel_data) %>%
  unnest(c(sim_data_quantile)) %>%
  ggplot() +
  geom_boxplot(aes(x = as.factor(id_x), y = sim_data_quantile)) +
  facet_wrap(~id_facet)

compute_pairwise_max(sim_panel_data, "id_x", "id_facet",
  response = sim_data
)

Sayani07/gravitas documentation built on June 18, 2022, 2:40 a.m.