fslaborg / RProvider

Access R packages from F#
http://fslab.org/RProvider/
Other
235 stars 69 forks source link

R Type Provider and Stan: model compilation failed with "Error in cpp_object_initializer" #187

Closed easysoft2k15 closed 2 years ago

easysoft2k15 commented 6 years ago

I'm trying to use Stan with R Type Provider. Stan is already setup and working on RStudio and model compile and works fine. When a try from F# it fail compilation with error:

Error in cpp_object_initializer(.self, .refClassDef, ...) : could not find function "cpp_object_initializer" failed to create the sampler; sampling not done

I suspect is due to some further setup I need to make in F# code but after a few tries I'm stuck. Any help would be appreciated!

Here F# code:

#r @"C:\Users\alessandro\Documents\Visual Studio 2017\Projects\ConsoleApplication2\packages\R.NET.Community.1.6.5\lib\net40\RDotNet.dll"
#r @"C:\Users\alessandro\Documents\Visual Studio 2017\Projects\ConsoleApplication2\packages\R.NET.Community.FSharp.1.6.5\lib\net40\RDotNet.FSharp.dll"
#r @"C:\Users\alessandro\Documents\Visual Studio 2017\Projects\ConsoleApplication2\packages\R.NET.Community.1.6.5\lib\net40\RDotNet.NativeLibrary.dll"
#r @"C:\Users\alessandro\Documents\Visual Studio 2017\Projects\ConsoleApplication2\packages\RProvider.1.1.20\lib\net40\RProvider.dll"
#r @"C:\Users\alessandro\Documents\Visual Studio 2017\Projects\ConsoleApplication2\packages\RProvider.1.1.20\lib\net40\RProvider.Runtime.dll"

open System
open RDotNet
open RProvider
open RProvider.graphics
open RProvider.stats
open RProvider.rstan
open RProvider.ggplot2
open RProvider.methods
open RProvider.Rcpp
open RProvider.StanHeaders
open RProvider.rstudioapi

let fit=R.stan(file= @"C:\Users\alessandro\Documents\Visual Studio 2017\Projects\ConsoleApplication2\ConsoleApplication2\normal.stan")

Here stan model (normal.stan)

parameters {
  real y;
}

model {
  target+= normal_lpdf(y|0,1);
}
AndrewIOM commented 2 years ago

I have tried to reproduce this in the current RProvider (.net 5 version). The same script and model runs for me on macOS with R 4.1.1.

Script:

#I "../src/RProvider/bin/Debug/net5.0/"
#r "RDotNet.dll"
#r "RProvider.Runtime.dll"
#r "RProvider.DesignTime.dll"
#r "RProvider.dll"

open System
open RDotNet
open RProvider
open RProvider.graphics
open RProvider.stats
open RProvider.rstan
open RProvider.ggplot2
open RProvider.methods
open RProvider.Rcpp
open RProvider.StanHeaders
open RProvider.rstudioapi

let fit = R.stan(file= "normal.stan")
parameters {
  real y;
}

model {
  target+= normal_lpdf(y|0,1);
}

The FSI output is:

Loading required package: rstudioapi
Loading required package: StanHeaders
Loading required package: Rcpp
Loading required package: ggplot2
Loading required package: rstan
rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)

SAMPLING FOR MODEL 'normal' NOW (CHAIN 1).
Chain 1: 
Chain 1: Gradient evaluation took 1.1e-05 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.11 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0.009977 seconds (Warm-up)
Chain 1:                0.010099 seconds (Sampling)
Chain 1:                0.020076 seconds (Total)
Chain 1: 

SAMPLING FOR MODEL 'normal' NOW (CHAIN 2).
Chain 2: 
Chain 2: Gradient evaluation took 9e-06 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.09 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2: 
Chain 2: 
Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 2: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 2: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 2: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 2: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 2: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 2: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 2: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 2: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 2: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 2: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 2: 
Chain 2:  Elapsed Time: 0.011078 seconds (Warm-up)
Chain 2:                0.010809 seconds (Sampling)
Chain 2:                0.021887 seconds (Total)
Chain 2: 

SAMPLING FOR MODEL 'normal' NOW (CHAIN 3).
Chain 3: 
Chain 3: Gradient evaluation took 1.3e-05 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.13 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3: 
Chain 3: 
Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 3: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 3: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 3: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 3: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 3: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 3: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 3: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 3: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 3: 
Chain 3:  Elapsed Time: 0.011508 seconds (Warm-up)
Chain 3:                0.011385 seconds (Sampling)
Chain 3:                0.022893 seconds (Total)
Chain 3: 

SAMPLING FOR MODEL 'normal' NOW (CHAIN 4).
Chain 4: 
Chain 4: Gradient evaluation took 1.1e-05 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.11 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4: 
Chain 4: 
Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 4: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 4: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 4: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 4: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 4: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 4: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 4: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 4: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 4: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 4: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 4: 
Chain 4:  Elapsed Time: 0.011565 seconds (Warm-up)
Chain 4:                0.010064 seconds (Sampling)
Chain 4:                0.021629 seconds (Total)
Chain 4: 
val fit : SymbolicExpression
AndrewIOM commented 2 years ago

Closing as seemingly fixed in latest version. Please open again if you find the same issue again.