[R] Vectorize a series of Matrix Multiplications

2013-06-17 Thread G Vishwanath
Can I have some help in vectorizing a series of matrix multiplications?
Toy Example

mat_size=2; num_matrices=3; num_users=2

ToyArray=array(1,dim=c(mat_size,   mat_size, num_matrices, num_users))
/* So I open an 4-dim array to store 3  2 X 2 matrrices  for 2 users.  For each 
user I want to multiple the 3, 2 X 2 matrices so that  at the end I have 1 
matrix for each of the 2 users */

This works:
 output=array(NA,dim=c(mat_size, mat_size, num_users));

 for ( i in 1:num_users) {
  output[,,i]= as.matrix(ToyArray[,,1,i]) %*% as.matrix(ba[,,2,i]) %*% 
as.matrix(ba[,,3,i])
 };

Can I do better? Can I vectorize this over the user dimension?


And even better (and a different question): what strategy can I use if the 
matrices are not all of the same  size?

GR
[[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.


[R] Matrix Mulitplication: A*B*C*D...?

2013-06-16 Thread G Vishwanath
Is there a R package or function that will multiple an arbitrary number of 
matrices supplied to it, preferably optimizing the sequence

R_function(matA, matB, matC,  matD,)
   return matA %*%  matB %*%  matC %matD %
  
and optimizing wether it is better to do A(BC) or (AB)C ?


Thanks,
Shiv
[[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.


Re: [R] nls and rbinom function: step factor 0.000488281 reduced below 'minFactor' of 0.000976562

2012-01-03 Thread G Vishwanath
...(1000,0,1), I presume.


Yes thank you.


 ...z - nls(y ~ rbinom(1000,1,a+b*x),data=d,start= list(a =0.1,b=0.2),trace=T);


This makes no sense. Random numbers in a model specification 

And maybe that  model spec is nonsense, although it seems to me that it 
correctly incorporates the idea of a linear density function.     But any ideas 
on how you might correctly specify such a model?

Thanks,
GR



 From: peter dalgaard pda...@gmail.com

Cc: r-help@r-project.org r-help@r-project.org 
Sent: Tuesday, January 3, 2012 3:11 AM
Subject: Re: [R] nls and rbinom function: step factor 0.000488281 reduced below 
'minFactor' of 0.000976562


On Jan 3, 2012, at 05:25 , G Vishwanath wrote:

 I  am trying to learn nls using a simple simulation. I assumed that the 
 binomial prob varies linearly as 0.2 + 0.3*x in  x {0,1},
 and the objective is to recover the known parameters a=0.2, b=0.3
 
 ..data frame d has 1000 rows...
 
 d$x-runif(0,1)  

...(1000,0,1), I presume.

            
 
 d$y-rbinom(1000,1,0.2+0.3*d$x) 
 
 table(d$y,cut(d$x,breaks=5));
 
   (-0.000585,0.199] (0.199,0.399] (0.399,0.599] (0.599,0.799] (0.799,0.999]
   0               154           149           130           122           114
   1                34            48            71            76           102
 
  z - nls(y ~ rbinom(1000,1,a+b*x),data=d,start= list(a =0.1,b=0.2),trace=T);
 

This makes no sense. Random numbers in a model specification 

y~a+b*x might give a result, but you're fitting a model which assumes Gaussian 
errors with constant variance to data that are nothing of the sort.


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.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.


[R] nls and rbinom function: step factor 0.000488281 reduced below 'minFactor' of 0.000976562

2012-01-02 Thread G Vishwanath
I  am trying to learn nls using a simple simulation. I assumed that the 
binomial prob varies linearly as 0.2 + 0.3*x in  x {0,1},
and the objective is to recover the known parameters a=0.2, b=0.3

..data frame d has 1000 rows...

d$x-runif(0,1)              

d$y-rbinom(1000,1,0.2+0.3*d$x) 

table(d$y,cut(d$x,breaks=5));

  (-0.000585,0.199] (0.199,0.399] (0.399,0.599] (0.599,0.799] (0.799,0.999]
  0               154           149           130           122           114
  1                34            48            71            76           102

 z - nls(y ~ rbinom(1000,1,a+b*x),data=d,start= list(a =0.1,b=0.2),trace=T);

374 :  0.1 0.2 
361 :  0.1 0.2 
350 :  0.1 0.2 
Error in nls(y ~ rbinom(1000, 1, a + b * x), data = d, start = list(a = 0.1,  : 
  step factor 0.000488281 reduced below 'minFactor' of 0.000976562

I have tried plinear, got the same

Additionaly- why does the parameters not change with the iteration ?
[[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.