当我有多个 (400k+) 行时,如何对连续数据点的方差相等性进行测试

How can I perform tests for equality of variance of data points in a row when I have multiple (400k+) rows

我有两组数据点排列在同一行,以及多行数据(400k+)。我想比较这 400K+ 行中每一行的两组方差。数据将类似于以下内容 -

y<-rbind(c(1,2,20,50,100,1,2,3,1,2),c(20,2,80,50,100,1,2,3,1,2))
group<-structure(c(1L,1L,1L,1L,1L,2L,2L,2L,2L,2L), .Label = c("T","C"), class="factor")

我可以 运行 来自 car 包的 leveneTest 单行数据,例如 -

leveneTest(y = y[1,], group = group) # first row of data

Levene's Test for Homogeneity of Variance (center = median)
  Df F value  Pr(>F)  
group  1   4.527 0.06603 .
   8 

leveneTest (y = y[2,], group = group) # second row of data

Levene's Test for Homogeneity of Variance (center = median)
  Df F value   Pr(>F)   
group  1   11.92 0.008662 **
   8 

但显然这对于​​ 400k+ 行数据来说是不切实际的。

我认为这会很简单,比如像使用 t.test 一样使用 apply,例如 -

apply(y, 1, function (x) t.test(x[1:5],x[6:10])$p.value)
[1] 0.15260837 0.05551746

但是当我尝试 leveneTest

apply(y, 1, function(x) leveneTest (y = y, group = group))

我收到以下错误

Error in complete.cases(y, group) : 
not all arguments have the same length

有人知道怎么做吗?

由于我们使用的是匿名函数调用,因此 leveneTest 的 'y' 是 'x'(即每行中的值)而不是完整数据集。

apply(y, 1, function(x) leveneTest (y = x, group = group))

或者不使用匿名调用,下面的方法也应该有效

apply(y, 1, FUN = leveneTest, group=group)