Thanks so much Arun.  If you have time, what reference do you use to learn 
seq_len(nrow…etc and other aspects of your code?

Again. Thanks very much.  Jan Beckstrand

 

From: arun kirshna [via R] [mailto:ml-node+s789695n4664037...@n4.nabble.com] 
Sent: Friday, April 12, 2013 2:01 AM
To: Beckstrand, Janis, NCOD
Subject: Re: processing matrix equation derived from rows of two matrices

 

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) 

______________________________________________ 
[hidden email] 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. 



________________________________

If you reply to this email, your message will be added to the discussion below:

http://r.789695.n4.nabble.com/processing-matrix-equation-derived-from-rows-of-two-matrices-tp4664033p4664037.html
 

To unsubscribe from processing matrix equation derived from rows of two 
matrices, click here 
<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4664033&code=amFuaXMuYmVja3N0cmFuZEB2YS5nb3Z8NDY2NDAzM3wtNjAyODIyMzIx>
 .
NAML 
<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
  





--
View this message in context: 
http://r.789695.n4.nabble.com/processing-matrix-equation-derived-from-rows-of-two-matrices-tp4664033p4664070.html
Sent from the R help mailing list archive at Nabble.com.
        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to