Skip to contents

For each position i, averages a[j] weighted by weights[j] over every later position (tda_mwvec: every j > i; tda_mwvec1: every j with t[j] > t[i], letting an arbitrary order vector t stand in for position – tda_mwvec is the special case t = seq_along(a)) – TDA's mwvec/mwvec1. Falls back to a[i] unchanged wherever the weights beyond i sum to zero (including the last position, which always has nothing after it). No base-R builtin matches this directly.

Usage

tda_mwvec(a, weights, dir = tempfile("tda"))

tda_mwvec1(a, weights, t, dir = tempfile("tda"))

Arguments

a, weights

numeric vectors of the same length.

dir

working directory.

t

for tda_mwvec1, a numeric order vector the same length as a; ties are included on neither side (only strictly later positions count).

Value

A numeric vector, the same length as a.

Examples

a <- c(10, 20, 30, 40)
w <- c(1, 1, 1, 1)
# position 1 averages 20, 30, 40; position 3 averages 40 alone;
# the last position has nothing after it and keeps its own value
tda_mwvec(a, w)
#> [1] 30 35 40 40
# with an order vector, "later" means a larger t: positions 1 and 2
# share t = 1, so both average the values at t = 2 and t = 3
tda_mwvec1(a, w, t = c(1, 1, 2, 3))
#> [1] 35 35 40 40