nls参数估计
利用nls进行非线性模型中的参数估计
R中的nls用于非线性回归建模,对非线性函数的参数进行最优化的过程,最优化后的参数会使得模型的RSS(残差平方和)达到最小。
1、非线性函数
本例中的函数(如下):因变量为K,自变量为theta_w,常数值Ks,n,未知参数为ts,theta_c。 现在实测值K,以及其对应的theta_w已知,利用R中的nls对未知参数参数进行估计。
f <- function(theta_w,Ks,n,ts,theta_c){
Ksat <- Ks^(1-n)*0.6^n
Kdry <- 0.75*10^(-1.2*n)
b1<- ((-theta_c*Ksat^(1/ts))+((n- theta_c)*Kdry^(1/ts))) / (2*(n-theta_c))
b2<- (Ksat^(1/ts)-Kdry^(1/ts))/ (2*(n-theta_c))
b3<- ((theta_c*Ksat^(1/ts)-(n-theta_c)*Kdry^(1/ts))^2+(4*theta_c*(n-theta_c)*Ksat^(1/ts)*Kdry^(1/ts)))/((Ksat^(1/ts)-Kdry^(1/ts))^2)
sgn<- ifelse (ts>0,1,-1)
b<-(b3+2*theta_w*b1*b2^(-1)+theta_w^2)
(b1+b2*theta_w+sgn*b2*sqrt(b))^ts
}
2、样本数据
Ks | N | Vw | K | |
---|---|---|---|---|
959 | 3 | 0.4 | 0.08 | 0.70 |
960 | 3 | 0.4 | 0.16 | 0.92 |
961 | 3 | 0.4 | 0.24 | 1.14 |
962 | 3 | 0.4 | 0.32 | 1.32 |
963 | 3 | 0.4 | 0.40 | 1.47 |
3、拟合
# nls函数
Kmodel <- nls(K~f(Vw,Ks,N,ts,theta_c),
data=b,
start = list(ts=0.3,theta_c=0.01),
trace = T,
algorithm = "plinear")
# 生成新的数据,检验拟合效果
# 自变量Vw
linedata <- data.frame(Vw=seq(range(b$Vw)[1],range(b$Vw)[2],length.out = 1000))
# 模型预测的因变量p.precict
linedata$