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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With