# [R] processing matrix equation derived from rows of two matrices

arun smartpink111 at yahoo.com
Fri Apr 12 07:59:25 CEST 2013

Hi,

May be this helps:

tb[1,]%*%(((val-rep(meansb79[1,],5))^2)/6)
#        [,1]
#[1,] 1.47619
tryvarb<-c(1,2,3,4,4,4,4)
var(tryvarb)
#[1] 1.47619

tb[2,]%*%(((val-rep(meansb79[2,],5))^2)/6)
#         [,1]
#[1,] 1.904762

sapply(seq_len(nrow(tb)),function(i) tb[i,]%*%(((val-rep(meansb79[i,],5))^2/6)))
# [1] 1.4761905 1.9047619 1.9047619 1.9047619 1.9047619 2.2857143 1.9047619
# [8] 1.9047619 2.2857143 2.2857143 1.9047619 1.9047619 2.2857143 2.2857143
#[15] 2.2857143 1.9523810 1.9523810 2.2857143 2.2857143 2.2857143 2.2857143
#[22] 1.6190476 1.6190476 1.9047619 1.9047619 1.9047619 1.9047619 1.8095238
#[29] 0.9047619 0.9047619 1.1428571 1.1428571 1.1428571 1.1428571 1.0000000
#[36] 0.4761905

#or
mat1<-matrix(((val-rep(meansb79,each=5))^2/6),ncol=5,byrow=TRUE)

diag(t(apply(tb,1,function(x) x))%*% apply(mat1,1,function(x) x))
# [1] 1.4761905 1.9047619 1.9047619 1.9047619 1.9047619 2.2857143 1.9047619
#[8] 1.9047619 2.2857143 2.2857143 1.9047619 1.9047619 2.2857143 2.2857143
#[15] 2.2857143 1.9523810 1.9523810 2.2857143 2.2857143 2.2857143 2.2857143
#[22] 1.6190476 1.6190476 1.9047619 1.9047619 1.9047619 1.9047619 1.8095238
#[29] 0.9047619 0.9047619 1.1428571 1.1428571 1.1428571 1.1428571 1.0000000
#[36] 0.4761905

#or
diag(apply(tb,1,function(x) x%*% t(mat1)))
# [1] 1.4761905 1.9047619 1.9047619 1.9047619 1.9047619 2.2857143 1.9047619
#[8] 1.9047619 2.2857143 2.2857143 1.9047619 1.9047619 2.2857143 2.2857143
#[15] 2.2857143 1.9523810 1.9523810 2.2857143 2.2857143 2.2857143 2.2857143
#[22] 1.6190476 1.6190476 1.9047619 1.9047619 1.9047619 1.9047619 1.8095238
#[29] 0.9047619 0.9047619 1.1428571 1.1428571 1.1428571 1.1428571 1.0000000
#[36] 0.4761905

A.K.

>I have a matrix (tb) that represents all samples of size n=7 from a group of N=9, with scores c(1,2,3,4,4,4,4,5,5).  I want to
>calculate the variance for every sample.  Here is my code.  The
bottom shows the matrix equations and an attempt to process it for each
row. I got >the strategies from reading the r-help, but neither works. (I
do not understand the syntax well enough.) Any suggestions.  (I need to
do may :>additional matrices in the same way.) Thanks. Jan

>require(combinat)
>n=7
>base79<-combn(c(1,2,3,4,4,4,4,5,5), n, tabulate,nbins=5) #find
all samples,n=7 (gives cols of  7 cases  in sample like 1 1 1 4 0 )
>tb<-t(base79)
>val<-c(1,2,3,4,5)  #values on the scale
>meansb79<-t(base79)%*% (val/7)

>tb[ ,1])%*%(((val-rep(meansb79[1,],5))^2)/6)   #computes the sample variance for the first sample
#check
>tryvarb<-c(1,2,3,4,4,4,4)
>var(tryvarb)

#Now I try to get the variance for each sample (row) in tb, but neither of the following attempts work.

>trybase <- apply(tb,1,function(i)
>t(base79[,i])%*%(((val-rep(meansb79[i,],5))^2)/6))

#or

>domatrix.f <- function(tb, meansb79) {
>   a <- nrow(A); b <- nrow(B);
>C <- matrix(NA, nrow=a, ncol=b);
>for (i in 1:a)
> for (j in 1:b)
> C[i,j] <- t(A[,i])%*%(((val-rep(B[i,],5))^2)/6) }
>domatrix.f(tb, meansb79)