# Sampling method does not work

**URL:** <https://discourse.mc-stan.org/t/sampling-method-does-not-work/26204>\
**Category:** CmdStan\
**Tags:** cmdstan, cmdstanr\
**Created:** [February 3, 2022, 7:47am UTC](https://discourse.mc-stan.org/t/sampling-method-does-not-work/26204 "2022-02-03T07:47:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![emilija](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@emilija](https://discourse.mc-stan.org/u/emilija)\
**Post date:** [February 3, 2022, 7:47am UTC](https://discourse.mc-stan.org/t/sampling-method-does-not-work/26204/1 "2022-02-03T07:47:07Z")

</div>

Hello, I am trying to implement Pathfinder algorithm in R, hoever, I get stuck in this step:

```no-highlight
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!

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [February 3, 2022, 7:57am UTC](https://discourse.mc-stan.org/t/sampling-method-does-not-work/26204/2 "2022-02-03T07:57:13Z")

</div>

Hi check getting started with CmdStanR to see how sample should be called

> **[Getting started with CmdStanR](https://mc-stan.org/cmdstanr/articles/cmdstanr.html)**
>
> cmdstanr
