Hello, I am trying to implement Pathfinder algorithm in R, hoever, I get stuck in this step:
rm(list = ls())
library(tidyverse)
library(cmdstanr)
library(rstan)
library(brms)
tryCatch({cmdstanr::set_cmdstan_path('/home/cmdstanr/cmdstan-2.28.2')})
cmdstan_version()
parallel::detectCores(all.tests = TRUE, logical = TRUE)
# ------------------------------------------------------------------------------
# Generate data
# ------------------------------------------------------------------------------
N <- 1000
nchains = 3
ncores = 3
nthreads = 8
data0 <-
tibble(
x = rnorm(N, 0, 1), # x = F1
x1 = rnorm(N, 1*x, 1),
x2 = rnorm(N, 0.5*x, 1),
x3 = rnorm(N, 1*x, 1),
F1 = as.numeric(NA)
)
bf0 = x ~ x1+ x2 +(1| x3)
prior0 <- prior(constant(1), class = "b", resp = "x1") +
prior(constant(1), class = "sigma", resp = "x1") +
prior(normal(0, 10), class = "b", resp = "x2") +
prior(constant(1), class = "sigma", resp = "x2")
threads = 2 # needs to be tuned, as more threads does not guarantee better performance in the case of latent variable model
# make native stan code from brm objects and save the code as stan object
my_stan_code <- make_stancode(
formula = bf0,
data = data0,
#family = gaussian(),
# prior = prior0,
autocor = NULL,
data2 = NULL,
cov_ranef = NULL,
sparse = NULL,
sample_prior = "no",
stanvars = NULL,
stan_funs = NULL,
knots = NULL,
threads = threading(threads = threads),
normalize = getOption("brms.normalize", TRUE),
save_model = '/experiments/psc_20220117.stan'
)
# make native stan data structure from brm objects
my_stan_data <- make_standata(
formula = bf0,
data = data0,
#family = gaussian(),
# prior = prior0,
autocor = NULL,
data2 = NULL,
cov_ranef = NULL,
sample_prior = "no",
stanvars = NULL,
threads = threading(threads = threads),
knots = NULL
)
# need to unclass to work in further steps
my_stan_data <- unclass(my_stan_data)
# need to specify missing parameter
# this parameter needs to be tuned!!!!
# https://mc-stan.org/docs/2_23/stan-users-guide/reduce-sum.html
my_stan_data$grainsize <- max( c(100,ceiling( dim(data0)[1]/(1*threads) )) )
# download stan code
mod <- cmdstan_model(stan_file = 'experiments/psc_20220117.stan',
include_paths = '/experiments/')
posterior <- sampling(mod, data = my_stan_data, chains = 1, iter = 1, refresh = 0,algorithm = "Fixed_param")
I get error:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘sampling’ for signature ‘“CmdStanModel”’
My session info:
sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS
Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] brms_2.16.1 Rcpp_1.0.8 optimx_2021-10.12 matrixStats_0.61.0 Matrix_1.3-3 patchwork_1.1.1
[7] bayesplot_1.8.1 loo_2.4.1 posterior_1.1.0 cmdstanr_0.4.0.9001 forcats_0.5.1 stringr_1.4.0
[13] dplyr_1.0.7 purrr_0.3.4 readr_2.1.0 tidyr_1.1.3 tibble_3.1.6 ggplot2_3.3.5
[19] tidyverse_1.3.1 foreach_1.5.1 rstan_2.26.4 StanHeaders_2.26.4
Any help would be appreciated!