这是一个例子:
x1 = runif(10, -1, 1)
x2 = runif(10, -1, 1)
transformed = as.matrix(data.frame(x1 = x1, x2 = x2, x1x2 = x1*x2, x1squared
= x1^2, x2squared = x2^2))
这不是问题,但是如果我想要3个向量的所有组合,那么列将是:
x1,x2,x3,x1 * x2,x1 * x3,x2 * x3,x1 ^ 2,x2 ^ 2,x3 ^ 2,x1 * x2 * x3,x1 ^ 2 * x2 * x3 ...
有没有自动的方法来获得这样的矩阵?顺便说一句,我应该以某种方式知道哪一列。
您也可以尝试使用model.matrix
生成正交多项式(不是简单的多项式),并且R将其用于解释其方便的formula
界面。有关此处可用选项的详细信息:
x1 <- rnorm(20)
x2 <- rnorm(20)
x3 <- rnorm(20)
## compute all combos of polynomials up to degree 3
model.matrix(~ poly(x1, 3) * poly(x2, 3) * poly(x3, 3))
编辑:在下面的@DavidArenburg评论后修复
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句