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)
}
}