Skip to content

Commit

Permalink
fix check issues for forthcoming updates
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 22, 2024
1 parent 0d91cc6 commit a049b99
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
6 changes: 3 additions & 3 deletions R/estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#' @param extend Extend the range of the x axis by a factor of `extend_scale`.
#' @param extend_scale Ratio of range by which to extend the x axis. A value of `0.1`
#' means that the x axis will be extended by `1/10` of the range of the data.
#' @param select Character vector of column names. If NULL (the default), all
#' numeric variables will be selected. Other arguments from [datawizard::find_columns()]
#' (such as `exclude`) can also be used.
#' @param select Character vector of column names. If `NULL` (the default), all
#' numeric variables will be selected. Other arguments from
#' [`datawizard::extract_column_names()`] (such as `exclude`) can also be used.
#' @param by Optional character vector. If not `NULL` and input is a data frame,
#' density estimation is performed for each group (subsets) indicated by `by`.
#' See examples.
Expand Down
44 changes: 22 additions & 22 deletions R/simulate_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @param sd A value or vector corresponding to the SD of the variables.
#' @param names A character vector of desired variable names.
#' @param ... Arguments passed to or from other methods.
#' @examples
#' @examplesIf requireNamespace("MASS", quietly = TRUE)
#'
#' # Correlation --------------------------------
#' data <- simulate_correlation(r = 0.5)
Expand Down Expand Up @@ -70,7 +70,7 @@ simulate_correlation <- function(n = 100,
if (any(r > 1)) {
insight::format_error("`r` should only contain values between -1 and 1.")
} else {
sigma <- r
dispersion <- r
}
} else {
insight::format_error("`r` should be a symetric matrix (relative to the diagonal).")
Expand All @@ -79,38 +79,38 @@ simulate_correlation <- function(n = 100,
if (abs(r) > 1) {
insight::format_error("`r` should only contain values between -1 and 1.")
} else {
sigma <- matrix(c(1, r, r, 1), nrow = 2)
dispersion <- matrix(c(1, r, r, 1), nrow = 2)
}
} else {
insight::format_error("`r` should be a value (e.g., r = 0.5) or a square matrix.")
}


# Get data
data <- MASS::mvrnorm(
out <- MASS::mvrnorm(
n = n,
mu = rep_len(0, ncol(sigma)), # Means of variables
Sigma = sigma,
mu = rep_len(0, ncol(dispersion)), # Means of variables
Sigma = dispersion,
empirical = TRUE
)

# Adjust scale
if (any(sd != 1)) {
data <- t(t(data) * rep_len(sd, ncol(sigma)))
out <- t(t(out) * rep_len(sd, ncol(dispersion)))
}

# Adjust mean
if (any(mean != 0)) {
data <- t(t(data) + rep_len(mean, ncol(sigma)))
out <- t(t(out) + rep_len(mean, ncol(dispersion)))
}

data <- as.data.frame(data)
out <- as.data.frame(out)

# Rename
if (!is.null(names) && length(names) == ncol(data)) {
names(data) <- names
if (!is.null(names) && length(names) == ncol(out)) {
names(out) <- names
}
data
out
}


Expand All @@ -123,13 +123,13 @@ simulate_ttest <- function(n = 100, d = 0.5, names = NULL, ...) {
pr <- 1 / (1 + exp(-z)) # Pass it through an inverse logit function
y <- distribution_binomial(n, 1, pr, random = 3) # Bernoulli response variable

data <- data.frame(y = as.factor(y), x = x)
names(data) <- paste0("V", 0:(ncol(data) - 1))
out <- data.frame(y = as.factor(y), x = x)
names(out) <- paste0("V", 0:(ncol(out) - 1))

if (!is.null(names) && length(names) == ncol(data)) {
names(data) <- names
if (!is.null(names) && length(names) == ncol(out)) {
names(out) <- names
}
data
out
}


Expand All @@ -139,16 +139,16 @@ simulate_difference <- function(n = 100, d = 0.5, names = NULL, ...) {
x <- distribution_normal(round(n / 2), -d / 2, 1)
y <- distribution_normal(round(n / 2), d / 2, 1)

data <- data.frame(
out <- data.frame(
y = as.factor(rep(c(0, 1), each = round(n / 2))),
x = c(x, y)
)
names(data) <- paste0("V", 0:(ncol(data) - 1))
names(out) <- paste0("V", 0:(ncol(out) - 1))

if (!is.null(names) && length(names) == ncol(data)) {
names(data) <- names
if (!is.null(names) && length(names) == ncol(out)) {
names(out) <- names
}
data
out
}


Expand Down
6 changes: 3 additions & 3 deletions man/estimate_density.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/simulate_correlation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/testthat/test-simulate_data.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
skip_if_not_installed("MASS")

test_that("simulate_correlation", {
set.seed(333)
data <- simulate_correlation(r = 0.5, n = 50)
Expand Down

0 comments on commit a049b99

Please sign in to comment.