R version of Probabilistic Machine Learning (for longitudinal data) Reserving (work in progress)

R-bloggers 2025-06-05

[This article was first published on T. Moudiki's Webpage - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Work in progress. Comments welcome.

!pip install git+https://github.com/Techtonique/mlreserving.git --verbose%load_ext rpy2.ipython%%Rinstall.packages("reticulate")%%Rlibrary(reticulate)library(ggplot2)library(dplyr)# Import Python modulespd <- import("pandas")np <- import("numpy")plt <- import("matplotlib.pyplot")MLReserving <- import("mlreserving")$MLReservingRidgeCV <- import("sklearn.linear_model")$RidgeCVExtraTreesRegressor <- import("sklearn.ensemble")$ExtraTreesRegressorRandomForestRegressor <- import("sklearn.ensemble")$RandomForestRegressor# Load the dataseturl <- "https://raw.githubusercontent.com/Techtonique/datasets/refs/heads/main/tabular/triangle/raa.csv"df <- pd$read_csv(url)# Print head and tailprint(head(df))print(tail(df))library(reticulate)library(ggplot2)library(dplyr)# Import Python modules with convert=FALSE for more controlpd <- import("pandas", convert=FALSE)np <- import("numpy", convert=FALSE)MLReserving <- import("mlreserving", convert=FALSE)$MLReservingRidgeCV <- import("sklearn.linear_model", convert=FALSE)$RidgeCVExtraTreesRegressor <- import("sklearn.ensemble", convert=FALSE)$ExtraTreesRegressorRandomForestRegressor <- import("sklearn.ensemble", convert=FALSE)$RandomForestRegressor# Load the dataset and convert to Python objecturl <- "https://raw.githubusercontent.com/Techtonique/datasets/refs/heads/main/tabular/triangle/raa.csv"py_df <- pd$read_csv(url)# Print head and tailprint(py_df$head())print(py_df$tail())# Create models listmodels <- list(  RidgeCV(),  ExtraTreesRegressor(),  RandomForestRegressor())# Helper function to create Python integerspy_int <- function(x) {  floor(x)}for (mdl in models) {  # Initialize the model with explicit Python integers  model <- MLReserving(    model = mdl,    level = py_int(95),  # 95% confidence level    random_state = py_int(42)  )  # Fit the model  model$fit(    py_df,    origin_col = "origin",    development_col = "development",    value_col = "values"  )  # Uncomment to make predictions  result <- model$predict()  print(result)}(mean_df <- py_to_r(result$ibnr_mean))(lower_df <- py_to_r(result$ibnr_lower))(upper_df <- py_to_r(result$ibnr_upper))mean_vals <- mean_dflower_vals <- lower_dfupper_vals <- upper_df# Get origin years as numeric valuesyears <- as.numeric(names(mean_vals))# Set up plotplot(years, mean_vals,     ylim = range(c(lower_vals, upper_vals)),     xlab = "Origin Year", ylab = "IBNR Value",     main = "IBNR Estimates with Confidence Interval",     pch = 19, col = "blue", type = "n")# Add polygons and linesfor (i in seq_along(years)) {  # Coordinates for polygon: x and y pairs for upper and lower bounds  x_poly <- c(years[i] - 0.2, years[i] + 0.2, years[i] + 0.2, years[i] - 0.2)  y_poly <- c(lower_vals[i], lower_vals[i], upper_vals[i], upper_vals[i])  polygon(x_poly, y_poly, col = rgb(0, 0, 1, 0.2), border = NA)  # Draw the mean point on top  points(years[i], mean_vals[i], pch = 19, col = "blue")}# Optional: Add a legendlegend("topleft", legend = c("IBNR Mean", "Confidence Interval"),       col = c("blue", rgb(0, 0, 1, 0.2)), pch = c(19, 15), bty = "n")  development origin values1        1981   1981   50122        1982   1982    1063        1983   1983   34104        1984   1984   56555        1985   1985   10926        1986   1986   1513   development origin values50        1988   1981  1860851        1989   1982  1616952        1990   1983  2346653        1989   1981  1866254        1990   1982  1670455        1990   1981  18834   development  origin  values0         1981    1981 5012.001         1982    1982  106.002         1983    1983 3410.003         1984    1984 5655.004         1985    1985 1092.00    development  origin   values50         1989    1982 16169.0051         1990    1983 23466.0052         1989    1981 18662.0053         1990    1982 16704.0054         1990    1981 18834.00DescribeResult(mean=dev         1        2        3        4        5        6        7        8   \origin                                                                          1981   5012.00  8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00   1982    106.00  4285.00  5396.00 10666.00 13782.00 15599.00 15496.00 16169.00   1983   3410.00  8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00   1984   5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 22908.59   1985   1092.00  9565.00 15836.00 22169.00 25955.00 26180.00 21823.11 24108.16   1986   1513.00  6445.00 11702.00 12935.00 15852.00 20182.39 22964.19 25369.88   1987    557.00  4020.00 10946.00 12314.00 17925.30 21236.16 24164.31 26696.94   1988   1351.00  6947.00 13112.00 15015.00 18859.87 22344.37 25426.49 28092.70   1989   3133.00  5395.00 11465.39 15796.71 19842.66 23509.80 26753.91 29560.66   1990   2063.00  7394.65 12061.46 16618.70 20876.12 24735.39 28149.89 31104.52   dev          9        10  origin                    1981   18662.00 18834.00  1982   16704.00 23699.40  1983   23509.79 24943.97  1984   24742.62 26253.21  1985   26039.42 27630.45  1986   27403.47 29079.18  1987   28838.23 30603.08  1988   30347.32 32206.00  1989   31934.55 33891.99  1990   33603.93 35665.33  , lower=dev         1        2        3        4        5        6        7        8   \origin                                                                          1981   5012.00  8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00   1982    106.00  4285.00  5396.00 10666.00 13782.00 15599.00 15496.00 16169.00   1983   3410.00  8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00   1984   5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00  8974.39   1985   1092.00  9565.00 15836.00 22169.00 25955.00 26180.00  8549.13  9444.35   1986   1513.00  6445.00 11702.00 12935.00 15852.00  7906.33  8996.17  9938.66   1987    557.00  4020.00 10946.00 12314.00  7022.06  8319.17  9466.35 10458.57   1988   1351.00  6947.00 13112.00  5881.88  7388.20  8753.34  9960.84 11005.39   1989   3133.00  5395.00  4491.24  6188.14  7773.24  9209.93 10480.88 11580.50   1990   2063.00  2896.43  4724.76  6510.17  8178.12  9690.08 11027.79 12185.34   dev          9        10  origin                    1981   18662.00 18834.00  1982   16704.00  9284.21  1983    9209.92  9771.80  1984    9692.91 10284.72  1985   10200.96 10824.29  1986   10735.37 11391.87  1987   11297.47 11988.89  1988   11888.69 12616.87  1989   12510.53 13277.40  1990   13164.55 13972.15  , upper=dev         1        2        3        4        5        6        7        8   \origin                                                                          1981   5012.00  8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00   1982    106.00  4285.00  5396.00 10666.00 13782.00 15599.00 15496.00 16169.00   1983   3410.00  8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00   1984   5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 58475.51   1985   1092.00  9565.00 15836.00 22169.00 25955.00 26180.00 55704.84 61537.39   1986   1513.00  6445.00 11702.00 12935.00 15852.00 51516.90 58617.43 64757.93   1987    557.00  4020.00 10946.00 12314.00 45755.71 54206.64 61680.72 68145.25   1988   1351.00  6947.00 13112.00 38327.19 48141.20 57035.33 64902.42 71707.89   1989   3133.00  5395.00 29266.86 40322.51 50649.76 60010.10 68290.64 75454.84   1990   2063.00 18876.34 30788.30 42420.62 53287.66 63138.39 71853.88 79395.53   dev          9        10  origin                    1981   18662.00 18834.00  1982   16704.00 60494.05  1983   60010.06 63670.80  1984   63156.85 67012.63  1985   66466.91 70528.02  1986   69948.64 74225.89  1987   73610.85 78115.62  1988   77462.79 82207.05  1989   81514.19 86510.54  1990   85775.26 91036.96  , ibnr_mean=origin1982    23699.401983    48453.761984    73904.421985    99601.141986   124999.111987   149464.021988   172291.741989   192755.681990   210209.98Name: values, dtype: float64, ibnr_lower=origin1982    9284.211983   18981.721984   28952.021985   39018.731986   48968.391987   58552.501988   67495.211989   75511.841990   82349.38Name: values, dtype: float64, ibnr_upper=origin1982    60494.051983   123680.861984   188644.981985   254237.161986   319066.801987   381514.791988   439783.881989   492019.431990   536572.94Name: values, dtype: float64)DescribeResult(mean=dev         1        2        3        4        5        6        7        8   \origin                                                                          1981   5012.00  8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00   1982    106.00  4285.00  5396.00 10666.00 13782.00 15599.00 15496.00 16169.00   1983   3410.00  8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00   1984   5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 23466.00   1985   1092.00  9565.00 15836.00 22169.00 25955.00 26180.00 23192.71 23466.00   1986   1513.00  6445.00 11702.00 12935.00 15852.00 22839.00 23192.71 23466.00   1987    557.00  4020.00 10946.00 12314.00 21412.04 22839.00 23192.71 23466.00   1988   1351.00  6947.00 13112.00 20144.66 21412.04 22839.00 23192.71 23466.00   1989   3133.00  5395.00 18855.41 20144.66 21412.04 22839.00 23192.71 23466.00   1990   2063.00 14144.04 18855.41 20144.66 21412.04 22839.00 23192.71 23466.00   dev          9        10  origin                    1981   18662.00 18834.00  1982   16704.00 16824.72  1983   21775.25 21906.31  1984   21775.25 21906.31  1985   21775.25 21906.31  1986   21775.25 21906.31  1987   21775.25 21906.31  1988   21775.25 21906.31  1989   21775.25 21906.31  1990   21775.25 21906.31  , lower=dev         1        2        3        4        5        6        7        8   \origin                                                                          1981   5012.00  8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00   1982    106.00  4285.00  5396.00 10666.00 13782.00 15599.00 15496.00 16169.00   1983   3410.00  8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00   1984   5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00  5439.95   1985   1092.00  9565.00 15836.00 22169.00 25955.00 26180.00  5376.58  5439.95   1986   1513.00  6445.00 11702.00 12935.00 15852.00  5294.57  5376.58  5439.95   1987    557.00  4020.00 10946.00 12314.00  4963.73  5294.57  5376.58  5439.95   1988   1351.00  6947.00 13112.00  4669.88  4963.73  5294.57  5376.58  5439.95   1989   3133.00  5395.00  4370.96  4669.88  4963.73  5294.57  5376.58  5439.95   1990   2063.00  3278.60  4370.96  4669.88  4963.73  5294.57  5376.58  5439.95   dev          9        10  origin                    1981   18662.00 18834.00  1982   16704.00  3900.13  1983    5047.94  5078.33  1984    5047.94  5078.33  1985    5047.94  5078.33  1986    5047.94  5078.33  1987    5047.94  5078.33  1988    5047.94  5078.33  1989    5047.94  5078.33  1990    5047.94  5078.33  , upper=dev         1        2        3        4        5        6         7   \origin                                                                  1981   5012.00  8269.00 10907.00 11805.00 13539.00 16181.00  18009.00   1982    106.00  4285.00  5396.00 10666.00 13782.00 15599.00  15496.00   1983   3410.00  8992.00 13873.00 16141.00 18735.00 22214.00  22863.00   1984   5655.00 11555.00 15766.00 21266.00 23425.00 26083.00  27067.00   1985   1092.00  9565.00 15836.00 22169.00 25955.00 26180.00 100034.29   1986   1513.00  6445.00 11702.00 12935.00 15852.00 98508.72 100034.29   1987    557.00  4020.00 10946.00 12314.00 92354.22 98508.72 100034.29   1988   1351.00  6947.00 13112.00 86887.95 92354.22 98508.72 100034.29   1989   3133.00  5395.00 81327.36 86887.95 92354.22 98508.72 100034.29   1990   2063.00 61007.06 81327.36 86887.95 92354.22 98508.72 100034.29   dev           8        9        10  origin                              1981    18608.00 18662.00 18834.00  1982    16169.00 16704.00 72568.93  1983    23466.00 93920.76 94486.02  1984   101213.01 93920.76 94486.02  1985   101213.01 93920.76 94486.02  1986   101213.01 93920.76 94486.02  1987   101213.01 93920.76 94486.02  1988   101213.01 93920.76 94486.02  1989   101213.01 93920.76 94486.02  1990   101213.01 93920.76 94486.02  , ibnr_mean=origin1982    16824.721983    43681.561984    67147.561985    90340.271986   113179.271987   134591.311988   154735.971989   173591.371990   187735.41Name: values, dtype: float64, ibnr_lower=origin1982    3900.131983   10126.261984   15566.211985   20942.791986   26237.371987   31201.091988   35870.971989   40241.931990   43520.53Name: values, dtype: float64, ibnr_upper=origin1982    72568.931983   188406.781984   289619.791985   389654.081986   488162.801987   580517.031988   667404.981989   748732.341990   809739.40Name: values, dtype: float64)DescribeResult(mean=dev         1        2        3        4        5        6        7        8   \origin                                                                          1981   5012.00  8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00   1982    106.00  4285.00  5396.00 10666.00 13782.00 15599.00 15496.00 16169.00   1983   3410.00  8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00   1984   5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 22799.06   1985   1092.00  9565.00 15836.00 22169.00 25955.00 26180.00 22560.59 22799.06   1986   1513.00  6445.00 11702.00 12935.00 15852.00 22227.42 22560.59 22799.06   1987    557.00  4020.00 10946.00 12314.00 20320.41 22227.42 22560.59 22799.06   1988   1351.00  6947.00 13112.00 19568.73 20320.41 22227.42 22560.59 22799.06   1989   3133.00  5395.00 16013.73 19568.73 20320.41 22227.42 22560.59 22799.06   1990   2063.00 13812.49 16013.73 19568.73 20320.41 22227.42 22560.59 22799.06   dev          9        10  origin                    1981   18662.00 18834.00  1982   16704.00 17280.71  1983   22747.00 22747.00  1984   22747.00 22747.00  1985   22747.00 22747.00  1986   22747.00 22747.00  1987   22747.00 22747.00  1988   22747.00 22747.00  1989   22747.00 22747.00  1990   22747.00 22747.00  , lower=dev         1        2        3        4        5        6        7        8   \origin                                                                          1981   5012.00  8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00   1982    106.00  4285.00  5396.00 10666.00 13782.00 15599.00 15496.00 16169.00   1983   3410.00  8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00   1984   5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00  6829.01   1985   1092.00  9565.00 15836.00 22169.00 25955.00 26180.00  6757.57  6829.01   1986   1513.00  6445.00 11702.00 12935.00 15852.00  6657.77  6757.57  6829.01   1987    557.00  4020.00 10946.00 12314.00  6086.50  6657.77  6757.57  6829.01   1988   1351.00  6947.00 13112.00  5861.33  6086.50  6657.77  6757.57  6829.01   1989   3133.00  5395.00  4796.39  5861.33  6086.50  6657.77  6757.57  6829.01   1990   2063.00  4136.98  4796.39  5861.33  6086.50  6657.77  6757.57  6829.01   dev          9        10  origin                    1981   18662.00 18834.00  1982   16704.00  5175.93  1983    6813.41  6813.41  1984    6813.41  6813.41  1985    6813.41  6813.41  1986    6813.41  6813.41  1987    6813.41  6813.41  1988    6813.41  6813.41  1989    6813.41  6813.41  1990    6813.41  6813.41  , upper=dev         1        2        3        4        5        6        7        8   \origin                                                                          1981   5012.00  8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00   1982    106.00  4285.00  5396.00 10666.00 13782.00 15599.00 15496.00 16169.00   1983   3410.00  8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00   1984   5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 76110.60   1985   1092.00  9565.00 15836.00 22169.00 25955.00 26180.00 75314.53 76110.60   1986   1513.00  6445.00 11702.00 12935.00 15852.00 74202.33 75314.53 76110.60   1987    557.00  4020.00 10946.00 12314.00 67836.33 74202.33 75314.53 76110.60   1988   1351.00  6947.00 13112.00 65327.03 67836.33 74202.33 75314.53 76110.60   1989   3133.00  5395.00 53459.67 65327.03 67836.33 74202.33 75314.53 76110.60   1990   2063.00 46111.46 53459.67 65327.03 67836.33 74202.33 75314.53 76110.60   dev          9        10  origin                    1981   18662.00 18834.00  1982   16704.00 57689.14  1983   75936.79 75936.79  1984   75936.79 75936.79  1985   75936.79 75936.79  1986   75936.79 75936.79  1987   75936.79 75936.79  1988   75936.79 75936.79  1989   75936.79 75936.79  1990   75936.79 75936.79  , ibnr_mean=origin1982    17280.711983    45493.991984    68293.051985    90853.641986   113081.061987   133401.481988   152970.201989   168983.931990   182796.42Name: values, dtype: float64, ibnr_lower=origin1982    5175.931983   13626.821984   20455.831985   27213.401986   33871.171987   39957.671988   45818.991989   50615.381990   54752.36Name: values, dtype: float64, ibnr_upper=origin1982    57689.141983   151873.581984   227984.181985   303298.711986   377501.041987   445337.371988   510664.401989   564124.081990   610235.54Name: values, dtype: float64)

image-title-here

Open In Colab

To leave a comment for the author, please follow the link and comment on their blog: T. Moudiki's Webpage - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Continue reading: R version of Probabilistic Machine Learning (for longitudinal data) Reserving (work in progress)