title: Using nnet for prediction, am i doing it right?
body: <p>I'm still pretty new to R and AI / <a href="http://en.wikipedia.org/wiki/Machine_learning">ML</a> techniques. I would like to use a neural net for prediction, and since I'm new I would just like to see if this is how it should be done. </p>
  
  <p>As a test case, I'm predicting values of <code>sin()</code>, based on 2 previous values. For training I create a data frame with<code>y = sin(x)</code>, <code>x1 = sin(x-1)</code>, <code>x2 = sin(x-2)</code>, then use the formula <code>y ~ x1 + x2</code>. </p>
  
  <p>It seems to work, but I am just wondering if this is the right way to do it, or if there is a more idiomatic way.</p>
  
  <p>This is the code:</p>
  
  <pre><code>require(quantmod) #for Lag()
  requre(nnet)
  x &lt;- seq(0, 20, 0.1)
  y &lt;- sin(x)
  te &lt;- data.frame(y, Lag(y), Lag(y,2))
  names(te) &lt;- c("y", "x1", "x2")
  p &lt;- nnet(y ~ x1 + x2, data=te, linout=TRUE, size=10)
  ps &lt;- predict(p, x1=y)
  plot(y, type="l")
  lines(ps, col=2)
  </code></pre>
  
  <p>Thanks</p>
  
  <h2>[edit]</h2>
  
  <p>Is this better for the predict call? </p>
  
  <pre><code>t2 &lt;- data.frame(sin(x), Lag(sin(x)))
  names(t2) &lt;- c("x1", "x2")
  vv &lt;- predict(p, t2)
  plot(vv)
  </code></pre>
  
  <p>I guess I'd like to see that the nnet is actually working by looking at its predictions (which should approximate a sin wave.)</p>