R如何将变量名称列表一起传递给函数
Posted
技术标签:
【中文标题】R如何将变量名称列表一起传递给函数【英文标题】:R How to pass a list of variables names together into a function 【发布时间】:2022-01-11 06:10:16 【问题描述】:我正在尝试使用数百个交互项构建线性回归。我现在手动将它们输入到方程式中。
model <- lm(Y ~ A + B + C + A*X1 + A*X2 + A*X3+...+A*X100 + B*X1 + B*X2 + B*X3...+B*X100, data)
有没有办法更有效地编码?我正在考虑类似的事情:
Group <- X1 + X2 + X3+ ... + X99 + X100
model <- lm(Y~ A + B + C + A*Group + B*Group)
这是否可以在 R 中通过任何包或函数来实现?
【问题讨论】:
【参考方案1】:你可以试试:
xs <- paste(paste("X", 1:100, sep=""),collapse="+")
form1 <- as.formula(sprintf("Y~ C+(A+B)*(%s)",xs))
model <- lm(form1)
form1
Y ~ C + (A + B) * (X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 +
X10 + X11 + X12 + X13 + X14 + X15 + X16 + X17 + X18 + X19 +
X20 + X21 + X22 + X23 + X24 + X25 + X26 + X27 + X28 + X29 +
X30 + X31 + X32 + X33 + X34 + X35 + X36 + X37 + X38 + X39 +
X40 + X41 + X42 + X43 + X44 + X45 + X46 + X47 + X48 + X49 +
X50 + X51 + X52 + X53 + X54 + X55 + X56 + X57 + X58 + X59 +
X60 + X61 + X62 + X63 + X64 + X65 + X66 + X67 + X68 + X69 +
X70 + X71 + X72 + X73 + X74 + X75 + X76 + X77 + X78 + X79 +
X80 + X81 + X82 + X83 + X84 + X85 + X86 + X87 + X88 + X89 +
X90 + X91 + X92 + X93 + X94 + X95 + X96 + X97 + X98 + X99 +
X100)
第一行构建X1 + X2 + ... + X100
部分。第二行创建了带有A
、B
和C
主效应的公式,其中A
和B
与每个X
交叉。 as.formula
会将字符串转换为公式。
【讨论】:
【参考方案2】:考虑reformulate
,通过构建右侧变量的向量,然后将其作为第一个参数传递:
rhs_vars <- c(
"A", "B", "C",
paste0("A*X", 1:100),
paste0("B*X", 1:100)
)
model <- lm(reformulate(rhs_vars, "Y"), data)
【讨论】:
【参考方案3】:我们可以使用paste0
和reformulate
的组合:
Group <- paste0("X", 1:100)
rhs_expanded<-c(paste0("A*", Group), paste0("B*", Group))
lm_formula<-reformulate(response = 'Y', termlabels = c('A', 'B', 'C', rhs_expanded))
lm(formula = lm_formula, data)
查看由reformulate
生成的lm_formula
对象:
lm_formula
Y ~ A + B + C + A * X1 + A * X2 + A * X3 + A * X4 + A * X5 +
A * X6 + A * X7 + A * X8 + A * X9 + A * X10 + A * X11 + A *
X12 + A * X13 + A * X14 + A * X15 + A * X16 + A * X17 + A *
X18 + A * X19 + A * X20 + A * X21 + A * X22 + A * X23 + A *
X24 + A * X25 + A * X26 + A * X27 + A * X28 + A * X29 + A *
X30 + A * X31 + A * X32 + A * X33 + A * X34 + A * X35 + A *
X36 + A * X37 + A * X38 + A * X39 + A * X40 + A * X41 + A *
X42 + A * X43 + A * X44 + A * X45 + A * X46 + A * X47 + A *
X48 + A * X49 + A * X50 + A * X51 + A * X52 + A * X53 + A *
X54 + A * X55 + A * X56 + A * X57 + A * X58 + A * X59 + A *
X60 + A * X61 + A * X62 + A * X63 + A * X64 + A * X65 + A *
X66 + A * X67 + A * X68 + A * X69 + A * X70 + A * X71 + A *
X72 + A * X73 + A * X74 + A * X75 + A * X76 + A * X77 + A *
X78 + A * X79 + A * X80 + A * X81 + A * X82 + A * X83 + A *
X84 + A * X85 + A * X86 + A * X87 + A * X88 + A * X89 + A *
X90 + A * X91 + A * X92 + A * X93 + A * X94 + A * X95 + A *
X96 + A * X97 + A * X98 + A * X99 + A * X100 + B * X1 + B *
X2 + B * X3 + B * X4 + B * X5 + B * X6 + B * X7 + B * X8 +
B * X9 + B * X10 + B * X11 + B * X12 + B * X13 + B * X14 +
B * X15 + B * X16 + B * X17 + B * X18 + B * X19 + B * X20 +
B * X21 + B * X22 + B * X23 + B * X24 + B * X25 + B * X26 +
B * X27 + B * X28 + B * X29 + B * X30 + B * X31 + B * X32 +
B * X33 + B * X34 + B * X35 + B * X36 + B * X37 + B * X38 +
B * X39 + B * X40 + B * X41 + B * X42 + B * X43 + B * X44 +
B * X45 + B * X46 + B * X47 + B * X48 + B * X49 + B * X50 +
B * X51 + B * X52 + B * X53 + B * X54 + B * X55 + B * X56 +
B * X57 + B * X58 + B * X59 + B * X60 + B * X61 + B * X62 +
B * X63 + B * X64 + B * X65 + B * X66 + B * X67 + B * X68 +
B * X69 + B * X70 + B * X71 + B * X72 + B * X73 + B * X74 +
B * X75 + B * X76 + B * X77 + B * X78 + B * X79 + B * X80 +
B * X81 + B * X82 + B * X83 + B * X84 + B * X85 + B * X86 +
B * X87 + B * X88 + B * X89 + B * X90 + B * X91 + B * X92 +
B * X93 + B * X94 + B * X95 + B * X96 + B * X97 + B * X98 +
B * X99 + B * X100
【讨论】:
以上是关于R如何将变量名称列表一起传递给函数的主要内容,如果未能解决你的问题,请参考以下文章