Sarah May Muholland
Sarah May Muholland

Reputation: 187

Using moving averages in R

I need some help smoothing out some data in R. So basically, I just have a 'time' column and a 'velocity' column. The velocity basically represents the movement of certain tadpoles. Its just that my data has a lot of noise and I think that using 'moving averages' might help me smooth out my graphs and reveal certain patterns . How do I do this in R? Or are there better smoothing techniques I can use that might be easier for a rookie R user like me to understand?

Thanks guys

My data basically looks like this...but it lasts up 9000s

Time    Velocity
1.36    2.4
1.81    1.2
2.19    2.4
2.51    2.1
2.98    1.8
3.51    3.0
4.88    2.1
5.38    2.0
6.52    2.4
6.71    1.2
7.29    2.4
7.67    2.1
8.27    1.8
9.13    3.0
9.95    2.1
10.69   2.0
11.29   2.54
12.82   1.64
13.32   2.70
13.89   2.19
14.33   2.44
14.93   2.93
15.75   2.77
17.63   3.21
18.18   2.4
18.82   1.2
20.02   2.4
20.86   2.1
21.44   1.8
22.24   3.0
23.07   2.1
23.67   2.0

Upvotes: 2

Views: 7752

Answers (3)

hvollmeier
hvollmeier

Reputation: 2986

If you load the "TTR" package (Technical Trading Rules ) you can pick one of the many MAs from the MA "family".

?SMA

SMA(x, n = 10, ...)

EMA(x, n = 10, wilder = FALSE, ratio = NULL, ...)

DEMA(x, n = 10, v = 1, wilder = FALSE, ratio = NULL)

WMA(x, n = 10, wts = 1:n, ...)

EVWMA(price, volume, n = 10, ...)

ZLEMA(x, n = 10, ratio = NULL, ...)

VWAP(price, volume, n = 10, ...)

VMA(x, w, ratio = 1, ...)

Upvotes: 2

fdetsch
fdetsch

Reputation: 5308

Have a look at the rollmean function from R's zoo package. This should fit your needs!

Update:

Now that I've got a little more time, here's some sample code. If you'd like to fill the values at the beginning and at the end of your time series, you have to use rollapply, otherwise just take rollmean. Have a look at the console output, it'll become clear what I mean.

# Packages
library(zoo)

# Start RNG
set.seed(10)

# Sample data
tmp <- data.frame(time = 1:30, 
                  velocity = round(runif(30, 1, 3), digits = 2))

# Moving average (window size = 5) using rollmean
rollmean(tmp[, 2], k = 5, fill = NA)
 [1]    NA    NA 1.806 1.694 1.682 1.620 1.588 1.726 1.896 2.014 1.952 1.944 1.916 1.828 1.620 1.680 1.602 1.792 1.966 2.192 2.396 2.378 2.206 2.142
[25] 2.232 2.018 2.184 2.164    NA    NA

# Moving average (window size = 5) using rollapply
rollapply(tmp[, 2], width = 5, function(...) {round(mean(...), digits = 3)}, partial = TRUE)
 [1] 1.823 1.965 1.806 1.694 1.682 1.620 1.588 1.726 1.896 2.014 1.952
[12] 1.944 1.916 1.828 1.620 1.680 1.602 1.792 1.966 2.192 2.396 2.378
[23] 2.206 2.142 2.232 2.018 2.184 2.164 2.103 1.910

Upvotes: 6

rinni
rinni

Reputation: 2366

A moving average in R is simple:

MoveAve <- function(x, width) {
    as.vector(filter(x, rep(1/width, width), sides=2));
}

Where x is your data and width is the length of your averaging window.

With the sides parameter of the filter function you can control the position of the window, see the documentation:

If sides = 1 the filter coefficients are for past values only; if sides = 2 they are centred around lag 0. In this case the length of the filter should be odd, but if it is even, more of the filter is forward in time than backward.

Upvotes: 2

Related Questions