# ANN: ShiftedArrays and support for ShiftedArrays in GroupedErrors

**URL:** https://discourse.julialang.org/t/ann-shiftedarrays-and-support-for-shiftedarrays-in-groupederrors/9162
**Category:** Community
**Tags:** announcement
**Created:** [February 19, 2018, 12:34pm UTC](https://discourse.julialang.org/t/ann-shiftedarrays-and-support-for-shiftedarrays-in-groupederrors/9162 "2018-02-19T12:34:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [February 19, 2018, 12:34pm UTC](https://discourse.julialang.org/t/ann-shiftedarrays-and-support-for-shiftedarrays-in-groupederrors/9162/1 "2018-02-19T12:34:39Z")

</div>

This is an announcement of two things at the same time:

1. A package for lazily shifting arrays: [ShiftedArrays](https://github.com/piever/ShiftedArrays.jl)
2. Support for aligning, grouping and averaging time varying signals in [GroupedErrors](https://github.com/piever/GroupedErrors.jl)

## Lazily shifting arrays

`lag`, `lead` functions, to shift an array and add `missing` (or a custom default value in the latest not yet released version) where the data is not available, or `circshift` for shifting circularly in a lazy (non allocating) way:

```julia
julia> v = [1.2, 2.3, 3.4]
3-element Array{Float64,1}:
 1.2
 2.3
 3.4

julia> lag(v)
3-element ShiftedArrays.ShiftedArray{Float64,Missings.Missing,1,Array{Float64,1}}:
  missing
 1.2
 2.3

julia> lag(v, default = NaN)
3-element ShiftedArrays.ShiftedArray{Float64,Float64,1,Array{Float64,1}}:
 NaN
   1.2
   2.3

julia> ShiftedArrays.circshift(v, 1)
3-element ShiftedArrays.CircShiftedArray{Float64,1,Array{Float64,1}}:
 3.4
 1.2
 2.3

```

Just `copy` the returned custom array type to get a regular `Array`.

### Time varying signal

A particular use case is the analysis of a time varying signal. For example imagine you run an experiment where you measure the heart rate of a subject (which is a vector `h`) and show her some images (at times `ts`, another vector). You want to compute the average heart rate around the time an image is shown:

```julia
h = randn(100)
ts = [13, 45, 76]
# create the vector of ShiftedArrays: as they do not allocate this is cheap
signal = [ShiftedArray(h, -t) for t in ts]
range = -5:5 # specify range around which to compute average heart rate: -5:5 is our x axis
avg = reduce_vec(mean, signal, range)
using Plots
plot(range, avg)

```

The usual GroupedErrors machinery applies to split data or compute error across subjects, see a slightly more realistic [example](https://github.com/piever/GroupedErrors.jl#analysis-of-time-varying-signals). The example is with JuliaDB but will also work with a DataFrame with the same columns.

### Feedback

I’m very happy to get feedback on the time varying analysis part as I do not work with them a lot (but some of my colleagues - which I carelessly convinced to use Julia - do).

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [February 19, 2018, 7:09pm UTC](https://discourse.julialang.org/t/ann-shiftedarrays-and-support-for-shiftedarrays-in-groupederrors/9162/2 "2018-02-19T19:09:45Z")

</div>

I had implemented a few shift arrays operations in some of my packages which I would be glad to port to use `ShiftedArrays` instead. As for the time series, I did work at a neuroeconometrics lab for a few years so I am familiar with those operations. Might be nice to provide a function to resample and provide statistic by various intervals. For example, a `Vector{<:DateTime}` that can resample the mean from `seconds` to `minutes` and whatnot.

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [February 20, 2018, 2:45pm UTC](https://discourse.julialang.org/t/ann-shiftedarrays-and-support-for-shiftedarrays-in-groupederrors/9162/3 "2018-02-20T14:45:36Z")

</div>

Thanks for the input. I should probably look into how to add actual `DateTime` vectors to the picture (here is all index based, so I’m tacitly assuming some fixed timestep, but there so reason to impose this limitation). How is it generally done in your field? You would have a vector `signal` and `t::Vector{<:DateTime}` of the same length and you would expect some utility functions `averageby(signal, t, Dates.Minute)` that would give two new vectors `s, tm` with the new timestamps (this time one per minute) and `s` being the average per minute?

This can be done quite elegantly with JuliaDB tables with sorted keys and could easily be incorporated in `GroupedErrors` without extra dependencies (plus, I already have support for binning data in GroupedErrors so I should extend it to time data).
