因此,请遵循匹配包中的示例,特别是GenMatch示例.这是从前一个问题继续
按照中的例子 GenMatch
library(Matching)
data(lalonde)
attach(lalonde)
X = cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74)
BalanceMat <- cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74,
I(re74*re75))
genout <- GenMatch(Tr=treat, X=X, BalanceMatrix=BalanceMat, estimand="ATE", M=1,
pop.size=16, max.generations=10, wait.generations=1)
genout$matches
genout$ecaliper
Y=re78/1000
mout <- Match(Y=Y, Tr=treat, X=X, Weight.matrix=genout)
summary(mout)
Run Code Online (Sandbox Code Playgroud)
我们看到185个治疗观察与270个非治疗观察配对.
我们可以通过以下方式生成一个表格,其中包含左侧的治疗病例及其年龄,以及右侧的对照病例和年龄:
pairs <- data.frame(mout$index.treated, lalonde$age[mout$index.treated], mout$index.control, lalonde$age[mout$index.control])
Run Code Online (Sandbox Code Playgroud)
现在,关于Weight.Matrix生成的文献GenMatch是非常神秘的,并没有解释这些值代表什么.我在这里有一个未解决的问题.现在假设我们想要放宽匹配,以便在年龄标准上进行更灵活的配对.
我们看到这sd(lalonde$age)为我们的数据提供了7年的SD.
所以我想要Weight.matrix解释这一点.我想对age变量使用1 SD的限制,因此返回比原始185-270更多的对.
我的猜测是生成第二个GenMatch函数,然后继续我的代码.所以我使用: …
拿一个简单的数据集
a <- c(1,2,3,4,5,6,7,8)
b <- c(1,2,2,1,2,2,2,2)
c <- c(1,1,1,2,2,2,3,3)
d <- data.frame(a,b,c)
Run Code Online (Sandbox Code Playgroud)
现在我想过滤我的数据,以便我们group_by(c)然后删除所有没有b=1发生的数据.
因此,results(e)应该看起来像d但没有两个底行
我试过用
e <- d %>%
group_by(c) %>%
filter(n(b)>1)
Run Code Online (Sandbox Code Playgroud)
输出应包含下面的绿色数据,并删除红色数据

因此,请遵循Matching包中的示例,特别是GenMatch示例 链接到pdf
按照这里的例子
library(Matching)
data(lalonde)
attach(lalonde)
X = cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74)
BalanceMat <- cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74,
I(re74*re75))
genout <- GenMatch(Tr=treat, X=X, BalanceMatrix=BalanceMat, estimand="ATE", M=1,
pop.size=16, max.generations=10, wait.generations=1)
Y=re78/1000
mout <- Match(Y=Y, Tr=treat, X=X, Weight.matrix=genout)
summary(mout)
Run Code Online (Sandbox Code Playgroud)
我们看到所有治疗病例都与对照病例相匹配.现在假设我们想要在已婚状态(或任何其他变量)上进行精确匹配.但是我们仍然希望使用之前创建的GenMatch矩阵.
参考链接
Exact = .....如果提供了逻辑向量,则应为X中的每个协变量提供逻辑值.使用逻辑向量允许用户为某些变量指定精确匹配,但不为其他变量指定精确匹配.如果未找到完全匹配,则会删除观察结果.
因此以下是正确的??
mout2 <- Match(Y=Y, Tr=treat, X=X, exact=c(0,0,0,0,1,0,0,0,0,0), Weight.matrix=genout)
summary(mout2)
Run Code Online (Sandbox Code Playgroud)
我会说那是不正确的,就像你比较一样
summary(mout$weights)
summary(mout2$weights)
Run Code Online (Sandbox Code Playgroud)
你得到相同的价值观
参考匹配包,我们看一下使用的示例GenMatch.
我们读到,Weight Matrix创建的是一个矩阵,其对角线对应于X中每个变量的权重
但我们不确定生成的值代表什么 - 它们是否与标准偏差有关.
让我们举个例子 GenMatch
library(Matching)
data(lalonde)
attach(lalonde)
#The covariates we want to match on
X = cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74)
#The covariates we want to obtain balance on
BalanceMat <- cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74,
I(re74*re75))
#Let's call GenMatch() to find the optimal weight to give each
#covariate in 'X' so as we have achieved balance on the …Run Code Online (Sandbox Code Playgroud) 采用以下通用数据
A <- c(5,7,11,10,23,30,24,6)
B <- c(1,2,3,1,2,3,1,2)
C <- data.frame(A,B)
Run Code Online (Sandbox Code Playgroud)
和以下间隔
library(intervals)
interval1 <- Intervals(
matrix(
c(
5, 15,
15, 25,
25, 35,
35, 100
),
ncol = 2, byrow = TRUE
),
closed = c( TRUE, FALSE ),
type = "Z"
)
rownames(interval1) <- c("A","B","C", "D")
interval2 <- Intervals(
matrix(
c(
0, 10,
12, 20,
22, 30,
30, 100
),
ncol = 2, byrow = TRUE
),
closed = c( TRUE, FALSE ),
type = "Z"
)
rownames(interval2) <- …Run Code Online (Sandbox Code Playgroud) 说我有如下数据
A <- c(1,1,1,2,2,2,3,3,3)
B <- c(1,0,0,1,0,0,1,0,0)
C <- c(8,7,6,8,7,8,9,9,11)
D <- data.frame(A,B,C)
D
library(dplyr)
E <- D %>%
group_by(B) %>%
filter(abs(diff(C)) <= 1)
Run Code Online (Sandbox Code Playgroud)
删除这些情况,以便删除黄色显示

换句话说,对于每个a身份,当我们评估b=0相对于b=1,c超过差异1的任何值.
因此,让我们从 ?t.test()
我们通过以下方法对数据进行了两个样本的t检验:
t.test(1:10, y = c(7:20))
Run Code Online (Sandbox Code Playgroud)
现在我只对保存感兴趣。p-value
当我输入以下代码时,$p.value也将保存。
t.test(1:10, y = c(7:20))[3]
Run Code Online (Sandbox Code Playgroud)
我只希望p-value保存的(带有$p.value)作为数字/整数/双精度数。很抱歉问这样一个简单的问题
让我们先拿一些随机数据
A <- c(1:5)
score_one <- c(123.5, 223.1, 242.2, 351.8, 123.1)
score_two <- c(324.2, 568.2, 124.9, 323.1, 213.4)
score_three <- c(553.1, 412.3, 435.7, 523.1, 365.4)
score_four <- c(123.2, 225.1, 243.6, 741.1, 951.2)
df1 <- data.frame(A, score_one, score_two, score_three, score_four)
library(dplyr)
library(tidyr)
df2 <- df1 %>%
group_by(A) %>%
mutate_each(funs(substr(.,1,1))) %>%
ungroup %>%
gather(variable, type, -c(A)) %>%
select(-variable) %>%
mutate(type = paste0("type_",type),
value = 1) %>%
group_by(A,type) %>%
summarise(value = sum(value)) %>%
ungroup %>%
spread(type, value, fill=0) %>%
inner_join(df1, by=c("A")) %>%
select(A, …Run Code Online (Sandbox Code Playgroud) 说我有以下数据
B <- (5:20)
C <- (6:21)
D <- (7:22)
E <- (8:23)
data <- data.frame(B,C,D,E)
Run Code Online (Sandbox Code Playgroud)
我也有一个矩阵
id <- c(4,7,9,12,15)
Run Code Online (Sandbox Code Playgroud)
这个矩阵代表我想输出到一个新的 data.frame 的行标识
如何使用该subset函数对原始数据进行子集化
new <- subset(data, .....)
Run Code Online (Sandbox Code Playgroud)
所以 new 只包含 5 个观察值
Say I have the following data
set.seed(123)
a <- c(rep(1,30),rep(2,30))
b <- rep(1:30)
c <- sample(20:60, 60, replace = T)
data <- data.frame(a,b,c)
data
Run Code Online (Sandbox Code Playgroud)
Now I want to extract data whereby:
For each unique value of a, extract/match data where the b value is the same and the c value is within a limit of +-5
so a desired output should produce:
