On Nov 11, 2009, at 2:24 PM, Murat Tasan wrote:

hi all - quick question:

i have a matrix 'y' of response values, with two explanatory variables
'x1' and 'x2'.
tested values of 'x1' and 'x2' are sitting in two vectors 'x1' and
'x2'.
i want to learn model parameters without "unrolling" the matrix of
response values.
example below:

# some fake data for the example
x1 <- 1:5
x2 <- 1:10
y <- matrix(runif(50), nrow = 5)

# current method:
z <- vector()
for(i in x1) for(j in x2) z <- c(z, i, j, y[i, j])
z <- data.frame(matrix(z, ncol = 3, byrow = TRUE))
colnames(z) <- c("x1", "x2", "y")
m <- glm(y ~ x1 + x2 + x1:x2, family = binomial, data = z)

# what i'd like to do, kind of:

m <- glm(as.vector(y) ~ expand.grid(x1 , x2)^2)

Perhaps:
> zdf <- expand.grid(x1,x2)
> zdf$y <- as.vector(y)
> m <- glm(as.vector(y) ~ (Var1 + Var2)^2, data=zdf)
> m

Call:  glm(formula = as.vector(y) ~ (Var1 + Var2)^2, data = zdf)

Coefficients:
(Intercept)         Var1         Var2    Var1:Var2
   0.425943     0.066960    -0.001198    -0.006480

Degrees of Freedom: 49 Total (i.e. Null);  46 Residual
Null Deviance:      4.067
Residual Deviance: 3.759        AIC: 22.5



basically, i have to "unfold" the matrix 'y' to a data frame 'z' then
solve.

as.vector would do that, but I don't know how well it works within a formula.


this is somewhat tedious.
anyone know of a way i can do this more generally, especially if
working in even higher dimensions than 2 (i.e. with an arbitrary-
dimension array of response values)?

?formula

You can get all two way interactions with ( )^2

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to