[R] smooth.spline() unique 'x' values error

Nicholas Reich nick at schoolph.umass.edu
Wed Feb 15 16:00:03 CET 2012


I'm getting an unexpected result when running smooth.spline().  Here is a simple example that replicates the error I'm getting:

> aa <- c(1, 2, 3, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 13, 14)
> bb <- 1:length(aa)
> plot(aa, bb)
> smooth.spline(aa, bb)
Error in smooth.spline(aa, bb) : need at least four unique 'x' values

As you can see from the example, my 'x' clearly has more than 4 values.  The problem is the form of my 'x'.  When I dig around in the smooth.spline() code, it looks like it is relying on a call to IQR(x) which, with data such as the object aa above, returns 0 because more than half of the values are stacked on one value in the middle of the data.  The call to smooth.spline() works if I add some random noise to the Xs, but I'd like to be able to avoid that type of workaround.  Any other suggestions?


Nicholas G. Reich, Ph.D.
Research Assistant Professor
Division of Biostatistics and Epidemiology
School of Public Health and Health Sciences
University of Massachusetts, Amherst

nick at schoolph.umass.edu
on twitter: @reichlab

More information about the R-help mailing list