title: Isolation Forest in R: incorrect number of dimensions in Isolation Forest (variation of RandomForest)
body: <p>Goal: Compute Score on Anomalies in data using a RandomForest variant Isolation Forest</p>
  <p>Language: R, libraries: IsolationForest</p>
  <p>Problem: The runtime R error is &quot;Error in weather_trees[, -1] incorrect number of dimensions&quot;. I originally followed an article from the following link which shows how to develop an R Isolation (Random Forest): <a href="https://www.r-bloggers.com/2018/06/anomaly-detection-for-business-metrics-with-r/" rel="nofollow noreferrer">https://www.r-bloggers.com/2018/06/anomaly-detection-for-business-metrics-with-r/</a> Inside this 2018 article, I used the essential same code to experiment, then apply my customization for this effort, when I got stuck on the incorrect number of dimensions in Isolation Forest object.</p>
  <p>Code Traceback:</p>
  <pre><code>Called from: Ops.data.frame(dataset[el[i, ], attrClass], dataset[i, attrClass])
  Browse[1]&gt; IsolationTreeAnomolyPlot(tt_weather)
  Error during wrapup: incorrect number of dimensions
  Browse[1]&gt; 
  </code></pre>
  <p>Code: The base function code and required R libraries. IsolationTrees() builds the isolation tree. Its definition. The problem that is occurring is on the x parameter of IsolationTrees()</p>
  <pre><code>Usage
  
         IsolationTrees(x, ntree=10, hlim=as.integer(ceiling(log2(nrow(x)))), rowSamp=F, nRowSamp=nrow(x), nmin=1, rFactor=1, colSamp=F, nColSamp=ncol(x), colWeight=c(rep(1,ncol(x))))
  Arguments
  
  x   
  a data frame of training samples
  ntree   
  number of tree to build
  </code></pre>
  <pre><code>library(AnomalyDetection)
  library(IsolationForest)
  
  IsolationTreeAnomolyPlot &lt;- function (tt_weather) {
   weather_trees &lt;- IsolationTrees(tt_weather[, -1])
   weather_anom_score &lt;- AnomalyScore(weather_trees[, -1], weather_trees)
  </code></pre>
  <p>Data:
  The tt_weather data set is the prepared data for input as passed to function. I have included a small sample of data that shows fields: date, prcp, with valid data. The structure for &quot;weather_trees&quot; is the computed object from &quot;weather_trees &lt;- IsolationTrees(tt_weather[, -1])&quot; (see above code sample)</p>
  <pre><code> dput(tt_weather)
  structure(list(date = structure(c(18536, 18537, 18538, 18539, 
  18540, 18541, 18542, 18543, 18544, 18545, 18546, 18547, 18548, 
  18549, 18550, 18551, 18552, 18553, 18554, 18555, 18556, 18557, 
  18558, 18559, 18560, 18561, 18562, 18563, 18564, 18565, 18566, 
  18567, 18568, 18569, 18570, 18571, 18572, 18573, 18574, 18575, 
  18576, 18577, 18578, 18579, 18580, 18581, 18582, 18583, 18584, 
  18585, 18586, 18587, 18588, 18589, 18590, 18591, 18592, 18593, 
  18594, 18595, 18596, 18597, 18598, 18599, 18600, 18601, 18602, 
  18603, 18604, 18605, 18606, 18607, 18608, 18609, 18610, 18611, 
  18612, 18613, 18614, 18615, 18616, 18617, 18618, 18619, 18620, 
  18621, 18622, 18623, 18624, 18625, 18626, 18627), class = &quot;Date&quot;), 
      prcp = c(0, 0, 0, 0, 0, 0, 0, 1.3, 21.8, 10.75, 0, 0, 0, 
      0, 0.8, 0, 0, 0, 0, 0, 0, 0, 3.3, 0, 0.3, 0.3, 0, 15.7, 0, 
      0, 0, 0, 0, 0, 0, 0, 0, 0.4, 1.8, 1.45, 0, 0, 0, 0, 0, 0.4, 
      0, 0, 0, 0, 0, 0, 0.3, 0, 1.3, 14.5, 0, 0, 0, 7.35, 0, 0, 
      17.5, 2, 0, 0, 0, 0, 0, 0, 0, 26.2, 0, 31, 0, 4.3, 0, 0, 
      0, 17.8, 0, 0, 0, 0, 0, 0, 0, 0, 0.8, 0, 26.4,
  </code></pre>
  <p><a href="https://i.stack.imgur.com/VLNDW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VLNDW.png" alt="date set" /></a></p>
  <p><a href="https://i.stack.imgur.com/ofmEN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ofmEN.png" alt="Isolation Tree object" /></a></p>