diff --git a/DESCRIPTION b/DESCRIPTION index a37258d..1b592f7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: SimCorMultRes Type: Package Title: Simulates Correlated Multinomial Responses Description: Simulates correlated multinomial responses conditional on a marginal model specification. -Version: 1.6.8 +Version: 1.6.9 Depends: R(>= 2.15.0) Imports: evd, diff --git a/R/SimCorMultRes_internals.R b/R/SimCorMultRes_internals.R index 8beb82d..12438f3 100644 --- a/R/SimCorMultRes_internals.R +++ b/R/SimCorMultRes_internals.R @@ -1,288 +1,358 @@ check_cluster_size <- function(cluster_size) { - if (all.equal(cluster_size, as.integer(cluster_size)) != TRUE | - cluster_size < 2) - stop("'clsize' must be a positive integer greater than or equal to two") + if (all.equal(cluster_size, as.integer(cluster_size)) != TRUE | + cluster_size < 2) { + stop("'clsize' must be a positive integer greater than or equal to two") + } } check_ncategories <- function(categories_no) { - if (!is.numeric(categories_no) | categories_no < 3) - stop("'ncategories' must be numeric greater or equal to three") - if (all.equal(categories_no, as.integer(categories_no)) != TRUE) - stop("'ncategories' must be a positive integer") - categories_no + if (!is.numeric(categories_no) | categories_no < 3) { + stop("'ncategories' must be numeric greater or equal to three") + } + if (all.equal(categories_no, as.integer(categories_no)) != TRUE) { + stop("'ncategories' must be a positive integer") + } + categories_no } check_correlation_matrix <- function(correlation_matrix, cluster_size, rfctn, - categories_no = NULL) { - if (!is.numeric(correlation_matrix)) - stop("'cor_matrix' must be numeric") - if (!is.matrix(correlation_matrix)) - stop("'cor_matrix' must be a matrix") - if (rfctn == "rbin" | rfctn == "rmult.clm") { - if (ncol(correlation_matrix) != cluster_size | - nrow(correlation_matrix) != cluster_size) - stop("'cor.matrix' must be a ", cluster_size, "x", - cluster_size, " matrix") - } else { - ncateg2 <- ifelse(rfctn == "rmult.bcl", categories_no, - categories_no - 1) - dimcor <- cluster_size * ncateg2 - if (ncol(correlation_matrix) != dimcor | - nrow(correlation_matrix) != dimcor) - stop("'cor_matrix' must be a ", dimcor, "x", dimcor, " matrix") - for (i in 1:cluster_size) { - diag_index <- 1:ncateg2 + (i - 1) * ncateg2 - correlation_matrix[diag_index, diag_index] <- diag(1, ncateg2) - } + categories_no = NULL) { + if (!is.numeric(correlation_matrix)) { + stop("'cor_matrix' must be numeric") + } + if (!is.matrix(correlation_matrix)) { + stop("'cor_matrix' must be a matrix") + } + if (rfctn == "rbin" | rfctn == "rmult.clm") { + if (ncol(correlation_matrix) != cluster_size | + nrow(correlation_matrix) != cluster_size) { + stop( + "'cor.matrix' must be a ", cluster_size, "x", cluster_size, " matrix" + ) } - if (!isSymmetric(correlation_matrix)) - stop("'cor_matrix' must be a symmetric matrix") - if (any(diag(correlation_matrix) != 1)) - stop("the diagonal elements of 'cor_matrix' must be one") - if (any(correlation_matrix > 1) | any(correlation_matrix < -1)) - stop("all the elements of 'cor_matrix' must be on [-1,1]") - correlation_matrix_eigen <- eigen(correlation_matrix, symmetric = TRUE, - only.values = TRUE) - if (any(correlation_matrix_eigen$values <= 0)) - stop("'cor_matrix' must be positive definite") - correlation_matrix + } else { + ncateg2 <- ifelse( + rfctn == "rmult.bcl", categories_no, categories_no - 1 + ) + dimcor <- cluster_size * ncateg2 + if (ncol(correlation_matrix) != dimcor | + nrow(correlation_matrix) != dimcor) { + stop("'cor_matrix' must be a ", dimcor, "x", dimcor, " matrix") + } + for (i in 1:cluster_size) { + diag_index <- 1:ncateg2 + (i - 1) * ncateg2 + correlation_matrix[diag_index, diag_index] <- diag(1, ncateg2) + } + } + if (!isSymmetric(correlation_matrix)) { + stop("'cor_matrix' must be a symmetric matrix") + } + if (any(diag(correlation_matrix) != 1)) { + stop("the diagonal elements of 'cor_matrix' must be one") + } + if (any(correlation_matrix > 1) | any(correlation_matrix < -1)) { + stop("all the elements of 'cor_matrix' must be on [-1,1]") + } + correlation_matrix_eigen <- eigen( + correlation_matrix, + symmetric = TRUE, only.values = TRUE + ) + if (any(correlation_matrix_eigen$values <= 0)) { + stop("'cor_matrix' must be positive definite") + } + correlation_matrix } check_xformula <- function(xformula) { - linear_predictor_formula <- as.formula(xformula) - if (length(paste0(attr(terms(linear_predictor_formula), "variables"))) == 1) - stop("No covariates were found in 'formula' ") - if (attr(terms(linear_predictor_formula), "intercept") == 0) { - linear_predictor_formula <- update(linear_predictor_formula, ~. + 1) - } - linear_predictor_formula + linear_predictor_formula <- as.formula(xformula) + if (length(paste0(attr(terms(linear_predictor_formula), "variables"))) == 1) { + stop("No covariates were found in 'formula' ") + } + if (attr(terms(linear_predictor_formula), "intercept") == 0) { + linear_predictor_formula <- update(linear_predictor_formula, ~ . + 1) + } + linear_predictor_formula } check_intercepts <- function(intercepts, cluster_size, rfctn, sample_size = NULL) { - if (!is.numeric(intercepts)) - stop("'intercepts' must be numeric") - if (any(is.infinite(intercepts))) - stop("'intercepts' must not be Inf or -Inf") - if (rfctn == "rbin") { - if (!(is.vector(intercepts) & !is.list(intercepts))) - stop("'intercepts' must be a vector") - if (length(intercepts) == 1) - intercepts <- rep(intercepts, cluster_size) - if (length(intercepts) != cluster_size) - stop("'intercepts' must have either one or ", cluster_size, - " elements") + if (!is.numeric(intercepts)) { + stop("'intercepts' must be numeric") + } + if (any(is.infinite(intercepts))) { + stop("'intercepts' must not be Inf or -Inf") + } + if (rfctn == "rbin") { + if (!(is.vector(intercepts) & !is.list(intercepts))) { + stop("'intercepts' must be a vector") + } + if (length(intercepts) == 1) { + intercepts <- rep(intercepts, cluster_size) + } + if (length(intercepts) != cluster_size) { + stop( + "'intercepts' must have either one or ", cluster_size, + " elements" + ) + } + intercepts <- cbind(-Inf, intercepts, Inf) + } else { + if (!(is.vector(intercepts) & !is.list(intercepts)) & + !is.matrix(intercepts)) { + stop("'intercepts' must be a vector or a matrix") + } + if (is.vector(intercepts) & !is.list(intercepts)) { + if (length(intercepts) == 1) { + stop("'intercepts' must have at least 2 elements") + } + if (rfctn == "rmult.clm" & any(diff(intercepts) <= 0)) { + stop("'intercepts' must be increasing") + } + categories_no <- length(intercepts) + 1 + if (rfctn == "rmult.clm") { + intercepts <- matrix(intercepts, cluster_size, categories_no - 1, TRUE) intercepts <- cbind(-Inf, intercepts, Inf) + } else if (rfctn == "rmult.crm") { + intercepts <- matrix( + intercepts, sample_size, cluster_size * (categories_no - 1), TRUE + ) + } else { + intercepts <- matrix(intercepts, cluster_size, categories_no - 1, TRUE) + } } else { - if (!(is.vector(intercepts) & !is.list(intercepts)) & - !is.matrix(intercepts)) - stop("'intercepts' must be a vector or a matrix") - if (is.vector(intercepts) & !is.list(intercepts)) { - if (length(intercepts) == 1) - stop("'intercepts' must have at least 2 elements") - if (rfctn == "rmult.clm" & any(diff(intercepts) <= 0)) - stop("'intercepts' must be increasing") - categories_no <- length(intercepts) + 1 - if (rfctn == "rmult.clm") { - intercepts <- matrix(intercepts, cluster_size, - categories_no - 1, TRUE) - intercepts <- cbind(-Inf, intercepts, Inf) - } else if (rfctn == "rmult.crm") { - intercepts <- matrix(intercepts, sample_size, - cluster_size * (categories_no - 1), TRUE) - } else { - intercepts <- matrix(intercepts, cluster_size, - categories_no - 1, byrow = TRUE) - } - } else { - categories_no <- ncol(intercepts) + 1 - if (rfctn == "rmult.clm") { - intercepts <- cbind(-Inf, intercepts, Inf) - for (i in 1:cluster_size) { - if (any(diff(intercepts[i, ]) <= 0)) - stop("'intercepts' must be increasing at each row") - } - } else if (rfctn == "rmult.crm") { - categories_no <- ncol(intercepts) + 1 - intercepts <- matrix(t(intercepts), sample_size, - cluster_size * (categories_no - 1), TRUE) - } else { - categories_no <- ncol(intercepts) + 1 - intercepts <- matrix(intercepts, cluster_size, - categories_no - 1, TRUE) - } + categories_no <- ncol(intercepts) + 1 + if (rfctn == "rmult.clm") { + intercepts <- cbind(-Inf, intercepts, Inf) + for (i in 1:cluster_size) { + if (any(diff(intercepts[i, ]) <= 0)) { + stop("'intercepts' must be increasing at each row") + } } + } else if (rfctn == "rmult.crm") { + categories_no <- ncol(intercepts) + 1 + intercepts <- matrix( + t(intercepts), sample_size, cluster_size * (categories_no - 1), TRUE + ) + } else { + categories_no <- ncol(intercepts) + 1 + intercepts <- matrix(intercepts, cluster_size, categories_no - 1, TRUE) + } } - intercepts + } + intercepts } check_betas <- function(betas, cluster_size) { - if (!(is.vector(betas) & !is.list(betas)) & !is.matrix(betas)) - stop("'betas' must be a vector or a matrix") - if (!is.numeric(betas)) - stop("'betas' must be numeric") - if (is.vector(betas) & !is.list(betas)) { - betas <- rep(betas, cluster_size) - } else { - if (nrow(betas) != cluster_size) - stop("The number of rows in 'betas' should be equal to 'clsize'") - betas <- c(t(betas)) + if (!(is.vector(betas) & !is.list(betas)) & !is.matrix(betas)) { + stop("'betas' must be a vector or a matrix") + } + if (!is.numeric(betas)) { + stop("'betas' must be numeric") + } + if (is.vector(betas) & !is.list(betas)) { + betas <- rep(betas, cluster_size) + } else { + if (nrow(betas) != cluster_size) { + stop("The number of rows in 'betas' should be equal to 'clsize'") } - betas + betas <- c(t(betas)) + } + betas } create_linear_predictor <- function(betas, cluster_size, linear_predictor_formula, xdata, rfctn, categories_no = NULL) { - xmat <- model.matrix(linear_predictor_formula, data = xdata) - if (rfctn == "rmult.bcl") { - xmat <- apply(xmat, 2, function(x) rep(x, each = categories_no)) - if (length(betas) != (cluster_size * categories_no * ncol(xmat))) - stop("The length of 'betas' does not match with the provided covariates") # nolint - } else { - xmat <- matrix(xmat[, -1], ncol = ncol(xmat) - 1) - if (length(betas) != (cluster_size) * ncol(xmat)) - stop("The length of 'betas' does not match with the number of covariates") # nolint - } - linear_predictor <- matrix(betas, nrow = nrow(xmat), ncol = ncol(xmat), - byrow = TRUE) * xmat - if (rfctn == "rmult.bcl") { - linear_predictor <- matrix(rowSums(linear_predictor), - ncol = categories_no * cluster_size, - byrow = TRUE) - } else { - linear_predictor <- matrix(rowSums(linear_predictor), - ncol = cluster_size, byrow = TRUE) - } - as.matrix(linear_predictor) + xmat <- model.matrix(linear_predictor_formula, data = xdata) + if (rfctn == "rmult.bcl") { + xmat <- apply(xmat, 2, function(x) rep(x, each = categories_no)) + if (length(betas) != (cluster_size * categories_no * ncol(xmat))) { + stop("The length of 'betas' does not match with the provided covariates") + } # nolint + } else { + xmat <- matrix(xmat[, -1], ncol = ncol(xmat) - 1) + if (length(betas) != (cluster_size) * ncol(xmat)) { + stop("The length of 'betas' does not match with the number of covariates") + } # nolint + } + linear_predictor <- matrix(betas, + nrow = nrow(xmat), ncol = ncol(xmat), + byrow = TRUE + ) * xmat + if (rfctn == "rmult.bcl") { + linear_predictor <- matrix( + rowSums(linear_predictor), + ncol = categories_no * cluster_size, + byrow = TRUE + ) + } else { + linear_predictor <- matrix( + rowSums(linear_predictor), + ncol = cluster_size, byrow = TRUE + ) + } + as.matrix(linear_predictor) } create_distribution <- function(link) { - if (length(link) != 1) - stop("The length of 'link' must be one") - links <- c("probit", "logit", "cloglog", "cauchit") - if (!is.element(link, links)) - stop("'link' must be 'probit','logit','cloglog' and/or 'cauchit'") - distr <- switch(link, probit = "qnorm", logit = "qlogis", - cloglog = "qgumbel", cauchit = "qcauchy") - distr + if (length(link) != 1) { + stop("The length of 'link' must be one") + } + links <- c("probit", "logit", "cloglog", "cauchit") + if (!is.element(link, links)) { + stop("'link' must be 'probit','logit','cloglog' and/or 'cauchit'") + } + distr <- switch( + link, probit = "qnorm", logit = "qlogis", cloglog = "qgumbel", + cauchit = "qcauchy" + ) + distr } create_rlatent <- function(simulated_latent_variables, sample_size, link, cluster_size, correlation_matrix, rfctn, categories_no = NULL) { - if (is.null(simulated_latent_variables)) { - distr <- create_distribution(link) - correlation_matrix <- check_correlation_matrix(correlation_matrix, - cluster_size, rfctn, - categories_no) - simulated_latent_variables <- rnorta(sample_size, correlation_matrix, - rep(distr, - nrow(correlation_matrix))) - if (distr == "qgumbel" & rfctn != "rmult.bcl") - simulated_latent_variables <- -simulated_latent_variables + if (is.null(simulated_latent_variables)) { + distr <- create_distribution(link) + correlation_matrix <- check_correlation_matrix( + correlation_matrix, cluster_size, rfctn, categories_no + ) + simulated_latent_variables <- rnorta( + sample_size, correlation_matrix, rep(distr, nrow(correlation_matrix)) + ) + if (distr == "qgumbel" & rfctn != "rmult.bcl") { + simulated_latent_variables <- -simulated_latent_variables + } + } else { + if (!is.matrix(simulated_latent_variables)) { + stop("'rlatent' must be a matrix") + } + if (!is.numeric(simulated_latent_variables)) { + stop("'rlatent' must be numeric") + } + if (rfctn == "rbin" | rfctn == "rmult.clm") { + ncol_rlatent <- cluster_size + } else if (rfctn == "rmult.bcl") { + ncol_rlatent <- cluster_size * categories_no } else { - if (!is.matrix(simulated_latent_variables)) - stop("'rlatent' must be a matrix") - if (!is.numeric(simulated_latent_variables)) - stop("'rlatent' must be numeric") - if (rfctn == "rbin" | rfctn == "rmult.clm") { - ncol_rlatent <- cluster_size - } else if (rfctn == "rmult.bcl") { - ncol_rlatent <- cluster_size * categories_no - } else { - ncol_rlatent <- cluster_size * (categories_no - 1) - } - if (nrow(simulated_latent_variables) != sample_size | - ncol(simulated_latent_variables) != ncol_rlatent) - stop("'rlatent' must be a ", sample_size, "x", ncol_rlatent, " matrix") # nolint - correlation_matrix <- NULL - simulated_latent_variables <- simulated_latent_variables + ncol_rlatent <- cluster_size * (categories_no - 1) } - simulated_latent_variables + if (nrow(simulated_latent_variables) != sample_size | + ncol(simulated_latent_variables) != ncol_rlatent) { + stop("'rlatent' must be a ", sample_size, "x", ncol_rlatent, " matrix") + } # nolint + correlation_matrix <- NULL + simulated_latent_variables <- simulated_latent_variables + } + simulated_latent_variables } create_output <- function(simulated_responses, sample_size, cluster_size, simulated_latent_variables, linear_predictor_formula, xdata, rfctn, categories_no = NULL) { - y <- c(t(simulated_responses)) - id <- rep(1:sample_size, each = cluster_size) - time <- rep(1:cluster_size, sample_size) - rownames(simulated_responses) <- rownames(simulated_latent_variables) <- - paste("i", 1:sample_size, sep = "=") - colnames(simulated_responses) <- paste("t", 1:cluster_size, sep = "=") - colnames(simulated_latent_variables) <- - if (rfctn == "rbin" | rfctn == "rmult.clm") { - paste("t", 1:cluster_size, sep = "=") - } else if (rfctn == "rmult.bcl") { - paste("t=", rep(1:cluster_size, each = categories_no), " & j=", - rep(1:categories_no, cluster_size), sep = "") - } else { - paste("t=", rep(1:cluster_size, each = categories_no - 1), - " & j=", rep(1:(categories_no - 1), cluster_size), - sep = "") + y <- c(t(simulated_responses)) + id <- rep(1:sample_size, each = cluster_size) + time <- rep(1:cluster_size, sample_size) + rownames(simulated_responses) <- rownames(simulated_latent_variables) <- + paste("i", 1:sample_size, sep = "=") + colnames(simulated_responses) <- paste("t", 1:cluster_size, sep = "=") + colnames(simulated_latent_variables) <- + if (rfctn == "rbin" | rfctn == "rmult.clm") { + paste("t", 1:cluster_size, sep = "=") + } else if (rfctn == "rmult.bcl") { + paste("t=", rep(1:cluster_size, each = categories_no), " & j=", + rep(1:categories_no, cluster_size), + sep = "" + ) + } else { + paste("t=", rep(1:cluster_size, each = categories_no - 1), + " & j=", rep(1:(categories_no - 1), cluster_size), + sep = "" + ) } - sim_model_frame <- model.frame(formula = linear_predictor_formula, - data = xdata) - simdata <- data.frame(y, sim_model_frame, id, time) - list(Ysim = simulated_responses, simdata = simdata, - rlatent = simulated_latent_variables) + sim_model_frame <- model.frame( + formula = linear_predictor_formula, data = xdata + ) + simdata <- data.frame(y, sim_model_frame, id, time) + list( + Ysim = simulated_responses, simdata = simdata, + rlatent = simulated_latent_variables + ) } apply_threshold <- function(linear_predictor, simulated_latent_variables, cluster_size, rfctn, intercepts = NULL, categories_no = NULL) { - sample_size <- nrow(linear_predictor) - if (rfctn == "rmult.clm" | rfctn == "rmult.crm") { - u_sim <- simulated_latent_variables - linear_predictor - } else { - u_sim <- simulated_latent_variables + linear_predictor + sample_size <- nrow(linear_predictor) + if (rfctn == "rmult.clm" | rfctn == "rmult.crm") { + u_sim <- simulated_latent_variables - linear_predictor + } else { + u_sim <- simulated_latent_variables + linear_predictor + } + if (rfctn == "rbin") { + simulated_responses <- matrix(0, sample_size, cluster_size) + for (i in 1:cluster_size) { + simulated_responses[, i] <- + cut( + u_sim[, i] - 2 * linear_predictor[, i], intercepts[i, ], + labels = FALSE + ) } - if (rfctn == "rbin") { - simulated_responses <- matrix(0, sample_size, cluster_size) - for (i in 1:cluster_size) simulated_responses[, i] <- - cut(u_sim[, i] - 2 * linear_predictor[, i], intercepts[i, ], - labels = FALSE) - simulated_responses <- 2 - simulated_responses - } else if (rfctn == "rmult.bcl") { - u_sim <- matrix(as.vector(t(u_sim)), nrow = cluster_size * sample_size, - ncol = categories_no, byrow = TRUE) - simulated_responses <- apply(u_sim, 1, which.max) - simulated_responses <- matrix(simulated_responses, ncol = cluster_size, - byrow = TRUE) - } else if (rfctn == "rmult.clm") { - simulated_responses <- matrix(0, sample_size, cluster_size) - for (i in 1:cluster_size) simulated_responses[, i] <- - cut(u_sim[, i], intercepts[i, ], labels = FALSE) - } else { - simulated_responses <- matrix(as.numeric(t(u_sim <= intercepts)), - sample_size * cluster_size, categories_no - 1, TRUE) - for (i in 1:(categories_no - 1)) - simulated_responses[, i] <- ifelse(simulated_responses[, i] == 1, - i, categories_no) - simulated_responses <- apply(simulated_responses, 1, min) - simulated_responses <- matrix(simulated_responses, sample_size, - cluster_size, TRUE) + simulated_responses <- 2 - simulated_responses + } else if (rfctn == "rmult.bcl") { + u_sim <- matrix( + as.vector(t(u_sim)), cluster_size * sample_size, categories_no, TRUE + ) + simulated_responses <- apply(u_sim, 1, which.max) + simulated_responses <- matrix( + simulated_responses, + ncol = cluster_size, byrow = TRUE + ) + } else if (rfctn == "rmult.clm") { + simulated_responses <- matrix(0, sample_size, cluster_size) + for (i in 1:cluster_size) { + simulated_responses[, i] <- cut( + u_sim[, i], intercepts[i, ], + labels = FALSE + ) } - simulated_responses + } else { + simulated_responses <- matrix( + as.numeric(t(u_sim <= intercepts)), + sample_size * cluster_size, categories_no - 1, TRUE + ) + for (i in 1:(categories_no - 1)) { + simulated_responses[, i] <- ifelse( + simulated_responses[, i] == 1, i, categories_no + ) + } + simulated_responses <- apply(simulated_responses, 1, min) + simulated_responses <- matrix( + simulated_responses, sample_size, cluster_size, TRUE + ) + } + simulated_responses } create_betas_acl2bcl <- function(intercepts = intercepts, categories_no = categories_no, betas = betas) { - intercepts_bcl <- t(apply(intercepts, 1, function(z) rev(cumsum(rev(z))))) - cluster_size <- nrow(intercepts) - dim_betas <- length(betas) / cluster_size - betas_matrix <- matrix(betas, cluster_size, dim_betas, TRUE) - betas_matrix_bcl <- t(apply(betas_matrix, 1, function(z) - rep(categories_no - seq(categories_no - 1), each = length(z)) * z)) - betas_bcl <- matrix(0, cluster_size, - categories_no - 1 + ncol(betas_matrix_bcl)) - for (i in seq(categories_no - 1)) { - betas_bcl[, ((i - 1) * (dim_betas + 1) + 1):(i * (dim_betas + 1))] <- - cbind(intercepts_bcl[, i], - betas_matrix_bcl[, ((i - 1) * (dim_betas) + 1):(i * (dim_betas))]) - } - betas_bcl <- cbind(betas_bcl, matrix(0, cluster_size, dim_betas + 1)) - betas_bcl + intercepts_bcl <- t(apply(intercepts, 1, function(z) rev(cumsum(rev(z))))) + cluster_size <- nrow(intercepts) + dim_betas <- length(betas) / cluster_size + betas_matrix <- matrix(betas, cluster_size, dim_betas, TRUE) + betas_matrix_bcl <- t(apply(betas_matrix, 1, function(z) + rep(categories_no - seq(categories_no - 1), each = length(z)) * z)) + betas_bcl <- matrix( + 0, cluster_size, categories_no - 1 + ncol(betas_matrix_bcl) + ) + for (i in seq(categories_no - 1)) { + betas_bcl[, ((i - 1) * (dim_betas + 1) + 1):(i * (dim_betas + 1))] <- + cbind( + intercepts_bcl[, i], + betas_matrix_bcl[, ((i - 1) * (dim_betas) + 1):(i * (dim_betas))] + ) + } + betas_bcl <- cbind(betas_bcl, matrix(0, cluster_size, dim_betas + 1)) + betas_bcl } diff --git a/R/rbin.R b/R/rbin.R index b24c7ee..9f6506b 100644 --- a/R/rbin.R +++ b/R/rbin.R @@ -102,49 +102,62 @@ #' beta_coefficients <- 0.2 #' latent_correlation_matrix <- toeplitz(c(1, 0.9, 0.9, 0.9)) #' x <- rep(rnorm(sample_size), each = cluster_size) -#' simulated_binary_dataset <- rbin(clsize = cluster_size, -#' intercepts = beta_intercepts, betas = beta_coefficients, -#' xformula = ~ x, cor.matrix = latent_correlation_matrix, link = 'probit') +#' simulated_binary_dataset <- rbin( +#' clsize = cluster_size, +#' intercepts = beta_intercepts, betas = beta_coefficients, +#' xformula = ~x, cor.matrix = latent_correlation_matrix, link = "probit" +#' ) #' library(gee) -#' binary_gee_model <- gee(y ~ x, family = binomial('probit'), id = id, -#' data = simulated_binary_dataset$simdata) +#' binary_gee_model <- gee(y ~ x, +#' family = binomial("probit"), id = id, +#' data = simulated_binary_dataset$simdata +#' ) #' summary(binary_gee_model)$coefficients #' #' ## See Example 3.6 in the Vignette. #' set.seed(8) #' library(evd) -#' simulated_latent_variables1 <- rmvevd(sample_size, dep = sqrt(1 - 0.9), -#' model = 'log', d = cluster_size) -#' simulated_latent_variables2 <- rmvevd(sample_size, dep = sqrt(1 - 0.9), -#' model = 'log', d = cluster_size) +#' simulated_latent_variables1 <- rmvevd(sample_size, +#' dep = sqrt(1 - 0.9), +#' model = "log", d = cluster_size +#' ) +#' simulated_latent_variables2 <- rmvevd(sample_size, +#' dep = sqrt(1 - 0.9), +#' model = "log", d = cluster_size +#' ) #' simulated_latent_variables <- simulated_latent_variables1 - simulated_latent_variables2 # nolintr -#' simulated_binary_dataset <- rbin(clsize = cluster_size, -#' intercepts = beta_intercepts, betas = beta_coefficients, -#' xformula = ~ x, rlatent = simulated_latent_variables) -#' binary_gee_model <- gee(y ~ x, family = binomial('logit'), id = id, -#' data = simulated_binary_dataset$simdata) +#' simulated_binary_dataset <- rbin( +#' clsize = cluster_size, +#' intercepts = beta_intercepts, betas = beta_coefficients, +#' xformula = ~x, rlatent = simulated_latent_variables +#' ) +#' binary_gee_model <- gee(y ~ x, +#' family = binomial("logit"), id = id, +#' data = simulated_binary_dataset$simdata +#' ) #' summary(binary_gee_model)$coefficients -#' #' @export rbin <- function(clsize = clsize, intercepts = intercepts, betas = betas, - xformula = formula(xdata), xdata = parent.frame(), link = "logit", - cor.matrix = cor.matrix, rlatent = NULL){ # nolint - check_cluster_size(clsize) - beta_intercepts <- check_intercepts(intercepts, clsize, "rbin") - beta_coefficients <- check_betas(betas, clsize) - linear_predictor_formula <- check_xformula(xformula) - if (!is.environment(xdata)) xdata <- data.frame(na.omit(xdata)) - linear_predictor <- create_linear_predictor(beta_coefficients, clsize, - linear_predictor_formula, xdata, - "rbin") - sample_size <- nrow(linear_predictor) - simulated_latent_variables <- create_rlatent(rlatent, sample_size, link, - clsize, cor.matrix, "rbin") - simulated_binary_responses <- apply_threshold(linear_predictor, - simulated_latent_variables, - clsize, "rbin", - beta_intercepts) - create_output(simulated_binary_responses, sample_size, clsize, - simulated_latent_variables, linear_predictor_formula, xdata, - "rbin") + xformula = formula(xdata), xdata = parent.frame(), link = "logit", # nolintr + cor.matrix = cor.matrix, rlatent = NULL) { # nolint + check_cluster_size(clsize) + beta_intercepts <- check_intercepts(intercepts, clsize, "rbin") + beta_coefficients <- check_betas(betas, clsize) + linear_predictor_formula <- check_xformula(xformula) + if (!is.environment(xdata)) xdata <- data.frame(na.omit(xdata)) + linear_predictor <- create_linear_predictor( + beta_coefficients, clsize, linear_predictor_formula, xdata, "rbin" + ) + sample_size <- nrow(linear_predictor) + simulated_latent_variables <- create_rlatent( + rlatent, sample_size, link, clsize, cor.matrix, "rbin" + ) + simulated_binary_responses <- apply_threshold( + linear_predictor, simulated_latent_variables, clsize, "rbin", + beta_intercepts + ) + create_output( + simulated_binary_responses, sample_size, clsize, simulated_latent_variables, + linear_predictor_formula, xdata, "rbin" + ) } diff --git a/R/rmult.acl.R b/R/rmult.acl.R index 0053bf7..236dc64 100644 --- a/R/rmult.acl.R +++ b/R/rmult.acl.R @@ -105,28 +105,34 @@ #' xdata <- data.frame(x1, x2) #' identity_matrix <- diag(4) #' equicorrelation_matrix <- toeplitz(c(1, rep(0.95, cluster_size - 1))) -#' latent_correlation_matrix <- kronecker(equicorrelation_matrix, -#' identity_matrix) -#' simulated_ordinal_dataset <- rmult.acl(clsize = cluster_size, -#' intercepts = beta_intercepts, betas = beta_coefficients, -#' xformula = ~ x1 + x2, xdata = xdata, -#' cor.matrix = latent_correlation_matrix) -#' suppressPackageStartupMessages(library('multgee')) +#' latent_correlation_matrix <- kronecker( +#' equicorrelation_matrix, +#' identity_matrix +#' ) +#' simulated_ordinal_dataset <- rmult.acl( +#' clsize = cluster_size, +#' intercepts = beta_intercepts, betas = beta_coefficients, +#' xformula = ~ x1 + x2, xdata = xdata, +#' cor.matrix = latent_correlation_matrix +#' ) +#' suppressPackageStartupMessages(library("multgee")) #' ordinal_gee_model <- ordLORgee(y ~ x1 + x2, -#' data = simulated_ordinal_dataset$simdata, id = id, repeated = time, -#' LORstr = 'time.exch', link='acl') +#' data = simulated_ordinal_dataset$simdata, id = id, repeated = time, +#' LORstr = "time.exch", link = "acl" +#' ) #' round(coef(ordinal_gee_model), 2) -#' #' @export rmult.acl <- function(clsize = clsize, intercepts = intercepts, betas = betas, # nolint - xformula = formula(xdata), xdata = parent.frame(), cor.matrix = cor.matrix, # nolint - rlatent = NULL) { - check_cluster_size(clsize) - beta_intercepts <- check_intercepts(intercepts, clsize, "rmult.acl") - categories_no <- ncol(beta_intercepts) + 1 - beta_coefficients <- check_betas(betas, clsize) - betas_bcl <- create_betas_acl2bcl(beta_intercepts, categories_no, - beta_coefficients) - rmult.bcl(clsize, categories_no, betas_bcl, xformula, xdata, cor.matrix, - rlatent) + xformula = formula(xdata), xdata = parent.frame(), cor.matrix = cor.matrix, # nolint + rlatent = NULL) { + check_cluster_size(clsize) + beta_intercepts <- check_intercepts(intercepts, clsize, "rmult.acl") + categories_no <- ncol(beta_intercepts) + 1 + beta_coefficients <- check_betas(betas, clsize) + betas_bcl <- create_betas_acl2bcl( + beta_intercepts, categories_no, beta_coefficients + ) + rmult.bcl( + clsize, categories_no, betas_bcl, xformula, xdata, cor.matrix, rlatent + ) } diff --git a/R/rmult.bcl.R b/R/rmult.bcl.R index 7b92bc4..1163fdf 100644 --- a/R/rmult.bcl.R +++ b/R/rmult.bcl.R @@ -104,38 +104,42 @@ #' equicorrelation_matrix <- toeplitz(c(1, rep(0.95, cluster_size - 1))) #' identity_matrix <- diag(categories_no) #' latent_correlation_matrix <- kronecker(equicorrelation_matrix, identity_matrix) # nolint -#' simulated_nominal_dataset <- rmult.bcl(clsize = cluster_size, -#' ncategories = categories_no, betas = betas, xformula = ~ x1 + x2, -#' xdata = xdata, cor.matrix = latent_correlation_matrix) -#' suppressPackageStartupMessages(library('multgee')) +#' simulated_nominal_dataset <- rmult.bcl( +#' clsize = cluster_size, +#' ncategories = categories_no, betas = betas, xformula = ~ x1 + x2, +#' xdata = xdata, cor.matrix = latent_correlation_matrix +#' ) +#' suppressPackageStartupMessages(library("multgee")) #' nominal_gee_model <- nomLORgee(y ~ x1 + x2, -#' data = simulated_nominal_dataset$simdata, id = id, repeated = time, -#' LORstr = 'time.exch') +#' data = simulated_nominal_dataset$simdata, id = id, repeated = time, +#' LORstr = "time.exch" +#' ) #' round(coef(nominal_gee_model), 2) -#' #' @export rmult.bcl <- function(clsize = clsize, ncategories = ncategories, betas = betas, # nolint - xformula = formula(xdata), xdata = parent.frame(), cor.matrix = cor.matrix, # nolint - rlatent = NULL) { - check_cluster_size(clsize) - categories_no <- check_ncategories(ncategories) - betas <- check_betas(betas, clsize) - linear_predictor_formula <- check_xformula(xformula) - if (!is.environment(xdata)) xdata <- data.frame(na.omit(xdata)) - linear_predictor <- create_linear_predictor(betas, clsize, - linear_predictor_formula, xdata, - "rmult.bcl", categories_no) - sample_size <- nrow(linear_predictor) - simulated_latent_responses <- create_rlatent(rlatent, sample_size, - "cloglog", clsize, cor.matrix, - "rmult.bcl", categories_no) - simulated_nominal_responses <- apply_threshold(linear_predictor, - simulated_latent_responses, - clsize, "rmult.bcl", - categories_no = - categories_no) - linear_predictor_formula <- update(linear_predictor_formula, ~. - 1) - create_output(simulated_nominal_responses, sample_size, clsize, - simulated_latent_responses, linear_predictor_formula, xdata, - "rmult.bcl", categories_no = categories_no) + xformula = formula(xdata), xdata = parent.frame(), cor.matrix = cor.matrix, # nolint + rlatent = NULL) { + check_cluster_size(clsize) + categories_no <- check_ncategories(ncategories) + betas <- check_betas(betas, clsize) + linear_predictor_formula <- check_xformula(xformula) + if (!is.environment(xdata)) xdata <- data.frame(na.omit(xdata)) + linear_predictor <- create_linear_predictor( + betas, clsize, linear_predictor_formula, xdata, "rmult.bcl", categories_no + ) + sample_size <- nrow(linear_predictor) + simulated_latent_responses <- create_rlatent( + rlatent, sample_size, "cloglog", clsize, cor.matrix, "rmult.bcl", + categories_no + ) + simulated_nominal_responses <- apply_threshold( + linear_predictor, simulated_latent_responses, clsize, "rmult.bcl", + categories_no = categories_no + ) + linear_predictor_formula <- update(linear_predictor_formula, ~ . - 1) + create_output( + simulated_nominal_responses, sample_size, clsize, + simulated_latent_responses, linear_predictor_formula, xdata, "rmult.bcl", + categories_no = categories_no + ) } diff --git a/R/rmult.clm.R b/R/rmult.clm.R index b5476c4..3213a0d 100644 --- a/R/rmult.clm.R +++ b/R/rmult.clm.R @@ -104,43 +104,56 @@ #' beta_coefficients <- matrix(c(1, 2, 3, 4), 4, 1) #' x <- rep(rnorm(sample_size), each = cluster_size) #' latent_correlation_matrix <- toeplitz(c(1, 0.85, 0.5, 0.15)) -#' simulated_ordinal_dataset <- rmult.clm(clsize = cluster_size, -#' intercepts = beta_intercepts, betas = beta_coefficients, xformula = ~ x, -#' cor.matrix = latent_correlation_matrix, link = 'probit') +#' simulated_ordinal_dataset <- rmult.clm( +#' clsize = cluster_size, +#' intercepts = beta_intercepts, betas = beta_coefficients, xformula = ~x, +#' cor.matrix = latent_correlation_matrix, link = "probit" +#' ) #' head(simulated_ordinal_dataset$simdata, n = 8) #' #' ## Same sampling scheme except that the parameter vector is time-stationary. #' set.seed(12345) -#' simulated_ordinal_dataset <- rmult.clm(clsize = cluster_size, betas = 1, -#' xformula = ~ x, cor.matrix = latent_correlation_matrix, -#' intercepts = beta_intercepts, link = 'probit') +#' simulated_ordinal_dataset <- rmult.clm( +#' clsize = cluster_size, betas = 1, +#' xformula = ~x, cor.matrix = latent_correlation_matrix, +#' intercepts = beta_intercepts, link = "probit" +#' ) #' ## Fit a GEE model (Touloumis et al., 2013) to estimate the regression #' ## coefficients. #' library(multgee) -#' ordinal_gee_model <- ordLORgee(y ~ x, id = id, repeated = time, -#' link = 'probit', data = simulated_ordinal_dataset$simdata) +#' ordinal_gee_model <- ordLORgee(y ~ x, +#' id = id, repeated = time, +#' link = "probit", data = simulated_ordinal_dataset$simdata +#' ) #' coef(ordinal_gee_model) -#' #' @export rmult.clm <- function(clsize = clsize, intercepts = intercepts, betas = betas, # nolint - xformula = formula(xdata), xdata = parent.frame(), link = "logit", - cor.matrix = cor.matrix, rlatent = NULL) { # nolint - check_cluster_size(clsize) - beta_intercepts <- check_intercepts(intercepts, clsize, "rmult.clm") - beta_coefficients <- check_betas(betas, clsize) - linear_predictor_formula <- check_xformula(xformula) - if (!is.environment(xdata)) xdata <- data.frame(na.omit(xdata)) - linear_predictor <- create_linear_predictor(beta_coefficients, clsize, - linear_predictor_formula, xdata, - "rmult.clm") - sample_size <- nrow(linear_predictor) - simulated_latent_responses <- create_rlatent(rlatent, sample_size, link, - clsize, cor.matrix, - "rmult.clm") - simulated_ordinal_responses <- - apply_threshold(linear_predictor, simulated_latent_responses, clsize, - "rmult.clm", beta_intercepts) - create_output(simulated_ordinal_responses, sample_size, clsize, - simulated_latent_responses, linear_predictor_formula, xdata, - "rmult.clm") + xformula = formula(xdata), xdata = parent.frame(), link = "logit", # nolintr + cor.matrix = cor.matrix, rlatent = NULL) { # nolint + check_cluster_size(clsize) + beta_intercepts <- check_intercepts(intercepts, clsize, "rmult.clm") + beta_coefficients <- check_betas(betas, clsize) + linear_predictor_formula <- check_xformula(xformula) + if (!is.environment(xdata)) xdata <- data.frame(na.omit(xdata)) + linear_predictor <- create_linear_predictor( + beta_coefficients, clsize, + linear_predictor_formula, xdata, + "rmult.clm" + ) + sample_size <- nrow(linear_predictor) + simulated_latent_responses <- create_rlatent( + rlatent, sample_size, link, + clsize, cor.matrix, + "rmult.clm" + ) + simulated_ordinal_responses <- + apply_threshold( + linear_predictor, simulated_latent_responses, clsize, + "rmult.clm", beta_intercepts + ) + create_output( + simulated_ordinal_responses, sample_size, clsize, + simulated_latent_responses, linear_predictor_formula, xdata, + "rmult.clm" + ) } diff --git a/R/rmult.crm.R b/R/rmult.crm.R index fa274ad..0a8f6a3 100644 --- a/R/rmult.crm.R +++ b/R/rmult.crm.R @@ -107,40 +107,43 @@ #' categories_no <- 5 #' identity_matrix <- diag(1, (categories_no - 1) * cluster_size) #' equicorrelation_matrix <- toeplitz(c(0, rep(0.24, categories_no - 2))) -#' ones_matrix <- matrix(1, cluster_size, cluster_size) +#' ones_matrix <- matrix(1, cluster_size, cluster_size) #' latent_correlation_matrix <- identity_matrix + -#' kronecker(equicorrelation_matrix, ones_matrix) -#' simulated_ordinal_dataset <- rmult.crm(clsize = cluster_size, -#' intercepts = beta_intercepts, betas = beta_coefficients, xformula = ~ x, -#' cor.matrix = latent_correlation_matrix, link = 'probit') +#' kronecker(equicorrelation_matrix, ones_matrix) +#' simulated_ordinal_dataset <- rmult.crm( +#' clsize = cluster_size, +#' intercepts = beta_intercepts, betas = beta_coefficients, xformula = ~x, +#' cor.matrix = latent_correlation_matrix, link = "probit" +#' ) #' head(simulated_ordinal_dataset$Ysim) -#' -#'@export +#' @export rmult.crm <- function(clsize = clsize, intercepts = intercepts, betas = betas, # nolint - xformula = formula(xdata), xdata = parent.frame(), link = "logit", - cor.matrix = cor.matrix, rlatent = NULL) { # nolint - check_cluster_size(clsize) - beta_coefficients <- check_betas(betas, clsize) - linear_predictor_formula <- check_xformula(xformula) - if (!is.environment(xdata)) xdata <- data.frame(na.omit(xdata)) - linear_predictor <- create_linear_predictor(beta_coefficients, clsize, - linear_predictor_formula, xdata, - "rmult.clm") - sample_size <- nrow(linear_predictor) - beta_intercepts <- check_intercepts(intercepts, clsize, "rmult.crm", - sample_size) - categories_no <- ncol(beta_intercepts) / clsize + 1 - linear_predictor_extended <- t(apply(linear_predictor, 1, function(x) - rep(x, each = categories_no - 1))) - simulated_latent_responses <- create_rlatent(rlatent, sample_size, link, - clsize, cor.matrix, - "rmult.crm", categories_no) - simulated_ordinal_responses <- apply_threshold(linear_predictor_extended, - simulated_latent_responses, - clsize, "rmult.crm", - beta_intercepts, - categories_no) - create_output(simulated_ordinal_responses, sample_size, clsize, - simulated_latent_responses, linear_predictor_formula, xdata, - "rmult.crm", categories_no) + xformula = formula(xdata), xdata = parent.frame(), link = "logit", # nolintr + cor.matrix = cor.matrix, rlatent = NULL) { # nolint + check_cluster_size(clsize) + beta_coefficients <- check_betas(betas, clsize) + linear_predictor_formula <- check_xformula(xformula) + if (!is.environment(xdata)) xdata <- data.frame(na.omit(xdata)) + linear_predictor <- create_linear_predictor( + beta_coefficients, clsize, linear_predictor_formula, xdata, "rmult.clm" + ) + sample_size <- nrow(linear_predictor) + beta_intercepts <- check_intercepts( + intercepts, clsize, "rmult.crm", sample_size + ) + categories_no <- ncol(beta_intercepts) / clsize + 1 + linear_predictor_extended <- t(apply(linear_predictor, 1, function(x) + rep(x, each = categories_no - 1))) + simulated_latent_responses <- create_rlatent( + rlatent, sample_size, link, clsize, cor.matrix, "rmult.crm", categories_no + ) + simulated_ordinal_responses <- apply_threshold( + linear_predictor_extended, simulated_latent_responses, clsize, "rmult.crm", + beta_intercepts, categories_no + ) + create_output( + simulated_ordinal_responses, sample_size, clsize, + simulated_latent_responses, linear_predictor_formula, xdata, "rmult.crm", + categories_no + ) } diff --git a/R/rnorta.R b/R/rnorta.R index c2b2e9c..d3b0bed 100644 --- a/R/rnorta.R +++ b/R/rnorta.R @@ -49,64 +49,85 @@ #' sample_size <- 1000 #' latent_correlation_matrix <- toeplitz(c(1, rep(0.8, 2))) #' latent_correlation_matrix -#' common_marginal_distribution <- rep('qlogis', 3) -#' simulated_logistic_responses <- rnorta(R = sample_size, -#' cor.matrix = latent_correlation_matrix, -#' distr = common_marginal_distribution) +#' common_marginal_distribution <- rep("qlogis", 3) +#' simulated_logistic_responses <- rnorta( +#' R = sample_size, +#' cor.matrix = latent_correlation_matrix, +#' distr = common_marginal_distribution +#' ) #' #' ## The following lines exemplify the NORTA method. #' set.seed(1) -#' simulated_normal_responses <- rsmvnorm(R = sample_size, -#' cor.matrix = latent_correlation_matrix) +#' simulated_normal_responses <- rsmvnorm( +#' R = sample_size, +#' cor.matrix = latent_correlation_matrix +#' ) #' all(simulated_logistic_responses == qlogis(pnorm(simulated_normal_responses))) # nolintr #' #' ## Change the marginal distributions to standard normal, standard #' ## logistic and standard extreme value distribution. #' set.seed(1) -#' different_marginal_distributions <- c('qnorm', 'qlogis', 'qgumbel') -#' simulated_logistic_responses <- rnorta(R = sample_size, -#' cor.matrix = latent_correlation_matrix, -#' distr = different_marginal_distributions) +#' different_marginal_distributions <- c("qnorm", "qlogis", "qgumbel") +#' simulated_logistic_responses <- rnorta( +#' R = sample_size, +#' cor.matrix = latent_correlation_matrix, +#' distr = different_marginal_distributions +#' ) #' cor(simulated_logistic_responses) #' colMeans(simulated_logistic_responses) #' apply(simulated_logistic_responses, 2, sd) #' #' ## Same as above but using parameter values other than the default ones. #' set.seed(1) -#' qpars <- list(c(mean = 1, sd = 9), c(location = 2, scale = 1), -#' c(loc = 3, scale = 1)) -#' simulated_logistic_responses <- rnorta(R = sample_size, -#' cor.matrix = latent_correlation_matrix, -#' distr = different_marginal_distributions, qparameters = qpars) +#' qpars <- list( +#' c(mean = 1, sd = 9), c(location = 2, scale = 1), +#' c(loc = 3, scale = 1) +#' ) +#' simulated_logistic_responses <- rnorta( +#' R = sample_size, +#' cor.matrix = latent_correlation_matrix, +#' distr = different_marginal_distributions, qparameters = qpars +#' ) #' cor(simulated_logistic_responses) #' colMeans(simulated_logistic_responses) #' apply(simulated_logistic_responses, 2, sd) #' @export rnorta <- function(R = R, cor.matrix = cor.matrix, distr = distr, # nolint - qparameters = NULL) { - if (all.equal(R, as.integer(R)) != TRUE | R < 1) - stop("'R' must be a positive integer") - quantile_functions <- as.character(distr) - ans <- rsmvnorm(R = R, cor.matrix = cor.matrix) - if (length(quantile_functions) != ncol(cor.matrix)) - stop("'distr' must be a ", ncol(cor.matrix), - "-variate vector of strings naming a valid quantile function") - if (!is.null(qparameters)) { - qparameters <- as.list(qparameters) - if (length(qparameters) != ncol(cor.matrix)) - stop("'qparameters' must be provided as a list of length ", - ncol(cor.matrix)) + qparameters = NULL) { + if (all.equal(R, as.integer(R)) != TRUE | R < 1) { + stop("'R' must be a positive integer") + } + quantile_functions <- as.character(distr) + ans <- rsmvnorm(R = R, cor.matrix = cor.matrix) + if (length(quantile_functions) != ncol(cor.matrix)) { + stop( + "'distr' must be a ", ncol(cor.matrix), + "-variate vector of strings naming a valid quantile function" + ) + } + if (!is.null(qparameters)) { + qparameters <- as.list(qparameters) + if (length(qparameters) != ncol(cor.matrix)) { + stop( + "'qparameters' must be provided as a list of length ", + ncol(cor.matrix) + ) } - ans <- pnorm(ans) - for (i in seq_len(ncol(cor.matrix))) { - quantile_function <- get(quantile_functions[i], mode = "function") - if (!is.function(quantile_function)) - stop("Character string ", i, " in `distr' does not correspond + } + ans <- pnorm(ans) + for (i in seq_len(ncol(cor.matrix))) { + quantile_function <- get(quantile_functions[i], mode = "function") + if (!is.function(quantile_function)) { + stop("Character string ", i, " in `distr' does not correspond to a valid function") - if (!is.null(qparameters)) - formals(quantile_function)[pmatch(names(qparameters[[i]]), - formalArgs(quantile_function))] <- qparameters[[i]] - ans[, i] <- quantile_function(ans[, i]) } - ans + if (!is.null(qparameters)) { + formals(quantile_function)[pmatch( + names(qparameters[[i]]), + formalArgs(quantile_function) + )] <- qparameters[[i]] + } + ans[, i] <- quantile_function(ans[, i]) + } + ans } diff --git a/R/rsmvnorm.R b/R/rsmvnorm.R index bdb50cf..a0c9aa4 100644 --- a/R/rsmvnorm.R +++ b/R/rsmvnorm.R @@ -19,29 +19,39 @@ #' set.seed(1) #' sample_size <- 10000 #' correlation_matrix <- toeplitz(c(1, 0.4)) -#' simulated_normal_responses <- rsmvnorm(R = sample_size, -#' cor.matrix = correlation_matrix) +#' simulated_normal_responses <- rsmvnorm( +#' R = sample_size, +#' cor.matrix = correlation_matrix +#' ) #' colMeans(simulated_normal_responses) #' apply(simulated_normal_responses, 2, sd) #' cor(simulated_normal_responses) #' @export rsmvnorm <- function(R = R, cor.matrix = cor.matrix) { # nolint - if (all.equal(R, as.integer(R)) != TRUE | R < 1) - stop("'R' must be a positive integer") - if (!is.numeric(cor.matrix)) - stop("'cor.matrix' must be numeric") - correlation_matrix <- as.matrix(cor.matrix) # nolint - if (!isSymmetric(correlation_matrix)) - stop("'cor.matrix' must be a symmetric matrix") - if (any(diag(correlation_matrix) != 1)) - stop("the diagonal elements of 'cor.matrix' must be equal to one") - if (any(correlation_matrix > 1) | any(correlation_matrix < -1)) - stop("all the elements of 'cor.matrix' must be on [-1,1]") - correlation_matrix_eigen <- eigen(correlation_matrix, symmetric = TRUE, - only.values = TRUE) - if (any(correlation_matrix_eigen$values <= 0)) - stop("'cor.matrix' must be a positive definite matrix") - p <- ncol(correlation_matrix) - ans <- matrix(rnorm(R * p), R, p) %*% chol(correlation_matrix) - ans + if (all.equal(R, as.integer(R)) != TRUE | R < 1) { + stop("'R' must be a positive integer") + } + if (!is.numeric(cor.matrix)) { + stop("'cor.matrix' must be numeric") + } + correlation_matrix <- as.matrix(cor.matrix) + if (!isSymmetric(correlation_matrix)) { + stop("'cor.matrix' must be a symmetric matrix") + } + if (any(diag(correlation_matrix) != 1)) { + stop("the diagonal elements of 'cor.matrix' must be equal to one") + } + if (any(correlation_matrix > 1) | any(correlation_matrix < -1)) { + stop("all the elements of 'cor.matrix' must be on [-1,1]") + } + correlation_matrix_eigen <- eigen( + correlation_matrix, + symmetric = TRUE, only.values = TRUE + ) + if (any(correlation_matrix_eigen$values <= 0)) { + stop("'cor.matrix' must be a positive definite matrix") + } + p <- ncol(correlation_matrix) + ans <- matrix(rnorm(R * p), R, p) %*% chol(correlation_matrix) + ans } diff --git a/README.Rmd b/README.Rmd index 2079d80..d48282b 100644 --- a/README.Rmd +++ b/README.Rmd @@ -96,7 +96,7 @@ The following R code illustrates how to use the core function `rbin`: ```{r} ## See Example 3.5 in the Vignette. set.seed(123) -sample_size <- 5000 +sample_size <- 100 cluster_size <- 4 beta_intercepts <- 0 beta_coefficients <- 0.2 diff --git a/README.md b/README.md index e0ffe2f..0962d76 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ # SimCorMultRes: Simulates Correlated Multinomial Responses [![Github -version](https://img.shields.io/badge/GitHub%20-1.6.8-orange.svg)](%22commits/master%22) +version](https://img.shields.io/badge/GitHub%20-1.6.9-orange.svg)](%22commits/master%22) [![Travis-CI Build Status](https://travis-ci.org/AnestisTouloumis/SimCorMultRes.svg?branch=master)](https://travis-ci.org/AnestisTouloumis/SimCorMultRes) [![Project Status: Active The project has reached a stable, usable state @@ -83,7 +83,7 @@ The following R code illustrates how to use the core function `rbin`: ``` r ## See Example 3.5 in the Vignette. set.seed(123) -sample_size <- 5000 +sample_size <- 100 cluster_size <- 4 beta_intercepts <- 0 beta_coefficients <- 0.2 @@ -97,11 +97,11 @@ binary_gee_model <- gee(y ~ x, family = binomial("probit"), id = id, data = simu #> Beginning Cgee S-function, @(#) geeformula.q 4.13 98/01/27 #> running glm to get initial regression estimate #> (Intercept) x -#> 0.002636705 0.204827031 +#> 0.1315121 0.2826005 summary(binary_gee_model)$coefficients -#> Estimate Naive S.E. Naive z Robust S.E. Robust z -#> (Intercept) 0.002636705 0.008929290 0.2952872 0.01572132 0.1677153 -#> x 0.204827031 0.009114596 22.4724192 0.01610695 12.7166857 +#> Estimate Naive S.E. Naive z Robust S.E. Robust z +#> (Intercept) 0.1315121 0.06399465 2.055048 0.1106696 1.188331 +#> x 0.2826006 0.07191931 3.929412 0.1270285 2.224703 ``` Additional examples can be found in Touloumis (2016) and in the vignette diff --git a/inst/NEWS b/inst/NEWS index 148a040..780c2fe 100644 --- a/inst/NEWS +++ b/inst/NEWS @@ -1,3 +1,6 @@ +Version 1.6.9 [2019-07-24] + * Cosmetic changes. + Version 1.6.8 [2019-07-23] * Added unit tests for internal functions. diff --git a/tests/testthat/test_core_functions.R b/tests/testthat/test_core_functions.R index ddc0b17..416b281 100644 --- a/tests/testthat/test_core_functions.R +++ b/tests/testthat/test_core_functions.R @@ -6,15 +6,16 @@ test_that("rbin constant beta_intercepts", { beta_coefficients <- 0.2 latent_correlation_matrix <- toeplitz(c(1, 0.9, 0.9, 0.9)) x <- rep(rnorm(sample_size), each = cluster_size) - simulated_binary_dataset <- - rbin(clsize = cluster_size, intercepts = beta_intercepts, - betas = beta_coefficients, xformula = ~ x, - cor.matrix = latent_correlation_matrix, link = "probit") + simulated_binary_dataset <- rbin( + clsize = cluster_size, intercepts = beta_intercepts, + betas = beta_coefficients, xformula = ~x, + cor.matrix = latent_correlation_matrix, link = "probit" + ) simulated_responses <- as.numeric(c(t(simulated_binary_dataset$rlatent)) <= - beta_intercepts + beta_coefficients * x) + beta_intercepts + beta_coefficients * x) expect_equal(c(t(simulated_binary_dataset$Ysim)), simulated_responses) - }) +}) test_that("rbin varying beta_intercepts", { @@ -25,16 +26,17 @@ test_that("rbin varying beta_intercepts", { beta_coefficients <- 0.2 latent_correlation_matrix <- toeplitz(c(1, 0.9, 0.9, 0.9)) x <- rep(rnorm(sample_size), each = cluster_size) - simulated_binary_dataset <- - rbin(clsize = cluster_size, intercepts = beta_intercepts, - betas = beta_coefficients, xformula = ~ x, - cor.matrix = latent_correlation_matrix, link = "probit") + simulated_binary_dataset <- rbin( + clsize = cluster_size, intercepts = beta_intercepts, + betas = beta_coefficients, xformula = ~x, + cor.matrix = latent_correlation_matrix, link = "probit" + ) beta_intercepts <- rep(beta_intercepts, sample_size) simulated_responses <- as.numeric(c(t(simulated_binary_dataset$rlatent)) <= - beta_intercepts + beta_coefficients * x) + beta_intercepts + beta_coefficients * x) expect_equal(c(t(simulated_binary_dataset$Ysim)), simulated_responses) - }) +}) test_that("rmult.bcl constant beta_coefficients", { @@ -48,20 +50,20 @@ test_that("rmult.bcl constant beta_coefficients", { xdata <- data.frame(x1, x2) latent_correlation_matrix <- kronecker( toeplitz(c(1, rep(0.95, cluster_size - 1))), diag(categories_no) - ) - simulated_nominal_dataset <- - rmult.bcl(clsize = cluster_size, ncategories = categories_no, - betas = beta_coefficients, xformula = ~ x1 + x2, xdata = xdata, - cor.matrix = latent_correlation_matrix) - xmat <- model.matrix(~x1 + x2, data = xdata) + ) + simulated_nominal_dataset <- rmult.bcl( + clsize = cluster_size, ncategories = categories_no, + betas = beta_coefficients, xformula = ~ x1 + x2, xdata = xdata, + cor.matrix = latent_correlation_matrix + ) + xmat <- model.matrix(~ x1 + x2, data = xdata) xmat <- apply(xmat, 2, function(x) rep(x, each = categories_no)) - lin_pred <- matrix(beta_coefficients, nrow = nrow(xmat), ncol = ncol(xmat), - byrow = TRUE) * xmat + lin_pred <- matrix(beta_coefficients, nrow(xmat), ncol(xmat), TRUE) * xmat lin_pred <- rowSums(lin_pred) + c(t(simulated_nominal_dataset$rlatent)) lin_pred <- matrix(lin_pred, sample_size * cluster_size, categories_no, TRUE) simulated_responses <- apply(lin_pred, 1, which.max) expect_equal(c(t(simulated_nominal_dataset$Ysim)), simulated_responses) - }) +}) test_that("rmult.bcl varying beta_coefficients", { @@ -69,31 +71,34 @@ test_that("rmult.bcl varying beta_coefficients", { categories_no <- 4 cluster_size <- 3 beta_coefficients <- matrix( - c(1, 3, 2, 1.25, 3.25, 1.75, 0.75, 2.75, 2.25, 0, 0, 0, + c( + 1, 3, 2, 1.25, 3.25, 1.75, 0.75, 2.75, 2.25, 0, 0, 0, 2, 1, 2, 1.15, 3.75, 1.25, 0.45, 2.65, 2.85, 0, 0, 0, - 1, 2, 2, 1.75, 3.15, 1.35, 0.55, 2.75, 2.95, 0, 0, 0), - nrow = cluster_size, byrow = TRUE) + 1, 2, 2, 1.75, 3.15, 1.35, 0.55, 2.75, 2.95, 0, 0, 0 + ), + nrow = cluster_size, byrow = TRUE + ) set.seed(1) x1 <- rep(rnorm(sample_size), each = cluster_size) x2 <- rnorm(sample_size * cluster_size) xdata <- data.frame(x1, x2) latent_correlation_matrix <- kronecker( - toeplitz(c(1, rep(0.95, cluster_size - 1))), diag(categories_no)) - simulated_nominal_dataset <- rmult.bcl(clsize = cluster_size, - ncategories = categories_no, - betas = beta_coefficients, - xformula = ~ x1 + x2, xdata = xdata, - cor.matrix = - latent_correlation_matrix) - xmat <- model.matrix(~x1 + x2, data = xdata) + toeplitz(c(1, rep(0.95, cluster_size - 1))), diag(categories_no) + ) + simulated_nominal_dataset <- rmult.bcl( + clsize = cluster_size, ncategories = categories_no, + betas = beta_coefficients, xformula = ~ x1 + x2, xdata = xdata, + cor.matrix = latent_correlation_matrix + ) + xmat <- model.matrix(~ x1 + x2, data = xdata) xmat <- apply(xmat, 2, function(x) rep(x, each = categories_no)) - lin_pred <- matrix(c(t(beta_coefficients)), nrow = nrow(xmat), - ncol = ncol(xmat), byrow = TRUE) * xmat + lin_pred <- matrix(c(t(beta_coefficients)), nrow(xmat), ncol(xmat), TRUE) * + xmat lin_pred <- rowSums(lin_pred) + c(t(simulated_nominal_dataset$rlatent)) lin_pred <- matrix(lin_pred, sample_size * cluster_size, categories_no, TRUE) simulated_responses <- apply(lin_pred, 1, which.max) expect_equal(c(t(simulated_nominal_dataset$Ysim)), simulated_responses) - }) +}) test_that("rmult.clm varying beta_coefficients", { @@ -104,17 +109,20 @@ test_that("rmult.clm varying beta_coefficients", { beta_coefficients <- matrix(c(1, 2, 3, 4), 4, 1) x <- rep(rnorm(sample_size), each = cluster_size) latent_correlation_matrix <- toeplitz(c(1, 0.85, 0.5, 0.15)) - simulated_ordinal_dataset <- - rmult.clm(clsize = cluster_size, intercepts = beta_intercepts, - betas = beta_coefficients, xformula = ~ x, - cor.matrix = latent_correlation_matrix, link = "probit") + simulated_ordinal_dataset <- rmult.clm( + clsize = cluster_size, intercepts = beta_intercepts, + betas = beta_coefficients, xformula = ~x, + cor.matrix = latent_correlation_matrix, link = "probit" + ) simulated_latent_responses <- c(t(simulated_ordinal_dataset$rlatent)) - c(beta_coefficients) * x beta_intercepts <- c(-Inf, beta_intercepts, Inf) - simulated_responses <- cut(simulated_latent_responses, beta_intercepts, - labels = FALSE) + simulated_responses <- cut( + simulated_latent_responses, beta_intercepts, + labels = FALSE + ) expect_equal(c(t(simulated_ordinal_dataset$Ysim)), simulated_responses) - }) +}) test_that("rmult.clm constant beta_coefficients", { @@ -125,17 +133,19 @@ test_that("rmult.clm constant beta_coefficients", { beta_coefficients <- 1 x <- rep(rnorm(sample_size), each = cluster_size) latent_correlation_matrix <- toeplitz(c(1, 0.85, 0.5, 0.15)) - simulated_ordinal_dataset <- - rmult.clm(clsize = cluster_size, intercepts = beta_intercepts, - betas = beta_coefficients, xformula = ~ x, - cor.matrix = latent_correlation_matrix, link = "probit") + simulated_ordinal_dataset <- rmult.clm( + clsize = cluster_size, intercepts = beta_intercepts, + betas = beta_coefficients, xformula = ~x, + cor.matrix = latent_correlation_matrix, link = "probit" + ) simulated_latent_responses <- c(t(simulated_ordinal_dataset$rlatent)) - c(beta_coefficients) * x beta_intercepts <- c(-Inf, beta_intercepts, Inf) - simulated_responses <- cut(simulated_latent_responses, beta_intercepts, - labels = FALSE) + simulated_responses <- cut( + simulated_latent_responses, beta_intercepts, labels = FALSE + ) expect_equal(c(t(simulated_ordinal_dataset$Ysim)), simulated_responses) - }) +}) test_that("rmult.acl constant beta_coefficients", { @@ -148,26 +158,28 @@ test_that("rmult.acl constant beta_coefficients", { x2 <- rnorm(sample_size * cluster_size) xdata <- data.frame(x1, x2) set.seed(1) - latent_correlation_matrix <- - kronecker(toeplitz(c(1, rep(0.95, cluster_size - 1))), diag(4)) - simulated_ordinal_dataset <- - rmult.acl(clsize = cluster_size, intercepts = beta_intercepts, - betas = beta_coefficients, xformula = ~ x1 + x2, xdata = xdata, - cor.matrix = latent_correlation_matrix) + latent_correlation_matrix <- kronecker( + toeplitz(c(1, rep(0.95, cluster_size - 1))), diag(4) + ) + simulated_ordinal_dataset <- rmult.acl( + clsize = cluster_size, intercepts = beta_intercepts, + betas = beta_coefficients, xformula = ~ x1 + x2, xdata = xdata, + cor.matrix = latent_correlation_matrix + ) beta_intercepts <- rev(cumsum(rev(c(beta_intercepts, 0)))) - beta_coefficients_bcl <- - c(beta_intercepts[1], 3, 3, - beta_intercepts[2], 2, 2, - beta_intercepts[3], 1, 1, - beta_intercepts[4], 0, 0) + beta_coefficients_bcl <- c( + beta_intercepts[1], 3, 3, beta_intercepts[2], 2, 2, + beta_intercepts[3], 1, 1, beta_intercepts[4], 0, 0 + ) set.seed(1) - simulated_nominal_dataset <- - rmult.bcl(clsize = cluster_size, ncategories = 4, - betas = beta_coefficients_bcl, xformula = ~ x1 + x2, - xdata = xdata, cor.matrix = latent_correlation_matrix) - expect_equal(c(t(simulated_ordinal_dataset$Ysim)), - c(t(simulated_nominal_dataset$Ysim))) - }) + simulated_nominal_dataset <- rmult.bcl( + clsize = cluster_size, ncategories = 4, betas = beta_coefficients_bcl, + xformula = ~ x1 + x2, xdata = xdata, cor.matrix = latent_correlation_matrix + ) + expect_equal( + c(t(simulated_ordinal_dataset$Ysim)), c(t(simulated_nominal_dataset$Ysim)) + ) +}) test_that("rmult.crm constant beta_coefficients", { @@ -177,23 +189,27 @@ test_that("rmult.crm constant beta_coefficients", { beta_coefficients <- 1 x <- rnorm(sample_size * cluster_size) categories_no <- 5 - latent_correlation_matrix <- - diag(1, (categories_no - 1) * cluster_size) + - kronecker(toeplitz(c(0, rep(0.24, categories_no - 2))), - matrix(1, cluster_size, cluster_size)) - simulated_ordinal_dataset <- - rmult.crm(clsize = cluster_size, intercepts = beta_intercepts, - betas = beta_coefficients, xformula = ~ x, - cor.matrix = latent_correlation_matrix, link = "probit") - simulated_latent_responses <- - c(t(simulated_ordinal_dataset$rlatent)) - + latent_correlation_matrix <- diag(1, (categories_no - 1) * cluster_size) + + kronecker( + toeplitz(c(0, rep(0.24, categories_no - 2))), + matrix(1, cluster_size, cluster_size) + ) + simulated_ordinal_dataset <- rmult.crm( + clsize = cluster_size, intercepts = beta_intercepts, + betas = beta_coefficients, xformula = ~x, + cor.matrix = latent_correlation_matrix, link = "probit" + ) + simulated_latent_responses <- c(t(simulated_ordinal_dataset$rlatent)) - rep(x, each = categories_no - 1) - simulated_responses <- - matrix(as.numeric(t(simulated_latent_responses <= beta_intercepts)), - sample_size * cluster_size, categories_no - 1, TRUE) - for (i in 1:(categories_no - 1)) - simulated_responses[, i] <- - ifelse(simulated_responses[, i] == 1, i, categories_no) + simulated_responses <- matrix( + as.numeric(t(simulated_latent_responses <= beta_intercepts)), + sample_size * cluster_size, categories_no - 1, TRUE + ) + for (i in 1:(categories_no - 1)) { + simulated_responses[, i] <- ifelse( + simulated_responses[, i] == 1, i, categories_no + ) + } simulated_responses <- apply(simulated_responses, 1, min) expect_equal(c(t(simulated_ordinal_dataset$Ysim)), simulated_responses) - }) +}) diff --git a/tests/testthat/test_internal_functions.R b/tests/testthat/test_internal_functions.R index 6460dc3..6ec47d9 100644 --- a/tests/testthat/test_internal_functions.R +++ b/tests/testthat/test_internal_functions.R @@ -1,19 +1,26 @@ -check_cluster_size - test_that("checking cluster size", { expect_silent(check_cluster_size(2)) - expect_error(check_cluster_size(1), - "'clsize' must be a positive integer greater than or equal to two") # nolintr - expect_error(check_cluster_size(2.5), - "'clsize' must be a positive integer greater than or equal to two") # nolintr + expect_error( + check_cluster_size(1), + "'clsize' must be a positive integer greater than or equal to two" + ) + expect_error( + check_cluster_size(2.5), + "'clsize' must be a positive integer greater than or equal to two" + ) }) + test_that("checking categories", { expect_silent(check_ncategories(3)) - expect_error(check_ncategories(2), - "'ncategories' must be numeric greater or equal to three") - expect_error(check_ncategories(5.5), - "'ncategories' must be a positive integer") + expect_error( + check_ncategories(2), + "'ncategories' must be numeric greater or equal to three" + ) + expect_error( + check_ncategories(5.5), + "'ncategories' must be a positive integer" + ) }) @@ -29,62 +36,101 @@ test_that("checking correlation matrix", { cluster_size <- 5 categories_no <- 3 correlation_matrix_1 <- matrix(1, cluster_size, cluster_size) - correlation_matrix_2 <- matrix(1, cluster_size * categories_no, - cluster_size * categories_no) - correlation_matrix_3 <- matrix(1, cluster_size * (categories_no - 1), - cluster_size * (categories_no - 1)) + correlation_matrix_2 <- matrix( + 1, cluster_size * categories_no, cluster_size * categories_no + ) + correlation_matrix_3 <- matrix( + 1, cluster_size * (categories_no - 1), + cluster_size * (categories_no - 1) + ) correlation_matrix_4 <- diag(1, cluster_size) - correlation_matrix_5 <- diag(1, cluster_size * categories_no) - correlation_matrix_6 <- diag(1, cluster_size * (categories_no - 1)) - expect_equal(check_correlation_matrix(correlation_matrix_4, cluster_size, - rfctn = "rbin"), - correlation_matrix_4) - expect_equal(check_correlation_matrix(correlation_matrix_4, cluster_size, - rfctn = "rmult.clm"), - correlation_matrix_4) - expect_equal(check_correlation_matrix(correlation_matrix_5, cluster_size, - rfctn = "rmult.bcl", categories_no), - correlation_matrix_5) - expect_equal(check_correlation_matrix(correlation_matrix_6, cluster_size, - rfctn = "rmult.crm", categories_no), - correlation_matrix_6) - expect_error(check_correlation_matrix(correlation_matrix_1, cluster_size, - rfctn = "rbin"), - "'cor_matrix' must be positive definite") - expect_error(check_correlation_matrix(correlation_matrix_1, cluster_size, - rfctn = "rmult.clm"), - "'cor_matrix' must be positive definite") - expect_error(check_correlation_matrix(correlation_matrix_2, cluster_size, - rfctn = "rmult.bcl", categories_no), - "'cor_matrix' must be positive definite") - expect_error(check_correlation_matrix(correlation_matrix_1, cluster_size, - rfctn = "rmult.bcl", categories_no), - "'cor_matrix' must be a 15x15 matrix") - expect_error(check_correlation_matrix(correlation_matrix_3, cluster_size, - rfctn = "rmult.crm", categories_no), - "'cor_matrix' must be positive definite") - expect_error(check_correlation_matrix(correlation_matrix_2, cluster_size, - rfctn = "rmult.crm", categories_no), - "'cor_matrix' must be a 10x10 matrix") + correlation_matrix_5 <- diag(1, cluster_size * categories_no) + correlation_matrix_6 <- diag(1, cluster_size * (categories_no - 1)) + expect_equal( + check_correlation_matrix( + correlation_matrix_4, cluster_size, "rbin" + ), + correlation_matrix_4 + ) + expect_equal( + check_correlation_matrix( + correlation_matrix_4, cluster_size, "rmult.clm" + ), + correlation_matrix_4 + ) + expect_equal( + check_correlation_matrix( + correlation_matrix_5, cluster_size, "rmult.bcl", categories_no + ), + correlation_matrix_5 + ) + expect_equal( + check_correlation_matrix( + correlation_matrix_6, cluster_size, "rmult.crm", categories_no + ), + correlation_matrix_6 + ) + expect_error( + check_correlation_matrix( + correlation_matrix_1, cluster_size, "rbin" + ), + "'cor_matrix' must be positive definite" + ) + expect_error( + check_correlation_matrix( + correlation_matrix_1, cluster_size, "rmult.clm" + ), + "'cor_matrix' must be positive definite" + ) + expect_error( + check_correlation_matrix( + correlation_matrix_2, cluster_size, "rmult.bcl", categories_no + ), + "'cor_matrix' must be positive definite" + ) + expect_error( + check_correlation_matrix( + correlation_matrix_1, cluster_size, "rmult.bcl", categories_no + ), + "'cor_matrix' must be a 15x15 matrix" + ) + expect_error( + check_correlation_matrix( + correlation_matrix_3, cluster_size, "rmult.crm", categories_no + ), + "'cor_matrix' must be positive definite" + ) + expect_error( + check_correlation_matrix( + correlation_matrix_2, cluster_size, "rmult.crm", categories_no + ), + "'cor_matrix' must be a 10x10 matrix" + ) }) + test_that("checking covariates formula", { - xformula <- ~ x1 + x2 + xformula <- ~ x1 + x2 expect_equal(check_xformula(xformula), as.formula(~ x1 + x2)) xformula <- y ~ x1 + x2 expect_equal(check_xformula(xformula), as.formula(y ~ x1 + x2)) xformula <- y ~ x1 + x2 - 1 expect_equal(check_xformula(xformula), as.formula(y ~ x1 + x2)) xformula <- ~ -1 - expect_error(check_xformula(xformula), - "No covariates were found in 'formula'") + expect_error( + check_xformula(xformula), + "No covariates were found in 'formula'" + ) xformula <- y ~ - expect_error(check_xformula(xformula), - "No covariates were found in 'formula'") + expect_error( + check_xformula(xformula), + "No covariates were found in 'formula'" + ) }) + test_that("checking intercepts", { - cluster_size <- 5 + cluster_size <- 4 rfctn <- "rbin" intercepts <- 2 ans <- rep(intercepts, cluster_size) @@ -96,11 +142,15 @@ test_that("checking intercepts", { colnames(ans) <- c("", "intercepts", "") expect_equal(check_intercepts(intercepts, cluster_size, rfctn), ans) intercepts <- c(-Inf, 3, Inf) - expect_error(check_intercepts(intercepts, cluster_size, rfctn), - "'intercepts' must not be Inf or -Inf") + expect_error( + check_intercepts(intercepts, cluster_size, rfctn), + "'intercepts' must not be Inf or -Inf" + ) intercepts <- c(2, 3) - expect_error(check_intercepts(intercepts, cluster_size, rfctn), - "'intercepts' must have either one or 5 elements") + expect_error( + check_intercepts(intercepts, cluster_size, rfctn), + "'intercepts' must have either one or 4 elements" + ) rfctn <- "rmult.clm" intercepts <- c(2, 3) ans <- matrix(intercepts, nrow = cluster_size, ncol = 2, byrow = TRUE) @@ -111,36 +161,50 @@ test_that("checking intercepts", { ans <- cbind(-Inf, intercepts, Inf) colnames(ans) <- NULL expect_equal(check_intercepts(intercepts, cluster_size, rfctn), ans) - intercepts <- matrix(c(3, 2, 1, 2, 3, 4, 5, 6, 7, 8), - nrow = cluster_size, ncol = 2, byrow = TRUE) + intercepts <- matrix(c(3, 2, 1, 2, 3, 4, 5, 6), cluster_size, 2, TRUE) intercepts <- c(-Inf, 3, Inf) - expect_error(check_intercepts(intercepts, cluster_size, rfctn), - "'intercepts' must not be Inf or -Inf") - intercepts <- matrix(c(3, 2, 1, 2, 3, 4, 5, 6, 7, 8), - nrow = cluster_size, ncol = 2, byrow = TRUE) - expect_error(check_intercepts(intercepts, cluster_size, rfctn), - "'intercepts' must be increasing at each row") + expect_error( + check_intercepts(intercepts, cluster_size, rfctn), + "'intercepts' must not be Inf or -Inf" + ) + intercepts <- matrix(c(3, 2, 1, 2, 3, 4, 5, 6), cluster_size, 2, TRUE) + expect_error( + check_intercepts(intercepts, cluster_size, rfctn), + "'intercepts' must be increasing at each row" + ) intercepts <- c(4, 2) - expect_error(check_intercepts(intercepts, cluster_size, rfctn), - "'intercepts' must be increasing") - intercepts <- matrix(1:10, nrow = 2, ncol = cluster_size, byrow = TRUE) + expect_error( + check_intercepts(intercepts, cluster_size, rfctn), + "'intercepts' must be increasing" + ) + intercepts <- matrix(1:8, nrow = 2, ncol = cluster_size, byrow = TRUE) expect_error(check_intercepts(intercepts, cluster_size, rfctn)) rfctn <- "rmult.crm" intercepts <- c(-1.5, -0.5, 0.5, 1.5) categories_no <- 5 - ans <- matrix(rep(intercepts, categories_no), categories_no, - (categories_no - 1) * cluster_size, TRUE) + ans <- matrix( + rep(intercepts, categories_no), categories_no, + (categories_no - 1) * cluster_size, TRUE + ) colnames(ans) <- NULL - expect_equal(check_intercepts(intercepts, cluster_size, rfctn, categories_no), - ans) - rfctn <- "rmult.crm" - intercepts <- matrix(runif(cluster_size * (categories_no - 1), 0, 1), - cluster_size, categories_no - 1) - ans <- matrix(c(t(intercepts)), categories_no, - (categories_no - 1) * cluster_size, TRUE) + expect_equal( + check_intercepts(intercepts, cluster_size, rfctn, categories_no), + ans + ) + intercepts <- matrix( + runif( + cluster_size * (categories_no - 1), 0, 1 + ), + cluster_size, categories_no - 1 + ) + ans <- matrix( + c(t(intercepts)), categories_no, + (categories_no - 1) * cluster_size, TRUE + ) colnames(ans) <- NULL - expect_equal(check_intercepts(intercepts, cluster_size, rfctn, categories_no), - ans) + expect_equal( + check_intercepts(intercepts, cluster_size, rfctn, categories_no), ans + ) }) @@ -151,6 +215,8 @@ test_that("checking betas", { betas <- matrix(1:4, cluster_size, 2, TRUE) expect_equal(check_betas(betas, cluster_size), c(1, 2, 3, 4)) betas <- matrix(1:6, 3, cluster_size, TRUE) - expect_error(check_betas(betas, cluster_size), - "The number of rows in 'betas' should be equal to 'clsize'") + expect_error( + check_betas(betas, cluster_size), + "The number of rows in 'betas' should be equal to 'clsize'" + ) }) diff --git a/tests/testthat/test_utility_functions.R b/tests/testthat/test_utility_functions.R index 09aa4a7..bc89460 100644 --- a/tests/testthat/test_utility_functions.R +++ b/tests/testthat/test_utility_functions.R @@ -3,39 +3,42 @@ test_that("rnorta", { sample_size <- 100 latent_correlation_matrix <- toeplitz(c(1, rep(0.8, 2))) common_marginals <- rep("qlogis", 3) - sim_logistic <- rnorta(R = sample_size, - cor.matrix = latent_correlation_matrix, - distr = common_marginals) + sim_logistic <- rnorta( + R = sample_size, cor.matrix = latent_correlation_matrix, + distr = common_marginals + ) set.seed(1) - sim_normal <- rsmvnorm(R = sample_size, - cor.matrix = latent_correlation_matrix) + sim_normal <- rsmvnorm( + R = sample_size, cor.matrix = latent_correlation_matrix + ) raw_code <- qlogis(pnorm(sim_normal)) expect_equal(sim_logistic, raw_code) - }) +}) test_that("rsmvnorm", { set.seed(1) sample_size <- 100 correlation_matrix <- toeplitz(c(1, 0.4)) - sim_bivariate_normal <- rsmvnorm(R = sample_size, - cor.matrix = correlation_matrix) + sim_bivariate_normal <- rsmvnorm( + R = sample_size, cor.matrix = correlation_matrix + ) set.seed(1) p <- ncol(correlation_matrix) raw_code <- matrix(rnorm(sample_size * p), sample_size, p) %*% chol(correlation_matrix) expect_equal(sim_bivariate_normal, raw_code) - }) +}) test_that("rnorta sample size", { sample_size <- 0 expect_true(all.equal(sample_size, as.integer(sample_size)) != TRUE | - sample_size < 1) + sample_size < 1) sample_size <- 3.4 expect_true(all.equal(sample_size, as.integer(sample_size)) != TRUE | - sample_size < 1) + sample_size < 1) sample_size <- -3 expect_true(all.equal(sample_size, as.integer(sample_size)) != TRUE | - sample_size < 1) - }) + sample_size < 1) +})