Skip to content

Commit

Permalink
Merge pull request #46 from Spatiotemporal-Exposures-and-Toxicology/m…
Browse files Browse the repository at this point in the history
…ain-sciome

Fix tests. Fix linting issues. Added release action.
  • Loading branch information
kyle-messier authored Jan 14, 2024
2 parents c060815 + 8ffb20b commit dc0bb9d
Show file tree
Hide file tree
Showing 40 changed files with 2,107 additions and 1,683 deletions.
3 changes: 2 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
^\.Rproj\.user$
^doc$
^Meta$
^\.github/
^\.lintr
^\.github
6 changes: 3 additions & 3 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# 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, build-workflow]
pull_request:
branches: [main, master]

Expand All @@ -12,6 +12,7 @@ jobs:
lint:
runs-on: ubuntu-latest
env:
R_LINTR_LINTER_FILE: .lintr
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
Expand All @@ -26,8 +27,7 @@ jobs:
needs: lint

- name: Lint
run: lintr::lint_package(linters = lintr::linters_with_defaults(object_name_linter = NULL,
commented_code_linter = NULL, cyclocomp_linter = NULL))
run: lintr::lint_package()
shell: Rscript {0}
env:
LINTR_ERROR_ON_LINT: true
109 changes: 109 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
#
# See https://github.com/r-lib/actions/tree/master/examples#readme for
# additional example workflows available for the R community.

name: Release New version

on:
push:
tags:
- '*'
branches: ["build-workflow"]
workflow_dispatch:

jobs:
create_release:
name: Create release
runs-on: ubuntu-latest
outputs:
upload_url: ${{ steps.create_release.outputs.upload_url }}
steps:
- name: Create release
id: create_release
uses: ncipollo/release-action@v1
with:
allowUpdates: true
build_upload_artefacts:
runs-on: ${{ matrix.config.os }}
name: ${{ matrix.config.os }} (${{ matrix.config.r }})
strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: macos-latest, r: 'release'}
# - {os: windows-latest, r: '4.2'}
# - {os: ubuntu-latest, r: '4.2'}
# - {os: macos-latest, r: '4.2'}
env:
R_KEEP_PKG_SOURCE: yes
steps:
# see this for details: https://msmith.de/2020/03/12/r-cmd-check-github-actions.html
- name: Configure git
run: git config --global core.autocrlf false
- uses: actions/checkout@v3
- uses: r-lib/actions/setup-pandoc@v2
- uses: r-lib/actions/setup-tinytex@v2
- uses: r-lib/actions/setup-r@v2
- uses: r-lib/actions/setup-r-dependencies@v2
with:
cache-version: 2
extra-packages: |
any::ggplot2
any::rcmdcheck
any::roxygen2
needs: |
check
roxygen2
- name: Read VERSION file
if: runner.os != 'macOs'
id: getversion
shell: bash
run: |
echo "VERSION=$(cat DESCRIPTION | grep -Po '(?<=Version\:\s).*')" >> $GITHUB_OUTPUT
- name: Read VERSION file (macOS)
if: runner.os == 'macOs'
id: getversion_mac
run: |
echo "VERSION=$(sed -n 's/Version:[[:space:]]*//p' DESCRIPTION | tr -d '[:space:]')" >> $GITHUB_OUTPUT
- name: Build package (Windows)
if: runner.os == 'Windows'
shell: cmd
run: R CMD build --no-build-vignettes .
- name: Build package
if: runner.os == 'Linux' || runner.os == 'macOs'
run: R CMD build --no-build-vignettes .
- name: Test Install (Windows)
if: runner.os == 'Windows'
shell: cmd
run: R CMD INSTALL --build PrestoGP_${{ steps.getversion.outputs.VERSION }}.tar.gz
- name: Test Install (Linux)
if: runner.os == 'Linux'
run: R CMD INSTALL --build PrestoGP_${{ steps.getversion.outputs.VERSION }}.tar.gz
- name: Test Install (macOs)
if: runner.os == 'macOs'
run: R CMD INSTALL --build PrestoGP_${{ steps.getversion_mac.outputs.VERSION }}.tar.gz
- uses: svenstaro/upload-release-action@v2
if: runner.os == 'macOs'
with:
tag: ${{ github.ref }}
file: PrestoGP_${{ steps.getversion_mac.outputs.VERSION }}.tgz
asset_name: "PrestoGP_${{ steps.getversion_mac.outputs.VERSION }}-x86_64-macOs-R.${{ matrix.config.r }}.tgz"
- uses: svenstaro/upload-release-action@v2
if: runner.os == 'Linux'
with:
tag: ${{ github.ref }}
file: "PrestoGP_${{ steps.getversion.outputs.VERSION }}_R_x86_64-pc-linux-gnu.tar.gz"
asset_name: PrestoGP_${{ steps.getversion.outputs.VERSION }}-x86_64-linux-R.${{ matrix.config.r }}.zip
- uses: svenstaro/upload-release-action@v2
if: runner.os == 'Windows'
with:
tag: ${{ github.ref }}
file: PrestoGP_${{ steps.getversion.outputs.VERSION }}.zip
asset_name: PrestoGP_${{ steps.getversion.outputs.VERSION }}-windows-R.${{ matrix.config.r }}.zip

24 changes: 12 additions & 12 deletions .github/workflows/sanitizers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ jobs:
run: rcmdcheck::rcmdcheck(build_args = "--no-build-vignettes", args = c("--no-codoc", "--no-examples", "--no-manual", "--ignore-vignettes"), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Upload check results
if: failure()
uses: actions/upload-artifact@v3
with:
name: ${{ runner.os }}-asan-rrelease-results
path: check
# - name: Upload check results
# if: failure()
# uses: actions/upload-artifact@v3
# with:
# name: ${{ runner.os }}-asan-rrelease-results
# path: check
sanitizer-check-usan:
runs-on: ubuntu-latest
env:
Expand Down Expand Up @@ -162,9 +162,9 @@ jobs:
run: rcmdcheck::rcmdcheck(build_args = "--no-build-vignettes", args = c("--no-codoc", "--no-examples", "--no-manual", "--ignore-vignettes"), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Upload check results
if: failure()
uses: actions/upload-artifact@v3
with:
name: ${{ runner.os }}-ubsan-rrelease-results
path: check
# - name: Upload check results
# if: failure()
# uses: actions/upload-artifact@v3
# with:
# name: ${{ runner.os }}-ubsan-rrelease-results
# path: check
13 changes: 13 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
linters: linters_with_defaults(
line_length_linter(160L),
commented_code_linter = NULL,
object_name_linter = NULL,
cyclocomp_linter = NULL,
object_length_linter = NULL,
indentation_linter(
indent = 2L,
hanging_indent_style = "never",
assignment_as_infix = FALSE
)
)
encoding: "UTF-8"
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: PrestoGP
Type: Package
Title: Penalized Regression for Spatio-Temporal Outcomes via Gaussian Processes
Version: 0.2.0.9021
Version: 0.2.0.9023
Authors@R: c(
person(given = "Eric",
family = "Bair",
Expand Down Expand Up @@ -47,7 +47,8 @@ Imports:
spam,
psych,
doParallel,
covr
covr,
mvtnorm
License: GPL-3
Encoding: UTF-8
VignetteBuilder: knitr
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import(glmnet)
import(ncvreg)
import(readxl)
import(scoringRules)
import(covr)
importFrom(aod,wald.test)
importFrom(dplyr,"%>%")
importFrom(foreach,"%dopar%")
Expand Down
59 changes: 32 additions & 27 deletions R/Log_Likelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@
#'
#' @examples
#' @noRd
negloglik_vecchia_ST <- function(logparms, res, vecchia.approx, param.seq,
scaling, nscale) {
negloglik_vecchia_ST <- function(logparms, res, vecchia.approx, param.seq, scaling, nscale) {
parms <- unlog.params(logparms, param.seq, 1)
locs.scaled <- vecchia.approx$locsord
for (j in 1:nscale) {
locs.scaled[, scaling == j] <- locs.scaled[, scaling == j] /
parms[param.seq[2, 1] + j - 1]
}
vecchia.approx$locsord <- locs.scaled
-vecchia_likelihood(res, vecchia.approx, c(parms[1], 1,
parms[param.seq[3, 1]]),
parms[param.seq[4, 1]])
-vecchia_likelihood(
res, vecchia.approx, c(
parms[1], 1,
parms[param.seq[3, 1]]
),
parms[param.seq[4, 1]]
)
}

#' negloglik_vecchia
Expand All @@ -42,8 +45,10 @@ negloglik_vecchia_ST <- function(logparms, res, vecchia.approx, param.seq,
#' @noRd
negloglik_vecchia <- function(logparms, res, vecchia.approx, param.seq) {
parms <- unlog.params(logparms, param.seq, 1)
-vecchia_likelihood(res, vecchia.approx, c(parms[1], parms[2], parms[3]),
parms[4])
-vecchia_likelihood(
res, vecchia.approx, c(parms[1], parms[2], parms[3]),
parms[4]
)
}

#' negloglik_full_ST
Expand Down Expand Up @@ -95,8 +100,10 @@ negloglik.full <- function(logparams, d, y, param.seq) {
params <- unlog.params(logparams, param.seq, 1)
# d <- fields::rdist(locs)
N <- nrow(d)
cov.mat <- params[1] * fields::Matern(d, range = params[2],
smoothness = params[3]) +
cov.mat <- params[1] * fields::Matern(d,
range = params[2],
smoothness = params[3]
) +
params[4] * diag(N)
return(-1 * mvtnorm::dmvnorm(y, rep(0, N), cov.mat, log = TRUE))
}
Expand Down Expand Up @@ -133,8 +140,7 @@ mvnegloglik <- function(logparams, vecchia.approx, y, param.seq, P) {
##############################################################################
### Flexible Spatiotemporal Multivariate Matern Negative Loglikelihood Function ###########

mvnegloglik_ST <- function(logparams, vecchia.approx, y, param.seq, P, scaling,
nscale) {
mvnegloglik_ST <- function(logparams, vecchia.approx, y, param.seq, P, scaling, nscale) {
# Input-
# logparams: A numeric vector of length (4*P)+(4*choose(P,2)).
# To construct these parameters we unlist a list of the 7 covariance
Expand Down Expand Up @@ -227,8 +233,7 @@ mvnegloglik.full <- function(logparams, locs, y, param.seq) {
}

##############################################################################
create.cov.upper.flex <- function(P, marg.var, marg.range, marg.smooth,
nugget, R.corr) {
create.cov.upper.flex <- function(P, marg.var, marg.range, marg.smooth, nugget, R.corr) {
# Create the symmetrical marginal+cross-covariance flexible matern from the
# given parameters. Output is a list of the 4 Matern parameters as matrices
sig2.mat <- diag(marg.var, P, P)
Expand All @@ -237,20 +242,17 @@ create.cov.upper.flex <- function(P, marg.var, marg.range, marg.smooth,
nugget.mat <- diag(nugget, P, P)
if (P > 1) {
combs <- gtools::combinations(P, 2)
for (iter in 1:nrow(combs)) {
for (iter in seq_len(nrow(combs))) {
i <- combs[iter, 1]
j <- combs[iter, 2]

smoothness.mat[i, j] <- (marg.smooth[i] + marg.smooth[j]) / 2
range.mat[i, j] <- 1 / sqrt(((1 / marg.range[i])^2 +
(1 / marg.range[j])^2) / 2)
range.mat[i, j] <- 1 / sqrt(((1 / marg.range[i])^2 + (1 / marg.range[j])^2) / 2)

s1 <- sqrt(marg.var[i] * marg.var[j])
s2 <- ((1 / marg.range[i])^marg.smooth[i] *
(1 / marg.range[j])^marg.smooth[j]) /
s2 <- ((1 / marg.range[i])^marg.smooth[i] * (1 / marg.range[j])^marg.smooth[j]) /
((1 / range.mat[i, j])^(2 * smoothness.mat[i, j]))
s3 <- gamma(smoothness.mat[i, j]) / (sqrt(gamma(marg.smooth[i])) *
sqrt(gamma(marg.smooth[j])))
s3 <- gamma(smoothness.mat[i, j]) / (sqrt(gamma(marg.smooth[i])) * sqrt(gamma(marg.smooth[j])))
s4 <- R.corr[iter]
sig2.mat[i, j] <- s1 * s2 * s3 * s4
}
Expand Down Expand Up @@ -284,20 +286,24 @@ cat.covariances <- function(locs.list, sig2, range, smoothness, nugget) {

l <- length(locs.list)
combs <- gtools::combinations(l, 2, repeats.allowed = TRUE)
for (iter in 1:nrow(combs)) {
for (iter in seq_len(nrow(combs))) {
i <- combs[iter, 1]
j <- combs[iter, 2]
# d <- fields::rdist.earth(locs.list[[i]],locs.list[[j]],miles = FALSE)
d <- fields::rdist(locs.list[[i]], locs.list[[j]])
# Calculate the covariance matrix - if/then based on its location in the super-matrix
N <- nrow(d)
if (i == j) { # To accomodate varying size outcomes- the nugget is not included on cross-covariances
cov.mat.ij <- sig2[i, j] * geoR::matern(d, phi = range[i, j], kappa =
smoothness[i, j]) +
cov.mat.ij <- sig2[i, j] * geoR::matern(d,
phi = range[i, j], kappa =
smoothness[i, j]
) +
nugget[i, j] * diag(N)
} else {
cov.mat.ij <- sig2[i, j] * geoR::matern(d, phi = range[i, j], kappa =
smoothness[i, j])
cov.mat.ij <- sig2[i, j] * geoR::matern(d,
phi = range[i, j], kappa =
smoothness[i, j]
)
}


Expand Down Expand Up @@ -328,8 +334,7 @@ cat.covariances <- function(locs.list, sig2, range, smoothness, nugget) {
##############################################################################
### Create the likelihood initial values #########

create.initial.values.flex <- function(marg.var, marg.range, marg.smooth,
nugget, R.corr, P) {
create.initial.values.flex <- function(marg.var, marg.range, marg.smooth, nugget, R.corr, P) {
# Log-transform the covariance parameters and arrange in the proper order
# for the likelihood function
logparams.init <- c(
Expand Down
Loading

0 comments on commit dc0bb9d

Please sign in to comment.