-2

I have the following set of data:

x = c(8,16,64,128,256)
y = c(7030.8, 3624.0, 1045.8,  646.2,  369.0)

Which, when plotted, looks like an exponential decay or negative ln function.

I'm trying to fit a smooth curve to this data, but I don't know how. I've tried nls and lm functions, but I can't seem to get it right. The online examples have too many steps for the simple data I have, and I can't understand well enough to modify the examples for what I need. Any help or advice would be appreciated. Thank you.

Edit: When I say I tried nls and lm functions, I mean that the lines produced were linear, no matter what parameters I tried. And when I say too many steps, I mean the examples I found were for predicting with 2 independent variables, or for creating multiple fit lines. What I'm asking is what is the best way to fit a simple smooth line to data that, when graphed, looks like an exponential decay or negative ln. What the equation of the line is isn't important, it's meant to be a reference for the shape of the data.

user2954167
  • 155
  • 1
  • 3
  • 14
  • 1
    log is a function, `curve(log(x))` – Rorschach Jul 07 '15 at 19:24
  • Show what code you tried and describe how it failed. You need to choose a method you want to fit the data. If you need statistical advice, you should be asking over at [stats.se] instead. How may steps is "too many"? Make sure you are asking for help rather than just asking someone to do it for you. – MrFlick Jul 07 '15 at 19:24

2 Answers2

3

A good way to fit a curve to a function is the built-in nls function, which performs non-linear least squares optimization. For example, if you wanted to fit the model y = b * x^e, you could do:

n <- nls(y ~ b * x ^ e, data = data.frame(x, y), start = c(b = 1000, e = -1))

(?nls, or this walkthrough, can tell you more about these options). You could then plot the curve on top of your points:

plot(x, y)
curve(predict(n, newdata = data.frame(x = x)), add = TRUE)

You can try a few other models (specified by that formula in nls) that may fit your data.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
0

Maybe 'lowess' is what you're looking for? Try:

plot(y ~ x)
lines(lowess(y ~ x))

That function just connects the dots. It sounds like you would prefer something that smooths out the elbows. In principle, 'loess' is useful for that, but you don't have enough data points here for that to work.

ulfelder
  • 5,305
  • 1
  • 22
  • 40