Hi, I have following kind of model : Y = X1 * a1 + X2 * a2 + error Here sampled data for Y, X1, X2 are like that : Y <- replicate(10, matrix(rnorm(2),2), simplify = F) X1 <- replicate(10, matrix(rnorm(4),2), simplify = F) X2 <- replicate(10, matrix(rnorm(4),2), simplify = F) My goal is to calculate LS estimates of vectors "a1" and "a2". Can anyone please guide me how to do that in R? Is there any special function to handle this kind of problem? Thanks -- View this message in context: http://www.nabble.com/%22Special%22-LS-estimation-problem-tp25083103p25083103.html Sent from the R help mailing list archive at Nabble.com.
On 21-Aug-09 16:28:26, megh wrote:> Hi, I have following kind of model : Y = X1 * a1 + X2 * a2 + error > > Here sampled data for Y, X1, X2 are like that : > > Y <- replicate(10, matrix(rnorm(2),2), simplify = F) > X1 <- replicate(10, matrix(rnorm(4),2), simplify = F) > X2 <- replicate(10, matrix(rnorm(4),2), simplify = F) > > My goal is to calculate LS estimates of vectors "a1" and "a2". > Can anyone please guide me how to do that in R? Is there any > special function to handle this kind of problem? > > ThanksIn your example, a1 and a2 cannot be vectors, since Y, X1 and X2 are all 2x2 matrices. On the other hand, either or both of a1, a2 could be either a scalar or a 2x2 matrix, any of which would make X1*a1 + X2*a2 (provided you interpret "*" as "%*%" in R) into a 2x2 matrix. But if (say) a1 is a vector it can only be a 2x1 vector (for multiplication conformity), and then X1*a1 (or X1%*%a1 in R) would be a 2x1 vector (in the linear algebra sense, i.e. a 2x1 matrix in the R sense). So what is it that you want in the model? Next, if in fact a1 and a2 are scalars, then in effect you are looking at Y1[1,1] = a1*X1[1,1] + a2*X2[1,1] + error[1,1] Y1[1,2] = a1*X1[1,2] + a2*X2[1,2] + error[1,2] Y1[2,1] = a1*X1[2,1] + a2*X2[2,1] + error[2,1] Y1[2,2] = a1*X1[2,2] + a2*X2[2,2] + error[2,2] 10 times over. The relevant question here is: Are you wanting to include possible covariance between the 4 elements of 'error'? One possibility in this case is to treat this as a multilevel or longitudinal model, with 4 observations "per subject", and correlated error within-subject. But you really need to spell out more detail of the kind of model you are seeking to fit. What you described is not enough! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 21-Aug-09 Time: 18:48:31 ------------------------------ XFMail ------------------------------
I found no fruitful suggestions as yet, therefore I have devised a simple mechanism for that. Here I can modify my model as : Y = X*a + error, X = (X1, X2), a = t(a1, a2) Now I can apply the standard LS procedure, to estimate a. Here is my code : Y <- replicate(10, matrix(rnorm(2),2), simplify = F) X1 <- replicate(10, matrix(rnorm(4),2), simplify = F) X2 <- replicate(10, matrix(rnorm(4),2), simplify = F) X <- lapply(1:10, function(i) cbind(X1[[i]], X2[[i]])) temp1 <- Reduce("cbind", lapply(X, "t")); temp2 <- Reduce("rbind", X); temp3 <- Reduce("rbind", Y) a <- solve(temp1%*%temp2) %*% (temp1%*%temp3); a Can I go ahead with this procedure? Somebody please validate this? Thanks megh wrote:> > Hi, I have following kind of model : Y = X1 * a1 + X2 * a2 + error > > Here sampled data for Y, X1, X2 are like that : > > Y <- replicate(10, matrix(rnorm(2),2), simplify = F) > X1 <- replicate(10, matrix(rnorm(4),2), simplify = F) > X2 <- replicate(10, matrix(rnorm(4),2), simplify = F) > > My goal is to calculate LS estimates of vectors "a1" and "a2". Can anyone > please guide me how to do that in R? Is there any special function to > handle this kind of problem? > > Thanks >-- View this message in context: http://www.nabble.com/%22Special%22-LS-estimation-problem-tp25083103p25094080.html Sent from the R help mailing list archive at Nabble.com.