[R] formula () problems
Marc Schwartz
marc_schwartz at me.com
Wed Dec 9 21:35:12 CET 2009
On Dec 9, 2009, at 2:22 PM, Dr R.K.S. Hankin wrote:
> Hi.
>
> I am having difficulty creating a formula for use with glm()
>
> I have a matrix of an unknown number of columns and wish to estimate a
> coefficient for each column, and one for the each product of a column
> with another column.
>
> In the case of a five-column matrix this would be:
>
>> x <- matrix(rnorm(100),ncol=5)
>> colnames(x) <- letters[1:5]
>> z <- rnorm(20)
>> lm(z~ -1+(a+b+c+d+e)^2,data=data.frame(x))
>
> Call:
> lm(formula = z ~ -1 + (a + b + c + d + e)^2, data = data.frame(x))
>
> Coefficients:
> a b c d e a:b a:c a:d -0.30021 -0.21465 0.12208 0.06308 0.28806
> 0.34482 -1.00072 0.48218
> a:e b:c b:d b:e c:d c:e d:e
> 0.28786 -0.46306 0.39844 0.04436 0.32236 -0.09210 -1.06625
>
> This is what I want: five single terms (a-e) and 5*(5-1)/2=10 (a:b to
> d:e) for the cross terms. If there were 6 columns I would want
> (a+b+c+d+e+f)^2 and have 21 (=6+15) terms.
>
> How do I create a formula that does this for an arbitrary number of
> columns?
>
>
> thanks
>
> Robin
Robin,
Try this:
lm(z ~ (.)^2 - 1, data = data.frame(x))
See the Details section of ?formula, which describes the use of '.' to
refer to all columns not otherwise already in the formula.
HTH,
Marc Schwartz
More information about the R-help
mailing list