我有以下数据:
x <- 0:10
y <- c(1, 0.0296734797447216, -0.115268522114696, 0.0685634237231258,
0.0462346454015914, 0.016874511238053, -0.00870738489741311,
0.0356310001815887, 0.0558631035027085, -0.116810154142989, -0.0460902529547028)
我的非线性函数是:
f <- function(t, coeff_sigma, coeff_alpha, coeff_omega) {
coeff_sigma * exp(-coeff_alpha * t) * cos(coeff_omega * t)
}
我安装了一个 nls
模型:
fit <- nls(y ~ f(x, coeff_sigma, coeff_alpha, coeff_omega),
start=list(coeff_sigma=2, coeff_alpha=2, coeff_omega=2),
control = nls.control(maxiter = 1000))
拟合模型打印:
Nonlinear regression model
model: y ~ f(x, coeff_sigma, coeff_alpha, coeff_omega)
data: df_acf
coeff_sigma coeff_alpha coeff_omega
0.9996 1.0482 1.5588
residual sum-of-squares: 0.02705
Number of iterations to convergence: 10
Achieved convergence tolerance: 2.309e-06
问题:
我应该在后面的步骤中使用参数值(coeff_sigma
、coeff_alpha
、coeff_omega
),但我唯一能做的就是现在是从屏幕上复制它们。有什么方法可以从 fit
获取这些值吗?
最佳答案
你可以做到
fit$m$getPars()
#coeff_sigma coeff_alpha coeff_omega
# 0.999570 1.048175 1.558793
基本上,fit$m
中的这些函数可以让您访问各种内容。
您还可以使用泛型函数coef
,因为它有一个“nls”方法。
coef(fit)
#coeff_sigma coeff_alpha coeff_omega
# 0.999570 1.048175 1.558793
summary()
更全面。但除非您想要估计系数的标准误差、p 值等,否则没有必要使用它。
关于r - 如何从 `nls`获取估计参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73268142/