Skip to content

Commit

Permalink
Automated build.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeksterslab committed Jan 11, 2025
1 parent 6a766ea commit f733c44
Show file tree
Hide file tree
Showing 116 changed files with 10,568 additions and 1,968 deletions.
Binary file modified .setup/build/cTMed.pdf
Binary file not shown.
Binary file modified .setup/build/cTMed_1.0.4.9000.tar.gz
Binary file not shown.
60 changes: 60 additions & 0 deletions .setup/cpp/cTMed-mc-phi-sigma.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// -----------------------------------------------------------------------------
// edit .setup/cpp/cTMed-mc-phi-sigma.cpp
// Ivan Jacob Agaloos Pesigan
// -----------------------------------------------------------------------------

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export(.MCPhiSigma)]]
Rcpp::List MCPhiSigma(const arma::vec& theta, const arma::mat& vcov_theta,
const arma::uword& R, bool test_phi = true) {
Rcpp::List output(R);
arma::uword n = theta.n_elem;
arma::uword p = (-1 + std::sqrt(1 + 24 * n)) / 6;
arma::uword q = (p * (p + 1)) / 2;
arma::uword index = 0;
arma::vec v_i(n, arma::fill::none);
arma::mat phi_i(p, p, arma::fill::none);
arma::vec phi_vec_i(p * p, arma::fill::none);
arma::mat sigma_i(p, p, arma::fill::none);
arma::vec sigma_vech_i(q, arma::fill::none);
arma::vec eigval;
arma::mat eigvec;
for (arma::uword i = 0; i < R; i++) {
bool run = true;
Rcpp::List output_i(2);
while (run) {
// generate data
v_i = arma::mvnrnd(theta, vcov_theta);
phi_vec_i = v_i(arma::span(0, (p * p) - 1));
sigma_vech_i = v_i(arma::span(p * p, n - 1));
// test phi
phi_i = arma::reshape(phi_vec_i, p, p);
if (test_phi) {
if (TestPhi(phi_i)) {
run = false;
}
} else {
run = false;
}
if (run == false) {
// test sigma
index = 0;
for (arma::uword i = 0; i < p; ++i) {
for (arma::uword j = i; j < p; ++j) {
sigma_i(i, j) = sigma_vech_i(index);
sigma_i(j, i) = sigma_vech_i(index);
index++;
}
}
arma::eig_sym(eigval, eigvec, sigma_i);
eigval.transform([](double val) { return std::max(val, 1e-8); });
sigma_i = eigvec * arma::diagmat(eigval) * eigvec.t();
}
}
output_i[0] = phi_i;
output_i[1] = sigma_i;
output[i] = output_i;
}
return output;
}
11 changes: 0 additions & 11 deletions .setup/latex/bib/quarto.bib

This file was deleted.

5 changes: 5 additions & 0 deletions .setup/latex/pdf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
*/
!*.pdf
!.gitignore
!bib.bib
5,635 changes: 5,635 additions & 0 deletions .setup/latex/pdf/bib.bib

Large diffs are not rendered by default.

Binary file added .setup/latex/pdf/cTMed-001-description.pdf
Binary file not shown.
Binary file added .setup/latex/pdf/cTMed-999-session.pdf
Binary file not shown.
Binary file added .setup/latex/pdf/cTMed-zzz-references.pdf
Binary file not shown.
Binary file added .setup/latex/pdf/cTMed-zzz-tests-benchmark.pdf
Binary file not shown.
Binary file added .setup/latex/pdf/cTMed-zzz-tests-external.pdf
Binary file not shown.
Binary file added .setup/latex/pdf/cTMed-zzz-tests-internal.pdf
Binary file not shown.
Binary file added .setup/latex/pdf/cTMed-zzz-tests-staging.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export(MCIndirectCentral)
export(MCMed)
export(MCMedStd)
export(MCPhi)
export(MCPhiSigma)
export(MCTotalCentral)
export(Med)
export(MedStd)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Patch

* Latest development version.
* Added the `MCPhiSigma()` function.
* Minor edits to tests.

# cTMed 1.0.4

Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
.Call(`_cTMed_MCPhiSigmaI`, theta, vcov_theta, test_phi)
}

.MCPhiSigma <- function(theta, vcov_theta, R, test_phi = TRUE) {
.Call(`_cTMed_MCPhiSigma`, theta, vcov_theta, R, test_phi)
}

.MCPhi <- function(phi, vcov_phi_vec_l, R, test_phi = TRUE) {
.Call(`_cTMed_MCPhi`, phi, vcov_phi_vec_l, R, test_phi)
}
Expand Down
5 changes: 1 addition & 4 deletions R/cTMed-mc-med-std.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,9 @@
#'
#' @author Ivan Jacob Agaloos Pesigan
#'
#' @param vcov_theta Numeric matrix.
#' The sampling variance-covariance matrix of
#' \eqn{\mathrm{vec} \left( \boldsymbol{\Phi} \right)} and
#' \eqn{\mathrm{vech} \left( \boldsymbol{\Sigma} \right)}
#' @inheritParams IndirectStd
#' @inheritParams MCMed
#' @inheritParams MCPhiSigma
#' @inherit IndirectStd references
#'
#' @return Returns an object
Expand Down
214 changes: 214 additions & 0 deletions R/cTMed-mc-phi-sigma.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
#' Generate Random Drift Matrices
#' and Process Noise Covariance Matrices
#' Using the Monte Carlo Method
#'
#' This function generates random
#' drift matrices \eqn{\boldsymbol{\Phi}}
#' and process noise covariabces matrices \eqn{\boldsymbol{\Sigma}}
#' using the Monte Carlo method.
#'
#' @details
#' ## Monte Carlo Method
#' Let \eqn{\boldsymbol{\theta}} be
#' a vector that combines
#' \eqn{\mathrm{vec} \left( \boldsymbol{\Phi} \right)},
#' that is,
#' the elements of the \eqn{\boldsymbol{\Phi}} matrix
#' in vector form sorted column-wise and
#' \eqn{\mathrm{vech} \left( \boldsymbol{\Sigma} \right)},
#' that is,
#' the unique elements of the \eqn{\boldsymbol{\Sigma}} matrix
#' in vector form sorted column-wise.
#' Let \eqn{\hat{\boldsymbol{\theta}}} be
#' a vector that combines
#' \eqn{\mathrm{vec} \left( \hat{\boldsymbol{\Phi}} \right)} and
#' \eqn{\mathrm{vech} \left( \hat{\boldsymbol{\Sigma}} \right)}.
#' Based on the asymptotic properties of maximum likelihood estimators,
#' we can assume that estimators are normally distributed
#' around the population parameters.
#' \deqn{
#' \hat{\boldsymbol{\theta}}
#' \sim
#' \mathcal{N}
#' \left(
#' \boldsymbol{\theta},
#' \mathbb{V} \left( \hat{\boldsymbol{\theta}} \right)
#' \right)
#' }
#' Using this distributional assumption,
#' a sampling distribution of \eqn{\hat{\boldsymbol{\theta}}}
#' which we refer to as \eqn{\hat{\boldsymbol{\theta}}^{\ast}}
#' can be generated by replacing the population parameters
#' with sample estimates,
#' that is,
#' \deqn{
#' \hat{\boldsymbol{\theta}}^{\ast}
#' \sim
#' \mathcal{N}
#' \left(
#' \hat{\boldsymbol{\theta}},
#' \hat{\mathbb{V}} \left( \hat{\boldsymbol{\theta}} \right)
#' \right) .
#' }
#'
#' @author Ivan Jacob Agaloos Pesigan
#'
#' @inheritParams IndirectStd
#' @inheritParams MCPhi
#' @param vcov_theta Numeric matrix.
#' The sampling variance-covariance matrix of
#' \eqn{\mathrm{vec} \left( \boldsymbol{\Phi} \right)} and
#' \eqn{\mathrm{vech} \left( \boldsymbol{\Sigma} \right)}
#'
#' @return Returns an object
#' of class `ctmedmc` which is a list with the following elements:
#' \describe{
#' \item{call}{Function call.}
#' \item{args}{Function arguments.}
#' \item{fun}{Function used ("MCPhiSigma").}
#' \item{output}{A list simulated drift matrices.}
#' }
#'
#' @examples
#' set.seed(42)
#' phi <- matrix(
#' data = c(
#' -0.357, 0.771, -0.450,
#' 0.0, -0.511, 0.729,
#' 0, 0, -0.693
#' ),
#' nrow = 3
#' )
#' colnames(phi) <- rownames(phi) <- c("x", "m", "y")
#' sigma <- matrix(
#' data = c(
#' 0.24455556, 0.02201587, -0.05004762,
#' 0.02201587, 0.07067800, 0.01539456,
#' -0.05004762, 0.01539456, 0.07553061
#' ),
#' nrow = 3
#' )
#' MCPhiSigma(
#' phi = phi,
#' sigma = sigma,
#' vcov_theta = 0.1 * diag(15),
#' R = 100L # use a large value for R in actual research
#' )
#'
#' @family Continuous Time Mediation Functions
#' @keywords cTMed mc
#' @export
MCPhiSigma <- function(phi,
sigma,
vcov_theta,
R,
test_phi = TRUE,
ncores = NULL,
seed = NULL) {
idx <- rownames(phi)
stopifnot(
idx == colnames(phi)
)
args <- list(
phi = phi,
sigma = sigma,
vcov_theta = vcov_theta,
R = R,
test_phi = test_phi,
ncores = ncores,
seed = seed,
method = "mc"
)
# nocov start
par <- FALSE
if (!is.null(ncores)) {
ncores <- as.integer(ncores)
if (ncores > R) {
ncores <- R
}
if (ncores > 1) {
par <- TRUE
}
}
if (par) {
os_type <- Sys.info()["sysname"]
if (os_type == "Darwin") {
fork <- TRUE
} else if (os_type == "Linux") {
fork <- TRUE
} else {
fork <- FALSE
}
if (fork) {
if (!is.null(seed)) {
set.seed(seed)
}
output <- parallel::mclapply(
X = seq_len(R),
FUN = function(i) {
return(
.MCPhiSigmaI(
theta = c(.Vec(phi), .Vech(sigma)),
vcov_theta = vcov_theta,
test_phi = test_phi
)
)
},
mc.cores = ncores
)
} else {
cl <- parallel::makeCluster(ncores)
on.exit(
parallel::stopCluster(cl = cl)
)
if (!is.null(seed)) {
parallel::clusterSetRNGStream(
cl = cl,
iseed = seed
)
}
output <- parallel::parLapply(
cl = cl,
X = seq_len(R),
fun = function(i) {
return(
.MCPhiSigmaI(
theta = c(.Vec(phi), .Vech(sigma)),
vcov_theta = vcov_theta,
test_phi = test_phi
)
)
}
)
}
# nocov end
} else {
if (!is.null(seed)) {
set.seed(seed)
}
output <- .MCPhiSigma(
theta = c(.Vec(phi), .Vech(sigma)),
vcov_theta = vcov_theta,
R = R,
test_phi = test_phi
)
}
output <- lapply(
X = output,
FUN = function(x) {
colnames(x[[1]]) <- rownames(x[[1]]) <- idx
return(x)
}
)
out <- list(
call = match.call(),
args = args,
fun = "MCPhiSigma",
output = output
)
class(out) <- c(
"ctmedmcphi",
class(out)
)
return(out)
}
32 changes: 26 additions & 6 deletions R/cTMed-methods-ctmedmcphi.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,31 @@
print.ctmedmcphi <- function(x,
digits = 4,
...) {
base::print(
lapply(
X = x$output,
FUN = round,
digits = digits
if (x$fun == "MCPhi") {
base::print(
lapply(
X = x$output,
FUN = round,
digits = digits
)
)
)
}
if (x$fun == "MCPhiSigma") {
base::print(
lapply(
X = x$output,
FUN = function(x) {
x[[1]] <- round(
x[[1]],
digits = digits
)
x[[2]] <- round(
x[[2]],
digits = digits
)
return(x)
}
)
)
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cTMed
================
Ivan Jacob Agaloos Pesigan
2025-01-09
2025-01-11

<!-- README.md is generated from README.Rmd. Please edit that file -->

Expand Down
1 change: 1 addition & 0 deletions man/BootBeta.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/BootBetaStd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f733c44

Please sign in to comment.