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? 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? 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.
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.
Thank you for the fast help! I am not sure though if I understand the predict.lm business. The newdata that I would make predictions from consists of six matrices, one for each variable. Do I cbind the matrices like you suggest for the regression and then convert them to a dataframe? How does R know which column in the matrix I created with cbind belongs to which variable in the regression? One last question. If I use predict.lm without newdata it gives me as many data points as were used for the regression. However, in my matrices I have rows of NA for days without observations. My problem is now that if predict.lm only gives me the datapoints used for regression I can not match them to the actual observations days anymore. Is there anyway of keeping the original matrix and put the predicted value in it? Thanks again, Frauke -- View this message in context: http://r.789695.n4.nabble.com/correlating-matrices-tp4643660p4643858.html Sent from the R help mailing list archive at Nabble.com.
Hello, As for the first question, predict.lm with new data uses the formula used in the fit so do not change the way you pass on your new data. If the formula was Y ~ X1 + X2 you can use newdata = data.frame(New1, New2) newdata = data.frame(cbind(New1, New2)) but the order must be kept. (And why cbind, by the way?) 2. Use something folloing these lines. (Untested, obviously, without a data example.) pred <- predict(...etc...) no_na <- complete.cases( cbind(matrix1, matrix2) ) matrix1[ no_na, ] <- pred[1, ] matrix2[ no_na, ] <- pred[2, ] Hope this helps, Rui Barradas Em 21-09-2012 17:09, frauke escreveu:> Thank you for the fast help! > > I am not sure though if I understand the predict.lm business. The newdata > that I would make predictions from consists of six matrices, one for each > variable. Do I cbind the matrices like you suggest for the regression and > then convert them to a dataframe? How does R know which column in the matrix > I created with cbind belongs to which variable in the regression? > > One last question. If I use predict.lm without newdata it gives me as many > data points as were used for the regression. However, in my matrices I have > rows of NA for days without observations. My problem is now that if > predict.lm only gives me the datapoints used for regression I can not match > them to the actual observations days anymore. Is there anyway of keeping the > original matrix and put the predicted value in it? > > Thanks again, Frauke > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/correlating-matrices-tp4643660p4643858.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.
Hi before you can use predict you need to do lm(whatever.....). If you used lm(whatever....., na.action=na.exclude) predict will give you results with correctly placed NA values. Regards Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of frauke > Sent: Friday, September 21, 2012 6:10 PM > To: r-help at r-project.org > Subject: Re: [R] correlating matrices > > Thank you for the fast help! > > I am not sure though if I understand the predict.lm business. The > newdata that I would make predictions from consists of six matrices, > one for each variable. Do I cbind the matrices like you suggest for the > regression and then convert them to a dataframe? How does R know which > column in the matrix I created with cbind belongs to which variable in > the regression? > > One last question. If I use predict.lm without newdata it gives me as > many data points as were used for the regression. However, in my > matrices I have rows of NA for days without observations. My problem is > now that if predict.lm only gives me the datapoints used for regression > I can not match them to the actual observations days anymore. Is there > anyway of keeping the original matrix and put the predicted value in > it? > > Thanks again, Frauke > > > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/correlating-matrices- > tp4643660p4643858.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.