-5
 X<-scan()
 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1
 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 0 0 1 1 1
 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1

Z<-scan()
   -0.05  0.11 -0.01  1.08  0.68 -1.79 -0.12 -0.06  0.17 -1.35  1.55  0.60
  -1.42 -1.21  0.97  0.23  0.20  0.89  0.28  0.56  1.02 -0.32  0.20 -1.35
   0.53 -0.52 -0.07 -1.07  0.10  0.53  0.97  0.32 -0.07  0.98 -1.23  0.72
  -0.09  0.31  1.25  0.60  1.16 -0.98  1.63  0.72  0.24 -0.02 -1.13  0.56
  0.78  1.75 -0.01 -0.44  0.47 -0.21  2.06  2.19 -0.94 -0.36  1.35 -1.35
   1.50  0.13 -0.20 -0.57 -0.14 -1.34 -1.17  2.04  0.21  1.47 -1.20 -0.60
   0.15 -0.64 -0.71  0.24 -0.86 -1.39 -0.63 -1.25  0.40 -0.76  0.73 -0.15
   0.09  0.35 -0.19  0.29  0.56  0.82 -0.28  0.63  1.35 -0.04  1.99  1.12
  -1.91  0.26 -1.18 -0.10

在向量 X 中,0 是对照组,1 是病例组。我想根据 Z 向量匹配这种情况和控件。实际上我想根据 Z 来匹配 X 的元素,从匹配的数据中获取样本。我应该怎么办?

4

3 回答 3

7

其他答案似乎认为您正在寻找子集,但我假设(基于您对语言“案例”和“控制”的使用)您正在谈论统计意义上的匹配。如果是这样,听起来您想要类似Matching包提供的功能,如下所示:

library(Matching)
out <- Match(Tr=X,X=Z)
out$mdata # list of `Y` outcome vector (if applicable),
          #         `Tr` treatment vector, and 
          #         `X` matrix of covariates for the matched sample

如果您也有一个结果度量,您可以指定Match它,它将为您提供治疗效果估计值。

还有其他软件包可以进行匹配,例如MatchItcemnonrandom(其中最后一个显然已从 CRAN 中删除),具体取决于您要进行的特定匹配过程。

于 2013-07-24T12:06:24.733 回答
3

我想你正在寻找

Z[as.logical(X)] # case

Z[!X]            # control
于 2013-07-24T11:46:34.510 回答
2

我想你的问题是关于子集的,这里有一些例子:

# Data
X<-c(1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,0,0,1,1,0,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1)
Z<-c(-0.05,0.11,-0.01,1.08,0.68,-1.79,-0.12,-0.06,0.17,-1.35,1.55,0.60,-1.42,-1.21,0.97,0.23,0.20,0.89,0.28,0.56,1.02,-0.32,0.20,-1.35,0.53,-0.52,-0.07,-1.07,0.10,0.53,0.97,0.32,-0.07,0.98,-1.23,0.72,-0.09,0.31,1.25,0.60,1.16,-0.98,1.63,0.72,0.24,-0.02,-1.13,0.56,0.78,1.75,-0.01,-0.44,0.47,-0.21,2.06,2.19,-0.94,-0.36,1.35,-1.35,1.50,0.13,-0.20,-0.57,-0.14,-1.34,-1.17,2.04,0.21,1.47,-1.20,-0.60,0.15,-0.64,-0.71,0.24,-0.86,-1.39,-0.63,-1.25,0.40,-0.76,0.73,-0.15,0.09,0.35,-0.19,0.29,0.56,0.82,-0.28,0.63,1.35,-0.04,1.99,1.12,-1.91,0.26,-1.18,-0.10)
myMatrix <- cbind(X,Z)

# Subsetting
myMatrixControls <- myMatrix[ myMatrix[,1]==0,]
myMatrixCases <- myMatrix[ myMatrix[,1]==1,]

# Example: get sum per group
sumZ_Contolrs <- sum(myMatrix[ myMatrix[,1]==0, 2])
sumZ_Cases <- sum(myMatrix[ myMatrix[,1]==1, 2])
于 2013-07-24T12:01:50.097 回答