rsupervised-learningmlrlongitudinal

Can the mlr package be used to make predictions based on data from a panel study?


I am planning to do a supvervised machine learning project where I use data from a longitudinal study (panel study). The goal is to use the 2004 and 2009 predictors to predict the 2014 outcomes. I have now done a first data-preprocessing and the data frame looks like the following in a highly abbreviated form:

data_ml <- structure(
  list(
    ID = c(
      201,
      203,
      602,
      901,
      1202,
      1501,
      1601,
      1602,
      1603,
      201,
      203,
      602,
      901,
      1202,
      1501,
      1601,
      1602,
      1603,
      201,
      203,
      602,
      901,
      1202,
      1501,
      1601,
      1602,
      1603
    ),
    Studyyear = c(
      2004,
      2004,
      2004,
      2004,
      2004,
      2004,
      2004,
      2004,
      2004,
      2009,
      2009,
      2009,
      2009,
      2009,
      2009,
      2009,
      2009,
      2009,
      2014,
      2014,
      2014,
      2014,
      2014,
      2014,
      2014,
      2014,
      2014
    ),
    Gender = c(2, 1, 2, 2, 2, 1, 1, 2, 1,
               2, 1, 2, 2, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 1, 2, 1),
    Predictor1 = c(6,
                   5, 4, 6, 4, 6, 4, 3, 3, 6, 5, 4, 6, 4, 6, 4, 3, 3, 6, 5, 4, 6,
                   4, 6, 4, 3, 3),
    Predictor2 = c(2, 2, 1, 1, 2, 2, 1, 2, 2, 2,
                   2, 1, 1, 2, 2, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 2, 2),
    Predictor3 = c(0,
                   6, 1, 6, 0, 0, 4, 2, 3, 0, 6, 1, 6, 0, 0, 4, 1, 1, 1, 6, 1, 6,
                   0, 0, 4, 1, 1),
    Outcome1 = c(0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1,
                 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1),
    Outcome2 = c(0,
                 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0,
                 1, 0, 1, 1, 0)
  ),
  class = c("tbl_df", "tbl", "data.frame"),
  row.names = c(NA,-27L)
)

Until now, my prediction projects did not include the time dimension (see data_ml: "Studyyear"). So I could just create a task and then make the prediction with the "mlr" package as follows:

library(mlr)
task <- makeClassifTask(data = data_ml, target = 'Outcome1', positive = '1')
measures = list(acc, auc, tpr, tnr, f1)
resampling_MC <- makeResampleDesc(method = 'Subsample', iters = 500) 
learner_logreg <- makeLearner('classif.logreg', predict.type = 'prob')
benchmark_MC <- benchmark(learners = learner_logreg, tasks = task, resamplings = resampling_MC, measures = measures)

Is it still possible to work with the "mlr" package with such a data frame as above and include the time dimension?


Solution

  • Yes, you can do this with the mlr3forecasting package, see the example here. The package isn't on CRAN yet though and still experimental, so you'll have to install it from Github (instructions on the package website) and things may not work as expected.