# Difference between revisions of "Integrating with R"

m (→Writing out the data and R commands) |
|||

(One intermediate revision by the same user not shown) | |||

Line 1: | Line 1: | ||

− | [ | + | [[Category: Integration Functions]] |

− | + | __TOC__ | |

− | + | [http://www.r-project.org R] is a data analysis environment created through the [http://www.gnu.org GNU Project]. This page provides a place for Analytica users to share tricks and techniques for integrating R with Analytica models. | |

− | + | ||

− | + | ||

+ | == Calling R from an Analytica model == | ||

+ | === Running a batch job === | ||

+ | ==== Scenario ==== | ||

You need to run a particular analysis using R code and libraries, possibly running on data from generated by your Analytica model, and then you'll need to read in the results of that R analysis into Analytica. | You need to run a particular analysis using R code and libraries, possibly running on data from generated by your Analytica model, and then you'll need to read in the results of that R analysis into Analytica. | ||

− | === Requirements === | + | ==== Requirements ==== |

− | + | ||

You'll need the Analytica Enterprise or better for this. | You'll need the Analytica Enterprise or better for this. | ||

− | === Basic steps === | + | ==== Basic steps ==== |

# [http://cran.r-project.org/mirrors.html Install the R Software for Windows] (if you haven't already) | # [http://cran.r-project.org/mirrors.html Install the R Software for Windows] (if you haven't already) | ||

# Figure out how to perform the analysis of interest in R. Note down the list of commands you would issue in R. | # Figure out how to perform the analysis of interest in R. Note down the list of commands you would issue in R. | ||

# From your Analytica model, use [[WriteTextFile]] to write out the R commands that will need to be executed to a file. | # From your Analytica model, use [[WriteTextFile]] to write out the R commands that will need to be executed to a file. | ||

− | # Use [[RunConsoleProcess]] to launch R.exe and run the analysis | + | # Use [[RunConsoleProcess]] to launch <code>R.exe</code> and run the analysis |

# Use [[ReadTextFile]] to read the results | # Use [[ReadTextFile]] to read the results | ||

# Use [[FindInText]], [[SplitText]] and [[ParseNumber]] to parse the results from R. | # Use [[FindInText]], [[SplitText]] and [[ParseNumber]] to parse the results from R. | ||

− | === An Example === | + | ==== An Example ==== |

I will use an example to help illustrate each step. In the example, we have the physical dimensions of a collection of 12 books, and the weight of each book. We will use R's linear regression command to obtain the regression coefficients and related statistics for the regression. This example would be very easy to do within Analytica using the [[Regression]] function -- much easier than implementing this integration -- but the purpose here is to illustrate the steps with a very simple example. | I will use an example to help illustrate each step. In the example, we have the physical dimensions of a collection of 12 books, and the weight of each book. We will use R's linear regression command to obtain the regression coefficients and related statistics for the regression. This example would be very easy to do within Analytica using the [[Regression]] function -- much easier than implementing this integration -- but the purpose here is to illustrate the steps with a very simple example. | ||

Line 28: | Line 28: | ||

It is assumed that you know '''R''' (which would explain why you want to use it). This is the Analytica Wiki -- we're not trying to teach R here. | It is assumed that you know '''R''' (which would explain why you want to use it). This is the Analytica Wiki -- we're not trying to teach R here. | ||

− | The Analytica model contains the physical book dimensions in a variable called physical_dimensions, and a variable named Weight with the weight of each book: | + | The Analytica model contains the physical book dimensions in a variable called <code>physical_dimensions</code>, and a variable named <code>Weight</code> with the weight of each book: |

::{| border="0" | ::{| border="0" | ||

Line 38: | Line 38: | ||

Introduction, Code and Commentary] by J H Maindonald. | Introduction, Code and Commentary] by J H Maindonald. | ||

− | ==== Writing out the data and R commands ==== | + | ===== Writing out the data and R commands ===== |

− | First, | + | First, we will construct an R command to create a data frame in R named <code>bookdata</code> that contains both the physical dimensions and weights. The following Analytica expression does this: |

+ | <pre style="background:white; border:white; margin-left: 1em;"> | ||

'bookdata <- structure(list(' & | 'bookdata <- structure(list(' & | ||

− | JoinText( chr(13)&Term & ' = c(' & JoinText(physical_dimensions,Data_Index,', ') & ')', Term, ', ') & ', | + | JoinText(chr(13) & Term & ' = c(' & JoinText(physical_dimensions, Data_Index, ', ') & ')', Term, ', ') & ', |

− | weight = c(' & JoinText( weight, Data_Index, ', ') & ')), ' & ' | + | weight = c(' & JoinText(weight, Data_Index, ', ') & ')), ' & ' |

.Names=c(' & JoinText('"' & Term & '"', Term, ', ') & ', "weight"), ' & ' | .Names=c(' & JoinText('"' & Term & '"', Term, ', ') & ', "weight"), ' & ' | ||

− | row.names = c(' & JoinText('"' & Data_Index & '"',Data_Index,',') & '), | + | row.names = c(' & JoinText('"' & Data_Index & '"', Data_Index,' ,') & '), |

class = "data.frame")' | class = "data.frame")' | ||

+ | </pre> | ||

− | This becomes the definition of | + | This becomes the definition of <code>R_cmd_data</code>, and when it is evaluated, the following R command is produced (we've introduced some additional new-lines here): |

+ | <pre style="background:white; border:white; margin-left: 1em;"> | ||

bookdata <- structure(list( | bookdata <- structure(list( | ||

thick = c(14, 15, 18, 23, 24, 25, 28, 28, 29, 30, 36, 44), | thick = c(14, 15, 18, 23, 24, 25, 28, 28, 29, 30, 36, 44), | ||

Line 57: | Line 60: | ||

weight = c(1075, 940, 625, 400, 550, 600, 450, 450, 300, 690, 400, 250)), | weight = c(1075, 940, 625, 400, 550, 600, 450, 450, 300, 690, 400, 250)), | ||

.Names=c("thick", "height", "width", "weight"), | .Names=c("thick", "height", "width", "weight"), | ||

− | row.names = c("1","2","3","4","5","6","7","8","9","10","11","12"), | + | row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), |

class = "data.frame") | class = "data.frame") | ||

+ | </pre> | ||

Next, to perform the analysis, we need to perform this command in R: | Next, to perform the analysis, we need to perform this command in R: | ||

− | + | :<code>summary(lm(weight~.,data=log(bookdata)))</code> | |

The Analytica code that writes the data and analysis command to a script file is thus: | The Analytica code that writes the data and analysis command to a script file is thus: | ||

− | + | <pre style="background:white; border:white; margin-left: 1em;"> | |

− | + | WriteTextFile(script_file, R_cmd_data, append: false, warn: false); | |

− | + | WriteTextFile(script_file, " | |

− | summary(lm(weight~.,data=log(bookdata)))", append:true, warn:false ) | + | |

+ | summary(lm(weight~.,data=log(bookdata)))", append: true, warn: false) | ||

+ | </pre> | ||

− | where Constant script_file contains the file name for the R commands, e.g., "C:\Temp\books.R" | + | where Constant <code>script_file</code> contains the file name for the R commands, e.g., <code>"C:\Temp\books.R"</code> |

− | === Running the R code === | + | ==== Running the R code ==== |

+ | ==== Reading and Parsing the results ==== | ||

− | == | + | == Using an Analytica model from R == |

+ | '''TBD: Would be done using [http://www.lumina.com/products/analytica-editions/analytica-decision-engine/ ADE] | ||

− | = | + | ==See Also== |

− | + | * [[WriteTextFile]] | |

− | + | * [[RunConsoleProcess]] | |

+ | * [[FindInText]] | ||

+ | * [[SplitText]] | ||

+ | * [[ParseNumber]] | ||

+ | * [[Analytica Modelers Guide]] |

## Latest revision as of 19:28, 18 March 2016

## Contents

R is a data analysis environment created through the GNU Project. This page provides a place for Analytica users to share tricks and techniques for integrating R with Analytica models.

## Calling R from an Analytica model

### Running a batch job

#### Scenario

You need to run a particular analysis using R code and libraries, possibly running on data from generated by your Analytica model, and then you'll need to read in the results of that R analysis into Analytica.

#### Requirements

You'll need the Analytica Enterprise or better for this.

#### Basic steps

- Install the R Software for Windows (if you haven't already)
- Figure out how to perform the analysis of interest in R. Note down the list of commands you would issue in R.
- From your Analytica model, use WriteTextFile to write out the R commands that will need to be executed to a file.
- Use RunConsoleProcess to launch
`R.exe`

and run the analysis - Use ReadTextFile to read the results
- Use FindInText, SplitText and ParseNumber to parse the results from R.

#### An Example

I will use an example to help illustrate each step. In the example, we have the physical dimensions of a collection of 12 books, and the weight of each book. We will use R's linear regression command to obtain the regression coefficients and related statistics for the regression. This example would be very easy to do within Analytica using the Regression function -- much easier than implementing this integration -- but the purpose here is to illustrate the steps with a very simple example.

It is assumed that you know **R** (which would explain why you want to use it). This is the Analytica Wiki -- we're not trying to teach R here.

The Analytica model contains the physical book dimensions in a variable called `physical_dimensions`

, and a variable named `Weight`

with the weight of each book:

The R example is lifted from [http://cran.r-project.org/doc/contrib/usingR.pdf Using R for Data Analysis and Graphics Introduction, Code and Commentary] by J H Maindonald.

##### Writing out the data and R commands

First, we will construct an R command to create a data frame in R named `bookdata`

that contains both the physical dimensions and weights. The following Analytica expression does this:

'bookdata <- structure(list(' & JoinText(chr(13) & Term & ' = c(' & JoinText(physical_dimensions, Data_Index, ', ') & ')', Term, ', ') & ', weight = c(' & JoinText(weight, Data_Index, ', ') & ')), ' & ' .Names=c(' & JoinText('"' & Term & '"', Term, ', ') & ', "weight"), ' & ' row.names = c(' & JoinText('"' & Data_Index & '"', Data_Index,' ,') & '), class = "data.frame")'

This becomes the definition of `R_cmd_data`

, and when it is evaluated, the following R command is produced (we've introduced some additional new-lines here):

bookdata <- structure(list( thick = c(14, 15, 18, 23, 24, 25, 28, 28, 29, 30, 36, 44), height = c(30.5, 29.1, 27.5, 23.2, 21.6, 23.5, 19.7, 19.8, 17.3, 22.8, 17.8, 13.5), width = c(23, 20.5, 18.5, 15.2, 14, 15.5, 12.6, 12.6, 10.5, 15.4, 11, 9.2), weight = c(1075, 940, 625, 400, 550, 600, 450, 450, 300, 690, 400, 250)), .Names=c("thick", "height", "width", "weight"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "data.frame")

Next, to perform the analysis, we need to perform this command in R:

`summary(lm(weight~.,data=log(bookdata)))`

The Analytica code that writes the data and analysis command to a script file is thus:

WriteTextFile(script_file, R_cmd_data, append: false, warn: false); WriteTextFile(script_file, " summary(lm(weight~.,data=log(bookdata)))", append: true, warn: false)

where Constant `script_file`

contains the file name for the R commands, e.g., `"C:\Temp\books.R"`

#### Running the R code

#### Reading and Parsing the results

## Using an Analytica model from R

**TBD: Would be done using ADE**

Enable comment auto-refresher