Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why sum and mean works different in apply.monthly? #124

Closed
fcasarramona opened this issue Oct 26, 2015 · 7 comments
Closed

Why sum and mean works different in apply.monthly? #124

fcasarramona opened this issue Oct 26, 2015 · 7 comments
Milestone

Comments

@fcasarramona
Copy link

When apply.monthly in a xts multivaluate time series (with more than one column) apply.monthly(xts.ts, sum) returns a one column time series but apply.monthly(xts.ts, mean) returns the same number of columns of the original.

Here is an example:

library(xts)
xts.ts <- xts(matrix(rnorm(462), nrow = 231),as.Date(13514:13744,origin="1970-01-01"))
dim(xts.ts)
[1] 231   2
dim(apply.monthly(xts.ts, mean))
[1] 8 2
dim(apply.monthly(xts.ts, sum))
[1] 8 1

The same is true with other xts apply functions.

This seems inconsistent.

@braverock
Copy link
Contributor

Why not use colMeans and colSums to make sure you get what you think you're getting?

x<-xts(matrix(rnorm(1000),ncol=10),order.by=as.Date(1:100))
mean(x)
sum(x)
apply.weekly(x,sum)
apply.weekly(x,mean)
apply.weekly(x,colSums)
apply.weekly(x,colMeans)

@fcasarramona
Copy link
Author

Thanks, just discovered I can do it with colSums.
In this case, apply.weekly(x,sum) and apply.weekly(x,mean) should return 1 column xts both.

@joshuaulrich
Copy link
Owner

The problem is that xts defines mean.xts in its namespace and it is being dispatched, even though it is not registered as a S3 method.

mean.xts was added when R Core decided that mean should not operate by-column for matrices and data.frames. Seems like it might be a good idea to rename both mean.xts and sd.xts as mean_xts and sd_xts, respectively, in order to avoid this inconsistency.

Or maybe they should just be removed all together? They're not exported, so CRAN likely would not allow their use in other packages.

@fcasarramona
Copy link
Author

In my opinion is better just remove them and let colSums and his family functions do the work. This will be consistent with matrix.

@sushant-choudhary
Copy link

Using colSums takes care of the sum function not being applied by columns, however what would be a similar fix for the sd( ) function, which, like sum( ), is not applied by columns in period.apply.

@pverspeelt
Copy link
Contributor

@sushant-choudhary ,

A late answer, but the package matrixStats has all these functions available for columns and rows. Using the example above that would be:

apply.weekly(x, matrixStats::colSds)

@joshuaulrich
Copy link
Owner

I just pushed a commit with the best solution I could think of. The current behavior is maintained, but prints a message every time period.apply(x, ep, FUN = mean) is called, unless options(xts.message.period.apply.mean = FALSE). This is a message instead of a warning because a warning would cause CRAN packages to fail R CMD check.

The message says to use FUN = colMeans for the current behavior and FUN = function(x) mean(x) to calculate the mean for all the data. Neither of those will print a message. I also added a note about this to the help files for period.apply() and apply.daily() (and friends).

@joshuaulrich joshuaulrich added this to the 0.13.2 milestone Aug 2, 2023
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Nov 24, 2024
# xts 0.14.x (202x-xx-xx)

* `plot.xts()` now renders all panels when 'x' has more than 8 columns and
  `multi.panel = TRUE`. Columns 9 and later didn't render because the default
  of `plot.xts()` is 'col = 1:8'. Thanks to Ethan Smith for the report and
  patch.
  ([#423](joshuaulrich/xts#423))
  ([#424](joshuaulrich/xts#424))

* `plot.xts()` no longer errors when 'ylim' is constant and negative. Thanks
  to Ethan Smith for the report.
  ([#422](joshuaulrich/xts#422))

* Do not use `SET_TYPEOF()` in C because it is not part of the public R API.

* `merge.xts()` no longer converts 'x' or 'y' from double to integer in the C
  code when they are not used in the result. This avoids an unnecessary and
  confusing warning. Thanks to Jeff Ryan for the report.

# xts 0.14.0 (2024-06-05)

* `addEventLines()` and `addLegend()` now draw on multiple panels when `on` is
  a vector. Thanks to Ethan Smith for the report.
  ([#420](joshuaulrich/xts#420))

* Replace `SET_TYPEOF()` in merge.c because it will error when it tries to
  convert a REAL to an INTEGER. Thanks to Kurt Hornik for the report!
  ([#419](joshuaulrich/xts#419))

* Fix crash when 'j' is not an integer and in [0, 1) (e.g. `j = 0.1`). Also
  throw a warning when 'j' is not an integer.
  ([#413](joshuaulrich/xts#413))
  ([#415](joshuaulrich/xts#415))

* Fix plot header when `layout()` is used to draw multiple plots on a single
  device. Thanks to Dirk Eddelbuettel for the report and testing!
  ([#412](joshuaulrich/xts#412))

* Fix plot legend location when the y-axis is log scale.
  ([#407](joshuaulrich/xts#407))

# xts 0.13.2 (2024-01-21)

* Print a message when `period.apply()` is called with `FUN = mean` because it
  calculates the mean for each column, not all the data in the subset like it
  does for all other functions. The message says to use `FUN = colMeans` for
  current behavior and `FUN = function(x) mean(x)` to calculate the mean for
  all the data. This information is also included in the help files. The option
  `xts.message.period.apply.mean = FALSE` suppresses the message.
  ([#124](joshuaulrich/xts#124))

* Fix error when `print.xts()` is called 'quote' or 'right' arguments.
  ([#401](joshuaulrich/xts#401))

* Fix `addPolygon()` so it renders when `observation.based = TRUE`.
  ([#403](joshuaulrich/xts#403))

* Print trailing zeros for index value with fractional seconds, so every index
  value has the same number of characters.
  ([#404](joshuaulrich/xts#404))

* Add ability to log scale the y-axis in `plot.xts()`.
  ([#103](joshuaulrich/xts#103))

* Actually change the underlying index values when 'tclass' is changed from a
  class with a timezone (e.g. POSIXct) to one without a timezone (e.g. Date).
  Add a warning when this happens, with a global option to always suppress the
  warning.
  ([#311](joshuaulrich/xts#311)).

* Significantly refactor the internals of `plot.xts()`.
  ([#408](joshuaulrich/xts#408))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants