简体   繁体   中英

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!

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.

在此处输入图片说明

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:

在此处输入图片说明

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM