Git Product home page Git Product logo

Comments (14)

hmansell avatar hmansell commented on July 26, 2024

Generally, using the R apply functions is sub-optimal. F# has far better functionality and syntax for apply-like operations as it has first-class support for functional programming. Similarly, F# and the .NET ecosystem has far better functionality than R for parallelism and asynchronous processing, so you don't need the parallel library.

However, where this gets thorny is when you want to execute some R function in parallel, which sounds like what you want to do. I've never tried running multi-threaded code using the R interpreter via the RProvider. My guess is that the R interpreter isn't properly thread-safe, but that is just a hunch.

We run R computations on a large compute grid, by using a .NET based distribution of the computation, and ensuring the R DLLs/libraries are available on each machine.

In terms of actually trying to use clusterApply, we don't currently have a way to pass a function - it's slightly tricky to implement something like that. The difficulty is that we can only pass a pure R function - we don't have a way to call back into .NET code. So we would need some way of doing that, probably using F# quotations. But it doesn't work right now.

from rprovider.

renkun-ken avatar renkun-ken commented on July 26, 2024

Thank you for your information. My main concern is computing performance. My work needs to intensively call R statistical fitting models to fit data like ARIMA and GARCH. Currently RDotNet does not yet support multithreading so .NET Parallelism does not work for RDotNet, nor for RProvider either. I did some performance tests to see how long it takes to run 1000 linear model fit for data of length 200/500/1000, or arima model fit and garch model fit for different lengths of data, in contrast with what takes to run the same fit in R. Unfortunately, in some cases it takes much longer time to run in F# (e.g. run 1000 iterations of arima model fit for data of length 50) than in R, which makes it desirable to call parallel computing in R. Is there any suggestion to boost the performance?

from rprovider.

sergey-tihon avatar sergey-tihon commented on July 26, 2024

I also faced with such case, when I need to define custom function and pass it to R.

Please look at Learning Kernels SVM. There we define custom kernel for svm and then pass this kernel for ksvm as a parameter. As I see, I cannot do this using RProvider.

Idea about F# quotations sound really cool (probably it is best option even if it is hard).

But probably we can define simple-to-use function

val execute : (string -> SymbolicExpression)

that execute R code from string and returns result back.

from rprovider.

hmansell avatar hmansell commented on July 26, 2024

You can probably do something using R.parse and R.eval, passing the necessary syntax to define a function to parse, then evaluating the result. I assume what you get back will be a SymbolicExpression representing a function, that you can pass into another function.

from rprovider.

renkun-ken avatar renkun-ken commented on July 26, 2024

I tried and it does not work:

let fun1 = R.eval("function(i) {mean(rnorm(i))}")
let nums = R.sapply(R.c(1,2,3),fun1)

which reports error:
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'function(i) {mean(rnorm(i))}' of mode 'function' was not found
The same error occurs when I use R.parse().

But I find a way like this:
First create an arbitrary R object like

let x = R.c(0)

Then we can use x.Engine to refer to REngine object the RProvider creates,

let fun1 = x.Engine.Evaluate("function(i) {mean(rnorm(i))}")
let nums = R.sapply(R.c(1,2,3),fun1)

This works perfectly.

from rprovider.

hmansell avatar hmansell commented on July 26, 2024

R.eval on a string produces something of type character. You have to call eval on the result of parse in order to get the expression evaluated. See http://stackoverflow.com/questions/1743698/r-eval-expression.

from rprovider.

renkun-ken avatar renkun-ken commented on July 26, 2024

Yes, thanks, it points out the problem exactly.

let fun1 = R.eval(R.parse(text="function(i) {mean(rnorm(i))}"))
let funs = R.sapply(R.c(1,2,3),fun1)

This works fine as suggested.

from rprovider.

hmansell avatar hmansell commented on July 26, 2024

Great!

So given that we have this solution, doesn't seem necessary to have an explicit function, but it probably deserves some documentation.

Ken - would you care to submit a pull-request with an appropriate change?

from rprovider.

renkun-ken avatar renkun-ken commented on July 26, 2024

I added a subsection “Creating and passing an R function” in the How-to wiki page

from rprovider.

hmansell avatar hmansell commented on July 26, 2024

Thanks!

I see you added this to the wiki, but now the main area of documentation is http://bluemountaincapital.github.io/FSharpRProvider/. @tpetricek - do you think we should disable the wiki (assuming that is possible). And can you advise how @renkun-ken can update the docs?

from rprovider.

tpetricek avatar tpetricek commented on July 26, 2024

I think we ported all existing content from the wiki to the new documentation, so I think we can disable it (there is a way to do that - but let's wait until @renkun-ken moves the new content).

@renkun-ken To add new content to the documentation, simply create a new F# Script file in the docs/content folder (here) - follow the pattern used in tutorial.fsx. If you start a comment with (**, then it will be recognized as a Markdown block. Code snippets will be nicely formatted automatically next time we publish the documentation.

We are using F# Formatting and you can find more information here

from rprovider.

renkun-ken avatar renkun-ken commented on July 26, 2024

Thank you for your kindly help, @hmansell and @tpetricek! I just send a pull request for the newly added subsection of creating R functions to passing-data.fsx, as long as it is merged we may disable the wiki pages.

from rprovider.

hmansell avatar hmansell commented on July 26, 2024

I merged, and disabled the wiki. How do we regen the docs and push?

from rprovider.

tpetricek avatar tpetricek commented on July 26, 2024

I updated the published docs.

(To do this, just run build Release from the windows command line - this rebuilds everything, including the documentation and then it should ask for a password to publish it.)

from rprovider.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.