Hello, can someone give me an example of how to fit a linear model where the response is a matrix, please? (other than a loop over lm calls) The ?lm points me to model.matrix but it is not clear to me how to use a model matrix in an lm call. fit<-lm(headmatrix ~ tailmatrix) causes problems as soon as I use predict(fit,newframe). The variables in tailmatrix are not found in newframe. Thanks, Claus --------------------------------- [[alternative HTML version deleted]]
Try: x = matrix(runif(30), 10, 3) newx = matrix(runif(15), 5, 3) y = rnorm(10) fit = lm(y ~ x) newyhat = predict(fit, newdata=data.frame(x=I(newx))) HTH, Andy> From: Claus Gwiggner > > Hello, > > can someone give me an example of how to fit a linear model > where the > response is a matrix, please? (other than a loop over lm calls) > The ?lm points me to model.matrix but it is not clear to me > how to use a > model matrix in an lm call. > > fit<-lm(headmatrix ~ tailmatrix) causes problems as soon as I > use predict(fit,newframe). The variables in tailmatrix are > not found in > newframe. > > Thanks, > > Claus
Your problem might be that the names of 'newframe' don't match the column names of 'tailmatrix'. It's hard to tell because you don't provide an example. Is 'newframe' a matrix or a dataframe? If it is a dataframe, try: names(newframe) <- colnames(tailmatrix) predict(fit, newframe) -roger Claus Gwiggner wrote:> Hello, > > can someone give me an example of how to fit a linear model where the > response is a matrix, please? (other than a loop over lm calls) > The ?lm points me to model.matrix but it is not clear to me how to use a > model matrix in an lm call. > > fit<-lm(headmatrix ~ tailmatrix) causes problems as soon as I > use predict(fit,newframe). The variables in tailmatrix are not found in > newframe. > > Thanks, > > Claus > > > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Reasonably Related Threads
- SLLOOOWWW function ...
- as.formula and lme ( Fixed effects: Error in as.vector(x, "list") : cannot coerce to vector)
- as.formula and lme ( Fixed effects: Error in as.vector(x, "list") : cannot coerce to vector)
- implicit data frame reference
- strange PREDICTIONS from a PIECEWISE LINEAR (mixed) MODEL