为什么我在应用于向量和 xts 对象时在 ks.boot 输出中得到不同的结果?

Why I'm getting different results in ks.boot output when applying to vectors andto xts objects?

我正在使用 ks.boot 函数 Matching 包,并且在应用尽管数据相同,但向量和 xts 对象的函数。有人可以提供线索吗?

A <- as.vector(seq(1:20))
B <- as.vector(seq(from = 2, to = 40, by =2))
require(Matching)
library(xts)
dates <- seq(as.Date("2000-01-01"), length = 20, by = "days")
C <- as.xts(A,dates)
D <- as.xts(B,dates)


输出:

ks.boot(B,A, alternative = "t")
$ks.boot.pvalue
[1] 0.009

$ks

    Two-sample Kolmogorov-Smirnov test

data:  Tr and Co
D = 0.5, p-value = 0.01348
alternative hypothesis: two-sided


$nboots
[1] 1000

attr(,"class")
[1] "ks.boot"

ks.boot(D,C, alternative = "t")
$ks.boot.pvalue
[1] 0.672

$ks

    Two-sample Kolmogorov-Smirnov test

data:  Tr and Co
D = 0.75, p-value = 2.601e-05
alternative hypothesis: two-sided


$nboots
[1] 1000

attr(,"class")
[1] "ks.boot"
Warning messages:
1: In c.xts(Tr, Co) : mismatched types: converting objects to numeric
2: In c.xts(x, y) : mismatched types: converting objects to numeric

问题是 xts 对象(以及它们所基于的 zoo 对象)总是有序的。 ks.boot() 函数对观察结果进行随机抽样和重新排序,但这对于 xts 对象是不可能的。

例如:

A <- 1:10
dates <- as.Date("2000-01-01") + A
C <- xts(A, dates)

# sample index
set.seed(21)
(i <- sample(1:10, 10, replace = TRUE))
## [1]  1  3  9 10  5  3  4 10  6  8

# subset xts
C[i,]
##            [,1]
## 2000-01-02    1
## 2000-01-04    3
## 2000-01-04    3
## 2000-01-05    4
## 2000-01-06    5
## 2000-01-07    6
## 2000-01-09    8
## 2000-01-10    9
## 2000-01-11   10
## 2000-01-11   10

请注意,数据是通过替换采样的,但索引是通过观察采样的,因此保留了原始顺序。

您的问题的解决方案是对传递给 ks.boot() 的数据使用 coredata()

ks.boot(coredata(D), coredata(C), alternative = "t")
## $ks.boot.pvalue
## [1] 0.006
## 
## $ks
## 
##  Two-sample Kolmogorov-Smirnov test
## 
## data:  Tr and Co
## D = 0.5, p-value = 0.01348
## alternative hypothesis: two-sided
## 
## 
## $nboots
## [1] 1000
## 
## attr(,"class")
## [1] "ks.boot"