1

I am about to use the package "modeest" to report the mode of a series dataset.

Please see this website about the package: modeest

For example:

x <- rbeta(1000,23,4)
M <- mlv(x, method = "kernel")
M[1]

M1 prints the Mode (most likely value).

In my case, I want to get the mode value every 10 numbers using running method.

If I have a series dataset contains 100 numbers, I want to get 10 mode values every 10 numbers.

Please let me guide me how I can do this in R.

I've looked into running median "runmed", I wish I can find similar method for reporting the running mode value.

Thanks so much.

Kuo-Hsien Chang
  • 925
  • 17
  • 40

1 Answers1

1

One way to do this is to first split the vector into chunks of equal size and then use lapply or sapply:

library(modeest)
x <- rbeta(100, 23, 4)
split_x <- split(x, ceiling(seq_along(x) / 10))
# alternative
# split_x <- split(x, cut(seq_along(x), 10, labels = FALSE))
sapply(split_x, function(x) mlv(x, method = "kernel"))

If you just want the mode you can modify the call to sapply to either sapply(split_x, function(x) mlv(x, method = "kernel")[1]) or sapply(split_x, function(x) mlv(x, method = "kernel")[[1]]).

NOTE: This is the mode value for every 10 numbers in the vector x. I am calling this out to avoid ambiguity with the "running" verbiage because when I hear "running mode" I tend to think in line with "moving or rolling average" which would infer a mode for x[1:10], x[2:11], x[3:12], ... , x[91:100] but that does not appear to be what you asked.

Community
  • 1
  • 1
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116