I am sending to this forum as stackoverflow has devolved into sth pretty bad. Below code shows how to get what I want in a clumsy way. cols <- letters[1:4] a1 <- outer(cols,cols,paste0) b1 <- a1[!lower.tri(a1)] X <- matrix(rnorm(80),ncol=4) colnames(X) <- cols X <- as.data.frame(X) XX <- matrix(0,nrow=nrow(X),ncol=length(b1)) colnames(XX) <- b1 for (k in 1:length(b1)){ XX[,k] <- X[,substr(b1[k],1,1)]*X[,substr(b1[k],2,2)] } Is there a way to get that using a formula or some neat trick? The above will not work for factors, so I will need to create the factor crossings using formula a*b*c and then cross with the numerics, which is even more clumsy. Thanks everybody
Daniel Nordlund
2025-Mar-24 09:07 UTC
[R] how to create model matrix of second order terms
Depending on what your ultimate goal is, you could use something like want <- model.matrix(? ~ (a + b + c + d)^2 , data=your_data) This will create a matrix with the appropriate main effects and first order interactions.? If you just want to run a simple regression, you could do it directly lm(y ~ (a + b + c + d)^2 , data=your_data) but the formula (a + b + c + d)^2 isn't accepted by all packages that do some form of regression. Hope this is helpful, Dan On 3/23/2025 10:47 AM, Stephen Bond via R-help wrote:> I am sending to this forum as stackoverflow has devolved into sth > pretty bad. > Below code shows how to get what I want in a clumsy way. > > cols <- letters[1:4] > a1 <- outer(cols,cols,paste0) > b1 <- a1[!lower.tri(a1)] > > X <- matrix(rnorm(80),ncol=4) > colnames(X) <- cols > X <- as.data.frame(X) > XX <- matrix(0,nrow=nrow(X),ncol=length(b1)) > colnames(XX) <- b1 > > for (k in 1:length(b1)){ > XX[,k] <- X[,substr(b1[k],1,1)]*X[,substr(b1[k],2,2)] > } > > > > Is there a way to get that using a formula or some neat trick? The > above will not work for factors, so I will need to create the factor > crossings using formula a*b*c and then cross with the numerics, which > is even more clumsy. > Thanks everybody > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Daniel Nordlund Port Townsend, WA (425) 273-5256
Full disclosure: I did not attempt to decipher your code. But ~(A+B +C)^2 - (A + B + C) gives all 2nd order interactions whether the terms are factors or numeric. ~I(A^2) + I(B^2) gives quadratics in A and B, which must be numeric, not factors, of course You can combine these as necessary to get a formula expression for just 2nd order terms. Wrapping this in model.matrix() should then give you the model matrix using "treatment" contrasts for the contrasts involving factors (you can change the contrast types using the 'contrasts.arg' argument of model.matrix()) 1. Does this help? 2. Do check this to make sure I'm correct Cheers, Bert "An educated person is one who can entertain new ideas, entertain others, and entertain herself." On Mon, Mar 24, 2025 at 12:22?AM Stephen Bond via R-help < r-help at r-project.org> wrote:> I am sending to this forum as stackoverflow has devolved into sth > pretty bad. > Below code shows how to get what I want in a clumsy way. > > cols <- letters[1:4] > a1 <- outer(cols,cols,paste0) > b1 <- a1[!lower.tri(a1)] > > X <- matrix(rnorm(80),ncol=4) > colnames(X) <- cols > X <- as.data.frame(X) > XX <- matrix(0,nrow=nrow(X),ncol=length(b1)) > colnames(XX) <- b1 > > for (k in 1:length(b1)){ > XX[,k] <- X[,substr(b1[k],1,1)]*X[,substr(b1[k],2,2)] > } > > > > Is there a way to get that using a formula or some neat trick? The > above will not work for factors, so I will need to create the factor > crossings using formula a*b*c and then cross with the numerics, which > is even more clumsy. > Thanks everybody > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]