Model matrix with all pairwise interactions between columns

前端 未结 2 1800
慢半拍i
慢半拍i 2020-12-15 11:04

Let\'s say that I have a numeric data matrix with columns w, x, y, z and I also want to add in the columns that are equivalent to w*x, w*y, w*z, x*y, x*z,

2条回答
  •  无人及你
    2020-12-15 11:57

    If you are doing a regression, you can just do something like

    reg <- lm(w ~ (x + y + z)^2
    

    and it will figure things out for you. For example,

    lm(Petal.Width ~ (Sepal.Length + Sepal.Width + Petal.Length)^2, iris)
    # Call:
    # lm(formula = Petal.Width ~ (Sepal.Length + Sepal.Width + Petal.Length)^2,
    #     data = iris)
    #      # Coefficients:
    #               (Intercept)               Sepal.Length                Sepal.Width
    #                  -1.05768                    0.07628                    0.22983
    #              Petal.Length   Sepal.Length:Sepal.Width  Sepal.Length:Petal.Length
    #                   0.47586                   -0.03863                   -0.03083
    #  Sepal.Width:Petal.Length
    #                   0.06493
    

提交回复
热议问题