Skip to content

Commit

Permalink
improved R code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
AnestisTouloumis committed Jul 22, 2019
1 parent bf9589a commit a7567b6
Show file tree
Hide file tree
Showing 22 changed files with 455 additions and 1,504 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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.6
Version: 1.6.7
Depends: R(>= 2.15.0)
Imports:
evd,
Expand Down
274 changes: 151 additions & 123 deletions R/SimCorMultRes_internals.R

Large diffs are not rendered by default.

51 changes: 28 additions & 23 deletions R/rbin.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,52 +94,57 @@
#' under Marginal Model Specification: The SimCorMultRes Package. \emph{The R
#' Journal} \bold{8}, 79--91.
#' @examples
#' ## See Example 3.4 in the Vignette.
#' ## See Example 3.5 in the Vignette.
#' set.seed(123)
#' sample_size <- 5000
#' cluster_size <- 4
#' beta_intercepts <- 0
#' 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_responses <- rbin(clsize = cluster_size,
#' 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_responses$simdata)
#' data = simulated_binary_dataset$simdata)
#' summary(binary_gee_model)$coefficients
#'
#' ## See Example 3.5 in the Vignette.
#' ## See Example 3.6 in the Vignette.
#' set.seed(8)
#' library(evd)
#' rlatent1 <- rmvevd(sample_size, dep = sqrt(1 - 0.9), model = 'log',
#' d = cluster_size)
#' rlatent2 <- rmvevd(sample_size, dep = sqrt(1 - 0.9), model = 'log',
#' d = cluster_size)
#' simulated_latent_variables <- rlatent1 - rlatent2
#' simulated_binary_responses <- rbin(clsize = 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_responses$simdata)
#' 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)
intercepts <- check_intercepts(intercepts, clsize, "rbin")
betas <- check_betas(betas, clsize)
lpformula <- check_xformula(xformula)
if (!is.environment(xdata))
xdata <- data.frame(na.omit(xdata))
lin_pred <- create_linear_predictor(betas, clsize, lpformula, xdata,
"rbin")
sample_size <- nrow(lin_pred) # nolint
rlatent <- create_rlatent(rlatent, sample_size, link, clsize, cor.matrix,
"rbin")
y_sim <- apply_threshold(lin_pred, rlatent, clsize, "rbin", intercepts)
create_output(y_sim, sample_size, clsize, rlatent, lpformula, xdata, "rbin")
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")
}
17 changes: 9 additions & 8 deletions R/rmult.acl.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@
#' equicorrelation_matrix <- toeplitz(c(1, rep(0.95, cluster_size - 1)))
#' latent_correlation_matrix <- kronecker(equicorrelation_matrix,
#' identity_matrix)
#' simulated_ordinal_reponses <- rmult.acl(clsize = cluster_size,
#' 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_reponses$simdata, id = id, repeated = time,
#' data = simulated_ordinal_dataset$simdata, id = id, repeated = time,
#' LORstr = 'time.exch', link='acl')
#' round(coef(ordinal_gee_model), 2)
#'
Expand All @@ -122,10 +122,11 @@ rmult.acl <- function(clsize = clsize, intercepts = intercepts, betas = betas, #
xformula = formula(xdata), xdata = parent.frame(), cor.matrix = cor.matrix, # nolint
rlatent = NULL) {
check_cluster_size(clsize)
intercepts <- check_intercepts(intercepts, clsize, "rmult.acl")
ncategories <- ncol(intercepts) + 1
betas <- check_betas(betas, clsize)
betas_bcl <- create_betas_acl2bcl(intercepts, ncategories, betas)
rmult.bcl(clsize, ncategories, betas_bcl, xformula, xdata, cor.matrix,
rlatent)
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)
}
38 changes: 21 additions & 17 deletions R/rmult.bcl.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@
#' 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_responses <- rmult.bcl(clsize = cluster_size,
#' 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_responses$simdata, id = id, repeated = time,
#' data = simulated_nominal_dataset$simdata, id = id, repeated = time,
#' LORstr = 'time.exch')
#' round(coef(nominal_gee_model), 2)
#'
Expand All @@ -118,20 +118,24 @@ rmult.bcl <- function(clsize = clsize, ncategories = ncategories, betas = betas,
xformula = formula(xdata), xdata = parent.frame(), cor.matrix = cor.matrix, # nolint
rlatent = NULL) {
check_cluster_size(clsize)
ncategories <- check_ncategories(ncategories)
categories_no <- check_ncategories(ncategories)
betas <- check_betas(betas, clsize)
lpformula <- check_xformula(xformula)
if (!is.environment(xdata))
xdata <- data.frame(na.omit(xdata))
lin_pred <- create_linear_predictor(betas, clsize, lpformula, xdata,
"rmult.bcl", ncategories = ncategories)
sample_size <- nrow(lin_pred)
rlatent <- create_rlatent(rlatent, sample_size, "cloglog", clsize,
cor.matrix, "rmult.bcl",
ncategories = ncategories)
y_sim <- apply_threshold(lin_pred, rlatent, clsize, "rmult.bcl",
ncategories = ncategories)
lpformula <- update(lpformula, ~. - 1)
create_output(y_sim, sample_size, clsize, rlatent, lpformula, xdata,
"rmult.bcl", ncategories = ncategories)
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)
}
36 changes: 20 additions & 16 deletions R/rmult.clm.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,39 +104,43 @@
#' 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_responses <- rmult.clm(clsize = cluster_size,
#' 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_responses$simdata, n = 8)
#' head(simulated_ordinal_dataset$simdata, n = 8)
#'
#' ## Same sampling scheme except that the parameter vector is time-stationary.
#' set.seed(12345)
#' simulated_ordinal_responses <- rmult.clm(clsize = cluster_size, betas = 1,
#' 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_responses$simdata)
#' 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)
intercepts <- check_intercepts(intercepts, clsize, "rmult.clm")
betas <- check_betas(betas, clsize)
lpformula <- check_xformula(xformula)
if (!is.environment(xdata))
xdata <- data.frame(na.omit(xdata))
lin_pred <- create_linear_predictor(betas, clsize, lpformula, xdata,
"rmult.clm")
sample_size <- nrow(lin_pred)
rlatent <- create_rlatent(rlatent, sample_size, link, clsize, cor.matrix,
"rmult.clm")
y_sim <- apply_threshold(lin_pred, rlatent, clsize, "rmult.clm", intercepts)
create_output(y_sim, sample_size, clsize, rlatent, lpformula, xdata,
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")
}
48 changes: 27 additions & 21 deletions R/rmult.crm.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,37 @@
#' ones_matrix <- matrix(1, cluster_size, cluster_size)
#' latent_correlation_matrix <- identity_matrix +
#' kronecker(equicorrelation_matrix, ones_matrix)
#' CorOrdRes <- rmult.crm(clsize = cluster_size, intercepts = beta_intercepts,
#' betas = beta_coefficients, xformula = ~ x,
#' cor.matrix = latent_correlation_matrix, link = 'probit')
#' head(CorOrdRes$Ysim)
#' 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
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)
betas <- check_betas(betas, clsize)
lpformula <- check_xformula(xformula)
if (!is.environment(xdata))
xdata <- data.frame(na.omit(xdata))
lin_pred <- create_linear_predictor(betas, clsize, lpformula, xdata,
"rmult.clm")
sample_size <- nrow(lin_pred)
intercepts <- check_intercepts(intercepts, clsize, "rmult.crm", sample_size)
ncategories <- ncol(intercepts) / clsize + 1
lin_pred_extended <- t(apply(lin_pred, 1,
function(x) rep(x, each = ncategories - 1)))
rlatent <- create_rlatent(rlatent, sample_size, link, clsize, cor.matrix,
"rmult.crm", ncategories)
y_sim <- apply_threshold(lin_pred_extended, rlatent, clsize, "rmult.crm",
intercepts, ncategories)
create_output(y_sim, sample_size, clsize, rlatent, lpformula, xdata,
"rmult.crm", ncategories)
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)
}
14 changes: 7 additions & 7 deletions R/rnorta.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ 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")
quantilefunctions <- as.character(distr)
quantile_functions <- as.character(distr)
ans <- rsmvnorm(R = R, cor.matrix = cor.matrix)
if (length(quantilefunctions) != ncol(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)) {
Expand All @@ -99,14 +99,14 @@ rnorta <- function(R = R, cor.matrix = cor.matrix, distr = distr, # nolint
}
ans <- pnorm(ans)
for (i in seq_len(ncol(cor.matrix))) {
quantilefunction <- get(quantilefunctions[i], mode = "function")
if (!is.function(quantilefunction))
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(quantilefunction)[pmatch(names(qparameters[[i]]),
formalArgs(quantilefunction))] <- qparameters[[i]]
ans[, i] <- quantilefunction(ans[, i])
formals(quantile_function)[pmatch(names(qparameters[[i]]),
formalArgs(quantile_function))] <- qparameters[[i]]
ans[, i] <- quantile_function(ans[, i])
}
ans
}
17 changes: 9 additions & 8 deletions R/rsmvnorm.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ rsmvnorm <- function(R = R, cor.matrix = cor.matrix) { # nolint
stop("'R' must be a positive integer")
if (!is.numeric(cor.matrix))
stop("'cor.matrix' must be numeric")
cor.matrix <- as.matrix(cor.matrix) # nolint
if (!isSymmetric(cor.matrix))
correlation_matrix <- as.matrix(cor.matrix) # nolint
if (!isSymmetric(correlation_matrix))
stop("'cor.matrix' must be a symmetric matrix")
if (any(diag(cor.matrix) != 1))
if (any(diag(correlation_matrix) != 1))
stop("the diagonal elements of 'cor.matrix' must be equal to one")
if (any(cor.matrix > 1) | any(cor.matrix < -1))
if (any(correlation_matrix > 1) | any(correlation_matrix < -1))
stop("all the elements of 'cor.matrix' must be on [-1,1]")
if (any(eigen(cor.matrix, symmetric = TRUE, only.values = TRUE)$values <=
0))
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(cor.matrix)
ans <- matrix(rnorm(R * p), R, p) %*% chol(cor.matrix)
p <- ncol(correlation_matrix)
ans <- matrix(rnorm(R * p), R, p) %*% chol(correlation_matrix)
ans
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SimCorMultRes: Simulates Correlated Multinomial Responses

[![Github
version](https://img.shields.io/badge/GitHub%20-1.6.6-orange.svg)](%22commits/master%22)
version](https://img.shields.io/badge/GitHub%20-1.6.7-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
Expand Down
4 changes: 4 additions & 0 deletions inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 1.6.7 [2019-07-22]
* Improved R code readability.
* Added unit tests for internal functions.

Version 1.6.6 [2019-07-09]
* Minor fixed to comply with lintr.

Expand Down
Loading

3 comments on commit a7567b6

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/testthat/test_internal_functions.R:18:54: style: Do not place spaces around code in parentheses or square brackets.

​  expect_equal(create_distribution("logit"), "qlogis" )
                                                     ^

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/testthat/test_internal_functions.R:18:54: style: Do not place spaces around code in parentheses or square brackets.

​  expect_equal(create_distribution("logit"), "qlogis" )
                                                     ^

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/testthat/test_internal_functions.R:18:54: style: Do not place spaces around code in parentheses or square brackets.

​  expect_equal(create_distribution("logit"), "qlogis" )
                                                     ^

Please sign in to comment.