Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 计算具有相邻值的行组之间的统计差异_R_Data.table - Fatal编程技术网

R 计算具有相邻值的行组之间的统计差异

R 计算具有相邻值的行组之间的统计差异,r,data.table,R,Data.table,我正在进行一些阶梯式楔形分析,希望找到一种方法来计算相邻时间段之间差异的统计值。我已经想出了一些对我的玩具示例有效的方法,但是有人知道一种更有效的方法来从我的输入到我想要的输出吗 library(coin) library(data.table) set.seed(1) inputDT = data.table(group = factor(c(rep('g1', times = 30), rep('g2', times

我正在进行一些阶梯式楔形分析,希望找到一种方法来计算相邻时间段之间差异的统计值。我已经想出了一些对我的玩具示例有效的方法,但是有人知道一种更有效的方法来从我的输入到我想要的输出吗

library(coin)
library(data.table)
set.seed(1)
inputDT = data.table(group = factor(c(rep('g1', times = 30),
                                      rep('g2', times = 30))),
                     step = rep(rep(1:3), each=10, times=2),
                     interventionStep = rep(2:3, each=30),
                     val = c(rnorm(mean = 20, sd = 3, n = 10),
                             rnorm(mean = 25, sd = 3, n = 20),
                             rnorm(mean = 15, sd = 3, n = 20),
                             rnorm(mean = 20, sd = 3, n = 10)))


desiredOutputDT = data.table(group2 = factor(c(rep('g1', times = 2),
                                        rep('g2', times = 2))),
                      step1 = rep(1:2,times = 2))
desiredOutputDT[,step2 := step1 + 1]

for (rowInd in 1:nrow(desiredOutputDT)) {
  g = desiredOutputDT[rowInd,group2]
  s1 = desiredOutputDT[rowInd,step1]
  s2 = desiredOutputDT[rowInd,step2]

  desiredOutputDT[rowInd, stat := as.numeric(statistic(independence_test(val ~ step, inputDT[group == g & (step==step1 | step==step2)])), type="standardized")]

}

> desiredOutputDT
   group2 step1 step2        stat
1:     g1     1     2  3.08815389
2:     g1     2     3 -0.84987412
3:     g2     1     2  0.04336604
4:     g2     2     3  3.17319127
1)一种可能的方法是使用非等联接:

inputDT[, s:=step]
inputDT[
    desiredOutputDT,
    on=.(group=group2, s>=step1, s<=step2), nomatch=0L, allow.cartesian=TRUE,
    .(stat=statistic(
            independence_test(val ~ step, .SD))[1L]),
    by=.EACHI]
您还可以使用创建
desiredOutput dt

desiredOutputDT <- inputDT[, CJ(group2=group, step1=seq(max(step)-1L), unique=TRUE)][, 
    step2 := step1 + 1L]
desiredOutputDT
desiredOutputDT <- inputDT[, CJ(group2=group, step1=seq(max(step)-1L), unique=TRUE)][, 
    step2 := step1 + 1L]