[R] transition matrix problem
Nick Bond
Nick.Bond at sci.monash.edu.au
Mon Mar 3 23:06:03 CET 2003
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"
Any suggestions as to where I'm going wrong greatly appreciated. Sorry
the message is lengthy!
cheers
Nick
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)
}
Basically, in the transition matrix, the columns represent the current
cell entry in the vector (e.g. column 1 represents 0 (in the vector),
and the rows in the matrix are the possible transition states. Hence,
cell entries in the transition matrix are the probabilities associated
with each transition.
The code works on the cumulative distribution of the relevant column of
the transition matrix, which is determined by the current cell value in
x.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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