[R] Efficient multinom probs

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Fri Jan 5 11:53:06 CET 2007


maybe

prob[cbind(1:length(y), y)]


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Ingmar Visser" <I.Visser at uva.nl>
To: <R-help at stat.math.ethz.ch>
Sent: Friday, January 05, 2007 11:41 AM
Subject: [R] Efficient multinom probs


> Dear R-helpers,
>
> I need to compute probabilties of multinomial observations, eg by 
> doing the
> following:
>
> y=sample(1:3,15,1)
> prob=matrix(runif(45),15)
> prob=prob/rowSums(prob)
> diag(prob[,y])
>
> However, my question is whether this is the most efficient way to do 
> this.
> In the call prob[,y] a whole matrix is computed which seems a bit of 
> a
> waste.
>
> Is there maybe a vectorized version of dmultinom which does this?
>
> Best, Ingmar
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list