[R] fitting a glm with matrix of responses

Murat Tasan mmuurr at gmail.com
Wed Nov 11 20:24:05 CET 2009


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(y ~ x1 + x2 + x1:x2)


basically, i have to "unfold" the matrix 'y' to a data frame 'z' then
solve.
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)?




More information about the R-help mailing list