Error when using smooth.spline

When trying to interpolate a series of data the cubic spline is a great technique to be used.
I choose to use the smooth.spline function, from the R stats package.

> smooth.spline(data$x, data$y)

Nevertheless, while running smooth.spline on a collection of datasets with different sizes I got the following error:

Error in smooth.spline(data$x, data$y),  :
  'tol' must be strictly positive and finite


After digging a little bit I discovered that the problem was that some datasets were really small and smooth.spline wasn't being able to compute anything.
Hence, make sure your dataset is big enough before applying smooth.spline to it.


> if(length(data$x) > 30) { smooth.spline(data$x, data$y) }

UPDATE: 

A more generalized solution would be:

> if(IQR(data$x) > 0) { smooth.spline(data$x, data$y) }

Comments

  1. Thanks, I ran into the same issue and this helped track it down. I just wanted to point out that your line "if(length(data$x) > 30)" depends on the size of your dataset. The problem happens when the inter quartile range (IQR) is 0, since tol is set as a multiple of the IQR yet tol needs to be > 0. A more generalized solution is: "if(IQR(data$x) > 0)"

    ReplyDelete
    Replies
    1. That is totally true, thanks for sharing! I will update the post! :D

      Delete
    2. Something specific to this function is it requires more at least 4 unique x values, so I was able to circumvent this error with...

      if(length(unique(data$x)) > 3) {...}

      Delete

Post a Comment

Popular posts from this blog

Apache Mahout

Slope One