Re: [R] how to create model matrix of second order terms

2025-04-05 Thread Ben Bolker
I'm still not entirely clear what you want, in particular what you want to with factor variables ('squaring' them doesn't really seem to make sense). Some combination of sprintf/paste/reformulate might do what you want: ## get all second-order terms (doesn't square numeric values) quad_terms

Re: [R] how to create model matrix of second order terms

2025-04-05 Thread Ebert,Timothy Aaron
mula(paste("y ~", paste(result, collapse = " + "))) model <- lda(formula, data = mydata) Tim -Original Message- From: R-help On Behalf Of Stephen Bond via R-help Sent: Monday, March 24, 2025 11:29 AM To: Bert Gunter Cc: r-help@r-project.org Subject: Re: [R] how to create

Re: [R] how to create model matrix of second order terms

2025-03-25 Thread Stephen Bond via R-help
@ Rui that is the idea, but how do I apply this to a matrix with 200 columns? I cannot write out the expression.  The colnames seem very messy, but they would be messy even under my scheme with as little as 100 vars. On Tue, 2025-03-25 at 06:15 +, Rui Barradas wrote: > Às 15:28 de 24/03/2025,

Re: [R] how to create model matrix of second order terms

2025-03-25 Thread Rui Barradas
Hello, Sorry, much simpler: poly(as.matrix(X), degree = 2L) Hope this helps, Rui Barradas Às 13:58 de 25/03/2025, Rui Barradas escreveu: Hello, This seems to work and is independent of the number of columns. 'p' is the output in my previous post. f <- function(x, data = X) with(data,

Re: [R] how to create model matrix of second order terms

2025-03-25 Thread Rui Barradas
Hello, This seems to work and is independent of the number of columns. 'p' is the output in my previous post. f <- function(x, data = X) with(data, eval(parse(text = x))) p2 <- poly(sapply(names(X), f), degree = 2L) identical(p, p2) # [1] TRUE Hope this helps, Rui Barradas Às 13:42 de 2

Re: [R] how to create model matrix of second order terms

2025-03-24 Thread Rui Barradas
Às 15:28 de 24/03/2025, Stephen Bond via R-help escreveu: Folks, I appreciate your effort, but maybe I was not explicit enough, so let me try again. The current setup for formulas does not allow for I(x^2) terms as explained in the MASS book at the end of Section 6.2 the x:x interaction is trea

Re: [R] how to create model matrix of second order terms

2025-03-24 Thread Stephen Bond via R-help
Folks, I appreciate your effort, but maybe I was not explicit enough, so let me try again. The current setup for formulas does not allow for I(x^2) terms as explained in the MASS book at the end of Section 6.2 the x:x interaction is treated as x. So I need to write my own code, which is clumsy u

Re: [R] how to create model matrix of second order terms

2025-03-24 Thread Bert Gunter
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

Re: [R] how to create model matrix of second order terms

2025-03-24 Thread Daniel Nordlund
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

[R] how to create model matrix of second order terms

2025-03-24 Thread Stephen Bond via R-help
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