About Me Blog
Dealing with heteroskedasticity; regression with robust standard errors using R Forecasting my weight with R Getting data from pdfs using the pdftools package Imputing missing values in parallel using {furrr} Missing data imputation and instrumental variables regression: the tidy approach {pmice}, an experimental package for missing data imputation in parallel using {mice} and {furrr} Building formulae Functional peace of mind Get basic summary statistics for all the variables in a data frame Getting {sparklyr}, {h2o}, {rsparkling} to work together and some fun with bash Importing 30GB of data into R with sparklyr Introducing brotools It's lists all the way down It's lists all the way down, part 2: We need to go deeper Keep trying that api call with purrr::possibly() Lesser known dplyr 0.7* tricks Lesser known dplyr tricks Lesser known purrr tricks Make ggplot2 purrr Mapping a list of functions to a list of datasets with a list of columns as arguments Predicting job search by training a random forest on an unbalanced dataset Teaching the tidyverse to beginners Why I find tidyeval useful tidyr::spread() and dplyr::rename_at() in action Easy peasy STATA-like marginal effects with R Functional programming and unit testing for data munging with R available on Leanpub How to use jailbreakr My free book has a cover! Work on lists of datasets instead of individual datasets by using functional programming Method of Simulated Moments with R New website! Nonlinear Gmm with R - Example with a logistic regression Simulated Maximum Likelihood with R Bootstrapping standard errors for difference-in-differences estimation with R Careful with tryCatch Data frame columns as arguments to dplyr functions Export R output to a file I've started writing a 'book': Functional programming and unit testing for data munging with R Introduction to programming econometrics with R Merge a list of datasets together Object Oriented Programming with R: An example with a Cournot duopoly R, R with Atlas, R with OpenBLAS and Revolution R Open: which is fastest? Read a lot of datasets at once with R Unit testing with R Update to Introduction to programming econometrics with R Using R as a Computer Algebra System with Ryacas

Using R as a Computer Algebra System with Ryacas

R is used to perform statistical analysis and doesn't focus on symbolic maths. But it is sometimes useful to let the computer derive a function for you (and have the analytic expression of said derivative), but maybe you don't want to leave your comfy R shell. It is possible to turn R into a full-fledged computer algebra system. CASs are tools that perform symbolic operations, such as getting the expression of the derivative of a user-defined (and thus completely arbitrary) function. Popular CASs include the proprietary Mathematica and Maple. There exists a lot of CASs under a Free Software license, Maxima (based on the very old Macsyma), Yacas, Xcas… In this post I will focus on Yacas and the Ryacas libarary. There is also the possibility to use the rSympy library that uses the Sympy Python library, which has a lot more features than Yacas. However, depending on your operating system installation can be tricky as it also requires rJava as a dependency.

Even though Ryacas is quite nice to have, there are some issues though. For example, let's say you want the first derivative of a certain function f. If you use Ryacas to get it, the returned object won't be a function. There is a way to “extract” the text from the returned object and make a function out of it. But there are still other issues; I'll discuss them later.


Installation should be rather painless. On Linux you need to install Yacas first, which should be available in the major distros' repositories. Then you can install Ryacas from within the R shell. On Windows, you need to run these three commands (don't bother installing Yacas first):


You can find more information on the project's page.

Example session

First, you must load Ryacas and define symbols that you will use in your functions.

## Loading required package: Ryacas Loading required package: XML
x <- Sym("x")

You can then define your fonctions:

my_func <- function(x) {
    return(x/(x^2 + 3))

And you can get the derivative for instance:

my_deriv <- yacas(deriv(my_func(x), x))
## [1] "Starting Yacas!"

If you check the class of my_deriv, you'll see that it is of class yacas, which is not very useful. Let's Ā«convertĀ» it to a function:

my_deriv2 <- function(x) {
    eval(parse(text = my_deriv$YacasForm))

We can then evaluate it. A lot of different operations are possible. But there are some problems.

Issues with Ryacas

You can't use elements of a vector as parameters of your function, i.e.:

theta <- Sym("theta")
func <- function(x) {
    return(theta[1] * x + theta[2])
# Let's integrate this
Func <- yacas(Integrate(func(x), x))

returns (x^2*theta)/2+NA*x; which is not quite what we want…there is a workaround however. Define your functions like this:

a <- Sym("a")
b <- Sym("b")
func2 <- function(x) {
    return(a * x + b)
# Let's integrate this
Func2 <- yacas(Integrate(func2(x), x))

we get the expected result: (x^2*a)/2+b*x;. Now replace a and b by the thetas:

Func2 <- gsub("a", "theta[1]", Func2$YacasForm)
Func2 <- gsub("b", "theta[2]", Func2)

Now we have what we want:

## [1] "(x^2*theta[1])/2+theta[2]*x;"

You can then copy-paste this result into a function.

Another problem is if you use built-in functions that are different between R and Yacas. For example:

my_log <- function(x) {
    return(sin(log(2 + x)))

Now try to differentiate it:

dmy_log <- yacas(deriv(my_log(x), x))

you get: Cos(Ln(x+2))/(x+2);. The problem with this, is that R doesn't recognize Cos as the cosine (which is cos in R) and the same goes for Ln. These are valid Yacas functions, but that is not the case in R. So you'll have to use gsub to replace these functions and then copy paste the end result into a function.


While it has some flaws, Ryacas can be quite useful if you need to derive or integrate complicated expressions that you then want to use in R. Using some of the tricks I showed here, you should be able to overcome some of its shortcomings. If installation of rJava and thus rSympy becomes easier, I'll probably also do a short blog-post about it, as it has more features than Ryacas.