Hi Folks, Can anyone give me the tip I've been groping for with the following question:? mu: kx2x2x2 array of reals corresponding to means of k RVs at the combinations of values (1,2)x(1,2)x(1,2) of dichotomous variables F1,F2,F3 mu prints out as k rows (one for each Xi) of 8 numbers M: N x (3+k) matrix of cases. The first 3 cols are values of (F1,F2,F3) as above (and the remainder are values of (X1,...,Xk) but this doesn't really matter). AIM: For each row of M, find from mu the set of k means mu_1,...,mu_k corresponding to the values of F1, F2, F3 in that row. E.g. if M[1,1:3] -> 2,1,1 I want mu[,2,1,1] BUT: Not to have to do it by mu[,M[1,1],M[1,2],M[1,3]] but more like mu[,M[1,1:3]] -- except that this kind of try does not work, leading to error: "incorrect number of dimensions". So: HOW To construct an object F from M[1,1:3] such that mu[,F] gives mu[,2,1,1] NEXT QUESTION: If the above can be done, I'd really like to be able to "vectorise" it so as to get ti all at once from all the rows of M, rather than having to bind the results of a loop over the rows of M, i.e. achieve what you'd naively expect mu[,M[,1:3]] to do (except that even mu[,M[1,1:3]] won't work). With thanks, Ted.
> Hi Folks, > Can anyone give me the tip I've been groping for with the > following question:? > > mu: kx2x2x2 array of reals corresponding to means of k RVs > at the combinations of values (1,2)x(1,2)x(1,2) > of dichotomous variables F1,F2,F3 > mu prints out as k rows (one for each Xi) of 8 numbersSorry -- this line may have misled. Suppose originally mu is as it describes: F3=1 F3=1 F3=1 F3=1 F3=2 F3=2 F3=2 F3=2 F2=1 F2=1 F2=2 F2=2 F2=1 F2=1 F2=2 F2=2 F1=1 F1=2 F1=1 F1=2 F1=1 F1=2 F1=1 F1=2 mu_1 1.111 1.211 1.121 1.221 1.112 1.212 1.122 1.222 mu_2 2.111 2.211 2.121 2.221 2.112 2.212 2.122 2.222 ... mu_k k.111 k.211 k.121 k.221 k.112 k.212 k.122 k.222 and suppose it is converted into an array: mu<-array(data=mu,dim=c(k,2,2,2)) Now the rest, below, refers to the latter form of mu. Apologies for any confusion, and thanks for any help!> > M: N x (3+k) matrix of cases. The first 3 cols are values > of (F1,F2,F3) as above (and the remainder are values > of (X1,...,Xk) but this doesn't really matter). > > AIM: For each row of M, find from mu the set of k means > mu_1,...,mu_k corresponding to the values of F1, F2, F3 > in that row. E.g. if M[1,1:3] -> 2,1,1 I want mu[,2,1,1] > > BUT: Not to have to do it by mu[,M[1,1],M[1,2],M[1,3]] but more > like mu[,M[1,1:3]] -- except that this kind of try does not > work, leading to error: "incorrect number of dimensions". > > So: > HOW To construct an object F from M[1,1:3] such that > > mu[,F] gives mu[,2,1,1] > > NEXT QUESTION: If the above can be done, I'd really like to be > able to "vectorise" it so as to get ti all at once from all the > rows of M, rather than having to bind the results of a loop over > the rows of M, i.e. achieve what you'd naively expect > > mu[,M[,1:3]] > > to do (except that even mu[,M[1,1:3]] won't work). > > With thanks, > Ted. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
> mu[M[,1:3]] > > should do what you want, I think. See chapter 1 of S Poetry. > > Patrick BurnsThanks Patrick (and honoured to hear from the author of S-Poetry!). This doesn't do what I want (I was probably not clear), though Tony Plate's reply looks promising if maybe cumbersome. (Thanks, Tony). Basically: Given that mu was made by mu<-array(data=x, dim=c(9,2,2,2)) say, where x has 72 reals in it, and given that the first three columns of M contain values (1 or 2) of p,q,r, I want to recover from mu the 9 values mu[,p,q,r] where (p,q,r) is the result of M[i,1:3] for some value of i. I don't want to do this as mu[,M[,1],M[,2],M[,3]] because in fact this is taking place inside a function where the dimensions of the arrays thrown at the function will not be known beforehand. And, if the result can be got for all rows of M in one swoop, instead of looping through the rows of M, all the better! Thanks! Ted.
Well, thanks to all who have come up with ideas and suggestions, and commments about how R and S work, which I must consider! Meanwhile, since it was beginning to appear that there might be no direct method of doing it in R, I went down the same road as Tony Plate's suggestion and gratefully adapted his template to do what I needed. His function was called 'array.pos', to find the location in an array of the element with given coordinates. I'm seeking lots, so marray.pos<-function(i,d){ sum(c(0,i-1)*c(1,cumprod(d[-length(d)]))) + (1:d[1]) } Example: x<-array((1:144),dim=c(6,2,2,3,2)) t<-rbind(c(1,2,2,1),c(2,2,3,1),c(1,2,1,2),...) Then the following returns a matrix with 6 columns, in which each row consists of all the 6 values in x whose coordinates [?,p,q,r,s] satisfy (p=1,q=2,r=2,s=1), then (p=2,q=2,r=3,s=1), etc., until tthe rows of t are exhausted: t(matrix(x[apply(t,1,marray.pos,dim(x))],ncol=nrow(t))) Once again, an interesting thread, and thanks to all who took part! Ted.