-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anestis Touloumis
committed
Jul 22, 2019
1 parent
e16cac5
commit bf9589a
Showing
4 changed files
with
1,345 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,167 +1,194 @@ | ||
test_that("rbin constant intercepts", { | ||
test_that("rbin constant beta_intercepts", { | ||
set.seed(1) | ||
n <- 10 | ||
clsize <- 4 | ||
intercepts <- 0 | ||
betas <- 0.2 | ||
cor_matrix <- toeplitz(c(1, 0.9, 0.9, 0.9)) | ||
x <- rep(rnorm(n), each = clsize) | ||
corbinres <- rbin(clsize = clsize, intercepts = intercepts, betas = betas, | ||
xformula = ~x, cor.matrix = cor_matrix, link = "probit") | ||
y_sim <- as.numeric(c(t(corbinres$rlatent)) <= intercepts + betas * x) | ||
expect_equal(c(t(corbinres$Ysim)), y_sim) | ||
sample_size <- 10 | ||
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, intercepts = beta_intercepts, | ||
betas = beta_coefficients, xformula = ~ x, | ||
cor.matrix = latent_correlation_matrix, link = "probit") | ||
y_sim <- | ||
as.numeric(c(t(simulated_binary_responses$rlatent)) <= | ||
beta_intercepts + beta_coefficients * x) | ||
expect_equal(c(t(simulated_binary_responses$Ysim)), y_sim) | ||
}) | ||
|
||
test_that("rbin varying intercepts", { | ||
test_that("rbin varying beta_intercepts", { | ||
set.seed(1) | ||
n <- 10 | ||
clsize <- 4 | ||
intercepts <- c(0, 0.1, 0.2, 0) | ||
betas <- 0.2 | ||
cor_matrix <- toeplitz(c(1, 0.9, 0.9, 0.9)) | ||
x <- rep(rnorm(n), each = clsize) | ||
corbinres <- rbin(clsize = clsize, intercepts = intercepts, betas = betas, | ||
xformula = ~x, cor.matrix = cor_matrix, link = "probit") | ||
intercepts <- rep(intercepts, n) | ||
y_sim <- as.numeric(c(t(corbinres$rlatent)) <= intercepts + betas * x) | ||
expect_equal(c(t(corbinres$Ysim)), y_sim) | ||
sample_size <- 10 | ||
cluster_size <- 4 | ||
beta_intercepts <- c(0, 0.1, 0.2, 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, intercepts = beta_intercepts, | ||
betas = beta_coefficients, xformula = ~ x, | ||
cor.matrix = latent_correlation_matrix, link = "probit") | ||
beta_intercepts <- rep(beta_intercepts, sample_size) | ||
y_sim <- | ||
as.numeric(c(t(simulated_binary_responses$rlatent)) <= | ||
beta_intercepts + beta_coefficients * x) | ||
expect_equal(c(t(simulated_binary_responses$Ysim)), y_sim) | ||
}) | ||
|
||
|
||
test_that("rmult.bcl constant betas", { | ||
betas <- c(1, 3, 2, 1.25, 3.25, 1.75, 0.75, 2.75, 2.25, 0, 0, 0) | ||
n <- 10 | ||
ncategories <- 4 | ||
clsize <- 3 | ||
test_that("rmult.bcl constant beta_coefficients", { | ||
beta_coefficients <- c(1, 3, 2, 1.25, 3.25, 1.75, 0.75, 2.75, 2.25, 0, 0, 0) | ||
sample_size <- 10 | ||
categories_no <- 4 | ||
cluster_size <- 3 | ||
set.seed(1) | ||
x1 <- rep(rnorm(n), each = clsize) | ||
x2 <- rnorm(n * clsize) | ||
x1 <- rep(rnorm(sample_size), each = cluster_size) | ||
x2 <- rnorm(sample_size * cluster_size) | ||
xdata <- data.frame(x1, x2) | ||
cor_matrix <- kronecker(toeplitz(c(1, rep(0.95, clsize - 1))), | ||
diag(ncategories)) | ||
cornomres <- rmult.bcl(clsize = clsize, ncategories = ncategories, | ||
betas = betas, xformula = ~x1 + x2, xdata = xdata, | ||
cor.matrix = cor_matrix) | ||
latent_correlation_matrix <- kronecker( | ||
toeplitz(c(1, rep(0.95, cluster_size - 1))), diag(categories_no) | ||
) | ||
simulated_nominal_responses <- | ||
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 = ncategories)) | ||
lin_pred <- matrix(betas, nrow = nrow(xmat), ncol = ncol(xmat), | ||
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 <- rowSums(lin_pred) + c(t(cornomres$rlatent)) | ||
lin_pred <- matrix(lin_pred, n * clsize, ncategories, TRUE) | ||
lin_pred <- rowSums(lin_pred) + c(t(simulated_nominal_responses$rlatent)) | ||
lin_pred <- matrix(lin_pred, sample_size * cluster_size, categories_no, TRUE) | ||
y_sim <- apply(lin_pred, 1, which.max) | ||
expect_equal(c(t(cornomres$Ysim)), y_sim) | ||
expect_equal(c(t(simulated_nominal_responses$Ysim)), y_sim) | ||
}) | ||
|
||
|
||
test_that("rmult.bcl varying betas", { | ||
n <- 10 | ||
ncategories <- 4 | ||
clsize <- 3 | ||
betas <- matrix(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 = clsize, byrow = TRUE) | ||
test_that("rmult.bcl varying beta_coefficients", { | ||
sample_size <- 10 | ||
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, | ||
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) | ||
set.seed(1) | ||
x1 <- rep(rnorm(n), each = clsize) | ||
x2 <- rnorm(n * clsize) | ||
x1 <- rep(rnorm(sample_size), each = cluster_size) | ||
x2 <- rnorm(sample_size * cluster_size) | ||
xdata <- data.frame(x1, x2) | ||
cor_matrix <- kronecker(toeplitz(c(1, rep(0.95, clsize - 1))), | ||
diag(ncategories)) | ||
cornomres <- rmult.bcl(clsize = clsize, ncategories = ncategories, | ||
betas = betas, xformula = ~x1 + x2, xdata = xdata, | ||
cor.matrix = cor_matrix) | ||
latent_correlation_matrix <- kronecker( | ||
toeplitz(c(1, rep(0.95, cluster_size - 1))), diag(categories_no)) | ||
simulated_nominal_responses <- 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 = ncategories)) | ||
lin_pred <- matrix(c(t(betas)), nrow = nrow(xmat), ncol = ncol(xmat), | ||
byrow = TRUE) * xmat | ||
lin_pred <- rowSums(lin_pred) + c(t(cornomres$rlatent)) | ||
lin_pred <- matrix(lin_pred, n * clsize, ncategories, TRUE) | ||
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 <- rowSums(lin_pred) + c(t(simulated_nominal_responses$rlatent)) | ||
lin_pred <- matrix(lin_pred, sample_size * cluster_size, categories_no, TRUE) | ||
y_sim <- apply(lin_pred, 1, which.max) | ||
expect_equal(c(t(cornomres$Ysim)), y_sim) | ||
expect_equal(c(t(simulated_nominal_responses$Ysim)), y_sim) | ||
}) | ||
|
||
|
||
|
||
test_that("rmult.clm varying betas", { | ||
test_that("rmult.clm varying beta_coefficients", { | ||
set.seed(1) | ||
n <- 10 | ||
clsize <- 4 | ||
intercepts <- c(-1.5, -0.5, 0.5, 1.5) | ||
betas <- matrix(c(1, 2, 3, 4), 4, 1) | ||
x <- rep(rnorm(n), each = clsize) | ||
cor_matrix <- toeplitz(c(1, 0.85, 0.5, 0.15)) | ||
corordres <- rmult.clm(clsize = clsize, intercepts = intercepts, | ||
betas = betas, xformula = ~x, cor.matrix = cor_matrix, | ||
link = "probit") | ||
u_sim <- c(t(corordres$rlatent)) - c(betas) * x | ||
intercepts <- c(-Inf, intercepts, Inf) | ||
y_sim <- cut(u_sim, intercepts, labels = FALSE) | ||
expect_equal(c(t(corordres$Ysim)), y_sim) | ||
sample_size <- 10 | ||
cluster_size <- 4 | ||
beta_intercepts <- c(-1.5, -0.5, 0.5, 1.5) | ||
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, intercepts = beta_intercepts, | ||
betas = beta_coefficients, xformula = ~ x, | ||
cor.matrix = latent_correlation_matrix, link = "probit") | ||
u_sim <- c(t(simulated_ordinal_responses$rlatent)) - c(beta_coefficients) * x | ||
beta_intercepts <- c(-Inf, beta_intercepts, Inf) | ||
y_sim <- cut(u_sim, beta_intercepts, labels = FALSE) | ||
expect_equal(c(t(simulated_ordinal_responses$Ysim)), y_sim) | ||
}) | ||
|
||
|
||
|
||
test_that("rmult.clm constant betas", { | ||
test_that("rmult.clm constant beta_coefficients", { | ||
set.seed(1) | ||
n <- 10 | ||
clsize <- 4 | ||
intercepts <- c(-1.5, -0.5, 0.5, 1.5) | ||
betas <- 1 | ||
x <- rep(rnorm(n), each = clsize) | ||
cor_matrix <- toeplitz(c(1, 0.85, 0.5, 0.15)) | ||
corordres <- rmult.clm(clsize = clsize, intercepts = intercepts, | ||
betas = betas, xformula = ~x, cor.matrix = cor_matrix, | ||
link = "probit") | ||
u_sim <- c(t(corordres$rlatent)) - c(betas) * x | ||
intercepts <- c(-Inf, intercepts, Inf) | ||
y_sim <- cut(u_sim, intercepts, labels = FALSE) | ||
expect_equal(c(t(corordres$Ysim)), y_sim) | ||
sample_size <- 10 | ||
cluster_size <- 4 | ||
beta_intercepts <- c(-1.5, -0.5, 0.5, 1.5) | ||
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_responses <- | ||
rmult.clm(clsize = cluster_size, intercepts = beta_intercepts, | ||
betas = beta_coefficients, xformula = ~ x, | ||
cor.matrix = latent_correlation_matrix, link = "probit") | ||
u_sim <- c(t(simulated_ordinal_responses$rlatent)) - c(beta_coefficients) * x | ||
beta_intercepts <- c(-Inf, beta_intercepts, Inf) | ||
y_sim <- cut(u_sim, beta_intercepts, labels = FALSE) | ||
expect_equal(c(t(simulated_ordinal_responses$Ysim)), y_sim) | ||
}) | ||
|
||
|
||
test_that("rmult.acl constant betas", { | ||
intercepts <- c(3, 1, 2) | ||
betas <- c(1, 1) | ||
n <- 10 | ||
clsize <- 3 | ||
test_that("rmult.acl constant beta_coefficients", { | ||
beta_intercepts <- c(3, 1, 2) | ||
beta_coefficients <- c(1, 1) | ||
sample_size <- 10 | ||
cluster_size <- 3 | ||
set.seed(321) | ||
x1 <- rep(rnorm(n), each = clsize) | ||
x2 <- rnorm(n * clsize) | ||
x1 <- rep(rnorm(sample_size), each = cluster_size) | ||
x2 <- rnorm(sample_size * cluster_size) | ||
xdata <- data.frame(x1, x2) | ||
set.seed(1) | ||
cor_matrix <- kronecker(toeplitz(c(1, rep(0.95, clsize - 1))), diag(4)) | ||
corordres <- rmult.acl(clsize = clsize, intercepts = intercepts, | ||
betas = betas, xformula = ~ x1 + x2, xdata = xdata, | ||
cor.matrix = cor_matrix) | ||
intercepts <- rev(cumsum(rev(c(intercepts, 0)))) | ||
betas_bcl <- c(intercepts[1], 3, 3, intercepts[2], 2, 2, | ||
intercepts[3], 1, 1, intercepts[4], 0, 0) | ||
latent_correlation_matrix <- | ||
kronecker(toeplitz(c(1, rep(0.95, cluster_size - 1))), diag(4)) | ||
simulated_ordinal_responses <- | ||
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) | ||
set.seed(1) | ||
cornomres <- rmult.bcl(clsize = clsize, ncategories = 4, betas = betas_bcl, | ||
xformula = ~x1 + x2, xdata = xdata, | ||
cor.matrix = cor_matrix) | ||
expect_equal(c(t(corordres$Ysim)), c(t(cornomres$Ysim))) | ||
simulated_nominal_responses <- | ||
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_responses$Ysim)), | ||
c(t(simulated_nominal_responses$Ysim))) | ||
}) | ||
|
||
|
||
test_that("rmult.crm constant betas", { | ||
n <- 10 | ||
clsize <- 4 | ||
intercepts <- c(-1.5, -0.5, 0.5, 1.5) | ||
betas <- 1 | ||
x <- rnorm(n * clsize) | ||
ncategories <- 5 | ||
cor_matrix <- diag(1, (ncategories - 1) * clsize) + | ||
kronecker(toeplitz(c(0, rep(0.24, ncategories - 2))), | ||
matrix(1, clsize, clsize)) | ||
corordres <- rmult.crm(clsize = clsize, intercepts = intercepts, | ||
betas = betas, xformula = ~x, cor.matrix = cor_matrix, | ||
link = "probit") | ||
u_sim <- c(t(corordres$rlatent)) - rep(x, each = ncategories - 1) | ||
y_sim <- matrix(as.numeric(t(u_sim <= intercepts)), n * clsize, ncategories - | ||
1, TRUE) | ||
for (i in 1:(ncategories - 1)) y_sim[, i] <- ifelse(y_sim[, i] == | ||
1, i, ncategories) | ||
test_that("rmult.crm constant beta_coefficients", { | ||
sample_size <- 10 | ||
cluster_size <- 4 | ||
beta_intercepts <- c(-1.5, -0.5, 0.5, 1.5) | ||
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_responses <- | ||
rmult.crm(clsize = cluster_size, intercepts = beta_intercepts, | ||
betas = beta_coefficients, xformula = ~ x, | ||
cor.matrix = latent_correlation_matrix, link = "probit") | ||
u_sim <- c(t(simulated_ordinal_responses$rlatent)) - | ||
rep(x, each = categories_no - 1) | ||
y_sim <- | ||
matrix(as.numeric(t(u_sim <= beta_intercepts)), sample_size * cluster_size, | ||
categories_no - 1, TRUE) | ||
for (i in 1:(categories_no - 1)) | ||
y_sim[, i] <- ifelse(y_sim[, i] == 1, i, categories_no) | ||
y_sim <- apply(y_sim, 1, min) | ||
expect_equal(c(t(corordres$Ysim)), y_sim) | ||
expect_equal(c(t(simulated_ordinal_responses$Ysim)), y_sim) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,43 @@ | ||
test_that("norta", { | ||
test_that("rnorta", { | ||
set.seed(1) | ||
sample_size <- 100 | ||
latentcorrelation <- toeplitz(c(1, rep(0.8, 2))) | ||
commonmarginals <- rep("qlogis", 3) | ||
simlogistic <- rnorta(R = sample_size, cor.matrix = latentcorrelation, | ||
distr = commonmarginals) | ||
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) | ||
set.seed(1) | ||
simnormal <- rsmvnorm(R = sample_size, cor.matrix = latentcorrelation) | ||
raw_code <- qlogis(pnorm(simnormal)) | ||
expect_equal(simlogistic, raw_code) | ||
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 # nolint | ||
cor_matrix <- toeplitz(c(1, 0.4)) # nolint | ||
simbivariatenormal <- rsmvnorm(R = sample_size, cor.matrix = cor_matrix) | ||
correlation_matrix <- toeplitz(c(1, 0.4)) # nolint | ||
sim_bivariate_normal <- rsmvnorm(R = sample_size, | ||
cor.matrix = correlation_matrix) | ||
set.seed(1) | ||
p <- ncol(cor_matrix) | ||
p <- ncol(correlation_matrix) | ||
raw_code <- matrix(rnorm(sample_size * p), sample_size, p) %*% | ||
chol(cor_matrix) | ||
expect_equal(simbivariatenormal, raw_code) | ||
chol(correlation_matrix) | ||
expect_equal(sim_bivariate_normal, raw_code) | ||
}) | ||
|
||
|
||
test_that("rnorta sample size", { | ||
R <- 0 | ||
expect_error(if (all.equal(R, as.integer(R)) != TRUE | R < 1) | ||
stop("'R' must be a positive integer")) | ||
R <- 3.4 | ||
expect_error( | ||
if (all.equal(R, as.integer(R)) != TRUE | R < 1) | ||
stop("'R' must be a positive integer")) | ||
R <- -3 | ||
expect_error( | ||
if (all.equal(R, as.integer(R)) != TRUE | R < 1) | ||
stop("'R' must be a positive integer")) | ||
}) |
Oops, something went wrong.
bf9589a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/testthat/test_utility_functions.R:32:3: style: Variable or function name should be snake_case.
tests/testthat/test_utility_functions.R:35:3: style: Variable or function name should be snake_case.
tests/testthat/test_utility_functions.R:39:3: style: Variable or function name should be snake_case.
bf9589a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/testthat/test_utility_functions.R:32:3: style: Variable or function name should be snake_case.
tests/testthat/test_utility_functions.R:35:3: style: Variable or function name should be snake_case.
tests/testthat/test_utility_functions.R:39:3: style: Variable or function name should be snake_case.
bf9589a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/testthat/test_utility_functions.R:32:3: style: Variable or function name should be snake_case.
tests/testthat/test_utility_functions.R:35:3: style: Variable or function name should be snake_case.
tests/testthat/test_utility_functions.R:39:3: style: Variable or function name should be snake_case.