基本上我想编写一个程序,将随机化我的数据的顺序n
次,然后完成一个生存分析并绘制输出过n
编写一个程序来介绍排列
所以,让我们从采取以下通用数据matching()
打包并创建治疗和未治疗人员的数据集。 Link to package
set.seed(123)
library(Matching)
data(lalonde)
lalonde$age_cat <- with(lalonde, ifelse(age < 24, 1, 2))
attach(lalonde)
lalonde$ID <- 1:length(lalonde$age)
#The covariates we want to match on
X = cbind(age_cat, educ, black, hisp, married, nodegr, u74, u75, re75, re74)
#The covariates we want to obtain balance on
BalanceMat <- cbind(age_cat, 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)
detach(lalonde)
# now lets pair the the non-treated collisions to the treated
# BUT lets pair WITHOUT REPLACEMENT
mout <- Match(Y=NULL, Tr=lalonde$treat, X=X,
Weight.matrix=genout, M=2,
replace=FALSE, ties=TRUE)
summary(mout)
# we see that for 130 treated observations, we have 260 non-treated
# this is because we set M=2
# and yes length(lalonde$age[lalonde$treat==0]) == 260 but just follow me please
# but this was done for a specific reason
# now lets create a table for our 130+260 collisions
treated <- lalonde[mout$index.treated,]
# now we only want one occurence of the treated variables
library(dplyr)
treat_clean <- treated %>%
group_by(ID) %>%
slice(1)
non.treated <- lalonde[mout$index.control,]
# finally we can combine to form one clear data.set
matched.data <- rbind(treat_clean, non.treated)
现在,我们可以做一个条件Logistic回归确定或与re78(1987年赚来的钱)和治疗相关。为此我们需要生存包。 Link to package
library(survival)
比方说,如果乘客收入在1978年
matched.data$success <- with(matched.data, ifelse(re78 > 8125, 1, 0))
output <- clogit(success ~ treat, matched.data, method = 'efron')
summary(output)
比8125更使我们看到,或用于治疗(治疗= 1)为1.495
发生了成功,我们可以保存为:
iteration.1 <- exp(output$coefficients[1])
现在我们从匹配包中读取(link)即replace = FALSE
请注意,如果FALSE, 匹配的顺序一般很重要。比赛将在 相同的顺序中找到的数据进行排序
所以我想要做的创建将用于n
次
- 随机化拉隆德$ ID为了
- 运行功能匹配处理
- 运行clogit算法
- 每次保存输出
exp(output$coefficients[1])
- 情节OR(
exp(output$coefficients[1])
)for each n
Essenece我想介绍排列到分析中。 如何才能做到这一点,当可以说N = 5
\ replicate \很好,我喜欢你使用的方式\ sample(nrow(lalonde))\ – lukeg