5

I'm a beginner to R and I'm trying to fit a curve onto a data set that (for example) might look like the below:

(x- value)  (y-value)
105 423
115 471
125 567
135 808
145 921.5
155 1040

The x value's represent the amount of stimulus and the y values represent motor responses (in uV). These are averages over 10 subjects, where the x-values are the same for each subject.

I was told that this data set normally follows a sigmoidal fit. I tried fitting it with the following:

fit <- lm( y ~ poly(x, 3) )

But I'm not sure if this is the appropriate way to do this :(

My code looks like this so far:

p <- ggplot (data, aes(x, y)) +
  geom_point(shape= 21, fill= "blue", colour= "black", size=2) +
  xlab("X value") + ylab("Y value") +
  geom_smooth(method= "lm", se= FALSE,  colour= "red", formula=y ~ poly(x, 3, raw=TRUE)) +
  geom_errorbar(aes(ymin=y-SE, ymax=y+SE), width=.9)+ 
  ggtitle ("Title")
p

Additionally: Once I fit the the curve I would also like to obtain the slope (calculated as the value of the tangent at the steepest point of the curve)

Thanks in advance, any help would be greatly appreciated!

Purrina
  • 175
  • 3
  • 7
  • Maybe, also, [this](http://stackoverflow.com/questions/15102254/how-do-i-add-different-trend-lines-in-r) helps. – alexis_laz Oct 24 '13 at 21:17
  • 3
    I don't see how this is a duplicate.. as I'm using ggplot and they aren't, and this is definitely not a homework question. I am trying to work out an automated system to process a large set of patient data obtained. I just want to fit a curve to them and also obtain the slope at the steepest point of this fit curve. – Purrina Oct 24 '13 at 21:19
  • `ggplot2` was developed for plotting. Trying to extract features from a convenience curve-fitting feature is ill-advised. (Was this a strategy suggested by someone with strong devotion to Excel?) You should be doing your curve fitting in regular R. – IRTFM Oct 25 '13 at 04:53
  • If you can elaborate on that I would appreciate it. R is very new to me and everything I know on it so far has been pieced together from online sources. – Purrina Oct 25 '13 at 06:19
  • 2
    A sigmoid is a very different function to a third order polynomial, are you sure you want to use the polynomial? Using `optim` and fitting the sigmoid directly should be easy. I presume you also have the variance of each average? that would probably give a more reliable fit. Using the hessian of the fit will let you get estimates of the error in the fit. – Sam Mason Oct 25 '13 at 10:49
  • Seems this is not a duplicate, given @SamMason comment about optim etc. which don't get a look in on the marked dup (which is about logistic fitting?). Pointing user to a worked example of a sigmoid or cumulative normal fitting script might help? – tim Sep 02 '14 at 11:53
  • This might help. It is about versions of normal curves and their fit to particular data, but shows fitting and plotting that might help you here. http://stats.stackexchange.com/questions/74544/fitting-a-sigmoid-function-why-is-my-fit-so-bad – tim Sep 02 '14 at 16:36

1 Answers1

2

I don't know how the poly() function is supposed to work but if you want a 3rd order polynomial fit just use:

   lm1=lm(y~I(x^3)+I(x^2)+x)

That makes a pretty good fit on your toy data above. For the slope at the inflection point. I would set the second derivative to zero and solve for x. Then compute the first dirivative at that x.

But I think you would prefer a logistic growth model.

Seth
  • 4,745
  • 23
  • 27