R Type Provider


R Provider Tutorial

Referencing the provider

In order to use the R provider, you need to reference the RDotNet.dll library (which is a .NET connector for R) and the RProvider.dll itself. For this tutorial, we use open to reference a number of packages including stats, tseries and zoo:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
#I "../packages/RProvider.1.0.3/lib"
#r "RDotNet.dll"
#r "RDotNet.FSharp.dll"
#r "RDotNet.NativeLibrary.dll"
#r "RProvider.dll"
#r "RProvider.Runtime.dll"
 
open RDotNet
open RProvider
open RProvider.graphics
open RProvider.stats
open RProvider.tseries
open RProvider.zoo

open System
open System.Net

If either of the namespaces above are unrecognized, you need to install the package in R using install.packages("stats").

Obtaining data

In this tutorial, we use F# Data to access stock prices from the Yahoo Finance portal. For more information, see the documentation for the CSV type provider.

The following snippet uses the CSV type provider to generate a type Stocks that can be used for parsing CSV data from Yahoo. Then it defines a function getStockPrices that returns array with prices for the specified stock and a specified number of days:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
#r "FSharp.Data.dll"
open FSharp.Data

type Stocks = CsvProvider<"http://ichart.finance.yahoo.com/table.csv?s=SPX">
 
/// Returns prices of a given stock for a specified number 
/// of days (starting from the most recent)
let getStockPrices stock count =
  let url = "http://ichart.finance.yahoo.com/table.csv?s="
  [| for r in Stocks.Load(url + stock).Take(count).Rows -> float r.Open |] 
  |> Array.rev

/// Get opening prices for MSFT for the last 255 days
let msftOpens = getStockPrices "MSFT" 255

Calling R functions

Now, we're ready to call R functions using the type provider. The following snippet takes msftOpens, calculates logarithm of the values using R.log and then calculates the differences of the resulting vector using R.diff:

1: 
2: 
// Retrieve stock price time series and compute returns
let msft = msftOpens |> R.log |> R.diff

If you want to see the resulting values, you can call msft.AsVector() in F# Interactive. Next, we use the acf function to display the atuo-correlation and call adf_test to see if the msft returns are stationary/non-unit root:

1: 
2: 
let a = R.acf(msft)
let adf = R.adf_test(msft) 

After running the first snippet, a window similar to the following should appear (note that it might not appear as a top-most window).

Finally, we can obtain data for multiple different indicators and use the R.pairs function to produce a matrix of scatter plots:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
// Build a list of tickers and get diff of logs of prices for each one
let tickers = 
  [ "MSFT"; "AAPL"; "X"; "VXX"; "SPX"; "GLD" ]
let data =
  [ for t in tickers -> 
      printfn "got one!"
      t, getStockPrices t 255 |> R.log |> R.diff ]

// Create an R data frame with the data and call 'R.pairs'
let df = R.data_frame(namedParams data)
R.pairs(df)

As a result, you should see a window showing results similar to these:

namespace RDotNet
namespace RProvider
namespace RProvider.graphics
namespace RProvider.stats
namespace RProvider.zoo
namespace System
namespace System.Net
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Data
type Stocks = obj

Full name: Tutorial.Stocks
val getStockPrices : stock:'a -> count:'b -> float []

Full name: Tutorial.getStockPrices


 Returns prices of a given stock for a specified number
 of days (starting from the most recent)
val stock : 'a
val count : 'b
val url : string
val r : obj
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val rev : array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.rev
val msftOpens : float []

Full name: Tutorial.msftOpens


 Get opening prices for MSFT for the last 255 days
val msft : Collections.Generic.IDictionary<string,obj>

Full name: Tutorial.msft
type R =
  static member MATCH : ?x: obj * ?table: obj * ?nomatch: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member MATCH_default : ?x: obj * ?table: obj * ?nomatch: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member MATCH_times : ?x: obj * ?table: obj * ?nomatch: obj * ?units: obj * ?eps: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member ORDER : ?x: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member ORDER_default : ?x: obj * ?___: obj * ?na_last: obj * ?decreasing: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member Sys_yearmon : ?NULL: obj -> SymbolicExpression + 1 overload
  static member Sys_yearqtr : ?NULL: obj -> SymbolicExpression + 1 overload
  static member as_Date : ?x: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member as_Date_numeric : ?x: obj * ?origin: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member as_Date_ts : ?x: obj * ?offset: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  ...

Full name: RProvider.zoo.R


An S3 class with methods for totally ordered indexed
             observations. It is particularly aimed at irregular time series
             of numeric vectors/matrices and factors. zoo's key design goals
             are independence of a particular index/date/time class and
             consistency with ts and base R by providing methods to extend
             standard generics.
val log : value:'T -> 'T (requires member Log)

Full name: Microsoft.FSharp.Core.Operators.log
val a : SymbolicExpression

Full name: Tutorial.a
type R =
  static member AIC : ?object: obj * ?___: obj * ?k: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member ARMAacf : ?ar: obj * ?ma: obj * ?lag_max: obj * ?pacf: obj -> SymbolicExpression + 1 overload
  static member ARMAtoMA : ?ar: obj * ?ma: obj * ?lag_max: obj -> SymbolicExpression + 1 overload
  static member BIC : ?object: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member Box_test : ?x: obj * ?lag: obj * ?type: obj * ?fitdf: obj -> SymbolicExpression + 1 overload
  static member C : ?object: obj * ?contr: obj * ?how_many: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member D : ?expr: obj * ?name: obj -> SymbolicExpression + 1 overload
  static member Gamma : ?link: obj -> SymbolicExpression + 1 overload
  static member HoltWinters : ?x: obj * ?alpha: obj * ?beta: obj * ?gamma: obj * ?seasonal: obj * ?start_periods: obj * ?l_start: obj * ?b_start: obj * ?s_start: obj * ?optim_start: obj * ?optim_control: obj -> SymbolicExpression + 1 overload
  static member IQR : ?x: obj * ?na_rm: obj * ?type: obj -> SymbolicExpression + 1 overload
  ...

Full name: RProvider.stats.R


R statistical functions.
R.acf(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.acf(?x: obj, ?lag_max: obj, ?type: obj, ?plot: obj, ?na_action: obj, ?demean: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression


Auto- and Cross- Covariance and -Correlation Function Estimation
val adf : obj

Full name: Tutorial.adf
val tickers : string list

Full name: Tutorial.tickers
val data : obj list

Full name: Tutorial.data
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val df : Collections.Generic.IDictionary<string,obj>

Full name: Tutorial.df
val namedParams : s:seq<string * 'a> -> Collections.Generic.IDictionary<string,obj>

Full name: RProvider.Helpers.namedParams
type R =
  static member Axis : ?x: obj * ?at: obj * ?___: obj * ?side: obj * ?labels: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member abline : ?a: obj * ?b: obj * ?h: obj * ?v: obj * ?reg: obj * ?coef: obj * ?untf: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member arrows : ?x0: obj * ?y0: obj * ?x1: obj * ?y1: obj * ?length: obj * ?angle: obj * ?code: obj * ?col: obj * ?lty: obj * ?lwd: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member assocplot : ?x: obj * ?col: obj * ?space: obj * ?main: obj * ?xlab: obj * ?ylab: obj -> SymbolicExpression + 1 overload
  static member axTicks : ?side: obj * ?axp: obj * ?usr: obj * ?log: obj * ?nintLog: obj -> SymbolicExpression + 1 overload
  static member axis : ?side: obj * ?at: obj * ?labels: obj * ?tick: obj * ?line: obj * ?pos: obj * ?outer: obj * ?font: obj * ?lty: obj * ?lwd: obj * ?lwd_ticks: obj * ?col: obj * ?col_ticks: obj * ?hadj: obj * ?padj: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member axis_Date : ?side: obj * ?x: obj * ?at: obj * ?format: obj * ?labels: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member axis_POSIXct : ?side: obj * ?x: obj * ?at: obj * ?format: obj * ?labels: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member barplot : ?height: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member barplot_default : ?height: obj * ?width: obj * ?space: obj * ?names_arg: obj * ?legend_text: obj * ?beside: obj * ?horiz: obj * ?density: obj * ?angle: obj * ?col: obj * ?border: obj * ?main: obj * ?sub: obj * ?xlab: obj * ?ylab: obj * ?xlim: obj * ?ylim: obj * ?xpd: obj * ?log: obj * ?axes: obj * ?axisnames: obj * ?cex_axis: obj * ?cex_names: obj * ?inside: obj * ?plot: obj * ?axis_lty: obj * ?offset: obj * ?add: obj * ?args_legend: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  ...

Full name: RProvider.graphics.R


R functions for base graphics.
R.pairs(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.pairs(?x: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression


Scatterplot Matrices
Fork me on GitHub