DasBetterR

jmh530 john.michael.hall at gmail.com
Tue Aug 1 12:37:58 UTC 2023


On Monday, 31 July 2023 at 21:36:45 UTC, bachmeier wrote:
> On Friday, 7 July 2023 at 21:54:12 UTC, jmh530 wrote:
>
>> Cool.
>>
>> The main thing I want to try is rstan. They have an interface 
>> called cmdstan that you can call from the command line that 
>> would be possible to use with D. The problem is that you have 
>> to write the data to a CSV file and then read it. So it would 
>> be kind of slow and I never got around to playing around with 
>> it in D. With your tool as it is, I would just have to copy 
>> the data in memory, which I would expect not to be as bad of 
>> an overhead as IO (but again haven't gotten around to do 
>> anything with it).
>
> I just pushed an update with a minimal example of how to call 
> RStan with the following workflow:
>
> - Allocate a double[] in D and fill it with data.
> - Use a custom allocator to pass a pointer to the double[] to R.
> - Call RStan functions using the data.
> - Pull the results into D.
>
> Note that:
>
> - This is the first I've used RStan. My previous (limited) 
> usage of Stan was at the command line. In practice, you'd want 
> to create a better interface, so that it feels like D rather 
> than passing strings of code around. I'm not familiar enough 
> with RStan to do that.
> - The custom allocator requires leaving 80 bits immediately 
> preceding the data free for R to store its metadata related to 
> the vector. There's no way around that as R would not know what 
> to do without the metadata.
>
> https://github.com/bachmeil/betterr/blob/main/testing/teststan.d

Very cool. I definitely will need to play around with it.

I typically save a ".stan" file with the program and then call 
the `stan` function with a file parameter to where it is saved 
(but how you did it is easier to understand for an example since 
everything is in this file). The `stan` function combines the 
`stan_model` and `sampling` functionality together.

You mention creating a better interface, but recall for rstan to 
work you need to put the data into an R list with names that 
match what is in your program (your stan program could be all 
kinds of complicated with all kinds of different naming 
conventions). For it to be seamless, you would need to be able to 
pass a named D tuple to R easily. rstanarm might be easier to 
create initially, but I don't use that much.

Anyway, it looks like you have done the hard work in terms of 
figuring out the code needed to pass a pointer to double[] from D 
to R. Is that something that you will incorporate into the 
broader project? That could be pretty useful more generally 
(suppose most of my analysis is in D, but then I really need one 
little thing from R that D doesn't have a library for).




More information about the Digitalmars-d-announce mailing list