Skip to content

Commit

Permalink
cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnestisTouloumis committed Jul 24, 2019
1 parent ff3b7a5 commit 8f68085
Show file tree
Hide file tree
Showing 15 changed files with 849 additions and 621 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.8
Version: 1.6.9
Depends: R(>= 2.15.0)
Imports:
evd,
Expand Down
532 changes: 301 additions & 231 deletions R/SimCorMultRes_internals.R

Large diffs are not rendered by default.

83 changes: 48 additions & 35 deletions R/rbin.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
46 changes: 26 additions & 20 deletions R/rmult.acl.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
64 changes: 34 additions & 30 deletions R/rmult.bcl.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
71 changes: 42 additions & 29 deletions R/rmult.clm.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
69 changes: 36 additions & 33 deletions R/rmult.crm.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Loading

0 comments on commit 8f68085

Please sign in to comment.