Create a R package using rstan

I am creating a package in R that uses Rstan. When I try to iterate the model and use options(mc.cores = parallel::detectCores()) beforehand, it generates the error below.

If I run the iteration without using this code, it also works.

options(mc.cores = parallel::detectCores())
fit <- fit_ordinal_soccer(Model = "cumulative logit",formula = R ~ 1, Data = dados,home = "h",away = "a")

Error in checkForRemoteErrors(val) : 
  4 nodes produced errors; first error: objeto 'rstantools_model_categorical' não encontrado

Hi, @Taioba and welcome to the Stan forums.

It can help I you give us more context. This looks like it’s using a package other than Stan because there’s a formula argument to your fit argument.

Oops, I meant to ping @bgoodri as he’s the one who knows the most about building R packages on top of RStan.

I’m guessing that it’s a wrapper around running RStan that takes a formula as input (similar to rstanarm).

@Taioba do you mean that it runs ok without using multiple cores but always errors when you tell it to use multiple cores?

Agree that Ben knows the most about this

Yes, that’s correct. The program works fine when running on a single core, but it encounters errors when I try to enable multiple cores

fit_ordinal_soccer <- function(Model = c("Cumulative Logit", "Cumulative Probit", "Adjacents Categories Logit", "Continuation Ratio Logit"), formula, Data, home, away, hyperparameters = list(), ...) {
  default_hyperparameters <- list(
    location_hyp = 0,
    scale_hyp = 2.5,
    mu_c1_hyp = 0,
    sigma_c1_hyp = 5,
    mu_c2_hyp = 1,
    sigma_c2_hyp = 5,
    mu_beta_hyp = 0,
    sigma_beta_hyp = 10
  )
  hyperparameters <- modifyList(default_hyperparameters, hyperparameters)
  #
  name_response <- as.character(formula)[2]
  P <- update(formula, . ~ . - 1)
  P1 <- model.matrix(P, Data)
  #
  Model_lw <- stringr::str_to_lower(Model)
  models <- c("cumulative logit", "cumulative probit", "adjacents categories logit", "continuation ratio logit")
  index_model <- match(Model_lw, models)
  if (is.na(index_model)) {
    stop(paste(
      "Error: model not found.\n",
      "It looks like the model'", Model, "' doesn´t exist. Please check the name and try again.\n"
    ))
  }
  #
  col_df <- names(Data)
  col_param <- list(home, away)
  col_nfound <- setdiff(col_param, col_df)
  if (length(col_nfound) > 0) {
    stop(paste(
      "columns specified as home and away were not found.",
      paste(col_nfound, collapse = ", ")
    ))
  }
  #
  z <- unique(c(Data[[home]], Data[[away]]))
  teams <- length(z)
  aux_home <- 0
  aux_away <- 0
  if (!is.numeric(z)) {
    teams_id <- factor(z)
    Data$index_home <- as.integer(factor(Data[[home]], levels = levels(teams_id)))
    Data$index_away <- as.integer(factor(Data[[away]], levels = levels(teams_id)))
    #
    D <- matrix(c(Data[[home]], Data[[away]], Data$index_home, Data$index_away), ncol = 2)
    colnames(D) <- c("Teams", "Index")
    index_dict <- unique(D)
    index_dict <- index_dict[order(index_dict[, 1]), ]
    aux_home <- Data$index_home
    aux_away <- Data$index_away
  } else {
    aux_home <- Data[[home]]
    aux_away <- Data[[away]]
  }
  #
  aux_M <- P1
  if (ncol(P1) == 0) {
    aux_M <- matrix(nrow = 0, ncol = 0)
  }
  data_stan <- list(
    Model = index_model,
    G = nrow(Data),
    T = teams,
    h = aux_home,
    a = aux_away,
    R = Data[[name_response]],
    p = ncol(P1),
    X = aux_M
  )
  data_stan <- append(data_stan, hyperparameters)
  fit <- rstan::sampling(stanmodels$categorical, data = data_stan, ...)
  if (!is.numeric(z)) {
    return(list(fit = fit, index_dict = index_dict, data = Data))
  } else {
    return(fit)
  }
}