#----------------------------------------------------------------------------
# sim_log_lognormal() examples
#----------------------------------------------------------------------------
library(depower)
# Independent two-sample data returned in a data frame
sim_log_lognormal(
n1 = 10,
n2 = 10,
ratio = 1.3,
cv1 = 0.35,
cv2 = 0.35,
cor = 0,
nsims = 1,
return_type = "data.frame"
)
# Independent two-sample data returned in a list
sim_log_lognormal(
n1 = 10,
n2 = 10,
ratio = 1.3,
cv1 = 0.35,
cv2 = 0.35,
cor = 0,
nsims = 1,
return_type = "list"
)
# Dependent two-sample data returned in a data frame
sim_log_lognormal(
n1 = 10,
n2 = 10,
ratio = 1.3,
cv1 = 0.35,
cv2 = 0.35,
cor = 0.4,
nsims = 1,
return_type = "data.frame"
)
# Dependent two-sample data returned in a list
sim_log_lognormal(
n1 = 10,
n2 = 10,
ratio = 1.3,
cv1 = 0.35,
cv2 = 0.35,
cor = 0.4,
nsims = 1,
return_type = "list"
)
# One-sample data returned in a data frame
sim_log_lognormal(
n1 = 10,
ratio = 1.3,
cv1 = 0.35,
nsims = 1,
return_type = "data.frame"
)
# One-sample data returned in a list
sim_log_lognormal(
n1 = 10,
ratio = 1.3,
cv1 = 0.35,
nsims = 1,
return_type = "list"
)
# Independent two-sample data: two simulations for four parameter combinations.
# Returned as a list-column of lists within a data frame
sim_log_lognormal(
n1 = c(10, 20),
n2 = c(10, 20),
ratio = 1.3,
cv1 = 0.35,
cv2 = 0.35,
cor = 0,
nsims = 2,
return_type = "list"
)
# Dependent two-sample data: two simulations for two parameter combinations.
# Returned as a list-column of lists within a data frame
sim_log_lognormal(
n1 = c(10, 20),
n2 = c(10, 20),
ratio = 1.3,
cv1 = 0.35,
cv2 = 0.35,
cor = 0.4,
nsims = 2,
return_type = "list"
)
# One-sample data: two simulations for two parameter combinations
# Returned as a list-column of lists within a data frame
sim_log_lognormal(
n1 = c(10, 20),
ratio = 1.3,
cv1 = 0.35,
nsims = 2,
return_type = "list"
)
#----------------------------------------------------------------------------
# Visualization of independent two-sample data from a log-transformed
# lognormal distribution with varying coefficient of variation.
#----------------------------------------------------------------------------
cv <- expand.grid(c(0.1, 0.5, 1), c(0.1, 0.5, 1))
set.seed(1234)
data <- mapply(
FUN = function(cv1, cv2) {
d <- sim_log_lognormal(
n1 = 10000,
n2 = 10000,
ratio = 1.5,
cv1 = cv1,
cv2 = cv2,
cor = 0,
nsims = 1,
return_type = "data.frame"
)
cbind(cv1 = cv1, cv2 = cv2, d)
},
cv1 = cv[[1]],
cv2 = cv[[2]],
SIMPLIFY = FALSE
)
data <- do.call(what = "rbind", args = data)
ggplot2::ggplot(
data = data,
mapping = ggplot2::aes(x = value, fill = condition)
) +
ggplot2::facet_grid(
rows = ggplot2::vars(.data$cv2),
cols = ggplot2::vars(.data$cv1),
labeller = ggplot2::labeller(
.rows = ggplot2::label_both,
.cols = ggplot2::label_both
)
) +
ggplot2::geom_density(alpha = 0.3) +
ggplot2::coord_cartesian(xlim = c(-3, 3)) +
ggplot2::labs(
x = "Value",
y = "Density",
fill = "Condition",
caption = "cor=0 and ratio=1.5"
)
#----------------------------------------------------------------------------
# Visualization of dependent two-sample data from a log-transformed lognormal
# distribution with varying correlation.
# The first figure shows the marginal distribution for each group.
# The second figure shows the joint distribution for each group.
# The third figure shows the distribution of differences.
#----------------------------------------------------------------------------
set.seed(1234)
data <- lapply(
X = c(-0.7, -0.4, 0, 0.4, 0.7),
FUN = function(x) {
d <- sim_log_lognormal(
n1 = 1000,
n2 = 1000,
cv1 = 0.5,
cv2 = 0.5,
ratio = 1.5,
cor = x,
nsims = 1,
return_type = "data.frame"
)
cor <- cor(
x = d[d$condition == 1, ]$value,
y = d[d$condition == 2, ]$value
)
cbind(cor = x, r = cor, d)
}
)
data <- do.call(what = "rbind", args = data)
# Density plot of marginal distributions
ggplot2::ggplot(
data = data,
mapping = ggplot2::aes(x = value, fill = condition)
) +
ggplot2::facet_wrap(
facets = ggplot2::vars(.data$cor),
ncol = 2,
labeller = ggplot2::labeller(.rows = ggplot2::label_both)
) +
ggplot2::geom_density(alpha = 0.3) +
ggplot2::coord_cartesian(xlim = c(-3, 3)) +
ggplot2::geom_text(
mapping = ggplot2::aes(
x = -2,
y = 0.8,
label = paste0("r = ", round(r, 2))
),
check_overlap = TRUE
) +
ggplot2::labs(
x = "Value",
y = "Density",
fill = "Condition",
caption = "cv1=0.5, cv2=0.5, ratio=1.5\nr=log-scale Pearson correlation"
)
# Reshape to wide format for scatterplot
data_wide <- data.frame(
cor = data[data$condition == "1", ]$cor,
r = data[data$condition == "1", ]$r,
value1 = data[data$condition == "1", ]$value,
value2 = data[data$condition == "2", ]$value
)
# Scatterplot of joint distribution
ggplot2::ggplot(
data = data_wide,
mapping = ggplot2::aes(x = value1, y = value2)
) +
ggplot2::facet_wrap(
facets = ggplot2::vars(.data$cor),
ncol = 2,
labeller = ggplot2::labeller(.rows = ggplot2::label_both)
) +
ggplot2::geom_point(alpha = 0.3) +
ggplot2::geom_smooth(
method = "lm",
se = FALSE,
color = "forestgreen"
) +
ggplot2::geom_text(
data = unique(data_wide[c("cor", "r")]),
mapping = ggplot2::aes(
x = -2.5,
y = 2.5,
label = paste0("r = ", round(r, 2))
),
hjust = 0
) +
ggplot2::coord_cartesian(xlim = c(-3, 3), ylim = c(-3, 3)) +
ggplot2::labs(
x = "Condition 1",
y = "Condition 2",
caption = paste0(
"cv1=0.5, cv2=0.5, ratio=1.5",
"\nr=log-scale Pearson correlation",
"\nSolid green line: linear regression"
)
)
# Density plot of differences
# Paired differences have decreasing variance as correlation increases.
data_wide$difference <- data_wide$value2 - data_wide$value1
ggplot2::ggplot(
data = data_wide,
mapping = ggplot2::aes(x = difference)
) +
ggplot2::facet_wrap(
facets = ggplot2::vars(.data$cor),
ncol = 2,
labeller = ggplot2::labeller(.rows = ggplot2::label_both)
) +
ggplot2::geom_density(alpha = 0.3, fill = "#F8766D") +
ggplot2::coord_cartesian(xlim = c(-3, 3)) +
ggplot2::labs(
x = "Difference (Condition 2 - Condition 1)",
y = "Density",
caption = "cv1=0.5, cv2=0.5, ratio=1.5"
)
Run the code above in your browser using DataLab