From af3f0d5c755aaf4ced59c43106bb6c1d53e6520c Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Wed, 22 Apr 2026 17:51:29 +0200 Subject: [PATCH] Fix package to re-upload on CRAN. TODO: check tests/example, write inst/extdata/simulateMr.R --- .gitignore | 9 +-- .../LeastSquareMoments_Auder.et.al_2020.pdf | 0 .../Properties.of.GMM_Hansen_1982.pdf | 0 ...orDecompositions_Anandkumar.et.al_2014.pdf | 0 ...ensorFactorization_Kuleshov.et.al_2015.pdf | 0 .../TensorMethods_Anandkumar.et.al_2016.pdf | 0 pkg/R/multiRun.R | 7 +- pkg/R/optimParams.R | 2 +- pkg/R/plot.R | 65 +++++++++++-------- pkg/R/utils.R | 2 + .../inst/extdata}/FLXMRglm.R | 3 + {patch_Bettina => pkg/inst/extdata}/code.R | 0 pkg/inst/extdata/simulateMr.R | 22 +++++++ 13 files changed, 75 insertions(+), 35 deletions(-) rename {doc => doc_PDF}/LeastSquareMoments_Auder.et.al_2020.pdf (100%) rename {doc => doc_PDF}/Properties.of.GMM_Hansen_1982.pdf (100%) rename {doc => doc_PDF}/TensorDecompositions_Anandkumar.et.al_2014.pdf (100%) rename {doc => doc_PDF}/TensorFactorization_Kuleshov.et.al_2015.pdf (100%) rename {doc => doc_PDF}/TensorMethods_Anandkumar.et.al_2016.pdf (100%) rename {patch_Bettina => pkg/inst/extdata}/FLXMRglm.R (97%) rename {patch_Bettina => pkg/inst/extdata}/code.R (100%) create mode 100644 pkg/inst/extdata/simulateMr.R diff --git a/.gitignore b/.gitignore index 1499b88..68db326 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -/data/ /_unversioned/ #ignore roxygen2 generated files @@ -18,18 +17,16 @@ !/reports/*.sh /vignettes/cache/ /vignettes/figure/ -/vignettes/report.html -/vignettes/report.md +/vignettes/*.html /vignettes/*.zip -/vignettes/report_*/ #ignore R session files .Rhistory .RData #ignore R CMD build/check generated files -/*.Rcheck/ -/*.tar.gz +*.Rcheck/ +*.tar.gz #ignore generated object files *.[oa] diff --git a/doc/LeastSquareMoments_Auder.et.al_2020.pdf b/doc_PDF/LeastSquareMoments_Auder.et.al_2020.pdf similarity index 100% rename from doc/LeastSquareMoments_Auder.et.al_2020.pdf rename to doc_PDF/LeastSquareMoments_Auder.et.al_2020.pdf diff --git a/doc/Properties.of.GMM_Hansen_1982.pdf b/doc_PDF/Properties.of.GMM_Hansen_1982.pdf similarity index 100% rename from doc/Properties.of.GMM_Hansen_1982.pdf rename to doc_PDF/Properties.of.GMM_Hansen_1982.pdf diff --git a/doc/TensorDecompositions_Anandkumar.et.al_2014.pdf b/doc_PDF/TensorDecompositions_Anandkumar.et.al_2014.pdf similarity index 100% rename from doc/TensorDecompositions_Anandkumar.et.al_2014.pdf rename to doc_PDF/TensorDecompositions_Anandkumar.et.al_2014.pdf diff --git a/doc/TensorFactorization_Kuleshov.et.al_2015.pdf b/doc_PDF/TensorFactorization_Kuleshov.et.al_2015.pdf similarity index 100% rename from doc/TensorFactorization_Kuleshov.et.al_2015.pdf rename to doc_PDF/TensorFactorization_Kuleshov.et.al_2015.pdf diff --git a/doc/TensorMethods_Anandkumar.et.al_2016.pdf b/doc_PDF/TensorMethods_Anandkumar.et.al_2016.pdf similarity index 100% rename from doc/TensorMethods_Anandkumar.et.al_2016.pdf rename to doc_PDF/TensorMethods_Anandkumar.et.al_2016.pdf diff --git a/pkg/R/multiRun.R b/pkg/R/multiRun.R index 767f6f9..88207fd 100644 --- a/pkg/R/multiRun.R +++ b/pkg/R/multiRun.R @@ -21,9 +21,13 @@ #' @return A list of nf aggregates of N results (matrices). #' #' @examples -#' \dontrun{ +#' \donttest{ #' β <- matrix(c(1,-2,3,1),ncol=2) #' +#' # This example requires a patch for the flexmix package written by Bettina Grün: +#' patch_path <- system.file("extdata", "FLXMRglm.R", package = "morpheus") +#' source(patch_path) +#' #' # Bootstrap + computeMu, morpheus VS flexmix #' io <- generateSampleIO(n=1000, p=1/2, β=β, b=c(0,0), "logit") #' μ <- normalize(β) @@ -84,6 +88,7 @@ #' }, N=10, ncores=3) #' for (i in 1:2) #' res[[i]] <- alignMatrices(res[[i]], ref=β, ls_mode="exact")} +#' #' @export multiRun <- function(fargs, estimParams, packages = c("morpheus"), prepareArgs = function(x,i) x, N=10, ncores=3, agg=lapply, verbose=FALSE) diff --git a/pkg/R/optimParams.R b/pkg/R/optimParams.R index c7b9898..c050e63 100644 --- a/pkg/R/optimParams.R +++ b/pkg/R/optimParams.R @@ -30,7 +30,7 @@ #' 1/2, matrix(c(1,-2,3,1),ncol=2), c(0,0), "logit") #' μ <- computeMu(io$X, io$Y, list(K=2)) #' o <- optimParams(io$X, io$Y, 2, "logit") -#' \dontrun{ +#' \donttest{ #' θ0 <- list(p=1/2, β=μ, b=c(0,0)) #' par0 <- o$run(θ0) #' # Compare with another starting point diff --git a/pkg/R/plot.R b/pkg/R/plot.R index b8024f5..f76017d 100644 --- a/pkg/R/plot.R +++ b/pkg/R/plot.R @@ -29,18 +29,21 @@ #' @param ... Additional graphical parameters (xlab, ylab, ...) #' #' @examples -#' \dontrun{ -#' β <- matrix(c(1,-2,3,1),ncol=2) -#' mr <- multiRun(...) #see bootstrap example in ?multiRun -#' #mr[[i]] is a list of estimated parameters matrices -#' μ <- normalize(β) -#' for (i in 1:2) -#' mr[[i]] <- alignMatrices(res[[i]], ref=μ, ls_mode="exact") -#' plotHist(mr, 2, 1) #second row, first column} +#' # mr[[i]] is a list of estimated parameters matrices (here random matrices). +#' # Should be mr <- multiRun(...) --> see bootstrap example in ?multiRun. +#' simmr_path <- system.file("extdata", "simulateMr.R", package = "morpheus") +#' source(simmr_path) +#' mr <- simulateMr(c(2,2), 10)$mr +#' plotHist(mr, 2, 1) #second row, first column +#' +#' @return No return value, called for side effects. #' #' @export plotHist <- function(mr, x, y, ...) { + oldpar <- par(no.readonly = TRUE) + on.exit(par(oldpar)) + params <- .extractParam(mr, x, y) L <- length(params) # Plot histograms side by side @@ -51,6 +54,7 @@ plotHist <- function(mr, x, y, ...) xlab=ifelse("xlab" %in% names(args), args$xlab, "Parameter value"), ylab=ifelse("ylab" %in% names(args), args$ylab, "Density")) } + NULL } # NOTE: roxygen2 bug, "@inheritParams plotHist" fails in next header: @@ -67,24 +71,29 @@ plotHist <- function(mr, x, y, ...) #' @param ... Additional graphical parameters (xlab, ylab, ...) #' #' @examples -#' \dontrun{ -#' β <- matrix(c(1,-2,3,1),ncol=2) -#' mr <- multiRun(...) #see bootstrap example in ?multiRun -#' #mr[[i]] is a list of estimated parameters matrices -#' μ <- normalize(β) -#' for (i in 1:2) -#' mr[[i]] <- alignMatrices(res[[i]], ref=μ, ls_mode="exact") -#' plotBox(mr, 2, 1) #second row, first column} +#' # mr[[i]] is a list of estimated parameters matrices (here random matrices). +#' # Should be mr <- multiRun(...) --> see bootstrap example in ?multiRun. +#' simmr_path <- system.file("extdata", "simulateMr.R", package = "morpheus") +#' source(simmr_path) +#' source(simmr_path) +#' mr <- simulateMr(c(2,2), 10)$mr +#' plotBox(mr, 2, 1) #second row, first column +#' +#' @return No return value, called for side effects. #' #' @export plotBox <- function(mr, x, y, ...) { + oldpar <- par(no.readonly = TRUE) + on.exit(par(oldpar)) + params <- .extractParam(mr, x, y) L <- length(params) # Plot boxplots side by side par(mfrow=c(1,L), cex.axis=1.5, cex.lab=1.5, mar=c(4.7,5,1,1)) for (i in 1:L) boxplot(params[[i]], ...) + NULL } #' plotCoefs @@ -101,19 +110,22 @@ plotBox <- function(mr, x, y, ...) #' @param ... Additional graphical parameters #' #' @examples -#' \dontrun{ -#' β <- matrix(c(1,-2,3,1),ncol=2) -#' mr <- multiRun(...) #see bootstrap example in ?multiRun -#' #mr[[i]] is a list of estimated parameters matrices -#' μ <- normalize(β) -#' for (i in 1:2) -#' mr[[i]] <- alignMatrices(res[[i]], ref=μ, ls_mode="exact") -#' params <- rbind( c(.5,.5), β, c(0,0) ) #p, β, b stacked in a matrix -#' plotCoefs(mr[[1]], params)} +#' # mr[[i]] is a list of estimated parameters matrices (here random matrices). +#' # Should be mr <- multiRun(...) --> see bootstrap example in ?multiRun. +#' simmr_path <- system.file("extdata", "simulateMr.R", package = "morpheus") +#' source(simmr_path) +#' mr_θ <- simulateMr(c(3,2), 10) +#' mr <- mr_θ$mr ; θ <- mr_θ$θ +#' plotCoefs(mr[[1]], θ) +#' +#' @return No return value, called for side effects. #' #' @export plotCoefs <- function(mr, params, ...) { + oldpar <- par(no.readonly = TRUE) + on.exit(par(oldpar)) + d <- nrow(mr[[1]]) K <- ncol(mr[[1]]) @@ -142,6 +154,5 @@ plotCoefs <- function(mr, params, ...) col=1, lty=c(1,5,3,3), type="l", lwd=2, xlab=ifelse("xlab" %in% names(args), args$xlab, "Parameter index"), ylab=ifelse("ylab" %in% names(args), args$ylab, "") ) - - #print(o) #not returning o to avoid weird Jupyter issue... (TODO:) + NULL } diff --git a/pkg/R/utils.R b/pkg/R/utils.R index 06fe25f..83cb0dc 100644 --- a/pkg/R/utils.R +++ b/pkg/R/utils.R @@ -10,6 +10,7 @@ #' x <- matrix(c(1,2,-1,3), ncol=2) #' normalize(x) #column 1 is 1/sqrt(5) (1 2), #' #and column 2 is 1/sqrt(10) (-1, 3) +#' #' @export normalize <- function(x) { @@ -30,6 +31,7 @@ normalize <- function(x) #' # Next line should be a real call to multiRun() #' mr <- list( list(matrix(c(1,2,3,4),ncol=2),matrix(c(2,2,1,1),ncol=2)) ) #' p <- pvalue(mr[[1]]) +#' #' @export pvalue <- function(mr) { diff --git a/patch_Bettina/FLXMRglm.R b/pkg/inst/extdata/FLXMRglm.R similarity index 97% rename from patch_Bettina/FLXMRglm.R rename to pkg/inst/extdata/FLXMRglm.R index 30fbff6..9af08f7 100644 --- a/patch_Bettina/FLXMRglm.R +++ b/pkg/inst/extdata/FLXMRglm.R @@ -1,3 +1,6 @@ +# Patch provided by Bettina Grün (flexmix author). +# https://cran.r-project.org/web/packages/flexmix/index.html + FLXMRglm <- function(formula=.~., family=gaussian, offset=NULL) { if (is.character(family)) diff --git a/patch_Bettina/code.R b/pkg/inst/extdata/code.R similarity index 100% rename from patch_Bettina/code.R rename to pkg/inst/extdata/code.R diff --git a/pkg/inst/extdata/simulateMr.R b/pkg/inst/extdata/simulateMr.R new file mode 100644 index 0000000..c09d7a4 --- /dev/null +++ b/pkg/inst/extdata/simulateMr.R @@ -0,0 +1,22 @@ +# simulateMr +# +# Simulate an output of multiRun(...), because this function is a little +# complicated to call in an example (see ?multiRun). +# +#' β <- matrix(c(1,-2,3,1),ncol=2) + + +# theta, not beta here (more general) +.simulateMr( +#' # mr[[i]] is a list of estimated parameters matrices (here random matrices). +#' # Should be mr <- multiRun(...) --> see bootstrap example in ?multiRun. +#' mr <- list() +#' μ <- normalize(β) +#' for (i in 1:2) { +#' mr[[i]] <- list() +#' for (j in 1:3) +#' mr[[i]][[j]] <- β + matrix(rnorm(4,sd=0.25),ncol=2) +#' mr[[i]] <- alignMatrices(mr[[i]], ref=β, ls_mode="exact") --> TODO: align in same function +#' } + + list(theta = ..., mr = ...) -- 2.53.0