# How to fit data to a piecewise function?

My question today regards a set of data that I wish to fit a piecewise-defined continuous function. This data set covers a domain of x-values from $$0$$ to $$\mu$$ on the x-axis.

What I need is to determine a value $$x_0 \in {\rm I\!R}+$$ and two functions of given forms $$f_1: [0, x_0] \mapsto {\rm I\!R}$$ and $$f_2: [x_0, \mu] \mapsto {\rm I\!R}$$ such that $$f_1$$ and $$f_2$$ are both continuous and $$f_1(x_0) = f_2(x_0)$$ and $$f_1 \cup f_2$$ is the piecewise function of this type which best fits the data.

Can I please have some information/instruction on the background theory regarding how to do this? Would linear least squares and elementary multivariable calculus be enough given that $$f_1$$ and $$f_2$$ must both be smooth on $$(0, x_0)$$ and $$(x_0, \mu)$$ respectively?

In this case, I need to find $$f_1 = ax^\frac{3}{2}$$ for some $$a \in {\rm I\!R}$$ and $$f_2 = bx + c$$ for some $$b, c \in {\rm I\!R}$$ such that $$b \in [-d,d]$$ for some given $$d \in {\rm I\!R}$$; so the goal is essentially to find a piecewise function consisting of a power law curve and a line that is nearly horizontal which best fits the data I am looking at.

I would very much appreciate help on this problem if you would be so kind.

• Google "piecewise regression" or "segmented regression". Also, you'll need to be able to state if $d$ is known or needs to be estimated along with the other parameters. And finally, is the variability about the two segments the same? You might want to consult with a statistician. – JimB Jan 17 '19 at 1:13
• Usually it requires an iterative estimation process. Rarely is there a closed-form solution. But if you're willing to "just make a hack", I hope it's not for anything important. – JimB Jan 17 '19 at 1:26
• Good. The usual approach is to let the data estimate all of the parameters including $d$. See en.wikipedia.org/wiki/Segmented_regression. – JimB Jan 17 '19 at 2:51
• As long as the two models are linear with respect to parameters, it is pretty simple and very unexpensive fixing $d=x_k$ (the $x$being sorted first) and varying $k$. When the best $k$ has been obtained, you have very good estimates adn you can easily polish all parameters (including $d$) using a simple optimizer. – Claude Leibovici Jan 17 '19 at 4:04
• As @ClaudeLeibovici stated your model IS linear with respect to the parameters. Not knowing that is another reason to consult a statistician. – JimB Jan 17 '19 at 4:13

## 1 Answer

I don't know what software package you might want to use but here is one of many ways to estimate the parameters using R.

First note that for the resulting function to be continuous you need

$$a x_0^{3/2} = b x_0 + c$$

So we could take $$c = a x_0^{3/2} - b x_0$$.

# Generate data from a segmented model and plot it
a = 2
b = 0.125
x = c(0:50)/10
x0 = 2
c = a*x0^(3/2) - b*x0
set.seed(12345)
y = (x<x0)*a*x^(3/2) + (x>=x0)*(b*x+c) + rnorm(51,0,0.5)
plot(x,y, las=1)

# Function to calculate sum of squared distance from observed value
ss = function(parameters, y) {
a = parameters
b = parameters
x0 = parameters
c = a*x0^(3/2) - b*x0
sum((y - ((x<x0)*a*x^(3/2) + (x>=x0)*(b*x + a*x0^(3/2) - b*x0)))^2)
}


Now with some starting values (and good starting values are very valuable) find the estimates that minimize the sum of squares:

sol = optim(c(1.5,0.1,1.5), ss, y=y)
sol$par # 2.05963514 0.04598089 2.01290367  Plot the data and the fit: • Wonderful answer, thank you. My research group uses Matlab and I am not aware that any of us are familiar with$R$. However, I am certain that we will take your advice here into account. I will keep the question open for now in case there is any more input that could be helpful, whether from you or from other users. – user454210 Jan 17 '19 at 3:48 • upon further evaluation of your answer, I think you may have mistaken$d$for the value I referred to as$x_0$in the problem statement. If this is a mistake, can you please amend your answer?$x_0$is supposed to be the knot/transition value for the piecewise function, not$d$.$d$is just a positive number that specifies the range of acceptable slope values for the line segment – user454210 Jan 17 '19 at 4:04 • What I didn't include was the estimation of confidence intervals for the parameters or for the predictions. When you do use Matlab, I would argue it is essential to report on the "goodness" of the fitted function. – JimB Jan 17 '19 at 4:05 • Got it. I'll change$d$to$x_0\$. – JimB Jan 17 '19 at 4:06