profr: a very nice convenience

Active Analytics Ltd: posted 4 December 2014 16:03 PST by Chibisi Chima-Okereke

Introduction

In R, the Rprof function allows users to obtain process information by statistical profiling meaning that it collects call information over intervals of time. The profr package gives a convenient rapper over the Rprof function in R and returns information in a data frame and allows you to represent the call stack graphically.

Example

We would like to look at the call stack in the MackChainLadder function in the ChainLadder package:

# Load the packages
require(profr)
require(ChainLadder)
# Run the profiler
p <- profr(MackChainLadder(auto[[3]]), 0.005)
head(p)
   level g_id t_id                     f start   end n  leaf  time      source
8      1    1    1       MackChainLadder 0.000 0.040 1 FALSE 0.040 ChainLadder
9      2    1    1           chainladder 0.000 0.020 1 FALSE 0.020 ChainLadder
10     2    2    1   predict.ChainLadder 0.020 0.035 1 FALSE 0.015        
11     2    3    1              Mack.S.E 0.035 0.040 1 FALSE 0.005        
12     3    1    1                lapply 0.000 0.020 1 FALSE 0.020        base
13     3    2    1 predict.TriangleModel 0.020 0.035 1 FALSE 0.015        
plot(p)

Be aware however that since it is a statistical profiler, you will get a different answer each time you run it on the same process and the granularity will also vary depending on the time interval you choose (it will vary anyway!).

Data Science Consulting & Software Training

Active Analytics Ltd. is a data science consultancy, and Open Source Statistical Software Training company. Please contact us for more details or to comment on the blog.

Dr. Chibisi Chima-Okereke, R Training, Statistics and Data Analysis.