[R] creating a loop for a function

R. Michael Weylandt michael.weylandt at gmail.com
Thu Oct 6 20:30:33 CEST 2011


Well you said you wanted statistics from the test, but you didn't say
which statistics you wanted: any of the following would work:

sapply(1:10, function(i) Box.test (lfut, lag = i, type="Ljung")$statistic)
sapply(1:10, function(i) Box.test (lfut, lag = i, type="Ljung")$parameter)
sapply(1:10, function(i) Box.test (lfut, lag = i, type="Ljung")$p.value)

"extractor" wasn't quite the right word, but more or less that's the
idea (if you happen to know it from other programming contexts).

Michael


On Thu, Oct 6, 2011 at 1:43 AM, upananda pani <upananda.pani at gmail.com> wrote:
> Respected Sir,
> I am grateful to you for your reply. This is working perfectly fine. I do
> not know how to add extractors. Please give me one example with this
> function.
> With regards,
> Upananda
>
> On Thu, Oct 6, 2011 at 1:04 AM, R. Michael Weylandt
> <michael.weylandt at gmail.com> wrote:
>>
>> lapply(1:10, function(i) Box.test (lfut, lag = i, type="Ljung"))
>>
>> Add extractors to get statistics as desired.
>>
>> Michael Weylandt
>>
>> On Wed, Oct 5, 2011 at 1:09 PM, upananda pani <upananda.pani at gmail.com>
>> wrote:
>> > Dear All,
>> >
>> > I want to create a loop within a function r. The example follows:
>> >
>> > Box.test (lfut, lag = 1, type="Ljung")
>> >
>> > if i want to compute the Box.test for lag 1 to 10, I have to write
>> > manually
>> >  change each time for different lag. So i wan to write a loop for the
>> > lag 1
>> > to 10 and return the statistics for each lag. Is there any method to do
>> > this
>> > ?
>> >
>> > With regards,
>> > Upananda
>> >
>> > --
>> >
>> >
>> > You may delay, but time will not.
>> >
>> >
>> > Research Scholar
>> > alternative mail id: upani at iitkgp.ac.in
>> > Department of HSS, IIT KGP
>> > KGP
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>
>
>
> --
>
>
> You may delay, but time will not.
>
>
> Research Scholar
> alternative mail id: upani at iitkgp.ac.in
> Department of HSS, IIT KGP
> KGP
>
>



More information about the R-help mailing list