【R語言】非線性最小二乘

以誤差的平方和最小爲準則來估計非線性靜態模型參數的一種參數估計方法。設非線性系統的模型爲y=f(x,θ),常用於傳感器參數設定。

線性和非線性迴歸的目的是調整模型參數的值,以找到最接近您的數據的線或曲線。在找到這些值時,我們將能夠以良好的精確度估計響應變量。

在最小二乘迴歸中,我們建立了一個迴歸模型,其中來自迴歸曲線的不同點的垂直距離的平方和被最小化。我們通常從定義的模型開始,並假設係數的一些值。然後我們應用R語言的nls()函數獲得更準確的值以及置信區間。

在R語言中創建非線性最小二乘測試的基本語法:

nls(formula, data, start)
  • 以下是所使用的參數的描述 -
  • formula是包括變量和參數的非線性模型公式。

  • data是用於計算公式中變量的數據框。

  • start是起始估計的命名列表或命名數字向量。

例 我們將考慮一個假設其係數的初始值的非線性模型。 接下來,我們將看到這些假設值的置信區間是什麼,以便我們可以判斷這些值在模型中有多好。所以讓我們考慮下面的方程爲這個目的:

a = b1*x^2+b2
xvalues <- c(1.6,2.1,2,2.23,3.71,3.25,3.4,3.86,1.19,2.21)
yvalues <- c(5.19,7.43,6.94,8.11,18.75,14.88,16.06,19.12,3.21,7.58)

# Give the chart file a name.
png(file = "nls.png")


# Plot these values.
plot(xvalues,yvalues)


# Take the assumed values and fit into the model.
model <- nls(yvalues ~ b1*xvalues^2+b2,start = list(b1 = 1,b2 = 3))

# Plot the chart with new data by fitting it to a prediction from 100 data points.
new.data <- data.frame(xvalues = seq(min(xvalues),max(xvalues),len = 100))
lines(new.data$xvalues,predict(model,newdata = new.data))

# Save the file.
dev.off()

# Get the sum of the squared residuals.
print(sum(resid(model)^2))

# Get the confidence intervals on the chosen values of the coefficients.
print(confint(model))

運行結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章