r - 在R中的循环中引用元素
问题描述
我想知道如何在 R 中引用循环中的元素。在 STATA 中,它是通过循环内的“var”完成的。我正在使用循环,我想引用每个循环内的变量,同时在变量列表(x1 x2 x3)上回归这些变量。x1 变量也有后缀,以便名称可以分成几个较短的部分。我将在 STATA 中编写的代码是:
foreach credit in "short_term" "medium_term" "long_term" {
foreach percentile in "p50" "p75" "p90" {
foreach type in "high4" "high5" "high6" {
reg y_-credit' x1_-percentile '_`type' x2 x3
}
}
}
在 R 中,如果我创建一个列表并进行循环,我如何引用列表中的每个元素?例如:
credit <- c("short_term","medium_term","long_term")
percentile <- c("p50","p75","p90")
type <- c("high4","high5","high6")
for (c in credit) {
for (p in percentile) {
for (t in type) {
baseline_[c]_[p]_[t] <- lm(y_[c] — xl_[p]_[t] + x2 + x3)
}
}
}
然后使用 sink 获取一个 .txt 文件以将所有结果(所有基线的汇总(基线))放在一起。
我希望我的插图足以解释我的疑问。由于这个(与 STATA 的 `var' 相比,次要 - )问题,我正在努力解决循环问题。
我在等你的回复。
谢谢你,普拉纳夫
解决方案
可以使用该formula()
函数从 R 中的字符串生成公式。
由于 OP 不可重现,我们将formula()
使用mtcars
数据集进行演示:
data(mtcars) # Use motor trend cars data set
dvs <- c("mpg","qsec")
ivs <- c("am","wt","disp")
for(d in dvs){
for(i in ivs){
message(paste("d is: ", d, "i is: ",i))
print(summary(lm(formula(paste(d,"~",i)),mtcars)))
}
}
...以及输出的第一部分:
> for(d in dvs){
+ for(i in ivs){
+ message(paste("d is: ", d, "i is: ",i))
+ print(summary(lm(formula(paste(d,"~",i)),mtcars)))
+ }
+ }
d is: mpg i is: am
Call:
lm(formula = formula(paste(d, "~", i)), data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-9.3923 -3.0923 -0.2974 3.2439 9.5077
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 17.147 1.125 15.247 1.13e-15 ***
am 7.245 1.764 4.106 0.000285 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 4.902 on 30 degrees of freedom
Multiple R-squared: 0.3598, Adjusted R-squared: 0.3385
F-statistic: 16.86 on 1 and 30 DF, p-value: 0.000285
由于 from 的输出lm()
可以保存在一个对象中,因此也可以生成一个list()
模型对象,并在 R 中进一步操作它们。
formula()
要从包含所需变量名称元素的向量生成语句的命名变量,可以以类似于上面对数据集采用的方法的方式使用paste()
或函数。默认参数之间没有空格,默认情况下在参数之间添加空格。 paste0()
mtcars
paste0()
paste()
同样,对实际预期的公式进行一些猜测,我们将使用 OP 嵌套for()
循环来生成可以formula()
在lm()
函数中使用的字符串。
#
# generate formulas using content from OP
#
credit <- c("short_term","medium_term","long_term")
percentile <- c("p50","p75","p90")
type <- c("high4","high5","high6")
for (c in credit) {
for (p in percentile) {
for (t in type) {
aFormula <- paste0("y_",c," ~ x1-",p,"_",t," + x2 + x3")
print(aFormula)
}
}
}
...以及输出的开始:
> credit <- c("short_term","medium_term","long_term")
> percentile <- c("p50","p75","p90")
> type <- c("high4","high5","high6")
>
> for (c in credit) {
+ for (p in percentile) {
+ for (t in type) {
+ aFormula <- paste0("y_",c," ~ x1_",p,"_",t," + x2 + x3")
+ print(aFormula)
+ }
+ }
+ }
[1] "y_short_term ~ x1_p50_high4 + x2 + x3"
[1] "y_short_term ~ x1_p50_high5 + x2 + x3"
[1] "y_short_term ~ x1_p50_high6 + x2 + x3"
[1] "y_short_term ~ x1_p75_high4 + x2 + x3"
[1] "y_short_term ~ x1_p75_high5 + x2 + x3"
[1] "y_short_term ~ x1_p75_high6 + x2 + x3"
[1] "y_short_term ~ x1_p90_high4 + x2 + x3"
[1] "y_short_term ~ x1_p90_high5 + x2 + x3"
[1] "y_short_term ~ x1_p90_high6 + x2 + x3"
[1] "y_medium_term ~ x1_p50_high4 + x2 + x3"
[1] "y_medium_term ~ x1_p50_high5 + x2 + x3"
[1] "y_medium_term ~ x1_p50_high6 + x2 + x3"
.
.
.
请注意,OP 中的内容不一致地使用-
vs. _
,因此我_
在公式中的所有相关位置都使用了。
推荐阅读
- jquery - Property 'toLowerCase' does not exist on type 'string | number | string[]'. Property 'toLowerCase' does not exist on type 'number'
- node-red - Add local files to Node-RED on server
- c++ - Double free or corruption error detected while doing BFS
- javascript - How can I collect data on all BrowserWindows synchronuously in Electron?
- python - Annotate point on axes with automatic tick formatting in matplotlib?
- android - 在 Google 地图中使用 dragListener 可编辑多个多边形
- linux - 无法运行编译的 go 文件
- php - POST 请求中的 Symfony 正文表单数据为空
- nginx - 无法通过子域上的反向代理访问父目录
- powershell - PowerShell - 测试 TXT/CSV 文件中的多个 URL 并将 HTTP 代码记录到 CSV 文件中以进行报告