Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sciome Update 1/10/2024 #44

Merged
merged 45 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
4e0e1ec
Pull request #7: Testing
ericbair-sciome Nov 7, 2023
f90ce5e
Fixed likelihood function and added additional test
ericbair-sciome Nov 14, 2023
6c17318
Fixed likelihood function and added additional tests
ericbair-sciome Nov 14, 2023
2dd74a2
Added a simulation file for testing
ericbair-sciome Nov 14, 2023
f52ebbb
Pull request #9: Testing
ericbair-sciome Nov 14, 2023
286ba5d
Merge branch 'main-sciome' of sciome-bot-git:Spatiotemporal-Exposures…
sciome-bot Nov 14, 2023
ea78ffa
Merge branch 'main-sciome' of sciome-bot-git:Spatiotemporal-Exposures…
sciome-bot Nov 20, 2023
4822fbc
Add UBSAN/ASAN sanitizers
sciome-bot Nov 20, 2023
80a9818
Added more likelihood and maxmin ordering tests
ericbair-sciome Nov 21, 2023
1ca9586
Fixed a bug in SparseNN
ericbair-sciome Nov 28, 2023
7f3ca3c
Fixed another sparseNN bug and updated tests
ericbair-sciome Nov 29, 2023
895100d
Merge branch 'to-git' of http://192.168.167.103:7990/bitbucket/scm/st…
ericbair-sciome Nov 29, 2023
17f5284
Pull request #12: Testing
ericbair-sciome Dec 1, 2023
e530819
Add USAN/ASAN pipeline
sciome-bot Dec 28, 2023
6af43f4
Merge branch 'master' of ssh://sciome-bot/stat/prestogp into build-wo…
sciome-bot Dec 28, 2023
2c9c2ed
Merge branch 'to-git' of ssh://sciome-bot/stat/prestogp into build-wo…
sciome-bot Dec 28, 2023
85517e7
Pull request #16: Add UBSAN/ASAN sanitizers
shail-choksi Dec 28, 2023
7c0bbfe
Ran auto-formatter/linter for R and C++ in vscode. Added some missing…
sciome-bot Dec 28, 2023
bea8382
Merge branch 'main-sciome' of sciome-bot-git:Spatiotemporal-Exposures…
sciome-bot Dec 28, 2023
d2a2e3a
Merge branch 'master' of ssh://sciome-bot/stat/prestogp
sciome-bot Dec 28, 2023
73aed75
Merge branch 'master' of ssh://sciome-bot/stat/prestogp into build-wo…
sciome-bot Dec 28, 2023
11771e4
Added missing imports
sciome-bot Dec 28, 2023
710520c
Pull request #18: Added missing imports and ran auto-formatter in vsc…
shail-choksi Dec 28, 2023
f037a0b
Add new files to Collate section in DESCRIPTION file
sciome-bot Dec 28, 2023
8dac095
R CMD check fixes
sciome-bot Dec 28, 2023
2e94403
Pull request #20: R CMD check fixes
shail-choksi Dec 28, 2023
03c3589
Merge branch 'to-git' of ssh://sciome-bot/stat/prestogp
sciome-bot Dec 28, 2023
d7794fd
Merge branch 'master' of ssh://sciome-bot/stat/prestogp
sciome-bot Dec 28, 2023
919cd36
More test, univariate code fixed
ericbair-sciome Dec 28, 2023
d6d62cf
Added new files for testing/documentation
ericbair-sciome Dec 28, 2023
1ff92a1
Merge branch 'master' of http://192.168.167.103:7990/bitbucket/scm/st…
ericbair-sciome Dec 29, 2023
53ac4d1
Merge branch 'master' of http://192.168.167.103:7990/bitbucket/scm/st…
ericbair-sciome Dec 29, 2023
b0534e5
Merge branch 'master' of http://192.168.167.103:7990/bitbucket/scm/st…
ericbair-sciome Dec 29, 2023
04e3f9d
Updated outdated DESCRIPTION file
ericbair-sciome Dec 29, 2023
5e4bfb5
Pull request #22: Testing
ericbair-sciome Dec 29, 2023
82eab44
Auto-formatting changes
sciome-bot Dec 29, 2023
9090112
Pull request #23: Auto-formatting changes
shail-choksi Dec 29, 2023
b50b49e
Univariate prediction, more testing
ericbair-sciome Jan 8, 2024
d361f2e
Added a data file for testing
ericbair-sciome Jan 8, 2024
e9e7607
Merge branch 'master' of ssh://sciome-bot/stat/prestogp into to-git
sciome-bot Jan 10, 2024
af6d756
Pull request #24: PrestoGP Update
ericbair-sciome Jan 10, 2024
723c3eb
Merge branch 'master' of ssh://sciome-bot/stat/prestogp into to-git
sciome-bot Jan 10, 2024
06918b0
Add missing comma in Imports section of DESCRIPTION
sciome-bot Jan 11, 2024
bdf52c6
Remove unneeded exports from NAMESPACE
sciome-bot Jan 11, 2024
6d69b69
Add missing dependency in Namespace/Description
sciome-bot Jan 11, 2024
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
12 changes: 7 additions & 5 deletions .github/workflows/check-standard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ jobs:
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 }}
Expand All @@ -41,7 +38,12 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
extra-packages: |
any::tictoc
any::units
any::rcmdcheck
any::knitr
any::rmarkdown
needs: check

- uses: r-lib/actions/check-r-package@v2
Expand Down
10 changes: 5 additions & 5 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.9020
Version: 0.2.0.9021
Authors@R: c(
person(given = "Eric",
family = "Bair",
Expand Down Expand Up @@ -45,7 +45,9 @@ Imports:
rlang,
mvtnorm,
spam,
psych
psych,
doParallel,
covr
License: GPL-3
Encoding: UTF-8
VignetteBuilder: knitr
Expand All @@ -55,10 +57,8 @@ Collate:
'PrestoGP-package.R'
'PrestoGP_CreateU_Multivariate.R'
'PrestoGP_Model.R'
'PrestoGP_Vecchia_Spatiotemporal.R'
'PrestoGP_Vecchia.R'
'PrestoGP_Full.R'
'PrestoGP_Vecchia_Spatial.R'
'PrestoGP_Full_Spatial.R'
'PrestoGP_Multivariate_Vecchia.R'
'PrestoGP_Util_Functions.R'
'RcppExports.R'
Expand Down
7 changes: 3 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

export(":=")
export(.data)
export(FullSpatialModel)
export(FullModel)
export(Kr_pred)
export(MultivariateVecchiaModel)
export(PrestoGPModel)
export(ST_Krig_Param_Avg)
export(SpatialModel)
export(SpatiotemporalFullModel)
export(SpatiotemporalModel)
export(VecchiaModel)
export(as_label)
export(as_name)
export(createUMultivariate)
Expand All @@ -31,6 +29,7 @@ import(glmnet)
import(ncvreg)
import(readxl)
import(scoringRules)
import(covr)
importFrom(aod,wald.test)
importFrom(dplyr,"%>%")
importFrom(foreach,"%dopar%")
Expand Down
125 changes: 68 additions & 57 deletions R/Log_Likelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
#'
#' @examples
#' @noRd
negloglik_vecchia_ST <- function(logparms, locs, res, vecchia.approx) {
parms <- exp(logparms)
locs <- locs / matrix(parms[c(2, 2, 3)], nrow = nrow(locs), ncol = 3, byrow = TRUE)
vecchia.approx$locsord <- locs
-vecchia_likelihood(res, vecchia.approx, c(parms[1], 1, 0.5), parms[4])
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]])
}

#' negloglik_vecchia
Expand All @@ -33,9 +40,10 @@
#'
#' @examples
#' @noRd
negloglik_vecchia <- function(logparms, locs, res, vecchia.approx) {
parms <- exp(logparms)
-vecchia_likelihood(res, vecchia.approx, c(parms[1], parms[2], 0.5), parms[3])
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])
}

#' negloglik_full_ST
Expand All @@ -52,12 +60,21 @@
#'
#' @examples
#' @noRd
negloglik_full_ST <- function(logparms, locs, y, N) {
parms <- exp(logparms)
locs.scaled <- cbind(locs[, 1] / parms[2], locs[, 2] / parms[2], locs[, 3] / parms[3])
negloglik_full_ST <- function(logparms, locs, y, param.seq, scaling, nscale) {
parms <- unlog.params(logparms, param.seq, 1)
locs.scaled <- locs
for (j in 1:nscale) {
locs.scaled[, scaling == j] <- locs.scaled[, scaling == j] /
parms[param.seq[2, 1] + j - 1]
}
d <- fields::rdist(locs.scaled)
cov.mat <- parms[1] * fields::Exponential(d, range = 1) + parms[4] * diag(N)
-mvtnorm::dmvnorm(y, rep(0, N), cov.mat, log = TRUE)
N <- nrow(d)
cov.mat <- parms[1] * fields::Matern(d,
range = 1,
smoothness = parms[param.seq[3, 1]]
) +
parms[param.seq[4, 1]] * diag(N)
return(-1 * mvtnorm::dmvnorm(y, rep(0, N), cov.mat, log = TRUE))
}

#' negloglik.full
Expand All @@ -74,15 +91,12 @@
#'
#' @examples
#' @noRd
negloglik.full <- function(logparams, locs, y) {
params <- c(
exp(logparams[1:2]),
gtools::inv.logit(logparams[3], 0, 2.5),
exp(logparams[4])
)
d <- fields::rdist(locs)
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 All @@ -94,10 +108,10 @@
mvnegloglik <- function(logparams, vecchia.approx, y, param.seq, P) {
# 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

Check warning on line 111 in R/Log_Likelihood.R

View workflow job for this annotation

GitHub Actions / lint

file=R/Log_Likelihood.R,line=111,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.
# categories- in order: (1) marginal variances, (2) Marginal ranges,

Check warning on line 112 in R/Log_Likelihood.R

View workflow job for this annotation

GitHub Actions / lint

file=R/Log_Likelihood.R,line=112,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.
# (3) Marginal smoothness, (4) Nuggets, and
# (5) cross-covariance correlation. These seven parameters are to be

Check warning on line 114 in R/Log_Likelihood.R

View workflow job for this annotation

GitHub Actions / lint

file=R/Log_Likelihood.R,line=114,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.
# created in a list. The variance, range, smoothness, and nugget
# have P terms, and the correlations have choose(P,2)
# terms. Use unlist() to create the vector of parameters.
Expand All @@ -110,32 +124,23 @@
# index locations of each parameter.

# P <- length(y)
# transform the postively constrained parameters from log-space to normal-space

Check warning on line 127 in R/Log_Likelihood.R

View workflow job for this annotation

GitHub Actions / lint

file=R/Log_Likelihood.R,line=127,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 81 characters.
params <- c(
exp(logparams[1:param.seq[2, 2]]),
gtools::inv.logit(logparams[param.seq[3, 1]:param.seq[3, 2]], 0, 2.5),
exp(logparams[param.seq[4, 1]:param.seq[4, 2]])
)
if (P > 1) {
params <- c(params, tanh(logparams[param.seq[5, 1]:param.seq[5, 2]]))
} else {
params <- c(params, 1)
}

params <- unlog.params(logparams, param.seq, P)
U.obj <- createUMultivariate(vecchia.approx, params)
-1 * GPvecchia:::vecchia_likelihood_U(y, U.obj)
}

##############################################################################
### Flexible Spatiotemporal Multivariate Matern Negative Loglikelihood Function ###########

Check warning on line 134 in R/Log_Likelihood.R

View workflow job for this annotation

GitHub Actions / lint

file=R/Log_Likelihood.R,line=134,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 91 characters.

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

Check warning on line 140 in R/Log_Likelihood.R

View workflow job for this annotation

GitHub Actions / lint

file=R/Log_Likelihood.R,line=140,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.
# categories- in order: (1) marginal variances, (2) Marginal ranges,

Check warning on line 141 in R/Log_Likelihood.R

View workflow job for this annotation

GitHub Actions / lint

file=R/Log_Likelihood.R,line=141,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.
# (3) Marginal smoothness, (4) Nuggets, and
# (5) cross-covariance correlation. These seven parameters are to be

Check warning on line 143 in R/Log_Likelihood.R

View workflow job for this annotation

GitHub Actions / lint

file=R/Log_Likelihood.R,line=143,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.
# created in a list. The variance, range, smoothness, and nugget
# have P terms, and the correlations have choose(P,2)
# terms. Use unlist() to create the vector of parameters.
Expand All @@ -148,17 +153,8 @@
# index locations of each parameter.

# P <- length(y)
# transform the postively constrained parameters from log-space to normal-space

Check warning on line 156 in R/Log_Likelihood.R

View workflow job for this annotation

GitHub Actions / lint

file=R/Log_Likelihood.R,line=156,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 81 characters.
params <- c(
exp(logparams[1:param.seq[2, 2]]),
gtools::inv.logit(logparams[param.seq[3, 1]:param.seq[3, 2]], 0, 2.5),
exp(logparams[param.seq[4, 1]:param.seq[4, 2]])
)
if (P > 1) {
params <- c(params, tanh(logparams[param.seq[5, 1]:param.seq[5, 2]]))
} else {
params <- c(params, 1)
}
params <- unlog.params(logparams, param.seq, P)
locs.scaled <- vecchia.approx$locsord
for (i in 1:P) {
for (j in 1:nscale) {
Expand Down Expand Up @@ -201,15 +197,7 @@
# P <- length(y)
# transform the postively constrained parameters from log-space to normal-space
P <- length(locs)
params <- c(
exp(logparams[1:param.seq[2, 2]]),
gtools::inv.logit(logparams[param.seq[3, 1]:param.seq[3, 2]], 0, 2.5),
exp(logparams[param.seq[4, 1]:param.seq[4, 2]])
)
if (P > 1) {
params <- c(params, tanh(logparams[param.seq[5, 1]:param.seq[5, 2]]))
}

params <- unlog.params(logparams, param.seq, P)
sig2 <- params[param.seq[1, 1]:param.seq[1, 2]]
range <- params[param.seq[2, 1]:param.seq[2, 2]]
smoothness <- params[param.seq[3, 1]:param.seq[3, 2]]
Expand Down Expand Up @@ -254,11 +242,15 @@
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]) / ((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])))
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])))
s4 <- R.corr[iter]
sig2.mat[i, j] <- s1 * s2 * s3 * s4
}
Expand Down Expand Up @@ -300,10 +292,12 @@
# 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 @@ -349,3 +343,20 @@
}
return(logparams.init)
}

##############################################################################
### Transform the log Matern parameters back to the original #########

unlog.params <- function(logparams, param.seq, P) {
params <- c(
exp(logparams[1:param.seq[2, 2]]),
gtools::inv.logit(logparams[param.seq[3, 1]:param.seq[3, 2]], 0, 2.5),
exp(logparams[param.seq[4, 1]:param.seq[4, 2]])
)
if (P > 1) {
params <- c(params, tanh(logparams[param.seq[5, 1]:param.seq[5, 2]]))
} else {
params <- c(params, 1)
}
return(params)
}
22 changes: 16 additions & 6 deletions R/PrestoGP_CreateU_Multivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ max_min_ordering <- function(locs, dist_func) {
#' @param dist_func Any distance function with a signature of dist(query_location, locations_matrix)
#'
#' @return A vector containing the indices of the neighbors
knn_indices <- function(ordered_locs, query, n_neighbors, dist_func, dist_func_code) {
knn_indices <- function(ordered_locs, query, n_neighbors,
dist_func, dist_func_code) {
if (dist_func_code == "custom") {
dists <- dist_func(query, ordered_locs)
dists_order <- order(dists)
Expand All @@ -90,25 +91,34 @@ knn_indices <- function(ordered_locs, query, n_neighbors, dist_func, dist_func_c
#' @param dist_func Any distance function with a signature of dist(query_location, locations_matrix)
#'
#' @return A list containing two matrices, each with one row per location: an indices matrix with the indices of nearest neighbors for each location, and a distance matrix with the associated distances
sparseNN <- function(ordered_locs, n_neighbors, dist_func, dist_func_code, ordered_locs_pred = NULL) {
sparseNN <- function(ordered_locs, n_neighbors,
dist_func, dist_func_code, ordered_locs_pred = NULL) {
ee <- min(apply(ordered_locs, 2, stats::sd))
n <- nrow(ordered_locs)
ordered_locs <- ordered_locs + matrix(
ee * 1e-04 *
stats::rnorm(n * ncol(ordered_locs)),
n, ncol(ordered_locs)
)
indices_matrix <- matrix(data = NA, nrow = nrow(ordered_locs), ncol = n_neighbors)
distances_matrix <- matrix(data = NA, nrow = nrow(ordered_locs), ncol = n_neighbors)
indices_matrix <- matrix(data = NA, nrow = nrow(ordered_locs),
ncol = n_neighbors)
distances_matrix <- matrix(data = NA, nrow = nrow(ordered_locs),
ncol = n_neighbors)
for (row in 1:n_neighbors) {
# for the locations from 1 to n_neighbors, use the entire locs list to find the neighbors
nn <- knn_indices(ordered_locs[1:(n_neighbors + 1), , drop = FALSE][-row, , drop = FALSE], ordered_locs[row, , drop = FALSE], n_neighbors, dist_func, dist_func_code)
nn <- knn_indices(ordered_locs[1:
(n_neighbors + 1), , drop = FALSE][-row, ,
drop = FALSE],
ordered_locs[row, , drop = FALSE], n_neighbors,
dist_func, dist_func_code)
indices_matrix[row, 1:n_neighbors] <- nn$indices[1:n_neighbors]
distances_matrix[row, 1:n_neighbors] <- nn$distances[1:n_neighbors]
}
for (row in (n_neighbors + 1):nrow(ordered_locs)) {
# get the m nearest neighbors from the locs before this one in the max-min order
nn <- knn_indices(ordered_locs[1:(row - 1), , drop = FALSE], ordered_locs[row, , drop = FALSE], n_neighbors, dist_func, dist_func_code)
nn <- knn_indices(ordered_locs[1:(row - 1), , drop = FALSE],
ordered_locs[row, , drop = FALSE], n_neighbors,
dist_func, dist_func_code)
indices_matrix[row, 1:n_neighbors] <- nn$indices[1:n_neighbors]
distances_matrix[row, 1:n_neighbors] <- nn$distances[1:n_neighbors]
}
Expand Down
Loading
Loading