2015-05-29 71 views
0

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

回答

1

我的replicate大風扇這樣的事情:

X <- cbind(...)       # what you had before 
BalanceMat <- cbind(...)    # ditto 
lalonde$ID <- seq.int(nrow(lalonde)) 

results <- replicate(1000, { 
    ## not certain if it's just $ID order that matters 
    lalonde$ID <- sample(nrow(lalonde)) 
    ## lalonde <- lalonde[ sample(nrow(lalonde)), ] 

    ## ... 
    ## rest of your computation 
    ## ... 

    #### optionally return everything 
    ## output 
    #### return just the minimum 
    exp(output$coefficients[1]) 
}) 

#### if you returned output earlier, you'll need this, otherwise not 
## coef <- exp(sapply(results, function(z) z$coefficients[1])) 

## plot as needed 

我不知道你的意思只是ID事情的順序,或者如果整個數據庫的順序;相應地調整replicate循環的第一對耦合線。

+0

\ replicate \很好,我喜歡你使用的方式\ sample(nrow(lalonde))\ – lukeg

1

您可以使用sample引進排列

data(lalonde) 
lalonde$age_cat <- with(lalonde, ifelse(age < 24, 1, 2)) 
lalonde$ID <- 1:length(lalonde$age) 
n <- 5 
res <- rep(NA, n) 
for (i in 1:n) { 
    lalonde <- lalonde[sample(1:nrow(lalonde)), ] # randomise order 
    ## rest of code 
    res[i] <- exp(output$coefficients[1]) 
} 

plot(1:n, res, main="Odds Ratios") 
+0

也是一個很好的答案,工作正常。感謝您的巨大貢獻 – lukeg

+0

爲什麼在隨機化訂單時,您會使用'lalonde [樣品(1:nrow(lalonde))']''而不是'lalonde [樣品((lalonde))'] – lukeg

+0

感謝您的補充建議,如果你想從L1的'for'循環中省略'lalonde <-'(即寫'lalonde [sample(1:nrow(lalonde)),]''而不是'lalonde < - lalonde [sample(1:nrow (lalonde)),]'隨機化不會被應用,是正確的嗎? – lukeg