ove*_*cup 0 r non-linear-regression
我正在尝试将数据建模为 R 中的正态曲线。我使用方程的形式:
下面是方程参数计算的代码:
filtered_sp14 <- c(549.778714, 259.835892, 992.874178, 75.267324, 53.014376, 128.281700, 10.471976, 58.904862,
88.357293, 102.756260, 201.585529, 29.452431, 1130.973355, 198.967535, 233.001455, 106.028752,
962.112750, 75.921822, 858.047494, 82.466807, 198.967535, 70.685835, 68.722339, 130.899694,
52.359878, 41.233404, 53.014376, 187.186562, 10.471976, 26.179939, 39.269908, 7.853982,
26.179939, 47.123890, 311.541271, 157.734131, 111.919238, 53.014376, 392.699082, 58.904862,
294.524311, 141.371669, 52.359878, 114.537232, 15.707963, 47.123890, 147.262156, 23.561945,
16.755161, 26.179939)
filtered_osf <- c(22.885584, 24.905062, 8.816039, 6.877041, 8.782836, 5.853161, 4.088499, 11.792189, 17.533123, 7.043904,
9.189750, 11.216450, 22.821028, 27.308823, 10.652498, 18.590091, 5.973716, 4.387657, 5.091982, 5.973609,
5.012901, 22.547434, 11.238273, 19.235493, 6.206930, 3.276575, 3.791221, 3.460380, 5.404704, 12.278297,
4.604634, 10.614706, 5.425239, 3.450711, 2.214432, 2.573192, 5.037149, 40.996842, 26.001749, 6.615399,
8.947295, 3.335943, 48.198579, 10.110514, 9.286836, 7.668401, 17.463995, 14.653925, 10.574502, 8.403515)
A_initial <- max(filtered_sp14)
mean_initial <- mean(filtered_osf)
sd_initial <- sd(filtered_osf)
# Set up the non-linear least squares model
nls_model <- nls(filtered_sp14 ~ A * exp(-((filtered_osf - mean)^2) / (2 * sd^2)),
start = list(A=A_initial, mean=mean_initial, sd=sd_initial),
control = nls.control(maxiter = 1000))
plot(filtered_osf, filtered_sp14, main="Non-linear Regression", xlab="X", ylab="Y", pch=19, col="blue")
Run Code Online (Sandbox Code Playgroud)
模型的系数如下所示。平均值具有视觉意义,但负标准差应该是不可能的。
> coefficients(nls_model)
A mean sd
284.14143 25.33275 -16.82272
Run Code Online (Sandbox Code Playgroud)
为什么nls()显示负 SD?
因为你没有指定下限。这可以通过使用“端口”算法来实现:
nls_model <- nls(filtered_sp14 ~ A * exp(-((filtered_osf - mean)^2) / (2 * sd^2)),
start = list(A=A_initial, mean=mean_initial, sd=sd_initial),
algorithm = "port", lower = c(0, 0, 0),
control = nls.control(maxiter = 1000))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44 次 |
| 最近记录: |