À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
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
(I neglected to cc the list)
-- Forwarded message -
From: Bert Gunter
Date: Mon, Mar 24, 2025 at 11:00 AM
Subject: Re: [R] how to create model matrix of second order terms
To: Stephen Bond
In view of the fact that your claim that:
"The current setup for formulas does not allow
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
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
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
6 matches
Mail list logo