简体   繁体   中英

R statistics Package ISLR with RProvider and F#

I have trying to use the ISLR package ( http://cran.r-project.org/web/packages/ISLR/index.html ).

Is it possible to use the package with F# ? I found RProvider 1.1.8 ( https://www.nuget.org/packages/RProvider/ ) and I tried my first F# program:

open System
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.graphics
open RProvider.stats
// open RProvider.ISLR

let rng = Random()
let rand () = rng.NextDouble()
let X1s = [ for i in 0 .. 9 -> 10. * rand () ]
let X2s = [ for i in 0 .. 9 -> 5. * rand () ]

// Build Ys, following the "true" model
let Ys = [ for i in 0 .. 9 -> 5. + 3. * X1s.[i] - 2. * X2s.[i] + rand () ]

let dataset =
    namedParams [
        "Y", box Ys;
        "X1", box X1s;
        "X2", box X2s; ]
    |> R.data_frame

let result = R.lm(formula = "Y~X1+X2", data = dataset)
R.plot result

This program runs perfectly. However, when I tried to use ISLR (commented out “open RProvider.stats”) F# complained that “lm” was not found. I wish to use the ISLR's version of “lm” and it should be a part of ISLR

My environment is: Visual Studio 2013, RStudio version 0.98.1091, ISLR version 1.0

Any suggestions on how to use ISLR with F# will be greatly appreciated.

As others pointed out, the ISLR package contains just data sets and you can definitely access those through the R type provider. After installing the package, the following worked for me:

#load "packages/RProvider.fsx"
open RProvider
open RProvider.ISLR

R.Wage.Value // One of the data sets from ISLR

You can explore what is provided from a specific package using full path:

RProvider.ISLR.R.Wage.Value

If there is something that you can access in R, but not in F#, then we can definitely fix this, but I think there are no issues with ISLR. It is worth pointing out that you might be interested in using R provider with Deedle , which gives you interop between R and F# data frames. (Install the FsLab package which contains all you need.)

If you don't have the package installed, you can actually do that from R provider too :-)

open RProvider.utils
R.install_packages("ISLR")

My experience with F# is now going on 12 hours, 23 minutes and 14 seconds. Whew! As a long time C# programmer, this is painful. My working code is:

open System
open System.Diagnostics
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.graphics
open RProvider.stats
open RProvider.e1071

let x = array2D [ [  0.4992207;  0.83459057 ];
                  [  0.5095988;  0.86535664 ];
                  [ -0.3130123; -0.54000741 ];
                  [  0.7136758;  0.09454017 ];
                  [ -1.0506291; -0.49390571 ];
                  [ -0.7504743;  0.19165412 ];
                  [ -0.3502830; -0.16827662 ];
                  [  0.6202636; -0.03877393 ];
                  [ -0.2169360;  0.03409674 ];
                  [ -0.2485535;  0.68234694 ];
                  [  1.3264276;  1.40061717 ];
                  [  0.6886685;  1.38550956 ];
                  [  2.7057305;  1.52631182 ];
                  [  2.3354574; -0.49779748 ];
                  [ -1.3406379;  2.20544301 ];
                  [  1.2445945; -0.18418390 ];
                  [  0.7721881;  1.53916368 ];
                  [ -0.3640324;  2.32334502 ];
                  [  1.6404738;  1.17708567 ];
                  [ -1.0440209;  1.95016425 ] ]

let y = [| -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1 |]

let colors = Array.create y.Length 0
for k=0 to (Array.length y)-1 do colors.[k] <- y.[k] + 3;
let pch = 19

let status = R.plot(namedParams [ "x", box x; "col", box colors; "pch", box pch; "xlab", box "x[,1]"; "ylab", box "x[,2]"])

let args =
    namedParams [
        "formula", box "y~.";
        "x", box x;
        "y", box y;
        "type", box "C-classification";
        "kernel", box "linear";
        "cost", box 10;
        "scale", box false]

let svmfit = R.svm(args)
R.print(svmfit) |> ignore

I sure you guys will laugh at what I have done; but, what the hell, my reputation is mortally wounded as it is.

I'm still struggling with a problem. The R code to plot the results of the “svm” function looks like:

library(e1071)
df = data.frame(x, y = as.factor(y))
svmfit = svm(y~., data=df, kernel="linear", cost=10, scale=FALSE)
print(svmfit)
plot(svmfit, df)

My problem is how to convert: plot(svmfit, df) . I tried:

let labels = R.as_factor(y)
let df = 
    namedParams [
        "x", box x;
        "data", box labels ]
    |> R.data_frame

let arg =
    namedParams [
        "formula", box svmfit;
        "data", box df
    ]

R.plot(arg)

But that does not work. I don't know how to declare “svmfit”. My attempt to use “formula” is not correct, and I don't know what to use in this field.

Bye the way, thanks for the suggestion to use "Deedle". I will try that next.

Charles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM