Giter Site home page Giter Site logo

implementing-cplusplus-algorithms-in-r's Introduction

Using C++ to Implement Efficient Algorithms in R

Background

The efficiency and speed of a function can often be optimised by defining it in C++ and calling it from R, as opposed to using available R packages or functions.

Objective

The aim of this project is to generate efficient functions for calculating moving average of period k across a vector using a C++ function that is callable from R and to benchmark this function against the orginal R-function, and apply it to investigate the changes in climate temperature over the last century. A second objective is to define a random walk function, and to apply it to characteise the movement of a tourist across a town.

Data and Analysis

A function was written in C++ to calculate the moving average and implemented in R using the cxxfunction of the RCPP package. Function arguments passed from R to C++ were a vector (x) that contained the data to be averaged, and an integer (k) that gave the size of the moving average window. The function consisted of a for loop that iterated through the elements of vector x, summing the elements within each sequential window, dividing by the window length, storing the result in a vector that was finally returned to R at the end of the loop. A while loop controlled the progress of the function, ending when the last window length in the vector passed from R is reached. The function speed was returned using the command microbenchmark, from the microbenchmark package in R.

A second function was written in C++ to calculate a random walk. The code was implemented in R using the cxxfunction of the RCPP package. Function arguments passed from R to C++ were an integer that contained the number of walks that were to be simulated using the model. The function consisted of a single for loop that iterated through the length of the walk integer passed from R. A random number generator returned 0, 1 or -1 that was stored in a “step” variable to indicate the step taken at each iteration. The value for step was next used to update a vector that indicated the current position along the x and y coordinates. An if statement was used to check if the current position matched the destination and if this was true, then an integer variable “reach_dest” that stored the number of times the destination was reached was advanced by one. This variable was returned to R at the end of the loop.

Further details of the functions are here

The R code used to implement the C++ functions is here

Conclusion

The Rcpp functions were superior to the R functions in terms of speed of processing. The Rcpp function coded for a moving average function was used to calculate moving average ocean temperatures in five year windows (from 1882–2017) and 30-year windows (from 1894–2004). The 30 year window smooths out variation over shorter time periods, and a 5 year window made the upward trajectory of temperature since 1920 more clear. The 30-year window removes the effects of year-to-year changes in temperature that are of small consequence to the overall trend across the 140 year time range

Acknowledgments

This project was submitted as part of the coursework required for the module, "Programming in C++" for an a MSc (Data Analytics) course at UCD in 2022.

implementing-cplusplus-algorithms-in-r's People

Contributors

cawyse9 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.