[R] processing matrix equation derived from rows of two matrices
arun
smartpink111 at yahoo.com
Fri Apr 12 19:57:30 CEST 2013
Hi Janis,
For example
The goal here is to get results per row
BTW, your original posting had tb[,1]
tb[,1]%*%(((val-rep(meansb79[1,],5))^2)/6)
#Error in tb[, 1] %*% (((val - rep(meansb79[1, ], 5))^2)/6) :
# non-conformable arguments
I guess it should be:
tb[1,]%*%(((val-rep(meansb79[1,],5))^2)/6)
# [,1]
#[1,] 1.47619
If you can split the codes in the first solution:
seq_len(nrow(tb))
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#[26] 26 27 28 29 30 31 32 33 34 35 36
sapply(seq_len(nrow(tb)),function(i) i)
#[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#[26] 26 27 28 29 30 31 32 33 34 35 36
#you can also do with lapply (makes it more easy to understand)
lapply(seq_len(nrow(tb)),function(i) i)[1:2]
#[[1]]
#[1] 1
#[[2]]
#[1] 2
lapply(seq_len(nrow(tb)),function(i) tb[i,])[1:2]
#[[1]]
#[1] 1 1 1 4 0
#[[2]]
#[1] 1 1 1 3 1
tb[1:2,]
# [,1] [,2] [,3] [,4] [,5]
#[1,] 1 1 1 4 0
#[2,] 1 1 1 3 1
lapply(seq_len(nrow(tb)),function(i) (((val-rep(meansb79[i,],5))^2/6)))[1:2]
#[[1]]
#[1] 0.765306122 0.217687075 0.003401361 0.122448980 0.574829932
#[[2]]
#[1] 0.87074830 0.27551020 0.01360544 0.08503401 0.48979592
(((val-rep(meansb79[1:2,],5))^2/6))
# [1] 0.765306122 0.275510204 0.003401361 0.085034014 0.574829932 0.870748299
#[7] 0.217687075 0.013605442 0.122448980 0.489795918
lapply(seq_len(nrow(tb)),function(i)tb[i,] %*%(((val-rep(meansb79[i,],5))^2/6)))[1:2]
#[[1]]
# [,1]
#[1,] 1.47619
#[[2]]
# [,1]
#[1,] 1.904762
sapply(seq_len(nrow(tb)),function(i)tb[i,] %*%(((val-rep(meansb79[i,],5))^2/6)))[1:2]
#[1] 1.476190 1.904762
Hope it helps.
A.K.
----- Original Message -----
From: "Beckstrand, Janis, NCOD" <Janis.Beckstrand at va.gov>
To: smartpink111 at yahoo.com
Cc:
Sent: Friday, April 12, 2013 1:05 PM
Subject: FW: [R] processing matrix equation derived from rows of two matrices
Thanks so much Arun. If you have time, what reference(s) do you use to
learn the techniques like "seq_len(nrow" etc and other aspects of your
code?
Again. Thanks very much. Jan Beckstrand
From: arun kirshna [via R]
[mailto:ml-node+s789695n4664037h93 at 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-ro
ws-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=unsubscrib
e_by_code&node=4664033&code=amFuaXMuYmVja3N0cmFuZEB2YS5nb3Z8NDY2NDAzM3wt
NjAyODIyMzIx> .
NAML
<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_view
er&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.Bas
icNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.tem
plate.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml
-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemai
l.naml>
--
View this message in context:
http://r.789695.n4.nabble.com/processing-matrix-equation-derived-from-ro
ws-of-two-matrices-tp4664033p4664070.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]
More information about the R-help
mailing list