Skip to content

Instantly share code, notes, and snippets.

@syu-id
Last active May 20, 2016 11:32
Show Gist options
  • Save syu-id/00dfc53bc1ab479875a5 to your computer and use it in GitHub Desktop.
Save syu-id/00dfc53bc1ab479875a5 to your computer and use it in GitHub Desktop.
RによるMATTRの実装 https://github.com/rongmu/mattr
# An alternative implementation of the MATTR algorithm in R
# author: Shaoyun YU <eric.rongmu@gmail.com>
# ref: Covington & Mcfall (2010) Cutting the Gordian Knot: The Moving-Average Type-Token Ratio
# usage: mattr(vector_of_tokens, window_size)
window_types <- function(i_start, win_size, data) {
i_end <- i_start + win_size - 1
win <- data[i_start:i_end]
length(unique(win))
}
mattr <- function(x, win_size, full.pass = FALSE) {
if (full.pass) {
x <- c(x, x[1:(win_size - 1)])
}
n_win <- length(x) - win_size + 1
types <- vapply(seq_len(n_win), window_types, integer(1),
win_size = win_size, data = x)
mean(types) / win_size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment