Sampling from a dataset
In addition to generating random samples with the sample
function or from any underlying probability distribution, one can sample the subset of a given dataset. In this recipe, we will introduce how to sample a subset from a financial dataset.
Getting ready
In this recipe, you need to prepare your environment with R installed and a computer that can access the Internet.
How to do it…
Please perform the following steps to sample a subset from a given financial dataset:
First, install and load the
quantmod
package:> install.packages("quantmod") > library(quantmod)
Next, we can use
getSymbols
to obtain the Dow Jones Industrial Average data, and then subset the 2014 data from the population:> getSymbols('^DJI') > stock = DJI['2014']
You can then randomly choose
100
samples from the dataset:> set.seed(5) > sub <- stock[sample(nrow(stock), 100), ]
Furthermore, you can calculate the daily change from the generated subset:
> sub$change = (sub$DJI.Close...