In scientific programming speed is important. Functions written for general public use have a lot of control-flow checks which are not necessary if you are confident enough with your code.To quicken your code execution I suggest to strip run-of-the-mill functions to their bare bones. You can save serious wall-clock time by using only the laborers code lines. Below is a walk-through example of what I mean.
Category: R
R Packages Download Stats
One big advantage of using open-source tools is the fantastic ecosystems that typically accompany them. Being able to tap into a massive open-source community, by way of downloading freely available code is decidedly useful. But, yes, there are downsides to downloads.
For one, there are too many packages out there. There are imperfect duplicates. You can easily end up downloading inferior code/package/module compared to existing other. Second, there is a matter of security. I myself try to refrain from downloading relatively new code, not yet tried-and-true. How do we know if a package is solid?
Most popular posts – 2021
Kind of sad, but the same intro which served last year, befits this year also.
Littered with Corona, this year was not easy. But looking around me, I feel grateful. The following quote by Socrates comes to mind:
“If all our misfortunes were laid in one common heap whence everyone must take an equal portion, most people would be content to take their own and depart.”
On topic, as with previous years I checked my website traffic-analytics. Without further ado here are the three most popular posts for 2021.
Publication in Significance – code
Couple of months ago I published a paper in Significance – couple of pages describing the essence of deep learning algorithms, and why they are so popular. I got a few requests for the code which generated the figures in that paper. This weekend I reviewed my code and was content to see that I used a pseudorandom numbers, with a seed (as oppose to completely random numbers; without a seed). So now the figures are exactly reproducible. The actual code to produce the figures, and the figures themselves (e.g. for teaching purposes) are provided below.
R tips and tricks – readClipboard
Here is a small utility function to save you some boring work.
Say you have a file to read into R. The file path is C:\Users\folder1\folder2\folder3\mydata.csv
. So what do you do? you copy the path, paste it to the editor, and start reversing the backslash into a forward slash so that R can read your file.
With the help of the rstudioapi
package, the readClipboard
function and the following function:
1 2 3 4 5 6 7 |
get_path <- function(){ x <- readClipboard(raw= F) rstudioapi::insertText( paste("#",x, "\n") ) x } |
You can
1. Simply copy the path C:\Users\folder1\folder2\folder3\mydata.csv
2. execute pathh <- get_path()
3. use pathh
which is now R-ready.
No more reversing or escaping backslash.
R + Python = Rython
Enough! Enough with that pointless R versus Python debate. I find it almost as pointless as the Bayesian vs Frequentist “dispute”. I advocate here what I advocated there (“..don’t be a Bayesian, nor be a Frequenist, be opportunist“).
Nowadays even marginally tedious computation is being sent to faster, minimum-overhead languages like C++. So it’s mainly syntax administration we insist to insist on. What does it matter if we have this:
1 2 3 |
xsquare <- function(x){ x^2 } |
Or that
1 2 3 4 |
def xsquare(x): return x**2 |
R tips and tricks – utilities
As the title reads, few more R-related tips and tricks. I hope you have not seen those before.
Some utilities
R tips and tricks – Paste a plot from R to a word file
In this post you will learn how to properly paste an R plot\chart\image to a word file. There are few typical problems that occur when people try to do that. Below you can find a simple, clean and repeatable solution.
Forecast Combination talk
Courtesy of R Consortium, you can view my forecast combination talk (16 mins) given in France few months ago, below.
Forecast Combination in R – slides
The useR! 2019 held in Toulouse ended couple of days ago.
Matrix-style screensaver in R
This post shares short code snippet to make your own screen saver in R, The Matrix-style:
R Journal publication
The R Journal is the open access, refereed journal of the R project for statistical computing. It features short to medium length articles covering topics that should be of interest to users or developers of R.
Christoph Weiss, Gernot Roetzer and myself have joined forces to write an R package and the accompanied paper: Forecast Combinations in R using the ForecastComb Package, which is now published in the R journal. Below you can find a few of my thoughts about the journey towards publication in the R journal, and a few words about working with a small team of three, from three different locations.
R tips and tricks – higher-order functions
A higher-order function is a function that takes one or more functions as arguments, and\or returns a function as its result. This can be super handy in programming when you want to tilt your code towards readability and still keep it concise.
Reproducible Finance with R – book review
Reproducible Finance with R is a clever book, with modern treatment of classical concepts. Here below is what I liked- and disliked about the book.
R tips and tricks – the assign() function
The R language has some quirks compared to other languages. One thing which you need to constantly watch for when moving to- or from R, is that R starts its indexing at one, while almost all other languages start indexing at zero, which takes some getting used to. Another quirk is the explicit need for clarity when modifying a variable, compared with other languages.
Take python for example, but I think it looks the same in most common languages: