# Curve fitting for oscillating data

This is my first question. I have the following data that I'd like to approximate as a parametric function:

\begin{align} y = a + (bx_1 + cx_2 + dx_3 + ex_1x_2 + fx_1x_3 + gx_2x_3 + hx_1x_2x_3 + i)*(j*\sin(kx_1x_2x_3) + l\cos(mx_1x_2x_3)) \end{align}

testdata <- read.csv('..path_to_file/gistfile1.txt', sep = "")
plot(1:35,testdata$X0, col = 'blue', pch = 19, ylim = c(0,8),type = "l",lwd = 2, xlab = "x", ylab = "y_i",cex.lab = 2) lines(1:35,testdata$X1, col = 'red', pch = 19, ylim = c(0,1),lwd = 2)
lines(1:35,testdata$X1.1, col = 'green', pch = 19, ylim = c(0,1),lwd = 2) lines(1:35,testdata$X8, col = 'magenta', pch = 19, ylim = c(0,1),lwd = 2)
leg.txt <- (c("y","x1","x2","x3"))
legend(18,8,leg.txt, col  =  c('blue','red','green','magenta'), lty = 1,lwd= 2,border = 'white')


:

Any suggestions how to modify the function shown below so as to get a closer fit?

• Depends, what is your use for this model once you think it's sufficiently good? There's various ways one could fit this data, but the choice really depends on how you would use the resulting model. Jun 19 '16 at 14:30
• I also just noticed your inputs have a pattern. X3 = 10 - X2 - X1. Based on this, you don't even really need X3. You could try building a model with just X1 & X2 and that may be better since they are independent dimensions. Jun 19 '16 at 14:42
• What is the x-axis in that plot? Are there three (or two) variables there? A minimal reproducible example would really help here. Jun 20 '16 at 3:27
• You can update your question to include the data and change your equation to LaTeX. Also, you can mention people usernames (@nicoguaro), so they know that you have answered. Jun 28 '16 at 13:16

Numerical judgement of model choice:

You model 36 observations with a model consisting of 12 or 13 predictor variables. This is most likely not a good model. Even if you reach a high $R^2_{adj}$, you most likely model a random pattern. Try to compare a computed $AIC$ (Akaike information criterion) or $BIC$ (Bayesian information criterion) of this model to the one of a simple sinus function with 2 parameters. You might see that your model gets rejected.

Explain-ability of model choice:

In general in Statistics you want to be able to explain the interaction of variables in your model. For example you analyze Death Rates $DR_i$ of a population $P_i$. You find out that $DR_i$ increases exponentially with extreme outside temperatures $T$. So you want to fit a model:

$DR_i = T_{Deviation}*(T -T_{mean})²$

Here you would most likely find biological reasons, for example $T_{mean}$ reflects the average temperature of the natural surroundings of $P_i$. The other fitted model parameter $t_{Deviation}$ reflects $P_i$'s resistance (warm or cold blooded). $P_i$ is cold blooded an so its fitted $t_{Deviation}$ is larger than the one of warm blooded species $P_j$.

You will find a regression tool for nonlinear fits with up to three variables here. You type in your $x,y$ matrix and it predicts a huge variety of different models. I think earlier versions had AIC included but maybe it is also somewhere else on this page.