基本上我想編寫一個程序,將隨機化我的數據的順序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