使用cobs软件包通过特定点进行约束曲线拟合时出错:外部函数调用中的NA / NaN / Inf

问题描述 投票:1回答:1

我正在尝试通过给定的一组点找到最佳拟合曲线。拟合曲线也必须通过这些点。我在交叉验证中发现了一个answer,建议使用cobs: Constrained B-Splines (Sparse Matrix Based)软件包。但是,在使用示例数据进行测试时出现错误:

cobs: Constrained B-Splines (Sparse Matrix Based)

我的问题:是什么引起了此错误,我该如何解决?我也欢迎使用不同方法/软件包的其他解决方案。谢谢!

Error in x %*% coefficients: NA/NaN/Inf in foreign function call (arg 2)  

“”

library(cobs)

dat <- data.frame(
  x = c(1e-06,0.25,0.5,0.75,1,2,3,4,5,6),
  y = c(1e-07,1.925,2.9625,3.469375,
        3.875,4.5315,4.89,5.09375,5.216,5.46))
dat
#>          x         y
#> 1  1.0e-06 0.0000001
#> 2  2.5e-01 1.9250000
#> 3  5.0e-01 2.9625000
#> 4  7.5e-01 3.4693750
#> 5  1.0e+00 3.8750000
#> 6  2.0e+00 4.5315000
#> 7  3.0e+00 4.8900000
#> 8  4.0e+00 5.0937500
#> 9  5.0e+00 5.2160000
#> 10 6.0e+00 5.4600000

# visual inspection
plot(dat); lines(dat)

# define constrained points con <- matrix( cbind(c(0,0,0,0,0,0,0,0,0,0), c(1e-06,0.25,0.5,0.75,1,2,3, 4,5,6), c(1e-07,1.925,2.9625,3.469375, 3.875,4.5315,4.89,5.09375,5.216, 5.46)), ncol = 3, nrow = 10) # curve fitting fit_result <- cobs(dat$x, dat$y, pointwise = con) #> qbsks2(): #> Performing general knot selection ... #> Error in x %*% coefficients: NA/NaN/Inf in foreign function call (arg 2) (v0.3.0)在2020-01-21创建

r constraints regression curve-fitting spline
1个回答
0
投票

您的问题听起来像“样条插值”。

可能是R中最简单的解决方案:

reprex package

我注意到曲线拟合有不同类型。例如,在回归建模中,通常的目标是找到最合适的线或平面(泛化为曲面)。样条曲线拟合和回归建模并非总是分开进行的,因为在某些情况下,样条曲线用于回归。

© www.soinside.com 2019 - 2024. All rights reserved.