title: R - predict on the base of trend and seasonality
body: <p>I'm writing function used for forecasting sales on the base of trend and seasonality. I use dummy variables to represent seasonality and time variable for trend. Here is the code:</p>
  
  <pre><code>forecast&lt;-function(data, time, fn) {
  
  n &lt;- length(data)
  seasonal &lt;- factor(cycle(data))
  new_data &lt;- seq(from=cycle(data)[n]+1, length=fn, by=1) 
  new_seasonal &lt;- factor(new_data)
  trend &lt;- rep(0, n)
  new_trend &lt;- rep(0, n)
  if (time &gt; 0) {
      trend &lt;- vector()
      new_trend &lt;- vector()
  
      ttrend &lt;- seq(from=1, to=n, by=1)
      tnew_trend &lt;- seq(from=n+1, length=fn, by=1)
  
      for(i in 1:time) {
          trend &lt;- cbind(trend, ttrend^i)
          new_trend &lt;- cbind(new_trend, tnew_trend^i)
      }
  
      model_trend &lt;- lm(data ~ seasonal + trend)
  } else {
  
      model_trend &lt;- lm(data ~ seasonal)
  }
  
  
  df &lt;- data.frame(new_seasonal, new_trend)
  
      p &lt;- predict(model_trend, df)
  
  }
  
  forecast(data = dane.ts[,"SALES"], time=2, fn=5)
  </code></pre>
  
  <p>However I get warning:</p>
  
  <p>Warning message:
  'newdata' had 5 rows but variable(s) found have 104 rows </p>
  
  <p>And it seems that sth is wrong with this function. I would appreciate any help.</p>