diff --git a/R/estimate_density.R b/R/estimate_density.R index 46fd1188a..5b105fb22 100644 --- a/R/estimate_density.R +++ b/R/estimate_density.R @@ -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. diff --git a/R/simulate_data.R b/R/simulate_data.R index ba14aaf12..0c566fcd3 100644 --- a/R/simulate_data.R +++ b/R/simulate_data.R @@ -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) @@ -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).") @@ -79,7 +79,7 @@ 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.") @@ -87,30 +87,30 @@ simulate_correlation <- function(n = 100, # 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 } @@ -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 } @@ -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 } diff --git a/man/estimate_density.Rd b/man/estimate_density.Rd index 98073e2d1..ea5ee5c61 100644 --- a/man/estimate_density.Rd +++ b/man/estimate_density.Rd @@ -46,9 +46,9 @@ changed for \code{"SJ"}, which is recommended.} \item{ci}{The confidence interval threshold. Only used when \code{method = "kernel"}. This feature is experimental, use with caution.} -\item{select}{Character vector of column names. If NULL (the default), all -numeric variables will be selected. Other arguments from \code{\link[datawizard:extract_column_names]{datawizard::find_columns()}} -(such as \code{exclude}) can also be used.} +\item{select}{Character vector of column names. If \code{NULL} (the default), all +numeric variables will be selected. Other arguments from +\code{\link[datawizard:extract_column_names]{datawizard::extract_column_names()}} (such as \code{exclude}) can also be used.} \item{by}{Optional character vector. If not \code{NULL} and input is a data frame, density estimation is performed for each group (subsets) indicated by \code{by}. diff --git a/man/simulate_correlation.Rd b/man/simulate_correlation.Rd index 6344cd120..8a4ad9b8b 100644 --- a/man/simulate_correlation.Rd +++ b/man/simulate_correlation.Rd @@ -33,6 +33,7 @@ the groups.} Simulate data with specific characteristics. } \examples{ +\dontshow{if (requireNamespace("MASS", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # Correlation -------------------------------- data <- simulate_correlation(r = 0.5) @@ -76,4 +77,5 @@ round(c(mean(data$V1), sd(data$V1)), 1) diff(t.test(data$V1 ~ data$V0)$estimate) summary(lm(V1 ~ V0, data = data)) summary(glm(V0 ~ V1, data = data, family = "binomial")) +\dontshow{\}) # examplesIf} } diff --git a/tests/testthat/test-simulate_data.R b/tests/testthat/test-simulate_data.R index f285e6697..b2c923ef2 100644 --- a/tests/testthat/test-simulate_data.R +++ b/tests/testthat/test-simulate_data.R @@ -1,3 +1,5 @@ +skip_if_not_installed("MASS") + test_that("simulate_correlation", { set.seed(333) data <- simulate_correlation(r = 0.5, n = 50)