Bug 16549 - bad value from splineDesign
Summary: bad value from splineDesign
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Accuracy (show other bugs)
Version: R-devel (trunk)
Hardware: x86_64/x64/amd64 (64-bit) Linux-Debian
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2015-09-30 12:12 UTC by roconnor
Modified: 2015-12-14 13:48 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description roconnor 2015-09-30 12:12:47 UTC
The following invocation of splineDesign gives a value of 0 for the
first B-spline evaluated at 0.  I believe it should be 1.

R --vanilla
> library(splines)
> knots <- c(0,0,0,0,1,1,1,1)
> splineDesign(knots, c(0,2), outer.ok = T)
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0

When not also evaluated at 2, it gives a value of 1, as expected:

> splineDesign(knots, c(0), outer.ok = T)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0

> version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          3                           
minor          2.2                         
year           2015                        
month          08                          
day            14                          
svn rev        69053                       
language       R                           
version.string R version 3.2.2 (2015-08-14)
nickname       Fire Safety                 

This is Debian testing, as of 9/29/15.
Comment 1 jeremiah.perry+rbugs 2015-10-07 15:10:09 UTC
This fixes the issue but I'm not sure if it introduces other bugs.



--- src/library/splines/R/splineClasses.R	2015-10-07 09:56:53.253373838 -0500
+++ src/library/splines/R/splineClassesNew.R	2015-10-07 09:56:43.065374052 -0500
@@ -62,7 +62,7 @@
     if(need.outer <- any(x < knots[ord] | knots[nk - o1] < x)) {
         if(outer.ok) { ## x[] is allowed to be 'anywhere'
             ## extend knots set "temporarily"
-            in.x <- knots[1L] < x & x < knots[nk]
+            in.x <- knots[1L] <= x & x <= knots[nk]
 	    knots <- knots[c(rep.int(1L, o1), 1L:nk, rep.int(nk, o1))]
             if((x.out <- !all(in.x))) {
                 x <- x[in.x]
Comment 2 Martin Maechler 2015-10-09 13:08:45 UTC
Thank you for the report.
I have committed a fix for it.   
Jeremiah's suggestion was close, but not quite correct, it needed
     
    in.x <- knots[1L] <= x & x < knots[nk]

i.e.  [a, b)   instead of previous (a, b)  and proposed  [a, b]
Comment 3 roconnor 2015-10-09 14:37:54 UTC
(In reply to Martin Maechler from comment #2)

This leaves a discrepancy on the right depending on whether you are also evaluating at an outside point.  With the fix:

> splineDesign(knots, c(0,1,2), outer.ok = T)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    0    0    0
[3,]    0    0    0    0
> splineDesign(knots, c(0,1), outer.ok = T)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    0    0    1
Comment 4 Martin Maechler 2015-10-12 18:04:53 UTC
(In reply to roconnor from comment #3)
> (In reply to Martin Maechler from comment #2)
> 
> This leaves a discrepancy on the right depending on whether you are also
> evaluating at an outside point.  With the fix:
> 
> > splineDesign(knots, c(0,1,2), outer.ok = T)
>      [,1] [,2] [,3] [,4]
> [1,]    1    0    0    0
> [2,]    0    0    0    0
> [3,]    0    0    0    0
> > splineDesign(knots, c(0,1), outer.ok = T)
>      [,1] [,2] [,3] [,4]
> [1,]    1    0    0    0
> [2,]    0    0    0    1

You are right .... and the whole problem is an order of magnitude more subtle than I had anticipated.
I have used random testing now to detect more problems,
and will commit patches later.
Comment 5 Martin Maechler 2015-10-13 15:26:11 UTC
(In reply to Martin Maechler from comment #4)
> (In reply to roconnor from comment #3)
> > (In reply to Martin Maechler from comment #2)
> > 
> > This leaves a discrepancy on the right depending on whether you are also
> > evaluating at an outside point.  With the fix:
> > 
> > > splineDesign(knots, c(0,1,2), outer.ok = T)
> >      [,1] [,2] [,3] [,4]
> > [1,]    1    0    0    0
> > [2,]    0    0    0    0
> > [3,]    0    0    0    0
> > > splineDesign(knots, c(0,1), outer.ok = T)
> >      [,1] [,2] [,3] [,4]
> > [1,]    1    0    0    0
> > [2,]    0    0    0    1
> 
> You are right .... and the whole problem is an order of magnitude more
> subtle than I had anticipated.
> I have used random testing now to detect more problems,
> and will commit patches later.

I have committed svn rev 69510  a few moments ago.
It was indeed more subtle with the main change in the lower level C function 
`basis_funcs()`.

The port from R-devel to 'R 3.2.2 patched'  should happen after a few days' waiting.