0
$\begingroup$

While studying xts time series in R I have come across the to.period() function that converts an OHLC or univariate object to a specified periodicity lower than the given data object.

I'm trying to understand how it aggregates the values to new values in the lower periodicity, I believed it to be the mean but that does not seem correct having calculated the value myself using the mean for that same period the results are different.

So what is the underlying function?

Here is an example that shows the different results:

data(sample_matrix)

samplexts <- as.xts(sample_matrix)

# Sample data contains OHLC values but I'm setting OHLC to False
# I believe that is irrelevant for the purpose of the question
to.period(samplexts["2007-06"], period = "months", OHLC = F )

               Open     High      Low    Close
2007-06-30 47.67468 47.94127 47.67468 47.76719

# I would have expected same values if applied the mean func
# but values are different from above
unlist(lapply(samplexts["2007-06"], mean))

    Open     High      Low    Close 
47.47717 47.57592 47.38255 47.46899 
$\endgroup$

closed as off-topic by Silverfish, kjetil b halvorsen, Michael Chernick, Nick Cox, Peter Flom May 27 '17 at 11:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Silverfish, kjetil b halvorsen, Michael Chernick, Nick Cox, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ It returns the closing value if you specify OHLC=FALSE, not the mean $\endgroup$ – Mohan Prashanth May 26 '17 at 14:01
1
$\begingroup$

to.period does not aggregate by using any function.

Here is what it does:

  • As Open of the new monthly series: the Open of first day of the month of the daily series is used.

  • As High of the new monthly series: The highest High of the month found in the daily series is used.

  • As Low of the new monthly series: The lowest Low of the month found in the daily series is used.

  • As Close of the new monthly series: the Close of last day of the month of the daily series is used.

Also, as per the help page:

only OHLC=TRUE currently supported

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.