gpt4 book ai didi

r - 如何以紧凑的形式描述回归交互项?

转载 作者:行者123 更新时间:2023-12-04 10:35:59 25 4
gpt4 key购买 nike

我有回归器 a , b , c , e_1e_2我想运行以下回归:
y ~ a + b + c + e_1 + e_2 + a.e_1 + a.e_2 + b.e_1 + b.e_2 + c.e_1 + c.e_2
每个e_1e_2必须与 a 互动, bc变量。

我实际上有 5 e 's 和其他 5 个变量。编写代码而不是单独编写的最快方法是什么a:e_1 , a:e_2等等。

最佳答案

怎么样~(a+b+c)*(e1+e2) ?

例子:

dd <- data.frame(a=1,b=1,c=1,e1=1,e2=1)
colnames(model.matrix(~(a+b+c)*(e1+e2),dd)
## [1] "(Intercept)" "a" "b" "c" "e1"
## [6] "e2" "a:e1" "a:e2" "b:e1" "b:e2"
## [11] "c:e1" "c:e2"

对于更大的变量集,@ThomasIsCoding 的解决方案的替代方案:
pfun <- function(x) paste("(", paste(x,collapse="+"), ")")
reformulate(paste(pfun(p), "*", pfun(q)), response="y")

关于r - 如何以紧凑的形式描述回归交互项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60183196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com