[R] Error with constrained curve fitting through specific points

Tung Nguyen th@nhtungm||@n @end|ng |rom gm@||@com
Thu Jan 23 15:35:55 CET 2020


Thanks Rui and everyone for your help!

Using ` constraint = "increase"` and `lambda = 0.1` did it.
If we ignore the first point, `lambda = -1` option is also possible. `cobs`
will automatically choose a lambda value

	plot(fit_result)
	summary(fit_result)
	# COBS smoothing spline (degree = 2) from call:
	#   cobs(x = dat$x, y = dat$y, constraint = "increase", lambda = -1,
   pointwise = con)
	# {tau=0.5}-quantile;  dimensionality of fit: 29 from {29}
	# x$knots[1:9]: -6.0e-06,  2.5e-01,  5.0e-01, ... ,  6.0e+00
	# lambda = 1.4494, selected via SIC, out of 25 ones.
	# with 10 pointwise constraints
	# coef[1:11]: -6.1866e-05,  1.1065e+00,  2.7435e+00,  3.1815e+00,
3.7572e+00, ... ,  1.9183e+01
	# R^2 = 100% ;  empirical tau (over all): 5/10 = 0.5 (target tau= 0.5)


Best,

-- Tung



On Wed, Jan 22, 2020 at 9:39 AM Rui Barradas <ruipbarradas using sapo.pt> wrote:

> Hello,
>
> This seems to "work". It doesn't give errors nor warnings and the fitted
> line passes through the given points.
>
>
> fit_result <- cobs(dat$x, dat$y,
>                     constraint = "increase",
>                     lambda = 0.1,
>                     pointwise = con)
>
> plot(y~x, dat)
> pred <- predict(fit_result)
> lines(pred[,1], pred[,2], col = "red")
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 15:00 de 22/01/20, Tung Nguyen escreveu:
> > Hello R-Help,
> >
> > I'm trying to find the best fitting curve through a given set of points.
> > The fitted curve must also pass through these points. I found an answer
> on
> > Cross Validated which suggested to use the `cobs: Constrained B-Splines
> > (Sparse Matrix Based)` package. However, I got an error while testing it
> > with my sample data:
> >
> > Error in x %*% coefficients: NA/NaN/Inf in foreign function call (arg 2)
> >
> > *My question*: what caused this error and how can I fix it? Any
> suggestion
> > is greatly appreciated. Thanks!
> >
> > 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)*
> >
> >
> > Best regards,
> >
> > -- Tung
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list