[R-SIG-Finance] apply.fromstart() warnings

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 2 16:43:40 CEST 2008


Although the gap in my post was defined as one off from theirs that
is not the relevant point.  Look at the answers from apply.fromstart
The last number in the last column should be 38 (mean of 26:50),
not 13.

> d <- zoo(matrix(1:50, 25), as.Date(1:25))
> apply.fromstart(d, gap = 20, FUN = "mean")

1970-01-02   NA   NA
1970-01-03   NA   NA
1970-01-04   NA   NA
1970-01-05   NA   NA
1970-01-06   NA   NA
1970-01-07   NA   NA
1970-01-08   NA   NA
1970-01-09   NA   NA
1970-01-10   NA   NA
1970-01-11   NA   NA
1970-01-12   NA   NA
1970-01-13   NA   NA
1970-01-14   NA   NA
1970-01-15   NA   NA
1970-01-16   NA   NA
1970-01-17   NA   NA
1970-01-18   NA   NA
1970-01-19   NA   NA
1970-01-20   NA   NA
1970-01-21 10.5 10.5
1970-01-22 11.0 11.0
1970-01-23 11.5 11.5
1970-01-24 12.0 12.0
1970-01-25 12.5 12.5
1970-01-26 13.0 13.0
There were 12 warnings (use warnings() to see them)


On Mon, Jun 2, 2008 at 10:30 AM, Murali Menon <feanor0 at hotmail.com> wrote:
> Gabor,
>
> I think in your code, the loop should be (to replicate apply.fromstart())
>
> for(i in 20:nrow(dd)) dd[i, ] <- colMeans(d[1:i, ])
>
> not 21:nrow(dd)
>
> I think that's where the difference you refer to comes from. IF you make it
> 20:nrow(dd), then the answers match apply.fromstart()
>
> My understanding of the gap parameter:
>
> If it is 20, then the mean of the first 20 entries of d are computed, and
> stored in the 20th entry of dd, not the 21st.
>
> What do you think?
>
> Cheers,
> Murali
>
>> Date: Mon, 2 Jun 2008 10:10:44 -0400
>> From: ggrothendieck at gmail.com
>> To: feanor0 at hotmail.com
>> Subject: Re: [R-SIG-Finance] apply.fromstart() warnings
>> CC: r-sig-finance at stat.math.ethz.ch
>>
>> Try the example in my post.
>>
>> On Mon, Jun 2, 2008 at 10:07 AM, Murali Menon <feanor0 at hotmail.com> wrote:
>> >
>> >
>> > Hi,
>> >
>> >
>> > Did you see where the answers are wrong? I tried the following, and it
>> > appears correct (except for the warnings):
>> >
>> >
>> >> apply.fromstart(cbind(1:20, 1:20), gap=10, FUN=mean)
>> >
>> > Column.1 Column.2
>> > 1970-01-02 NA NA
>> > 1970-01-03 NA NA
>> > 1970-01-04 NA NA
>> > 1970-01-05 NA NA
>> > 1970-01-06 NA NA
>> > 1970-01-07 NA NA
>> > 1970-01-08 NA NA
>> > 1970-01-09 NA NA
>> > 1970-01-10 NA NA
>> > 1970-01-11 5.5 5.5
>> > 1970-01-12 6.0 6.0
>> > 1970-01-13 6.5 6.5
>> > 1970-01-14 7.0 7.0
>> > 1970-01-15 7.5 7.5
>> > 1970-01-16 8.0 8.0
>> > 1970-01-17 8.5 8.5
>> > 1970-01-18 9.0 9.0
>> > 1970-01-19 9.5 9.5
>> > 1970-01-20 10.0 10.0
>> > 1970-01-21 10.5 10.5
>> > There were 22 warnings (use warnings() to see them)
>> >
>> > That's correct, no? The mean of the first 10 entries go into the 10th
>> > row, the mean of the first 11 entries go into the 11th row, etc.
>> >
>> > Funnily enough, there are no warnings if I run the function on a vector,
>> > say:
>> >
>> > apply.fromstart(1:20, gap=10, FUN=mean)
>> >
>> > Cheers,
>> > Murali
>> >
>> >> Date: Mon, 2 Jun 2008 09:40:53 -0400
>> >> From: ggrothendieck at gmail.com
>> >> To: r-sig-finance at stat.math.ethz.ch
>> >> Subject: Re: [R-SIG-Finance] apply.fromstart() warnings
>> >>
>> >> The problem goes beyond some warnings -- the answers from
>> >> apply.fromstart are wrong. You could report the problem to the
>> >> author and just use a suitably modified version of the loop I posted
>> >> as a workaround.
>> >>
>> >> On Mon, Jun 2, 2008 at 9:26 AM, Murali Menon wrote:
>> >>>
>> >>> Hi Gabor,
>> >>>
>> >>> Thanks for the suggestion. Actually, I just simplified the problem to
>> >>> a small bit of code that replicates the issue; in actuality, it's not the
>> >>> mean I am interested in calculating (but all sorts of other metrics), and
>> >>> the data are not randomly generated, but are financial time-series.
>> >>>
>> >>> The warning's probably coming from the apply.fromstart() function, but
>> >>> am not quite able to determine why. The initial 20 NAs are expected as there
>> >>> are no 'mean' computed for these (viz. the argument gap = 20).
>> >>>
>> >>> Cheers,
>> >>> Murali> Date: Mon, 2 Jun 2008 08:27:56 -0400> From:
>> >>> ggrothendieck at gmail.com> To: feanor0 at hotmail.com> Subject: Re:
>> >>> [R-SIG-Finance] apply.fromstart() warnings> CC:
>> >>> r-sig-finance at stat.math.ethz.ch>> To do it in zoo alone try this:>>
>> >>> library(zoo)> d <- zoo(matrix(1:50, 25), as.Date(1:25))>> dd <- NA * d>
>> >>> for(i in 21:nrow(dd)) dd[i, ] <- colMeans(d[1:i, ])>> You also might try
>> >>> updating the to latest version of all packages and R> although that does not
>> >>> appear to be the source of the problem here.>> On Mon, Jun 2, 2008 at 7:57
>> >>> AM, Murali Menon wrote:>>>>>> Folks,>>>> When I attempt to find the growing
>> >>> window columnwise mean of a two-dimensional array, I get a bunch of
>> >>> warnings. Any idea why? Here's my code:>>>>>
>> >>> library(PerformanceAnalytics)>>>>> d <- zoo(matrix(rnorm(50), ncol=2),
>> >>> order.by=as.Date(1:25))>>>>> apply.fromstart(d, gap=20, FUN="mean")>>
>> >>> 1970-01-02 NA NA>> 1970-01-03 NA NA>> 1970-01-04 NA NA>> 1970-01-05 NA NA>>
>> >>> 1970-01-06!
>> >> NA NA>> 1970-01-07 NA NA>> 1970-01-08 NA NA>> 1970-01-09 NA NA>>
>> >> 1970-01-10 NA NA>> 1970-01-11 NA NA>> 1970-01-12 NA NA>> 1970-01-13 NA NA>>
>> >> 1970-01-14 NA NA>> 1970-01-15 NA NA>> 1970-01-16 NA NA>> 1970-01-17 NA NA>>
>> >> 1970-01-18 NA NA>> 1970-01-19 NA NA>> 1970-01-20 NA NA>> 1970-01-21
>> >> -0.05899970 -0.05899970>> 1970-01-22 -0.05882669 -0.05882669>> 1970-01-23
>> >> -0.08635806 -0.08635806>> 1970-01-24 -0.15848480 -0.15848480>> 1970-01-25
>> >> -0.13168358 -0.13168358>> 1970-01-26 -0.17101046 -0.17101046>> There were 12
>> >> warnings (use warnings() to see them)>>>>>> When I check the warnings(),
>> >> here's the first string I get:>>>> 1: In column.Return.calc[i] =
>> >> apply(as.matrix(data.zoo[, ... :>> number of items to replace is not a
>> >> multiple of replacement length>>>> I'm not sure why this should be the
>> >> case?>>>>>> I'm using:>>>>> packageDescription("zoo")$Version>> [1]
>> >> "1.4-2">>> packageDescription("PerformanceAnalytics")$Version>> [1]
>> >> "0.9.6">>>>>> And R 2!
>> >> .6.1 on Windows XP.>>>>>> Thanks,>>>>>> Murali>>>> ___________
>> >> ______________________________________________________>> Give to a good
>> >> cause with every e-mail. Join the i'm Initiative from Microsoft.>>>>
>> >> _______________________________________________>>
>> >> R-SIG-Finance at stat.math.ethz.ch mailing list>>
>> >> https://stat.ethz.ch/mailman/listinfo/r-sig-finance>> -- Subscriber-posting
>> >> only.>> -- If you want to post, subscribe first.>>
>> >>> _________________________________________________________________
>> >>> Give to a good cause with every e-mail. Join the i'm Initiative from
>> >>> Microsoft.
>> >>>
>> >>> [[alternative HTML version deleted]]
>> >>>
>> >>>
>> >>> _______________________________________________
>> >>> R-SIG-Finance at stat.math.ethz.ch mailing list
>> >>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> >>> -- Subscriber-posting only.
>> >>> -- If you want to post, subscribe first.
>> >>>
>> >>
>> >> _______________________________________________
>> >> R-SIG-Finance at stat.math.ethz.ch mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> >> -- Subscriber-posting only.
>> >> -- If you want to post, subscribe first.
>> >
>> > _________________________________________________________________
>> > Give to a good cause with every e-mail. Join the i'm Initiative from
>> > Microsoft.
>> > http://im.live.com/Messenger/IM/Join/Default.aspx?souce=EML_WL_
>> > GoodCause
>
>
> ________________________________
> Change the world with e-mail. Join the i'm Initiative from Microsoft.



More information about the R-SIG-Finance mailing list