3

I’m working in R. I have a function (F) relating one variable (X) to another variable (Y). (F) has 3 mutable parameters (A, B, C), such that Y == F(X) == G(X, A, B, C). I would like to fit this function to a dataset which gives N (X, Y) pairs by finding the best (A, B, C).

The relationship between (X) and (Y) is neither polynomial and nor some kind of probability distribution. I'm curious if there's a way to do this for an arbitrary (F) as defined above.

cfi
  • 10,915
  • 8
  • 57
  • 103
  • 1
    You have to come up with some objective function to optimize. Like squared residuals. Or add an error term to it that is normally distributed or something and you do get a probability function to optimize. Plenty of tools to do that. `optim` being the main one. If you add an error term you could also use a Bayesian approach using BUGS, JAGS or Stan and their corresponding R packages. But I think this question isn't really suited for stackoverlow. It is too vague and not related to programming technical questions. You might want to try crossvalidated.com with an example of your function. – Sacha Epskamp Dec 03 '12 at 18:10
  • Hmmm, on re-reading your question: if you want the software to come up with the function itself (not just A,B,C) then take a look at `eureqa` over at Cornell.edu. – Carl Witthoft Dec 03 '12 at 18:18
  • Carl, to clarify, I have the function, I just need the parameters. And I have good initial guesses. I think nls() will eventually work, but all the examples I can find are waaaaay too complicated to parse without a ton of context. I would much appreciate it if you could point me to some extremely simple nls() examples. – user1873348 Dec 03 '12 at 19:40
  • Here's one from my old notes. `DF<-data.frame(x=1:10, y=3*(1:10)^.5+rnorm(10)) > nls(y~a*x^b, data=list(x=DF[,1], y=DF[,2]), start=list(a=3, b=.7))` – Carl Witthoft Dec 03 '12 at 19:45
  • Another [example in a question](http://stackoverflow.com/questions/14645199/curve-fitting-this-data-in-r). Seems simple enough but the other asks for other solutions. So it may not be helpful – cfi Sep 15 '15 at 07:14

1 Answers1

2

Yes, ?nls . There are a number of nonlinear regression packages for R; nls is kinda the basic function. If you've never used formulas in R, you should do a little reading up on them in the help files. But, like in any other computer language, you'll need to provide some initial "guess" values for your coefficients A,B,C .

for example, nls(Y ~ I(A*sin(x) + B*exp(x/C)),data=x, start=list(A=1,B=.5,C=3))

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73