在R中编写循环以遍历多次输入。

huangapple go评论53阅读模式
英文:

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 &lt;- read_dta(&quot;C:/Users/Folder/imputation_1.dta&quot;)

d1$race &lt;- factor(d1$race)
d1$educ &lt;- factor(d1$educ)

psm_1 &lt;- weightit(trtmnt ~ race + education + gender,
                 data = d1,
                 method = &quot;psm&quot;,
                 estimand = &quot;ATT&quot;)

d1$psm_weights &lt;- psm_1$weights

write_dta(d1, &quot;C:/Users/Folder/weighted_1.dta&quot;)

...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 &lt;- read_dta(&quot;C:/Users/Folder/imputation_2.dta&quot;)

d2$race &lt;- factor(d2$race)
d2$educ &lt;- factor(d2$educ)

psm_2 &lt;- weightit(trtmnt ~ race + education + gender,
                 data = d2,
                 method = &quot;psm&quot;,
                 estimand = &quot;ATT&quot;)

d2$psm_weights &lt;- psm_2$weights

write_dta(d2, &quot;C:/Users/Folder/weighted_2.dta&quot;)

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]] &lt;- read_dta(paste0(&quot;C://Users//Folder//imputation_&quot;,i,
&quot;.dta&quot;))

  d[[i]]$race &lt;- factor(d[[i]]$race)
  d[[i]]$educ &lt;- factor(d[[i]]$educ)

  psm[[i]] &lt;- weightit(trtmnt ~ race + education + gender,
                 data = d[[i]],
                 method = &quot;psm&quot;,
                 estimand = &quot;ATT&quot;)

  d[[i]]$psm_weights &lt;- psm[[i]]$weights

  write_dta(d[[i]], paste0(&quot;C://Users//Folder//weighted_&quot;,i,&quot;.dta&quot;))
}

huangapple
  • 本文由 发表于 2023年2月16日 09:30:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定