[R] Markov transition matrices , missing transitions for certain years

Abby_UNR abbylawson at gmail.com
Tue Apr 19 02:47:07 CEST 2011


Hi all,
I am working for nest box occupancy data for birds and would like to
construct a Markov transition matrix, to derive transition probabilities for
ALL years of the study (not separate sets of transition probabilities for
each time step). The actual dataset I'm working with is 125 boxes over 14
years that can be occupied by 7 different species, though I have provided a
slimmed down portion for this question...
-
A box can be in 1 of 4 "states" (i.e. bird species): 1,2,3,4
Included here are 4 "box histories" over 4 years (y97, y98, y99, y00)

These are the box histories
> b1 <- c(1,1,4,2)
> b2 <- c(1,4,4,3)
> b3 <- c(4,4,1,2)
> b4 <- c(3,1,1,1)

> boxes <- data.frame(rbind(b1,b2,b3,b4))
> colnames(boxes) <- c("y97","y98","y99","y00")
> boxes
   y97 y98 y99 y00
b1   1   1   4   2
b2   1   4   4   3
b3   4   4   1   2
b4   3   1   1   1
> 

My problem is that there are 16 possible transitions, but not all possible
transitions occur at each time step. Therefore, don't think I could do
something easy like create a table for each time step and add them together,
for example:

> t1.boxes <- table(boxes$y98, boxes$y97)
> t1.boxes
   
    1 3 4
  1 1 1 0
  4 1 0 1
> t2.boxes <- table(boxes$y99, boxes$y98)
> t2.boxes
   
    1 4
  1 1 1
  4 1 1
> 

t1.boxes and t2.boxes could not be added together to calculate the frequency
of each transition occurring because they are of different dimensions. I'm
not quite sure how to deal with this, I have attempted to write a function
(shown below), though I'm not sure if it is needed, I am a bit new the
programming world. If I could get some help either with the function or a
way around it that would be most appreciated! Thank you!

--------------
Function requires the commands already listed above:

FMAT <- matrx(0, nrow=4, ncol=4, byrow=TRUE)
#This is the matrix that will store the frequency of each possible
transition occurring over the 4 years

nboxes <- 4
nyears <- 4

for(row in 1:nboxes)
{
for(col in 1:(nyears-1))
{
FMAT[boxes[row,col+1], boxes[row,col]] <- boxes[boxes[row, col+1],
boxes[row,col]]
#This is the line of code I have been struggling with an am unsure about. I
have tried
#various versions of this and keep getting an assortment of error messages.
}
}

FMAT

}

--------------------------


--
View this message in context: http://r.789695.n4.nabble.com/Markov-transition-matrices-missing-transitions-for-certain-years-tp3459072p3459072.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list