# Avoiding a spline dip

First off, I'm not very experienced in math techniques, so I am wide open to suggestions.

I am working with a set of data in R. I have a curve (mostly linear at first, peaking suddenly, then decreasing.) I want to smooth it out, so I used a spline and that achieves what I want (especially the peak of the curve "off center" from the top point). But there is a dip right below the peak that I want to avoid, as its essentially linear right there.

I kind of understand (in a non-mathematical way) why this dip has to be there, to give the curve enough time to angle upwards. But it doesn't represent that data.

Is there any way to avoid this dip? I've thought about graphing it as two separate lines (a linear and a curved one), but that isn't appealing. Is there something besides a spline? Or code I can use to get around this?

Time          Feave
0.04    138.8181818
7    1258.636364
14    1320.545455
21     2110.37037
28    13730.37037
35    1550.909091


All I'm using is splinefun in R (so cubic)

plot(P0mM$Time,P0mM$Feave, ylab="Fe2+ uM", xlab="Time", main="0mM P", ylim=c(0,15000))
arrows(P0mM$Time, P0mM$Feave-P0mM$Festerr, P0mM$Time, P0mM$Feave+P0mM$Festerr, length=0.05, angle=90, code=3)
lines(spline(P0mM$Time,P0mM$Feave,n=200))

• What is your basis for saying "it doesn't represent the data"? You don't have any data between 14 and 21, so how do you know it doesn't go down there? If you have theoretical information that it should not, perhaps you should be using your knowledge to define the curve directly? Which raises the issue of why you are using splines in the first place? Is it just to make a pretty picture? You can get an answer to your question, but are you asking the right question? – Wayne Jun 26 '15 at 12:55
• Asking the right question...I guess I don't know. I want to be able to interpolate between the points, and remembering from classes I took years ago, that was the way to do it. I will look more into splines and see if that is exactly what I want, or else look elsewhere. Thank you. – Valerie S Jun 26 '15 at 23:23

There are a number of ways to avoid such effects (e.g. smoothing splines can often be tweaked so as to avoid a dip, or maybe some form of monotonic spline to the left of the peak will be needed), but I think in this particular case a simple approach might be to transform (perhaps take logs or square roots), fit a spline on that scale and transform back.

Unimodal splines exist and may suit you better.

I haven't used it (edit: well, I have now! see below), but I believe the package uniReg (on CRAN) will do unimodal splines.

...

Some code. Here I had previously but unimaginatively read your data into a data frame called a:

library(uniReg)
z=seq(min(a$Time),max(a$Time),length=201)
uf=with(a,unireg(Time,Fe2.,g=5,sigma=1))
plot(Fe2.~Time,a,ylim=c(0,14500))
lines(z,uf\$unimod.func(z))


The author also has a paper on unimodal splines - since published by the look, but I'll let you chase the paper up if you want it - but doesn't seem to mention it in the package documentation.

• Thanks. I didn't know you could do that. It never even crossed my mind. – Valerie S Jun 26 '15 at 4:31
• Normally I'd look to do something other than transformation but the noise is so low for most of the points it may do okay. Smoothing splines would be the next thing I'd look at. – Glen_b -Reinstate Monica Jun 26 '15 at 4:36
• Actually, I just tried the unireg function in uniReg and it worked a treat. I'll include some details – Glen_b -Reinstate Monica Jun 26 '15 at 4:54
• You rock! I'll give it a shot! – Valerie S Jun 26 '15 at 4:59
• Some example code is now included (I really made no attempt to do anything but make the code run okay). It looks like it can deal with different uncertainties on the points (but I set them all to be the same here). You might do much better; perhaps the default value for g should be used, or some of the other options changed. – Glen_b -Reinstate Monica Jun 26 '15 at 5:03