Having a dataset with an arbitrary number of columns N and rows T, I would like to obtain all the columns implied by a multinomial expansion of the columns sum raised to an arbitrary degree d.
To be clearer: having the following dataset as an example, with N = 3 and T = 10, with columns names being a
, b
, c
set.seed(123)
ds <- cbind("a"=rnorm(10),"b"=rnorm(10),"c"=rnorm(10)); ds
> ds
a b c
[1,] -0.56047565 1.2240818 -1.0678237
[2,] -0.23017749 0.3598138 -0.2179749
[3,] 1.55870831 0.4007715 -1.0260044
[4,] 0.07050839 0.1106827 -0.7288912
[5,] 0.12928774 -0.5558411 -0.6250393
[6,] 1.71506499 1.7869131 -1.6866933
[7,] 0.46091621 0.4978505 0.8377870
[8,] -1.26506123 -1.9666172 0.1533731
[9,] -0.68685285 0.7013559 -1.1381369
[10,] -0.44566197 -0.4727914 1.2538149
the desired output for degree d = 2 would be a dataset with columns { a
^2, b
^2, c
^2, a
* b
, a
* c
, b
* c
}, which in this case I can manually specify as
out <- cbind(ds[,"a"]^2, ds[,"b"]^2, ds[,"c"]^2, ds[,"a"]*ds[,"b"], ds[,"a"]*ds[,"c"], ds[,"b"]*ds[,"c"])
I am wondering what smart ways are out there to perform this automatically, maybe with a function that only takes ds
and d as arguments.
EDIT: as the MWE suggests, I am not really interested in the multinomial coefficients, so feel free to consider them for completeness or not.