<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/fitting-a-rational-function-in-r-using-ordinary-least-squares-regression/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2017/04/fitting-rational-functions-with-lm.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><em>by Srini Kumar, VP of Product Management and Data Science, LevaData; and Bob Horton, Senior Data Scientist, Microsoft</em></p>
<p>A rational function is defined as the ratio of two functions. The <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Pad%C3%A9_approximant">Padé Approximant</a> uses a ratio of polynomials to approximate functions:</p>
<p>$$<br>
R(x)= \frac{\sum_{j=0}^m a_j x^j}{1+\sum_{k=1}^n b_k x^k}=\frac{a_0+a_1x+a_2x^2+\cdots+a_mx^m}{1+b_1 x+b_2x^2+\cdots+b_nx^n}<br>
$$<br>
Here we show a way to fit this type of function to a set of data points using the <code>lm</code> function in R.</p>
<p>We’ll use a data-simulating function based on a ratio of polynomials to generate a set of <code>y</code> values for some random <code>x</code> values, add some noise, then see how well we can fit a curve to these noisy points.</p>
<pre>
set.seed(123)
N &lt;- 1000
x &lt;- rnorm(N)
f &lt;- function(x) 50*x^2/(1 + 4*x) # data-simulating function

y &lt;- f(x) + rnorm(N, sd=3)

point_data &lt;- data.frame(x, y)

library(&quot;tidyverse&quot;)

ggplot(point_data, aes(x=x, y=y)) + 
  geom_point() + 
  ylim(-100, 100) + 
  ggtitle(&quot;simulated data points&quot;)
</pre>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2737e00970c-pi"><img title="Point_data-1" alt="Point_data-1" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2737e00970c-800wi" border="0"></a></p>
<p>Note that in the formula for the Padé approximant the numerator polynomial has an zeroth-order coefficient (\(a_0\)), where the denominator just has a constant \( 1\). This makes it easy to rearrange the equation to express \(y\) as a linear combination of terms.</p>
<p>$$<br>
y = \frac{a_0 + a_1 x + a_2 x^2}{1 + b_1 x + b_2 x^2}<br>
$$<br>
$$<br>
y (1 + b_1 x + b_2 x^2) = a_0 + a_1 x + a_2 x^2<br>
$$<br>
$$<br>
y = a_0 + a_1 x + a_2 x^2 - b_1 x y - b_2 x^2 y<br>
$$</p>
<p>Now we have a form that <code>lm</code> can work with. We just need to specify a set of inputs that are powers of <code>x</code> (as in a traditional polynomial fit), and a set of inputs that are <code>y</code> times powers of <code>x</code>. This may seem like a strange thing to do, because we are making a model where we would need to know the value of <code>y</code> in order to predict <code>y</code>. But the trick here is that we will not try to use the fitted model to predict anything; we will just take the coefficients out and rearrange them in a function. The <code>fit_pade</code> function below takes a dataframe with <code>x</code> and <code>y</code> values, fits an <code>lm</code> model, and returns a function of <code>x</code> that uses the coefficents from the model to predict <code>y</code>:</p>
<pre>
fit_pade &lt;- function(point_data){
  fit &lt;- lm(y ~ x + I(x^2) + I(y*x) + I(y*x^2), point_data)
  lm_coef &lt;- as.list(coef(fit))
  names(lm_coef) &lt;- c(&quot;a0&quot;, paste0(rep(c('a','b'), each=2), 1:2))

  with(lm_coef, function(x)(a0 + a1*x + a2*x^2)/(1 - b1*x - b2*x^2))
}
</pre>
<p>We'll use <code>ggplot2</code> to visualize the results; since it drops points outside the plotted range, it does not try to connect the points on either side of the discontinuity, as long as the data gives expected y values past those limits.</p>
<pre>
plot_fitted_function &lt;- function(x_data, fitted_fun, title){
  x_data$y_hat &lt;- fitted_fun(x_data$x)
  g &lt;- ggplot(x_data, aes(x=x, y=y)) + 
    geom_point() + ylim(-100, 100) +
    geom_line(aes(y=y_hat), col=&quot;red&quot;, size=1) + 
    ggtitle(title)

  plot(g)
}
</pre>
<p>When we draw the fitted values over the input data points, we see that in this case the function describes the points quite well:</p>
<pre>
pade_approx &lt;- fit_pade(point_data)

plot_fitted_function(point_data, pade_approx, title=&quot;fitted function&quot;)
</pre>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb098c4949970d-pi"><img title="Show_approximations-1" alt="Show_approximations-1" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb098c4949970d-800wi" border="0"></a></p>
<p>Here are some more examples, showing that this approach can work with some interesting patterns of points:</p>
<pre>
function_list &lt;- list(
  function(x) (100 - 50*x - 100*x^2)/(1 - 10*x - 10*x^2),
  function(x) (100 - 50*x - 100*x^2)/(1 - 10*x - 5*x^2),
  function(x) (100 - 50*x - 100*x^2)/(1 - 50*x - 5*x^2)
)

for (f in function_list){
  sim_data &lt;- point_data %>% mutate(y=f(x) + 
      rnorm(nrow(point_data), sd=3))
  plot_fitted_function(sim_data, fit_pade(sim_data), 
      title=as.character(deparse(f))[2])
}
</pre>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c8e91d25970b-pi"><img title="Other_examples-1" alt="Other_examples-1" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c8e91d25970b-800wi" border="0"></a></p>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb098c4982970d-pi"><img title="Other_examples-2" alt="Other_examples-2" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb098c4982970d-800wi" border="0"></a></p>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c8e91d2c970b-pi"><img title="Other_examples-3" alt="Other_examples-3" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c8e91d2c970b-800wi" border="0"></a></p>
<p>Here we have used linear regression by ordinary least squares (with <code>lm</code>) to fit distinctly nonlinear rational functions. For simplicity, these examples focus on equations of second order (or less) in both numerator and denominator, but the idea extends to higher orders. On a cautionary note, this approach seems to have numerical stability issues with some inputs. For example, if you take one of the data-simulating equations above and make selected coefficients much larger, you can create datasets that are fitted poorly by this method. And if the data-simulating function does not have the correct form (for example, if the zeroth order term in the denominator is not 1), the fitted curves can be completely wrong. For practical purposes it might be preferable to use a nonlinear least squares approach (e.g., the <code>nls</code> function). Still, this approach works well in many examples, and it lets you fit some curves that cannot be represented by ordinary polynomials.</p>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2017/04/fitting-rational-functions-with-lm.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/98eRVILewsE"></a></div>