2

I'm trying to run a two sample t-test t.test with a 7 days rollying window with samples coming from two seperate columns (sample1 and sample2). I would like to obtain p-values for each test and add them to my current data, starting from day 1. I have try rollapply in several forms with no success.

# Stackoverflow question
library(data.table)
library(zoo)

# Create some Data
start = as.Date("2014-01-01")
end = as.Date("2014-12-31")

dat <- data.table(date = seq(start, end, by = "1 day"),
                  sample1 = sample(1:20, 365, replace = TRUE),
                  sample2 = sample(1:30, 365, replace = TRUE))

# Use rollapply with 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 get the following error:

# Error in t.test.default(data[replace(posns, !ix, 0)], ...) : 
# not enough 'x' observations

Second try:

# Use rollaply with 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 only one test and repeats value.

Third try:

# Tried to create vector using rollapply 
r <- rollapply(dat, width = 7,
            FUN = function(df) with(df, t.test(x=sample1, y=sample2)$p.value))

I get the 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())
user3498523
  • 370
  • 4
  • 10

0 Answers0