Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 47 additions & 26 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,60 @@
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches:
- main
- master
branches: [main, master]
pull_request:
branches:
- main
- master

name: R-CMD-check
name: R-CMD-check.yaml

permissions: read-all

jobs:
R-CMD-check:
runs-on: macOS-latest
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v2
- uses: r-lib/actions/setup-tinytex@v2
- uses: actions/checkout@v6
- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
- name: Install dependencies
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}

- uses: r-lib/actions/setup-tinytex@v2

- name: Install additional LaTeX packages
run: |
install.packages(c("remotes", "rcmdcheck", "tinytex",
"ggplot2", "gtools", "mvtnorm",
"testthat", "knitr", "rmarkdown",
"tidyr", "dplyr"))
tinytex::tlmgr_install(pkgs = c("ae", "thumbpdf",
"multirow", "listings",
"caption", "subcaption",
"tcolorbox", "pgf",
"environ", "babel-english",
"grfext", "hyperref"))
shell: Rscript {0}
- name: Check
run: rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "error")
install.packages("tinytex")
tinytex::tlmgr_install(c(
"thumbpdf", "multirow", "listings", "caption", "subcaption",
"tcolorbox", "pgf", "environ", "babel-english", "grfext", "hyperref"
))
shell: Rscript {0}

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
60 changes: 60 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:

name: test-coverage.yaml

permissions: read-all

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v6

- uses: r-lib/actions/setup-r@v2

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr, any::xml2
needs: coverage

- name: Test coverage
run: |
cov <- covr::package_coverage(
quiet = FALSE,
clean = FALSE,
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
print(cov)
covr::to_cobertura(cov)
shell: Rscript {0}

- uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7
with:
# Fail if error if not on PR, or if on PR and token is given
fail_ci_if_error: ${{ github.event_name != 'pull_request' || secrets.CODECOV_TOKEN }}
files: ./cobertura.xml
plugins: noop
disable_search: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v7
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ inst/doc
.Rhistory
clean_bib.sh
tests/testthat/Rplots.pdf
.DS_Store

4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: dirichletprocess
Type: Package
Title: Build Dirichlet Process Objects for Bayesian Modelling
Version: 0.4.2
Version: 0.4.2.9000
Authors@R: c(
person("Gordon", "J. Ross", email="gordon@gordonjross.co.uk", role=c("aut")),
person("Dean", "Markwick", email="dean.markwick@talk21.com", role=c("aut", "cre")),
Expand Down Expand Up @@ -36,4 +36,4 @@ Imports: gtools,
mvtnorm
URL: https://github.com/dm13450/dirichletprocess, https://dm13450.github.io/dirichletprocess/
BugReports: https://github.com/dm13450/dirichletprocess/issues
RoxygenNote: 7.2.3
Config/roxygen2/version: 8.0.0
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export(plot_dirichletprocess_multivariate)
export(plot_dirichletprocess_univariate)
export(true_cluster_labels)
export(weighted_function_generator)
importFrom(ggplot2,.data)
importFrom(stats,dbeta)
importFrom(stats,dbinom)
importFrom(stats,dexp)
Expand Down
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# dirichletprocess 0.4.0.9000
# dirichletprocess 0.4.2.9000

* Updated GitHub Actions
* Fixed ggplot deprecation
* Added some missing tests

# dirichletprocess 0.4.2

* Added PriorFunction and PriorClusters to draw from the base measure.
* Fixed a bug in the likelihood calculation (#21) by Filippo Fiocchi.
Expand Down
26 changes: 15 additions & 11 deletions R/diagnostic_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ DiagnosticPlots <- function(dpobj, gg = FALSE) {

}



#' @export
#' @describeIn DiagnosticPlots Trace plot of alpha.
#' @describeIn DiagnosticPlots Trace plot of alpha.
#' @importFrom ggplot2 .data
AlphaTraceplot <- function(dpobj, gg = TRUE) {

if (gg) {
p <- ggplot2::ggplot(data.frame(Alpha = dpobj$alphaChain,
Index = seq_along(dpobj$alphaChain)),
ggplot2::aes_string("Index", "Alpha")) +
ggplot2::aes(x=.data$Index, y=.data$Alpha)) +
ggplot2::geom_line() +
ggplot2::ggtitle("Traceplot of alpha")
return(p)
Expand All @@ -58,6 +57,7 @@ AlphaTraceplot <- function(dpobj, gg = TRUE) {

#' @export
#' @describeIn DiagnosticPlots Plot of the prior and posterior of alpha.
#' @importFrom ggplot2 .data
AlphaPriorPosteriorPlot <- function(dpobj, prior_color = "#2c7fb8", post_color = "#d95f02", gg = TRUE) {

dap <- dpobj$alphaPriorParameters
Expand All @@ -69,14 +69,17 @@ AlphaPriorPosteriorPlot <- function(dpobj, prior_color = "#2c7fb8", post_color =

p <- ggplot2::ggplot() +
ggplot2::geom_histogram(data = data.frame(Alpha = dpobj$alphaChain),
mapping = ggplot2::aes_string("Alpha",
"..density..",
colour = "'Posterior'", fill = "'Posterior'"),
mapping = ggplot2::aes(x=.data$Alpha,
ggplot2::after_stat(.data$density),
colour = "'Posterior'",
fill = "'Posterior'"),
bins = min(its / 10, 100)) +
ggplot2::stat_function(fun = prior_fun,
mapping = ggplot2::aes_string(colour = "'Prior'")) +
mapping = ggplot2::aes(colour = "'Prior'")) +
ggplot2::ggtitle("Prior and posterior of alpha") +
ggplot2::scale_colour_manual(labels = c("Posterior", "Prior"), values = c(prior_color, post_color), aesthetics = c("colour", "fill"), name = " ")
ggplot2::scale_colour_manual(labels = c("Posterior", "Prior"),
values = c(prior_color, post_color),
aesthetics = c("colour", "fill"), name = " ")
return(p)
} else {
graphics::hist(dpobj$alphaChain, freq = FALSE, breaks = min(its / 10, 100),
Expand All @@ -90,14 +93,15 @@ AlphaPriorPosteriorPlot <- function(dpobj, prior_color = "#2c7fb8", post_color =

#' @export
#' @describeIn DiagnosticPlots Trace plot of the number of clusters.
#' @importFrom ggplot2 .data
ClusterTraceplot <- function(dpobj, gg = TRUE) {

n_clust <- vapply(dpobj$labelsChain, function(x) length(unique(x)), numeric(1))

if (gg) {
p <- ggplot2::ggplot(data.frame(nclust = n_clust,
Index = seq_along(n_clust)),
ggplot2::aes_string("Index", "nclust")) +
ggplot2::aes(x=.data$Index, y=.data$nclust)) +
ggplot2::geom_line() +
ggplot2::ylab("Number of clusters") +
ggplot2::ggtitle("Traceplot of the number of clusters")
Expand All @@ -117,7 +121,7 @@ LikelihoodTraceplot <- function(dpobj, gg = TRUE) {
if (gg) {
p <- ggplot2::ggplot(data.frame(Lik = dpobj$likelihoodChain,
Index = seq_along(dpobj$likelihoodChain)),
ggplot2::aes_string("Index", "Lik")) +
ggplot2::aes(x=.data$Index, y=.data$Lik)) +
ggplot2::geom_line() +
ggplot2::ylab("Log-likelihood") +
ggplot2::ggtitle("Traceplot of the log-likelihood")
Expand Down
6 changes: 6 additions & 0 deletions R/dirichletprocess-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
## usethis namespace: end
NULL
27 changes: 18 additions & 9 deletions R/plot_dirichletprocess.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ plot_dirichletprocess.mvnormal <- function(x, ...) {

#' @export
#' @rdname plot.dirichletprocess
#' @importFrom ggplot2 .data
plot_dirichletprocess_univariate <- function(x,
likelihood = FALSE,
single = TRUE,
Expand All @@ -43,15 +44,17 @@ plot_dirichletprocess_univariate <- function(x,
quant_pts = 100,
xlim = NA) {

graph <- ggplot2::ggplot(data.frame(dt = x$data), ggplot2::aes_(x = ~dt)) +
graph <- ggplot2::ggplot(data.frame(dt = x$data), ggplot2::aes(x = .data$dt)) +
ggplot2::theme(axis.title = ggplot2::element_blank())

if (data_method == "density") {
graph <- graph + ggplot2::geom_density(fill = data_fill,
bw = ifelse(is.null(data_bw), "nrd0", data_bw))
bw = ifelse(is.null(data_bw),
"nrd0",
data_bw))
} else if (data_method == "hist" | data_method == "histogram") {
graph <- graph + ggplot2::geom_histogram(ggplot2::aes_(x = ~dt,
y = ~..density..),
graph <- graph + ggplot2::geom_histogram(ggplot2::aes(x = .data$dt,
ggplot2::after_stat(.data$density)),
fill = data_fill,
binwidth = data_bw)
} else if (data_method != "none") {
Expand All @@ -76,9 +79,9 @@ plot_dirichletprocess_univariate <- function(x,
quantile, probs = c(ci_size/2, 0.5, 1 - ci_size/2),
na.rm = TRUE)

graph <- graph + ggplot2::geom_line(data=data.frame(x=x_grid, y=posteriorCI[1,]), ggplot2::aes_(x=~x,y=~y, colour="Posterior"), linetype=2)
graph <- graph + ggplot2::geom_line(data=data.frame(x=x_grid, y=posteriorCI[2,]), ggplot2::aes_(x=~x,y=~y, colour="Posterior"))
graph <- graph + ggplot2::geom_line(data=data.frame(x=x_grid, y=posteriorCI[3,]), ggplot2::aes_(x=~x,y=~y, colour="Posterior"), linetype=2)
graph <- graph + ggplot2::geom_line(data=data.frame(x=x_grid, y=posteriorCI[1,]), ggplot2::aes(x=.data$x,y=.data$y, colour="Posterior"), linetype=2)
graph <- graph + ggplot2::geom_line(data=data.frame(x=x_grid, y=posteriorCI[2,]), ggplot2::aes(x=.data$x,y=.data$y, colour="Posterior"))
graph <- graph + ggplot2::geom_line(data=data.frame(x=x_grid, y=posteriorCI[3,]), ggplot2::aes(x=.data$x,y=.data$y, colour="Posterior"), linetype=2)

if (likelihood) {
graph <- graph + ggplot2::stat_function(fun = function(z) LikelihoodFunction(x)(z),
Expand All @@ -93,11 +96,17 @@ plot_dirichletprocess_univariate <- function(x,

#' @export
#' @rdname plot.dirichletprocess
#' @importFrom ggplot2 .data
plot_dirichletprocess_multivariate <- function(x) {

plotFrame <- data.frame(x1=x$data[,1], x2=x$data[,2], Cluster=as.factor(x$clusterLabel))
plotFrame <- data.frame(x1=x$data[,1],
x2=x$data[,2],
Cluster=as.factor(x$clusterLabel))

graph <- ggplot2::ggplot(plotFrame, ggplot2::aes_(x=~x1, y=~x2, colour=~Cluster)) +
graph <- ggplot2::ggplot(plotFrame,
ggplot2::aes(x=.data$x1,
y=.data$x2,
colour=.data$Cluster)) +
ggplot2::geom_point()
return(graph)
}
6 changes: 4 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ knitr::opts_chunk$set(

# dirichletprocess

[![R build status](https://github.com/dm13450/dirichletprocess/workflows/R-CMD-check/badge.svg)](https://github.com/dm13450/dirichletprocess/actions)
<!-- badges: start -->
[![R-CMD-check](https://github.com/dm13450/dirichletprocess/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/dm13450/dirichletprocess/actions/workflows/R-CMD-check.yaml)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/dm13450/dirichletprocess?branch=master&svg=true)](https://ci.appveyor.com/project/dm13450/dirichletprocess)
[![Coverage Status](https://codecov.io/gh/dm13450/dirichletprocess/branch/master/graph/badge.svg)](https://app.codecov.io/gh/dm13450/dirichletprocess)

[![Codecov test coverage](https://codecov.io/gh/dm13450/dirichletprocess/graph/badge.svg)](https://app.codecov.io/gh/dm13450/dirichletprocess)
<!-- badges: end -->

The dirichletprocess package provides tools for you to build custom Dirichlet process mixture models. You can use the pre-built Normal/Weibull/Beta distributions or create your own following the instructions in the vignette. In as little as four lines of code you can be modelling your data nonparametrically.

Expand Down
Loading