[R] correlating matrices
Rui Barradas
ruipbarradas at sapo.pt
Thu Sep 20 00:52:44 CEST 2012
Hello,
Inline.
Em 19-09-2012 19:56, frauke escreveu:
> Hi,
>
> thank you for taking the time and reading my question. My question is
> twofold:
>
> 1. I have several matrices with variables and one matrix with water levels.
> I want to predict the water level with the data in the other matrices.
> Basically,
> * mod<-lm(matrix1 ~ matrix2+matrix3)*
> ( What looks like a minus is meant to be the wiggly minus.)
> Of course I could dissemble the matrices and paste their columns into one
> long, long vector. But the method above gives reasonable results. Are there
> any methodological objections against doing this?
No, not really. I would use cbind(matrix2, matrix3) but the result is
the same.
It's a matter of choice.
>
> 2. After having done the regression I now want to make a prediction. Thus
> given the coefficients of my regression mod and matrix2 and matrix3 I want
> to predict a new matrix. This is where the matrix approach becomes
> difficult. I made it work, but it is very unelegant:
>
> *pred<-rep(coef(mod)[1],2)+coef(mod)[2]*matrix2+coef(mod)[3]*matrix3
> PRED<-matrix(pred, nrow=length(matrix1[,1]), ncol=length(matrix1[1,]),
> byrow=F)*
>
> Is there a more elegant way of doing this?
Yes!
?predict.lm
Without 'newdata' it gives you the fitted values. With new data it gives
you predictions.
Beware, 'newdata' must be a data.frame.
Hope this helps,
Rui Barradas
> Because I dont have two but six
> matrices as independent variables, so it becomes pretty lengthy. I could
> not make the command predict() work for this.
>
> Thank you!
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/correlating-matrices-tp4643660.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
More information about the R-help
mailing list