Last active
May 20, 2016 11:32
-
-
Save syu-id/00dfc53bc1ab479875a5 to your computer and use it in GitHub Desktop.
RによるMATTRの実装 https://github.com/rongmu/mattr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An alternative implementation of the MATTR algorithm in R | |
# author: Shaoyun YU <[email protected]> | |
# 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