r - t.test for two variables using rollapply in a data.table -
i'm trying run 2 sample t-test t.test
7 days rollying window samples coming 2 seperate columns (sample1 , sample2). obtain p-values each test , add them current data, starting day 1. have try rollapply
in several forms no success.
# stackoverflow question library(data.table) library(zoo) # create data start = as.date("2014-01-01") end = as.date("2014-12-31") dat <- data.table(date = seq(start, end, = "1 day"), sample1 = sample(1:20, 365, replace = true), sample2 = sample(1:30, 365, replace = true)) # use rollapply fun = t.test dat[, pvalue := rollapply(c(sample1, sample2), 7, fun=t.test, alternative="less", conf.level=0.95, by.column = false, partial = t, fill=na, align='left')$p.value]
i following error:
# error in t.test.default(data[replace(posns, !ix, 0)], ...) : # not enough 'x' observations
second try:
# use rollaply more specific fun dat[, pvalue := rollapply(dat, width = 7, fun = function(df) t.test(x=sample1, y=sample2, data = as.data.frame(df), alternative="less", conf.level=0.95)$p.value, by.column = false, partial = t, fill=na, align='left')]
creates 1 test , repeats value.
third try:
# tried create vector using rollapply r <- rollapply(dat, width = 7, fun = function(df) with(df, t.test(x=sample1, y=sample2)$p.value))
i following error:
# error in eval(substitute(expr), data, enclos = parent.frame()) : # invalid 'envir' argument of type 'character' # called from: eval(substitute(expr), data, enclos = parent.frame())
Comments
Post a Comment