title: randomForest error shown NA not permitted in predictors
body: <p>Can I get some help and suggestion from you guys since I am trying to run randomForest in classification problem on currency data but I got this pop-up showing NA not permitted in predictors. However, I have tried to solve it by myself but still cannot figure it out.</p>
  <pre><code>library(priceR)
  library(tidyverse)  
  library(quantmod) 
  library(dplyr) 
  </code></pre>
  <p>Get the data</p>
  <pre><code>a &lt;- historical_exchange_rates(&quot;THB&quot;, to = &quot;USD&quot;,start_date = &quot;2010-01-01&quot;, end_date = &quot;2021-12-31&quot;)
  </code></pre>
  <p>Set up input indicators</p>
  <pre><code>a.avg10 &lt;- rollapply(a[,2],10,mean)   
  a.avg20 &lt;- rollapply(a[,2],20,mean)   
  a.std10 &lt;- rollapply(a[,2],20,sd)   
  a.std20 &lt;- rollapply(a[,2],20,sd) 
  a.rsi5 &lt;- na.omit(RSI(a[,2],5,&quot;SMA&quot;))   
  a.rsi14 &lt;- na.omit(RSI(a[,2],14,&quot;SMA&quot;))  
  a.macd12269 &lt;- na.omit(MACD(a[,2],12,26,9,&quot;SMA&quot;))   
  a.macd7205 &lt;- na.omit(MACD(a[,2],7,20,5,&quot;SMA&quot;))   
  a.bbands &lt;- na.omit(BBands(a[,2],20,&quot;SMA&quot;,2))
  </code></pre>
  <p>Create variable direction</p>
  <pre><code>a.direction &lt;- a %&gt;% mutate(direction = ifelse(one_THB_equivalent_to_x_USD - lag(one_THB_equivalent_to_x_USD, 10) &lt;= 0, 0, 1))
  </code></pre>
  <p>Combining variables</p>
  <pre><code>a.data &lt;- cbind(a[1:4350,2],a.avg10[1:4350],a.avg20[1:4350],a.bbands[1:4350,1:4],a.std10[1:4350],a.std20[1:4350],a.rsi5[1:4350],a.rsi14[1:4350],a.macd12269[1:4350,1:2],a.macd7205[1:4350,1:2],a.direction[1:4350,3])
  </code></pre>
  <p>Train and test</p>
  <pre><code>a.split &lt;- sample(c(rep(0,0.7*nrow(a.data)),rep(1,0.3*nrow(a.data))))
  </code></pre>
  <p>Building in-sample and out-sample datasets</p>
  <pre><code>isa.data &lt;- a.data[a.split == 0,]  
  osa.data &lt;- a.data[a.split == 1,]
  </code></pre>
  <p>Standardizing the dataset of in-sample and out-sample</p>
  <pre><code>ismea.data &lt;- sapply(isa.data,mean,2) 
  issta.data &lt;- apply(isa.data,2,sd)   
  isida.data &lt;- matrix (1,dim(isa.data)[1],dim(isa.data)[2])  
  
  osmea.data &lt;- sapply(osa.data,mean,2)  
  ossta.data &lt;- apply(osa.data,2,sd)  
  osida.data &lt;- matrix (1,dim(osa.data)[1],dim(osa.data)[2])
  </code></pre>
  <p>Normalizing the data</p>
  <pre><code>norma.data &lt;- (isa.data - t(ismea.data*t(isida.data)))/t(issta.data*t(isida.data))  
  normosa.data &lt;- (osa.data - t(osmea.data*t(osida.data)))/t(ossta.data*t(osida.data))
  </code></pre>
  <p>Replacing last column with variable direction</p>
  <pre><code>a.dm &lt;- dim(isa.data)
  norma.data[,a.dm[2]] &lt;- a.direction[1:3045,3]   
  normosa.data[,a.dm[2]] &lt;- a.direction[3046:4350,3]
  </code></pre>
  <p>Combine as dataframe</p>
  <pre><code>isnorma.data &lt;- as.data.frame(norma.data)   
  osnorma.data &lt;- as.data.frame(normosa.data)   
  colnames(isnorma.data) &lt;- c(&quot;exchage rate&quot;, &quot;avg10&quot;, &quot;avg20&quot;, &quot;down&quot;, &quot;mavg&quot;, &quot;up&quot;, &quot;pctB&quot;, &quot;std10&quot;, &quot;std20&quot;, &quot;rsi5&quot;, &quot;rsi14&quot;, &quot;macd12269&quot;, &quot;signal12269&quot;, &quot;macd7205&quot;, &quot;signal7205&quot;, &quot;Direction&quot;)   
  colnames(osnorma.data) &lt;- c(&quot;exchage rate&quot;, &quot;avg10&quot;, &quot;avg20&quot;, &quot;down&quot;, &quot;mavg&quot;, &quot;up&quot;, &quot;pctB&quot;, &quot;std10&quot;, &quot;std20&quot;, &quot;rsi5&quot;, &quot;rsi14&quot;, &quot;macd12269&quot;, &quot;signal12269&quot;, &quot;macd7205&quot;, &quot;signal7205&quot;, &quot;Direction&quot;)
  </code></pre>
  <p>Modelling with random forest</p>
  <pre><code>rfisnorma.data &lt;- isnorma.data %&gt;% select(-Direction)   
  rfosnorma.data &lt;- osnorma.data %&gt;% select(-Direction)
  </code></pre>
  <p>Labeling train and test data with direction</p>
  <pre><code>a.lagret &lt;- (a[,2] - lag(a[,2],10))/ lag(a[,2],10)   
  rfa.direction &lt;- NULL   
  rfa.direction[a.lagret &gt; 0.02] &lt;- &quot;Up&quot;   
  rfa.direction[a.lagret &lt; -0.02] &lt;- &quot;Down&quot;  
  rfa.direction[a.lagret &lt; 0.02 &amp; a.lagret &gt; -0.02] &lt;- &quot;Nowhere&quot;   
  isdira.data &lt;- rfa.direction[1:3045]  
  osdira.data &lt;-  rfa.direction[3046:4350]
  </code></pre>
  <p>Convert labeled data into factors as only accepted by randomForest</p>
  <pre><code>isdira.data &lt;- na.omit(as.factor(isdira.data))   
  osdira.data &lt;- na.omit(as.factor(osdira.data))
  </code></pre>
  <p>Modelling data with input parameters</p>
  <pre><code>rfmodela.data &lt;- randomForest(rfisnorma.data[11:3045,1:15], y=as.factor(isdira.data), xtest=rfosnorma.data, ytest=as.factor(osdira.data), ntree=500, importance=TRUE)
  </code></pre>
  <p>In this step is where I got an error &quot;NA not permitted in predictors&quot;</p>