[R] transition matrix problem
Thomas W Blackwell
tblackw at umich.edu
Tue Mar 4 00:01:03 CET 2003
Nick -
Ask yourself what happens in min(as.integer(names(cump[rand<(cump*100)])))
when the random number happens to be larger than the first entry in the
relevant column of p.trans. How many elements in the comparison are true ?
In that case, I think you are taking the minimum of zero elements, and the
error message is quite correct.
- tom blackwell - u michigan medical school - ann arbor -
On Tue, 4 Mar 2003, Nick Bond wrote:
> I'm having trouble using some fairly simple code to change the entries
> in a vector (x - the numbers 0-5) according to a simple transition
> matrix that I've called p.dry.
>
> the error message I get is "no finite arguments to min; returning Inf"
>
> The code is
>
> p.trans<-matrix(c(1,0,0,0,0,0,0.7,0.3,0,0,0,0,0,0.6,0.4,0,0,0,0,0,0.5,0.5,0,0,0,0,0,0.4,0.6,0,0,0,0,0,0.3,0.7),6,6)
>
> x<-ceiling(runif(100,0,5))
>
> trans<-function(x) {
> x.new<-vector(,length(x))
> for (i in 1:length(x)) {
> if (x[i]==0) x.new[i]<-0
> else
> cump<-(cumsum(p.trans[,(x[i]+1)])) # +1 b.c p.trans[,1] relates to
> min(x)==0
> names(cump)<-c("0","1","2","3","4","5")
> rand<-ceiling(runif(1,0,100))
> x.new[i]<-min(as.integer(names(cump[rand<(cump*100)])))
> }
> return(x,x.new)
> }
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Dr Nick Bond
> Department of Biological Sciences
> Monash University (Clayton Campus)
> Victoria, Australia, 3800
> Ph: +61 3 9905 5606 Fax: +61 3 9905 5613
> Email: Nick.Bond at sci.monash.edu.au
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the R-help
mailing list