R Provider Tutorial

This tutorial demonstrates how to use the R type provider in an F# script. You can also use the R type provider in other scenarios such as apps and libraries.

System requirements

Make sure you have set up your system as specified here.

Referencing the provider

First, make a new F# script (e.g., sample.fsx). In your new script, first load the R type provider from the NuGet package repository.

#r "nuget:RProvider"

For this tutorial, we use open to reference a number of packages including stats, tseries and zoo:

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").

Pretty-printing R values

Add this line to your script to tell F# interactive how to print out the values of R objects:

fsi.AddPrinter FSIPrinters.rValue

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:

#r "nuget:FSharp.Data"
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:

// 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:

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:

// 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 RProvider
Multiple items
namespace RProvider

--------------------
type RProvider = inherit TypeProviderForNamespaces new : cfg:TypeProviderConfig -> RProvider

--------------------
new : cfg:CompilerServices.TypeProviderConfig -> RProvider
namespace RProvider.graphics
namespace RProvider.stats
namespace RProvider.tseries
namespace RProvider.zoo
namespace System
namespace System.Net
module FSIPrinters from RProvider
<summary> Print functions that may be used in F# interactive to 'pretty-print' R types to the console window. Use in your scripts by passing to `fsi.AddPrinter`. </summary>
val rValue : synexpr:RDotNet.SymbolicExpression -> string
<summary> Print any `SymbolicExpression` using R's built-in `print` function. </summary>
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
type Stocks = obj
type CsvProvider =
<summary>Typed representation of a CSV file.</summary> <param name='Sample'>Location of a CSV sample file or a string containing a sample CSV document.</param> <param name='Separators'>Column delimiter(s). Defaults to `,`.</param> <param name='InferRows'>Number of rows to use for inference. Defaults to `1000`. If this is zero, all rows are used.</param> <param name='Schema'>Optional column types, in a comma separated list. Valid types are `int`, `int64`, `bool`, `float`, `decimal`, `date`, `guid`, `string`, `int?`, `int64?`, `bool?`, `float?`, `decimal?`, `date?`, `guid?`, `int option`, `int64 option`, `bool option`, `float option`, `decimal option`, `date option`, `guid option` and `string option`. You can also specify a unit and the name of the column like this: `Name (type&lt;unit&gt;)`, or you can override only the name. If you don't want to specify all the columns, you can reference the columns by name like this: `ColumnName=type`.</param> <param name='HasHeaders'>Whether the sample contains the names of the columns as its first line.</param> <param name='IgnoreErrors'>Whether to ignore rows that have the wrong number of columns or which can't be parsed using the inferred or specified schema. Otherwise an exception is thrown when these rows are encountered.</param> <param name='SkipRows'>Skips the first n rows of the CSV file.</param> <param name='AssumeMissingValues'>When set to true, the type provider will assume all columns can have missing values, even if in the provided sample all values are present. Defaults to false.</param> <param name='PreferOptionals'>When set to true, inference will prefer to use the option type instead of nullable types, `double.NaN` or `""` for missing values. Defaults to false.</param> <param name='Quote'>The quotation mark (for surrounding values containing the delimiter). Defaults to `"`.</param> <param name='MissingValues'>The set of strings recogized as missing values. Defaults to `NaN,NA,N/A,#N/A,:,-,TBA,TBD`.</param> <param name='CacheRows'>Whether the rows should be caches so they can be iterated multiple times. Defaults to true. Disable for large datasets.</param> <param name='Culture'>The culture used for parsing numbers and dates. Defaults to the invariant culture.</param> <param name='Encoding'>The encoding used to read the sample. You can specify either the character set name or the codepage number. Defaults to UTF8 for files, and to ISO-8859-1 the for HTTP requests, unless `charset` is specified in the `Content-Type` response header.</param> <param name='ResolutionFolder'>A directory that is used when resolving relative file references (at design time and in hosted execution).</param> <param name='EmbeddedResource'>When specified, the type provider first attempts to load the sample from the specified resource (e.g. 'MyCompany.MyAssembly, resource_name.csv'). This is useful when exposing types generated by the type provider.</param>
val getStockPrices : stock:'a -> count:'b -> float []
 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)
<summary>Converts the argument to 64-bit float. This is a direct conversion for all primitive numeric types. For strings, the input is converted using <c>Double.Parse()</c> with InvariantCulture settings. Otherwise the operation requires an appropriate static conversion method on the input type.</summary>
<param name="value">The input value.</param>
<returns>The converted float</returns>


--------------------
[<Struct>] type float = Double
<summary>An abbreviation for the CLI type <see cref="T:System.Double" />.</summary>
<category>Basic Types</category>


--------------------
type float<'Measure> = float
<summary>The type of double-precision floating point numbers, annotated with a unit of measure. The unit of measure is erased in compiled code and when values of this type are analyzed using reflection. The type is representationally equivalent to <see cref="T:System.Double" />.</summary>
<category index="6">Basic Types with Units of Measure</category>
type Array = interface ICollection interface IEnumerable interface IList interface IStructuralComparable interface IStructuralEquatable interface ICloneable new : unit -> unit member Clone : unit -> obj member CopyTo : array: Array * index: int -> unit + 1 overload member GetEnumerator : unit -> IEnumerator ...
<summary>Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.</summary>
val rev : array:'T [] -> 'T []
<summary>Returns a new array with the elements in reverse order.</summary>
<param name="array">The input array.</param>
<returns>The reversed array.</returns>
<exception cref="T:System.ArgumentNullException">Thrown when the input array is null.</exception>
val msftOpens : float []
 Get opening prices for MSFT for the last 255 days
val msft : RDotNet.SymbolicExpression
type R = static member ! :?paramArray: obj [] -> SymbolicExpression + 1 overload static member != :?paramArray: obj [] -> SymbolicExpression + 1 overload static member !_hexmode :?a: obj -> SymbolicExpression + 1 overload static member !_octmode :?a: obj -> SymbolicExpression + 1 overload static member $ :?paramArray: obj [] -> SymbolicExpression + 1 overload static member $<- :?paramArray: obj [] -> SymbolicExpression + 1 overload static member $<-_data_frame :?x: obj *?name: obj *?value: obj -> SymbolicExpression + 1 overload static member $_DLLInfo :?x: obj *?name: obj -> SymbolicExpression + 1 overload static member $_package__version :?x: obj *?name: obj -> SymbolicExpression + 1 overload static member %% :?paramArray: obj [] -> SymbolicExpression + 1 overload ...
Base R functions.
R.log(paramsByName: Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.log(?paramArray: obj []) : RDotNet.SymbolicExpression
Logarithms and Exponentials
R.diff(paramsByName: Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.diff(?x: obj,?___: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
Lagged Differences
val a : RDotNet.SymbolicExpression
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 DF2formula :?x: obj *?env: 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 ...
R statistical functions.
R.acf(paramsByName: Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.acf(?x: obj,?lag_max: obj,?type: obj,?plot: obj,?na_action: obj,?demean: obj,?___: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
Auto- and Cross- Covariance and -Correlation Function Estimation
val adf : RDotNet.SymbolicExpression
type R = static member adf_test :?x: obj *?alternative: obj *?k: obj -> SymbolicExpression + 1 overload static member approx_irts :?object: obj *?time: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 1 overload static member arma :?x: obj *?order: obj *?lag: obj *?coef: obj *?include_intercept: obj *?series: obj *?qr_tol: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 1 overload static member as_irts :?object: obj -> SymbolicExpression + 1 overload static member bds_test :?x: obj *?m: obj *?eps: obj *?trace: obj -> SymbolicExpression + 1 overload static member daysecond :?object: obj *?tz: obj -> SymbolicExpression + 1 overload static member garch :?x: obj *?order: obj *?series: obj *?control: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 1 overload static member garch_control :?maxiter: obj *?trace: obj *?start: obj *?grad: obj *?abstol: obj *?reltol: obj *?xtol: obj *?falsetol: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 1 overload static member get_hist_quote :?instrument: obj *?start: obj *?end: obj *?quote: obj *?provider: obj *?method: obj *?origin: obj *?compression: obj *?retclass: obj *?quiet: obj *?drop: obj -> SymbolicExpression + 1 overload static member irts :?time: obj *?value: obj -> SymbolicExpression + 1 overload ...
Time series analysis and computational finance.
R.adf_test(paramsByName: Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.adf_test(?x: obj,?alternative: obj,?k: obj) : RDotNet.SymbolicExpression
Augmented Dickey-Fuller Test
val tickers : string list
val data : (string * RDotNet.SymbolicExpression) list
val t : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
<summary>Print to <c>stdout</c> using the given format, and add a newline.</summary>
<param name="format">The formatter.</param>
<returns>The formatted result.</returns>
val df : RDotNet.SymbolicExpression
R.data_frame(paramsByName: Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.data_frame(?___: obj,?row_names: obj,?check_rows: obj,?check_names: obj,?fix_empty_names: obj,?stringsAsFactors: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
Data Frames
val namedParams : s:seq<string * 'a> -> Collections.Generic.IDictionary<string,obj>
<summary> Construct a dictionary of named params to pass to an R function. ## Example For example, if you want to call the `R.plot` function with named parameters specifying `x`, `type`, `col` and `ylim`, you can use the following: [ "x", box widgets; "type", box "o"; "col", box "blue"; "ylim", box [0; 25] ] |&gt; namedParams |&gt; R.plot </summary>
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 *?gap_axis: 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 *?ann: obj *?args_legend: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 1 overload ...
R functions for base graphics.
R.pairs(paramsByName: Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.pairs(?x: obj,?___: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
Scatterplot Matrices