title: Implementing Longitudinal Random Forest with LongituRF package in R
body: <p>I have some high dimensional repeated measures data, and i am interested in fitting random forest model to investigate the suitability and predictive utility of such models. Specifically i am trying to implement the methods in the <code>LongituRF</code> package. The methods behind this package are detailed here :</p>
  <p><a href="https://journals.sagepub.com/doi/10.1177/0962280220946080" rel="noreferrer">Capitaine, L., et al. Random forests for high-dimensional longitudinal data. Stat Methods Med Res (2020) doi:10.1177/0962280220946080.</a></p>
  <p>Conveniently the authors provide some useful data generating functions for testing. So we have</p>
  <pre><code>install.packages(&quot;LongituRF&quot;)
  library(LongituRF)
  </code></pre>
  <p>Let's generate some data with <code>DataLongGenerator()</code> which takes as arguments n=sample size, p=number of predictors and G=number of predictors with temporal behavior.</p>
  <pre><code>my_data &lt;- DataLongGenerator(n=50,p=6,G=6)
  </code></pre>
  <p><code>my_data</code> is a list of what you'd expect Y (response vector),
  X (matrix of fixed effects predictors), Z (matrix of random-effects predictors),
  id (vector of sample identifier) and time (vector of time measurements). To fit random forest model simply</p>
  <pre><code>model &lt;- REEMforest(X=my_data$X,Y=my_data$Y,Z=my_data$Z,time=my_data$time,
                      id=my_data$id,sto=&quot;BM&quot;,mtry=2)
  </code></pre>
  <p>takes about 50secs here so bear with me</p>
  <p>so far so good. Now im clear about all the parameters here except for <code>Z</code>. <strong>What is</strong> <code>Z</code> <strong>when i go to fit this model on my actual data?</strong></p>
  <p>Looking at <code>my_data$Z</code>.</p>
  <pre><code>dim(my_data$Z)
  [1] 471   2
  head(my_data$Z)
        [,1]      [,2]
   [1,]    1 1.1128914
   [2,]    1 1.0349287
   [3,]    1 0.7308948
   [4,]    1 1.0976203
   [5,]    1 1.3739856
   [6,]    1 0.6840415
  </code></pre>
  <p>Each row of looks like an intercept term (i.e. 1) and values drawn from a uniform distribution <code>runif()</code>.</p>
  <p>The documentation of <code>REEMforest()</code> indicates that &quot;Z   [matrix]: A Nxq matrix containing the q predictor of the random effects.&quot; <strong>How is this matrix to be specified when using actual data?</strong></p>
  <p>My understanding is that traditionally Z is simply one-hot (binary) encoding of the group variables (e.g. <a href="https://stats.idre.ucla.edu/other/mult-pkg/introduction-to-linear-mixed-models/" rel="noreferrer">as described here</a>), so <code>Z</code> from the <code>DataLongGenerator()</code> should be nxG (471x6) sparse matrix no?</p>
  <p>Clarity on how to specify the <code>Z</code> parameter with actual data would be appreciated.</p>
  <p><strong>EDIT</strong></p>
  <p>My specific example is as follows, i have a response variable (<code>Y</code>). Samples (identified with <code>id</code>) were randomly assigned to intervention (<code>I</code>, intervention or no intervention). A high dimensional set of features (<code>X</code>). Features and response were measured at two timepoints (<code>Time</code>, baseline and endpoint). I am interested in predicting <code>Y</code>, using <code>X</code> and <code>I</code>. I am also interested in extracting which features were most important to predicting <code>Y</code> (the same way Capitaine et al. did with HIV in their paper).</p>
  <p>I will call <code>REEMforest()</code> as follows</p>
  <pre><code>REEMforest(X=cbind(X,I), Y=Y, time=Time, id=id)
  </code></pre>
  <p>What should i use for <code>Z</code>?</p>