Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
helske committed Jul 10, 2024
1 parent e6d7f0e commit 4d676bd
Show file tree
Hide file tree
Showing 29 changed files with 194 additions and 165 deletions.
11 changes: 3 additions & 8 deletions R/as_data_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ as.data.table.dynamitefit <- function(x, keep.rownames = FALSE,
permuted = FALSE
)
}
channel <- get_channel(x, response)
idx <- which(names(x$stan$responses) %in% response)
resps <- ifelse_(
identical(length(idx), 0L),
Expand Down Expand Up @@ -431,7 +430,6 @@ as_data_table_nu <- function(x, draws, n_draws, response, category, ...) {
"nu_", response, "_",
c(icpt, names(get_channel(x, response)$J_random))
)
n_vars <- length(var_names)
groups <- sort(unique(x$data[[x$group_var]]))
n_group <- length(groups)
data.table::data.table(
Expand Down Expand Up @@ -482,7 +480,6 @@ as_data_table_beta <- function(x, draws, n_draws, response, category, ...) {
"beta_", response, "_",
names(get_channel(x, response)$J_fixed)
)
n_vars <- length(var_names)
data.table::data.table(
parameter = rep(var_names, each = n_draws),
value = c(draws),
Expand Down Expand Up @@ -540,7 +537,6 @@ as_data_table_tau <- function(x, draws, n_draws, response, category, ...) {
#' @describeIn as_data_table_default Data Table for a "omega" Parameter
#' @noRd
as_data_table_omega <- function(x, draws, n_draws, response, category, ...) {
n_cat <- length(category)
D <- x$stan$model_vars$D
var_names <- paste0(
"omega_", response, "_",
Expand Down Expand Up @@ -569,8 +565,8 @@ as_data_table_omega <- function(x, draws, n_draws, response, category, ...) {

#' @describeIn as_data_table_default Data Table for a "omega_alpha" Parameter
#' @noRd
as_data_table_omega_alpha <-function(x, draws, n_draws, response,
category, ...) {
as_data_table_omega_alpha <- function(x, draws, n_draws, response,
category, ...) {
D <- x$stan$model_vars$D
data.table::data.table(
parameter = rep(
Expand Down Expand Up @@ -626,7 +622,6 @@ as_data_table_phi <- function(draws, response, ...) {
#' @describeIn as_data_table_default Data Table for a "lambda" Parameter
#' @noRd
as_data_table_lambda <- function(x, draws, n_draws, response, ...) {
n_group <- dim(draws)[3L]
data.table::data.table(
parameter = paste0("lambda_", response),
value = c(draws),
Expand Down Expand Up @@ -722,7 +717,7 @@ as_data_table_corr <- function(x, draws, n_draws, resps, ...) {
#' @describeIn as_data_table_default Data Table for a "cutpoint" Parameter
#' @noRd
as_data_table_cutpoint <- function(x, draws, response,
n_draws, include_fixed, ...) {
n_draws, include_fixed, ...) {
channel <- get_channel(x, response)
S <- channel$S
fixed <- x$stan$fixed
Expand Down
4 changes: 2 additions & 2 deletions R/as_draws.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ as_draws_df.dynamitefit <- function(x, parameters = NULL, responses = NULL,
types = NULL, times = NULL,
groups = NULL, ...) {
# avoid NSE notes from R CMD check
.chain <- .draw <- .iteration <- NULL
category <- group <- parameter <- response <- time <- type <- value <- NULL
.chain <- .iteration <- NULL
category <- group <- parameter <- time <- NULL
d <- as.data.table.dynamitefit(
x,
parameters = parameters,
Expand Down
3 changes: 0 additions & 3 deletions R/dynamice.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ dynamice <- function(dformula, data, time, group = NULL,
e <- new.env()
sf <- vector(mode = "list", length = m)
filenames <- character(m)
model <- NULL
tmp <- NULL
for (i in seq_len(m)) {
data_imputed <- ifelse_(
Expand Down Expand Up @@ -372,7 +371,6 @@ impute_wide <- function(dformula, data, time, group, mice_args) {
if (length(value_vars) == 1L) {
names(data_wide)[-1L] <- paste0(value_vars, "_", names(data_wide)[-1L])
}
wide_vars <- names(data_wide)[-1L]
mice_args$data <- data_wide
n_time <- n_unique(data[[time]])
pred_mat <- parse_predictors_wide(
Expand All @@ -393,7 +391,6 @@ impute_wide <- function(dformula, data, time, group, mice_args) {
#' @param all_vars \[`character()`]\cr Names of all data variables.
#' @noRd
parse_predictors_long <- function(dformula, time_var, group_var, all_vars) {
resp <- get_responses(dformula)
value_vars <- setdiff(all_vars, c(time_var, group_var))
pred_vars <- c(value_vars, time_var, group_var)
n_vars <- length(value_vars)
Expand Down
6 changes: 4 additions & 2 deletions R/dynamite-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
#' @format A `dynamitefit` object.
"gaussian_example_fit"

# #' Model Fit for the time-varying example in the `dynamite_simulation` Vignette
# #' Model Fit for the time-varying example in the `dynamite_simulation`
# #' Vignette
# #'
# #' A `dynamitefit` object obtained by running `dynamite` with the
# #' `"Fixed_param"` algorithm on the specified `inits` in the example.
Expand Down Expand Up @@ -232,7 +233,8 @@
# #' \describe{
# #' \item{y}{A continuos variable.}
# #' \item{id}{Variable defining individuals (1 to 100).}
# #' \item{time}{Variable defining the time point of the measurement (1 to 20).}
# #' \item{time}{Variable defining the time point of the measurement
# #' (1 to 20).}
# #' }
# "latent_factor_example"

Expand Down
5 changes: 2 additions & 3 deletions R/dynamite.R
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ check_stan_args <- function(dots, verbose, backend) {
)
dots_names[original_args] <- converted_args
names(dots)[original_args] <- converted_args
if (identical(backend, "cmdstanr") ) {
if (identical(backend, "cmdstanr")) {
valid_args <- !dots_names %in% cmdstanr_deprecated_args
deprecated_args <- dots_names[!valid_args]
if (verbose && any(deprecated_args)) {
Expand Down Expand Up @@ -904,7 +904,6 @@ parse_data <- function(dformula, data, group_var, time_var, verbose) {
val <- do.call(paste0("as.", type), args = list(col))
data.table::set(data, j = j, value = val)
}
resp <- get_responses(dformula)
finite_cols <- vapply(
data,
function(x) all(is.finite(x) | is.na(x)),
Expand Down Expand Up @@ -1017,7 +1016,7 @@ parse_components <- function(dformulas, data, group_var, time_var) {
"Cannot estimate latent factors using only one group."
)

if (attr(dformulas$stoch, "lfactor")$has_lfactor) {
if (attr(dformulas$stoch, "lfactor")$has_lfactor) {
nz <- which(attr(dformulas$stoch, "lfactor")$nonzero_lambda)
if (length(nz) > 0L) {
lresp <- attr(dformulas$stoch, "lfactor")$responses
Expand Down
2 changes: 1 addition & 1 deletion R/latent_factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#' )
#'
lfactor <- function(responses = NULL, nonzero_lambda = TRUE, correlated = TRUE,
noncentered_psi = FALSE, flip_sign = TRUE) {
noncentered_psi = FALSE, flip_sign = TRUE) {
stopifnot_(
checkmate::test_character(x = responses, min.len = 1L, null.ok = TRUE),
"Argument {.arg responses} must be a {.cls character} vector."
Expand Down
2 changes: 1 addition & 1 deletion R/lfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ lfo.dynamitefit <- function(x, L, verbose = TRUE, k_threshold = 0.7, ...) {
df = FALSE
)$simulated
# avoid NSE notes from R CMD check
loglik <- patterns <- .draw <- group <- groups <- time <- NULL
loglik <- patterns <- group <- groups <- time <- NULL
# sum the log-likelihood over the channels and non-missing time points
# for each group, time, and draw
# drop those id&time pairs which contain NA
Expand Down
20 changes: 10 additions & 10 deletions R/model_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ full_model.matrix <- function(dformula, data, group_var, fixed, verbose) {
vector(mode = "list", length = length(model_matrices)),
y_names
)
attr(model_matrix, "assign") <- empty_list
#attr(model_matrix, "assign") <- empty_list
attr(model_matrix, "fixed") <- empty_list
attr(model_matrix, "varying") <- empty_list
attr(model_matrix, "random") <- empty_list
Expand All @@ -60,15 +60,15 @@ full_model.matrix <- function(dformula, data, group_var, fixed, verbose) {
attr(model_matrix, type)[[i]] <- integer(0L)
}
}
attr(model_matrix, "assign")[[i]] <- sort(
unique(
c(
attr(model_matrix, "fixed")[[i]],
attr(model_matrix, "varying")[[i]],
attr(model_matrix, "random")[[i]]
)
)
)
# attr(model_matrix, "assign")[[i]] <- sort(
# unique(
# c(
# attr(model_matrix, "fixed")[[i]],
# attr(model_matrix, "varying")[[i]],
# attr(model_matrix, "random")[[i]]
# )
# )
# )
}
model_matrix
}
Expand Down
2 changes: 0 additions & 2 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ plot_dynamiteformula_tikz <- function(g) {
plot_dynamiteformula_ggplot <- function(g, vertex_size, label_size) {
# avoid NSE notes from R CMD check
var_expr <- NULL
v <- colnames(g$A)
layout <- g$layout
edgelist <- g$edgelist
layout$var_expr <- gsub("(.+)_\\{(.+)\\}", "\\1\\[\\2\\]", layout$var)
Expand Down Expand Up @@ -551,7 +550,6 @@ plot_varying <- function(coefs, level, alpha, scales, n_params) {
return(NULL)
}
coefs <- filter_params(coefs, n_params, 3)
n_coefs <- nrow(coefs)
title_spec <- "time-varying parameters"
if (n_unique(coefs$type) == 1L) {
title_spec <- switch(
Expand Down
4 changes: 2 additions & 2 deletions R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ predict.dynamitefit <- function(object, newdata = NULL,
#' `"loglik"`.
#' @noRd
initialize_predict <- function(object, newdata, type, eval_type, funs, impute,
new_levels, global_fixed, idx_draws, expand, df) {
n_draws <- length(idx_draws)
new_levels, global_fixed, idx_draws, expand,
df) {
newdata_null <- is.null(newdata)
newdata <- check_newdata(object, newdata)
fixed <- as.integer(attr(object$dformulas$all, "max_lag"))
Expand Down
16 changes: 8 additions & 8 deletions R/prepare_stan_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ prepare_stan_input <- function(dformula, data, group_var, time_var,
time <- sort(unique(data[[time_var]]))
T_full <- length(time)
T_idx <- seq.int(fixed + 1L, T_full)
has_groups <- !is.null(group_var)
group <- data[[group_var]]
spline_def <- attr(dformula, "splines")
random_def <- attr(dformula, "random_spec")
Expand Down Expand Up @@ -97,7 +96,6 @@ prepare_stan_input <- function(dformula, data, group_var, time_var,
X_na <- is.na(X)
# Placeholder for NAs in Stan
X[X_na] <- 0.0
assigned <- attr(model_matrix, "assign")
fixed_pars <- attr(model_matrix, "fixed")
varying_pars <- attr(model_matrix, "varying")
random_pars <- attr(model_matrix, "random")
Expand Down Expand Up @@ -266,10 +264,10 @@ prepare_stan_input <- function(dformula, data, group_var, time_var,
model_vars$P <- sampling_vars$P
model_vars$D <- sampling_vars$D
model_vars$K <- K
model_vars$common_priors = prior_list$common_priors
model_vars$spline_def = spline_def
model_vars$random_def = random_def
model_vars$lfactor_def = lfactor_def
model_vars$common_priors <- prior_list$common_priors
model_vars$spline_def <- spline_def
model_vars$random_def <- random_def
model_vars$lfactor_def <- lfactor_def
list(
channel_vars = channel_vars,
channel_group_vars = channel_group_vars,
Expand Down Expand Up @@ -436,7 +434,9 @@ initialize_multivariate_channel <- function(y, y_cg, y_name, cg_idx,
sampling[[paste0("n_obs_", y_cg)]] <- apply(
sampling[[paste0("obs_", y_cg)]],
2L,
function(x) { sum(x > 0L) }
function(x) {
sum(x > 0L)
}
)
sampling[[paste0("t_obs_", y_cg)]] <- which(
sampling[[paste0("n_obs_", y_cg)]] > 0L
Expand Down Expand Up @@ -992,7 +992,7 @@ prepare_channel_multinomial <- function(y, y_cg, Y, channel, sampling,
abort_factor(y_cg, "Multinomial", call = rlang::caller_env())
}
obs <- sampling[[paste0("n_obs_", y_cg)]] > 0L
Y_obs <- Y[obs, , ,drop = FALSE]
Y_obs <- Y[obs, , , drop = FALSE]
if (any(Y_obs < 0.0) || any(Y_obs != as.integer(Y_obs))) {
abort_negative(
y_cg,
Expand Down
3 changes: 2 additions & 1 deletion R/priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ default_priors <- function(y, channel, mean_gamma, sd_gamma, mean_y, sd_y,
if (is_cumulative(channel$family)) {
prior_distributions$tau_alpha_prior_distr <-
paste0("normal(", rep(0, channel$S - 1), ", ", sd_y, ")")
names(prior_distributions$tau_alpha_prior_distr) <- seq_len(channel$S - 1)
names(prior_distributions$tau_alpha_prior_distr) <-
seq_len(channel$S - 1)
} else {
prior_distributions$tau_alpha_prior_distr <-
paste0("normal(0, ", sd_y, ")")
Expand Down
2 changes: 1 addition & 1 deletion R/stan_utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ stan_array_arg <- function(backend, type, name, n_dims = 0, data = FALSE) {
ifelse_(
stan_supports_array_keyword(backend),
paste0(data, "array[", commas, "] ", type, " ", name),
paste0(data, type, "[",commas, "] ", name)
paste0(data, type, "[", commas, "] ", name)
)
}

Expand Down
3 changes: 1 addition & 2 deletions R/stanblocks.R
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,7 @@ create_parameters_lines <- function(idt, backend, cvars, cgvars) {
par_alpha,
.parse = FALSE
)
}
else {
} else {
cvars[[1L]]$default <- lines_wrap(
"parameters", "default", idt, backend, cvars[[1L]]
)
Expand Down
6 changes: 4 additions & 2 deletions R/stanblocks_families.R
Original file line number Diff line number Diff line change
Expand Up @@ -2257,7 +2257,8 @@ model_lines_multinomial <- function(cvars, cgvars, idt, backend,
)
}

model_lines_mvgaussian <- function(cvars, cgvars, idt, backend, threading, ...) {
model_lines_mvgaussian <- function(cvars, cgvars, idt, backend, threading,
...) {

y <- cgvars$y
y_cg <- cgvars$y_cg
Expand Down Expand Up @@ -2319,7 +2320,8 @@ model_lines_mvgaussian <- function(cvars, cgvars, idt, backend, threading, ...)
" {fun_args});"
)
} else {
likelihood <- loglik_lines_mvgaussian(idt, cvars, cgvars, backend, threading)
likelihood <- loglik_lines_mvgaussian(idt, cvars, cgvars, backend,
threading)
}
model_text <- paste_rows(
glue::glue("L_{y_cg} ~ {cgvars$prior_distr$L_prior_distr};"),
Expand Down
4 changes: 2 additions & 2 deletions data-raw/gaussian_example_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ set.seed(1)
gaussian_example_fit <- dynamite(
dformula =
obs(y ~ -1 + z + varying(~ x + lag(y)) + random(~1), family = "gaussian") +
random_spec() +
splines(df = 20),
random_spec() +
splines(df = 20),
data = gaussian_example,
time = "time",
group = "id",
Expand Down
Loading

0 comments on commit 4d676bd

Please sign in to comment.