# [R-SIG-Finance] Elegant bootstrapping with zoo

Rowe, Brian Lee Yung (Portfolio Analytics) B_Rowe at ml.com
Thu Mar 26 20:44:33 CET 2009

```I guess a better way to phrase the question is: can sample() can handle 2-dimensional data? At this point, I will switch to r-help since it is not specific to finance.

-----Original Message-----
From: r-sig-finance-bounces at stat.math.ethz.ch [mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Enrico Schumann
Sent: Thursday, March 26, 2009 3:34 PM
To: Rowe, Brian Lee Yung (Portfolio Analytics)
Cc: r-sig-finance at stat.math.ethz.ch
Subject: Re: [R-SIG-Finance] Elegant bootstrapping with zoo

hi brian,

one line of code, seems elegant to me ;) i guess sp500.subset has several
columns? i am not sure whether `sample' can be made to work row-wise on a
matrix, but you could write your own function.

require(zoo)

## create some artifical returns data
z <- rnorm(200) * 0.05
dim(z) <- c(100, 2)
z <- zoo(z, 1:100)

## function (x = matrix with time series in columns, nR = number of
replications)
bsRows <- function(x, nR){
z  <- as.matrix(x)
bs <- sample(seq(1,nrow(z)), nR, replace = TRUE)
z  <- z[bs,]
z  <- zoo(z, index(x)[bs])
return(z)
}

## try
bsRows(z, 10)

(you may get warnings for repeated dates)
regards,
enrico

-----Ursprüngliche Nachricht-----
Von: r-sig-finance-bounces at stat.math.ethz.ch
[mailto:r-sig-finance-bounces at stat.math.ethz.ch] Im Auftrag von Rowe, Brian
Lee Yung (Portfolio Analytics)
Gesendet: Donnerstag, 26. März 2009 19:48
An: r-sig-finance at stat.math.ethz.ch
Betreff: [R-SIG-Finance] Elegant bootstrapping with zoo

Hello,

I want to bootstrap from a population in a zoo object. For example, I have a
set of returns for the S&P and want to bootstrap from this set over time. I
can do this with the below code, and I wanted to know if there is a more
elegant approach?

> h <- sp500.subset[sample(index(sp500.subset), 100, TRUE),]

I was hoping that a cleaner call would work, but apparently the length is
being used in the call to sample:

> h <- sample(sp500.subset, 100, TRUE)
Error in `[.zoo`(x, .Internal(sample(length(x), size, replace, prob))) :
subscript out of bounds

Any thoughts or should I stick with the above approach?

Thanks,
Brian

--------------------------------------------------------------------------
This message w/attachments (message) may be privileged, confidential or
proprietary, and if you are not an intended recipient, please notify the
sender, do not use or share it and delete it. Unless specifically indicated,
this message is not an offer to sell or a solicitation of any investment
products or other financial product or service, an official confirmation of
any transaction, or an official statement of Merrill Lynch. Subject to
applicable law, Merrill Lynch may monitor, review and retain
e-communications (EC) traveling through its networks/systems. The laws of
the country of each sender/recipient may impact the handling of EC, and EC
may be archived, supervised and produced in countries other than the country
in which you are located. This message cannot be guaranteed to be secure or
error-free. References to "Merrill Lynch" are references to any company in
the Merrill Lynch & Co., Inc. group of companies, which are wholly-owned by
[[elided Yahoo spam]]
rities and Insurance Products: * Are Not FDIC Insured * Are Not Bank
Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition
to Any Banking Service or Activity * Are Not Insured by Any Federal
Government Agency. Attachments that are part of this E-communication may
have additional important disclosures and disclaimers, which you should
read. This message is subject to terms available at the following link:
http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch
you consent to the foregoing.
--------------------------------------------------------------------------

[[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.
No virus found in this incoming message.
Checked by AVG - www.avg.com

03/25/09
18:54:00

_______________________________________________
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.

```