我想知道如何在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)
}
}
}
然后使用接收器获取.txt文件以获取所有结果(所有基线的摘要(基线))。
我希望我的插图足以解释我的怀疑。由于这个原因,我正在努力解决这个问题(与STATA&#39;'var&#39;相比)问题很小。
我等待你的回复。
谢谢你, Pranav
答案 0 :(得分:1)
可以使用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
由于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中的内容不一致地使用_
与_
,因此我在公式中的所有相关位置使用了textArea
。