title: How to reproduce predict.svm() in R when doing regression?
body: <p>I have trained a svm model in R using eps-regression and a radial kernel in the e1017 package. I can make predictions for new observations when using the function predict() but I'm interested in reproducing this outside a R environment. To do so I need to know how does R predict new observations.</p>
  
  <p>There is an example of how to do so when using svm to do classification (<a href="https://stackoverflow.com/questions/14375072/how-to-reproduce-predict-svm-in-r?rq=1">here</a>) but it doesn't work when doing regression due to the fact that the predictive function has a different formula:
  In the case of classification the predictive function is given by </p>
  
  <pre><code>$\hat{f}(x)= \sum^N_{i=1} \hat{\alpha}_i y_i K(x,x_i)+\hat{\beta}_0 $, 
  </code></pre>
  
  <p>where $y_i$ is already contained in m$coefs</p>
  
  <p>whereas in the case of regression the predictive function is given by </p>
  
  <pre><code>$\hat{f}(x)= \sum^N_{i=1} \hat{\alpha}_i K(x,x_i) $
  </code></pre>
  
  <p>both formulas according to Hastie, Tibshirani, Friedman (2001).</p>
  
  <p>I have also read <a href="http://www.bioconductor.org/help/course-materials/2002/Heidelberg02/moldiag-svm.pdf" rel="nofollow noreferrer">here</a> that \hat{\alpha}_i are the support vectors so I tried to use them in my predictive function, instead of the coeficients used in the classification case.</p>
  
  <p>As an example one could use:</p>
  
  <pre><code>library(e1071)
  
  x &lt;- seq(0.1, 5, by = 0.05) 
  y &lt;- log(x) + rnorm(x, sd = 0.2)
  
  m   &lt;- svm(y~x) 
  new &lt;- predict(m, x)
  
  k&lt;-function(x,x1,gamma){   
     return(exp(-gamma*sum((x-x1)^2))) }
  
  f&lt;-function(x,m){   
     return(t(m$SV) %*% as.matrix(apply(m$SV,1,k,x,m$gamma))) }
  
  my.predict&lt;-function(m,x){   
              apply(x,1,f,m)}
  
  x&lt;-as.matrix(x) 
  my.predict(m,x)[1:10]
  new[1:10]
  </code></pre>
  
  <p>Can someone explain where is this going wrong and/or recommend literature on the subject?</p>
  
  <p>Thanks,<br>
  Marta </p>