英文:
Writing for loop in R over imputations
问题
我基本上有相同的代码序列,我想要重复对1到10的数字列表执行。在Stata中,我会这样做foreach num in numlist 1 2 3 4 5 6 7 8 9 10 {
这很简单。但在R中,我不太确定如何执行它。
所以这段代码...
d1 <- read_dta("C:/Users/Folder/imputation_1.dta")
d1$race <- factor(d1$race)
d1$educ <- factor(d1$educ)
psm_1 <- weightit(trtmnt ~ race + education + gender,
data = d1,
method = "psm",
estimand = "ATT")
d1$psm_weights <- psm_1$weights
write_dta(d1, "C:/Users/Folder/weighted_1.dta")
...我只想重复,同时将“1”替换为“2”,然后为“3”等等。我可以手动重复相同的代码来做到这一点(如下所示),但一定有一种高效的循环方式。
d2 <- read_dta("C:/Users/Folder/imputation_2.dta")
d2$race <- factor(d2$race)
d2$educ <- factor(d2$educ)
psm_2 <- weightit(trtmnt ~ race + education + gender,
data = d2,
method = "psm",
estimand = "ATT")
d2$psm_weights <- psm_2$weights
write_dta(d2, "C:/Users/Folder/weighted_2.dta")
我尝试遵循这个链接: https://cran.r-project.org/web/packages/foreach/vignettes/foreach.html 但它似乎不是我需要的确切方式(或者我只是没有完全理解它)。
英文:
I basically have the same sequence of code that I want to repeat for a list of numbers from 1 through 10. In Stata, I would do foreach num in numlist 1 2 3 4 5 6 7 8 9 10 {
and this would be straightforward. But in R, I'm not quite sure how to execute it.
So this code...
d1 <- read_dta("C:/Users/Folder/imputation_1.dta")
d1$race <- factor(d1$race)
d1$educ <- factor(d1$educ)
psm_1 <- weightit(trtmnt ~ race + education + gender,
data = d1,
method = "psm",
estimand = "ATT")
d1$psm_weights <- psm_1$weights
write_dta(d1, "C:/Users/Folder/weighted_1.dta")
...I just want to repeat that while replacing the "1" with a "2", and then a "3", and so on. I could just repeat the same code and do that manually (like below) but there must be a way to loop through efficiently.
d2 <- read_dta("C:/Users/Folder/imputation_2.dta")
d2$race <- factor(d2$race)
d2$educ <- factor(d2$educ)
psm_2 <- weightit(trtmnt ~ race + education + gender,
data = d2,
method = "psm",
estimand = "ATT")
d2$psm_weights <- psm_2$weights
write_dta(d2, "C:/Users/Folder/weighted_2.dta")
I tried following this: https://cran.r-project.org/web/packages/foreach/vignettes/foreach.html but it doesn't seem to be exactly what I need (or I just don't fully understand it).
答案1
得分: 1
这是一个建议,我按照1,2,3的顺序排列:
d=list()
psm=list()
for (i in 1:3)
{
d[[i]] <- read_dta(paste0("C://Users//Folder//imputation_",i,
".dta"))
d[[i]]$race <- factor(d[[i]]$race)
d[[i]]$educ <- factor(d[[i]]$educ)
psm[[i]] <- weightit(trtmnt ~ race + education + gender,
data = d[[i]],
method = "psm",
estimand = "ATT")
d[[i]]$psm_weights <- psm[[i]]$weights
write_dta(d[[i]], paste0("C://Users//Folder//weighted_",i,".dta"))
}
英文:
This is an suggestion and i sequence as 1,2,3:
d=list()
psm=list()
for (i in 1:3)
{
d[[i]] <- read_dta(paste0("C://Users//Folder//imputation_",i,
".dta"))
d[[i]]$race <- factor(d[[i]]$race)
d[[i]]$educ <- factor(d[[i]]$educ)
psm[[i]] <- weightit(trtmnt ~ race + education + gender,
data = d[[i]],
method = "psm",
estimand = "ATT")
d[[i]]$psm_weights <- psm[[i]]$weights
write_dta(d[[i]], paste0("C://Users//Folder//weighted_",i,".dta"))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论