Skip to content

Commit

Permalink
WIP: Fix linter warnings for 1:nrow, 1:ncol and 1:length. Increase li…
Browse files Browse the repository at this point in the history
…ne length to 160 chars
  • Loading branch information
sciome-bot committed Jan 12, 2024
1 parent 2db2e19 commit 6f6f91d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .lintr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
linters: linters_with_defaults(
line_length_linter = NULL,
line_length_linter(160),
commented_code_linter = NULL,
object_name_linter = NULL,
cyclocomp_linter = NULL)
Expand Down
4 changes: 2 additions & 2 deletions R/Log_Likelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ 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]

Expand Down Expand Up @@ -292,7 +292,7 @@ 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)
Expand Down
16 changes: 12 additions & 4 deletions R/PrestoGP_CreateU_Multivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ max_min_ordering <- function(locs, dist_func) {
# find the point closest to the mean of all points
dists <- dist_func(center, locs)
first <- which.min(dists)
unsolved <- 1:nrow(locs)
unsolved <- seq_len(nrow(locs))
unsolved <- unsolved[-first]
order <- c(first)

while (length(order) < nrow(locs)) {
max_min <- 0
max_min_i <- unsolved[1]
in_order <- locs[order[1:length(order)], ]
in_order <- locs[seq_along(order), ]
dim(in_order) <- c(length(order), ncol(locs))
for (i in unsolved) {
loc_i <- locs[i, ]
Expand Down Expand Up @@ -140,7 +140,7 @@ sparseNN <- function(ordered_locs, n_neighbors,
data = NA, nrow = nrow(ordered_locs_pred),
ncol = n_neighbors
)
for (row in 1:nrow(ordered_locs_pred)) {
for (row in seq_len(nrow(ordered_locs_pred))) {
nn <- knn_indices(
ordered_locs,
ordered_locs_pred[row, , drop = FALSE], n_neighbors,
Expand Down Expand Up @@ -291,7 +291,14 @@ vecchia_Mspecify <- function(locs.list, m, locs.list.pred = NULL,
olocs[-(1:n), , drop = FALSE]
)
}
last.obs <- max((1:length(obs))[obs])
# TODO: @Eric.Bair Can you verify the logic below is equivalent to the commented line?
# The linter does not allow using 1:length() due to a possibility of getting negative numbers.
if (any(obs)) {
last.obs <- max(which(obs))
} else {
last.obs <- NA
}
# last.obs <- max((1:length(obs))[obs])
q.list <- calc.q(nn.mat$indices, last.obs + 1)

return(list(
Expand Down Expand Up @@ -425,6 +432,7 @@ createUMultivariate <- function(vec.approx, params, cov_func = NULL) {
U1[7, ] <- c(1, 3, -1 * bi * ri^(-1 / 2))
# U[3,3] <- ri^(-1/2)
# U[1,3] <- -1*bi*ri^(-1/2)
i = NULL # lintr requirement

Check warning on line 435 in R/PrestoGP_CreateU_Multivariate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_CreateU_Multivariate.R,line=435,col=7,[assignment_linter] Use <-, not =, for assignment.
U2 <- foreach(i = 3:n, .combine = rbind) %dopar% {
# U[2*i,2*i] <- nugget[ondx[i]]^(-1/2)
# U[2*i-1,2*i] <- -1*U[2*i,2*i]
Expand Down
165 changes: 83 additions & 82 deletions R/PrestoGP_Model.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ setMethod(
cat("Covariance Parameters:\n")
Y_names <- colnames(object@Y_train)
if (is.null(Y_names)) {
Y_names <- unlist(lapply(1:ncol(object@Y_train), function(x) {
Y_names <- unlist(lapply(seq_len(ncol(object@Y_train)), function(x) {
paste("Outcome", x)
}))
}
Expand All @@ -111,21 +111,21 @@ setMethod(
# TODO compare to zero within a tolerance
# nnz_betas <- lapply(object@beta, 2, function(x){which(x != 0.0)})
nnz_betas <- list()
for (col in 1:ncol(object@Y_train)) {
for (col in seq_len(ncol(object@Y_train))) {
nnz_betas <- append(nnz_betas, list(which(object@beta[, col] != 0.0)))
}
X_names <- colnames(object@X_train)
if (is.null(X_names)) {
X_names <- unlist(lapply(1:ncol(object@X_train), function(x) {
X_names <- unlist(lapply(seq_len(ncol(object@X_train)), function(x) {
paste("Ind. Variable", x)
}))
}
X_names <- append("Intercept", X_names)
for (i in 1:ncol(object@Y_train)) {
for (i in seq_len(ncol(object@Y_train))) {
cat(Y_names[i], " Parameters:\n")
beta_summary <- data.frame(matrix(ncol = 4, nrow = 0, dimnames = list(NULL, c("Parameter", "Estimate", "Standard Error", "Walds P-value"))))
# for(nnz in nnz_betas[i]){
for (j in 1:length(nnz_betas[[i]])) {
for (j in seq_along(nnz_betas[[i]])) {
nnz <- nnz_betas[[i]][[j]]
walds <- wald.test(covm * mse[i, i], object@beta[, i], Terms = nnz)
std_err <- sqrt(diag(covm) * mse[i, i])
Expand All @@ -150,13 +150,13 @@ setMethod(
function(object, Y_names) {
theta_name_arr <- theta_names(object)
theta_summary <- data.frame(matrix(ncol = ncol(object@Y_train) + 1, nrow = length(theta_name_arr), dimnames = list(NULL, c("Parameter", Y_names))))
for (i in 1:length(theta_name_arr)) {
for (i in seq_along(theta_name_arr)) {
theta_row <- object@covparams[((i - 1) * ncol(object@Y_train) + 1):(i * ncol(object@Y_train))]
for (j in 1:ncol(object@Y_train)) {
for (j in seq_len(ncol(object@Y_train))) {
theta_summary[i, j + 1] <- theta_row[j]
}
}
for (j in 1:length(theta_name_arr)) {
for (j in seq_along(theta_name_arr)) {
theta_summary[j, 1] <- theta_name_arr[j]
}
print(theta_summary, row.names = FALSE)
Expand Down Expand Up @@ -207,34 +207,34 @@ setMethod(
parallel = FALSE, foldid = NULL) {
model <- check_input(model, Y, X, locs)
if (!is.null(beta.hat)) {
if (!is.vector(beta.hat) | !is.numeric(beta.hat)) {
stop("beta.hat parameter must be a numeric vector")
}
if (length(beta.hat) != (ncol(model@X_train) + 1)) {
stop("Length of beta.hat must match the number of predictors")
}
beta.hat <- as.matrix(beta.hat)
if (!is.vector(beta.hat) | !is.numeric(beta.hat)) {
stop("beta.hat parameter must be a numeric vector")
}
if (length(beta.hat) != (ncol(model@X_train) + 1)) {
stop("Length of beta.hat must match the number of predictors")
}
beta.hat <- as.matrix(beta.hat)
}
if (!is.numeric(tol)) {
stop("tol must be numeric")
}
if (length(tol) != 1) {
stop("tol must be a scalar")
}
if (tol<=0 | tol>1) {
if (tol <= 0 | tol > 1) {
stop("tol must satisfy 0<tol<=1")
}
if (is.null(scaling)) {
scaling <- rep(1, ncol(model@locs_train[[1]]))
nscale <- 1
scaling <- rep(1, ncol(model@locs_train[[1]]))
nscale <- 1
} else {
if (length(scaling) != ncol(model@locs_train[[1]])) {
stop("Length of scaling must equal ncol of locs")
}
nscale <- length(unique(scaling))
if (sum(sort(unique(scaling)) == 1:nscale) < nscale) {
stop("scaling must consist of sequential integers starting at 1")
}
if (length(scaling) != ncol(model@locs_train[[1]])) {
stop("Length of scaling must equal ncol of locs")
}
nscale <- length(unique(scaling))
if (sum(sort(unique(scaling)) == 1:nscale) < nscale) {
stop("scaling must consist of sequential integers starting at 1")
}
}
if (is.null(apanasovich)) {
if (nscale == 1) {
Expand Down Expand Up @@ -360,8 +360,8 @@ sparseToDenseBeta <- function(linear_model) {
}
beta_construct <- matrix(data = 0, nrow = coefs[[1]]@Dim[1], ncol = length(coefs))
# coefs[[1]]@Dim[1]+2s because dgCMatrix is 0 offset, and we want to include intercept
for (i in 1:length(coefs)) {
for (j in 1:length(coefs[[i]]@i)) {
for (i in seq_along(coefs)) {
for (j in seq_along(coefs[[i]]@i)) {
k <- coefs[[i]]@i[j]
# beta_construct[k+1,i] <- coefs[[i]]@x[j]
beta_construct[k + 1, i] <- coefs[[i]]@x[j]
Expand Down Expand Up @@ -418,64 +418,65 @@ setMethod("calc_covparams", "PrestoGPModel", function(model, locs, Y, covparams)
}
pseq <- create.param.sequence(P, model@nscale)
if (is.null(covparams)) {
col.vars <- rep(NA, P)
D.sample.bar <- rep(NA, model@nscale * P)
for (i in 1:P) {
col.vars[i] <- var(Y[[i]])
N <- length(Y[[i]])
# TODO find a better way to compute initial spatial range
for (j in 1:model@nscale) {
d.sample <- sample(1:N, max(2, ceiling(N / 50)), replace = FALSE)
D.sample <- rdist(locs[[i]][d.sample, model@scaling == j])
D.sample.bar[(i - 1) * model@nscale + j] <- mean(D.sample) / 4
}
col.vars <- rep(NA, P)
D.sample.bar <- rep(NA, model@nscale * P)
for (i in 1:P) {
col.vars[i] <- var(Y[[i]])
N <- length(Y[[i]])
# TODO find a better way to compute initial spatial range
for (j in 1:model@nscale) {
d.sample <- sample(1:N, max(2, ceiling(N / 50)), replace = FALSE)
D.sample <- rdist(locs[[i]][d.sample, model@scaling == j])
D.sample.bar[(i - 1) * model@nscale + j] <- mean(D.sample) / 4
}
model@logparams <- create.initial.values.flex(
c(0.9 * col.vars), # marginal variance
D.sample.bar, # range
rep(0.5, P), # smoothness
c(.1 * col.vars), # nuggets
rep(0, choose(P, 2)),
P
)
}
model@logparams <- create.initial.values.flex(
c(0.9 * col.vars), # marginal variance
D.sample.bar, # range
rep(0.5, P), # smoothness
c(.1 * col.vars), # nuggets
rep(0, choose(P, 2)),
P
)
} else {
if (P==1) {
if (length(covparams) != pseq[4,2]) {
stop("Incorrect number of parameters in covparams")
}
} else {
if (length(covparams) != pseq[5,2]) {
stop("Incorrect number of parameters in covparams")
}
}
init.var <- covparams[pseq[1,1]:pseq[1,2]]
init.range <- covparams[pseq[2,1]:pseq[2,2]]
init.smooth <- covparams[pseq[3,1]:pseq[3,2]]
init.nugget <- covparams[pseq[4,1]:pseq[4,2]]
if (P>1) {
init.corr <- covparams[pseq[5,1]:pseq[5,2]]
}
else {
init.corr <- 0
}
if (sum(init.var<=0)>0) {
stop("Initial variance estimates must be positive")
}
if (sum(init.range<=0)>0) {
stop("Initial range estimates must be positive")
if (P == 1) {
if (length(covparams) != pseq[4, 2]) {
stop("Incorrect number of parameters in covparams")
}
if (sum(init.nugget<=0)>0) {
stop("Initial nugget estimates must be positive")
}
if (sum(init.smooth<=0)>0 | sum(init.smooth>=2.5)>0) {
stop("Initial smoothness estimates must be between 0 and 2.5")
}
if (sum(init.corr < -1)>0 | sum(init.corr > 1)>0) {
stop("Initial correlation estimates must be between -1 and 1")
} else {
if (length(covparams) != pseq[5, 2]) {
stop("Incorrect number of parameters in covparams")
}
model@logparams <- create.initial.values.flex(init.var, init.range,
init.smooth, init.nugget,
init.corr, P)
}
init.var <- covparams[pseq[1, 1]:pseq[1, 2]]
init.range <- covparams[pseq[2, 1]:pseq[2, 2]]
init.smooth <- covparams[pseq[3, 1]:pseq[3, 2]]
init.nugget <- covparams[pseq[4, 1]:pseq[4, 2]]
if (P > 1) {
init.corr <- covparams[pseq[5, 1]:pseq[5, 2]]
} else {
init.corr <- 0
}
if (sum(init.var <= 0) > 0) {
stop("Initial variance estimates must be positive")
}
if (sum(init.range <= 0) > 0) {
stop("Initial range estimates must be positive")
}
if (sum(init.nugget <= 0) > 0) {
stop("Initial nugget estimates must be positive")
}
if (sum(init.smooth <= 0) > 0 | sum(init.smooth >= 2.5) > 0) {
stop("Initial smoothness estimates must be between 0 and 2.5")
}
if (sum(init.corr < -1) > 0 | sum(init.corr > 1) > 0) {
stop("Initial correlation estimates must be between -1 and 1")
}
model@logparams <- create.initial.values.flex(
init.var, init.range,
init.smooth, init.nugget,
init.corr, P
)
}
model@param_sequence <- pseq
model <- transform_covariance_parameters(model)
Expand All @@ -495,7 +496,7 @@ setMethod("scale_locs", "PrestoGPModel", function(model, locs) {
return(locs)
} else {
locs.out <- locs
for (i in 1:length(locs)) {
for (i in seq_along(locs)) {
for (j in 1:model@nscale) {
locs.out[[i]][, model@scaling == j] <-
locs[[i]][, model@scaling == j] /
Expand Down

0 comments on commit 6f6f91d

Please sign in to comment.