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

May Katharina may.katharina at googlemail.com
Wed Jun 6 22:40:58 CEST 2012


On Jun 6, 2012, at 10:19 , Gabor Grothendieck wrote:

> On Wed, Jun 6, 2012 at 3:55 PM, May Katharina
> <may.katharina at googlemail.com> wrote:
>> 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
>> 
>>> z
>> 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
>>> na.spline(z)
>> 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
>>> na.spline(z, maxgap=2)
>> Error in na.spline.vec(x., coredata(object.), xout = xout., ...) :
>>  attempt to apply non-function
>> ------>
>> 
> 
> 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)
> 
> 
> -- 
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com

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)

> na.spline(z)
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



More information about the R-help mailing list