Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curve Fitting in R

Any resources for curve fitting in R? I came across https://systatsoftware.com/products/sigmaplot/product-uses/sigmaplot-products-uses-curve-fitting-using-sigmaplot/

Any similar recommendations or libraries in R?

Thank you!

like image 620
mindhabits Avatar asked Oct 14 '25 03:10

mindhabits


1 Answers

Hi There are not one but several ways to do curve fitting in R. You could start with something as simple as below

x <- c(32,64,96,118,126,144,152.5,158)
#make y as response variable
y <- c(99.5,104.8,108.5,100,86,64,35.3,15)    
plot(x,y,pch=19)

This should give you the below plot. Eyeballing the curve tells us we can fit some nice polynomial curve here.

enter image description here

Now we could fit our curve(s) on the data below:

linMod  <- lm(y~x)
#second degree polynomial model
linMod2 <- lm(y~poly(x,2,raw=TRUE))
#third degree polynomial model
linMod3 <- lm(y~poly(x,3,raw=TRUE))
#fourth degree polynomial model
linMod4 <- lm(y~poly(x,4,raw=TRUE))
#generate new data in range of 50 numbers starting from 30 and ending at 160
newData <- seq(30,160, length=50)
plot(x,y,pch=19,ylim=c(0,150))
lines(newData, predict(linMod, data.frame(x=newData)), col="red")
lines(newData, predict(linMod2, data.frame(x=newData)), col="green")
lines(newData, predict(linMod3, data.frame(x=newData)), col="blue")
lines(newData, predict(linMod4, data.frame(x=newData)), col="purple")

Giving us:

enter image description here

This is just a simple illustration of curve fitting in R. There are tons of tutorials available out there, perhaps you could start looking here:

  • http://www.css.cornell.edu/faculty/dgr2/teach/R/R_CurveFit.pdf
  • https://rpubs.com/carlmart/228874
  • Fitting a curve to specific data
like image 171
humblecoder Avatar answered Oct 18 '25 07:10

humblecoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!