Skip to content

package zoo, function na.spline with option maxgap -> Error: attempt to apply non-function?

6 messages · R. Michael Weylandt, Gabor Grothendieck, May Katharina

#
Hello,

I'm trying to use na.spline (package zoo) to fill some missing data in a time series.
this works fine, however, if I apply the 'maxgap' argument, I always get the error:
<------
Error in na.spline.vec(x., coredata(object.), xout = xout., ...) :  attempt to apply non-function
------>

I couldn't find a similar error for this case in the mailing lists and zoo vignette, I hope I didn't
miss any basic thing, but as I don't know how to proceed, any help on why this isn't working
is very much appreciated  (I guess I don't have to mention I'm new to time series & zoo and that I'm terrible sorry if this is
an absolute dull question). 

The data I use is rather large, but the error can be reconstructed in a small example (s. below). 
If of any interest: I use R 2.15.0 GUI 1.51 Leopard build 32-bit (6148).

Many thanks and best regards, Katharina


<------
library(zoo)

#create data + gaps
z <- zoo(seq(1:30), seq(1900, length =30 )) 
z[c(2,5,6,7,9,10,15,16,17,20,21)] <- NA
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 
   1   NA    3    4   NA   NA   NA    8   NA   NA   11   12   13   14   NA   NA   NA   18   19   NA   NA   22 
1922 1923 1924 1925 1926 1927 1928 1929 
  23   24   25   26   27   28   29   30 

#1. fill using spline: this version without gap restriction works
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 
   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22 
1922 1923 1924 1925 1926 1927 1928 1929 
  23   24   25   26   27   28   29   30 

#2. fill using spline: a=with applying 'maxgap' an error is produced
Error in na.spline.vec(x., coredata(object.), xout = xout., ...) : 
  attempt to apply non-function
------>
#
Thanks for the great reproducible example -- I can confirm on the
devel version of zoo.

I'd venture it's a buglet in that maxgap gets passed (by way of dots)
to na.spline --> na.spline.zoo --> na.spline.default --> na.spline.vec
--> to both .fill_short_gaps (good) and spline (bad) which is where
the error arises.

I'll wait for Gabor or Achim to officially weigh in though.

Michael

On Wed, Jun 6, 2012 at 2:55 PM, May Katharina
<may.katharina at googlemail.com> wrote:
#
On Wed, Jun 6, 2012 at 3:55 PM, May Katharina
<may.katharina at googlemail.com> wrote:
Thanks. Its a bug.  maxgap seems to have been inadvertently omitted
from the signature.  In the meantime here is a workaround:

na.spline.zoo <- function(object, x = index(object), xout, maxgap,
..., na.rm = TRUE, along) {}
body(na.spline.zoo) <- body(zoo:::na.spline.zoo)
na.spline(z)
#
On Jun 6, 2012, at 10:19 , Gabor Grothendieck wrote:

            
Many thanks for the fast reply Gabor and Michael! 

But I still got problems with  applying the workaround stated.
Even though the error message  disappears, the maxgap argument is just ignored
(I added the e.g. '=2' in the function call):
<-------
na.spline.zoo <- function(object, x = index(object), xout, maxgap=2, 
..., na.rm = TRUE, along) {}
body(na.spline.zoo) <- body(zoo:::na.spline.zoo)
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 
   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22 
1922 1923 1924 1925 1926 1927 1928 1929 
  23   24   25   26   27   28   29   30 
------->

Do I miss something or did I misunderstood your code Gabor? 

Thanks once again and best regards, Katharina
#
On Wed, Jun 6, 2012 at 4:40 PM, May Katharina
<may.katharina at googlemail.com> wrote:
Yes, it seems that that bug hid a second bug and I have now hopefully
removed both.  Try this (or download and build the zoo package from
R-Forge):
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
   1    2    3    4   NA   NA   NA    8    9   10   11   12   13   14   NA   NA
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
  NA   18   19   20   21   22   23   24   25   26   27   28   29   30

  
    
1 day later
#
On Jun 7, 2012, at 12:55 , Gabor Grothendieck wrote:

            
Many thanks, it works like a dream now!
Great package and great support...

Best regards, Katharina