4

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!

mindhabits
  • 421
  • 1
  • 3
  • 10

1 Answers1

3

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:

humblecoder
  • 137
  • 1
  • 7