R中的曲线拟合

时间:2019-04-22 22:01:55

标签: r curve-fitting

R中是否有用于曲线拟合的资源? 我碰到了https://systatsoftware.com/products/sigmaplot/product-uses/sigmaplot-products-uses-curve-fitting-using-sigmaplot/

R中是否有类似的建议或库?

谢谢!

1 个答案:

答案 0 :(得分:1)

嗨,在R中没有一种方法可以进行曲线拟合。您可以从以下简单的步骤开始

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)

这应该给您下面的图。眼球曲线告诉我们我们可以在这里拟合一些漂亮的多项式曲线。

enter image description here

现在我们可以在以下数据上拟合曲线:

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")

给我们:

enter image description here

这只是R中曲线拟合的简单说明。那里有大量的教程,也许您可​​以在这里开始查找: