[R] Constructing a transition matrix

Hans Gardfjell hans.gardfjell at emg.umu.se
Wed Dec 7 12:06:44 CET 2005

```If you order your factor levels in your vectors in the order you want in the output,
then the prop.table(prop()) command will give you what you want.

But you have to reorder the factor levels so that the levels commands give the following output:

>levels(trans\$class)
[1] "seed" "veg" "repr"
>levels(trans\$fate)

That means that you  need to include "seed" as a factor level in fate
(even if that level is unused). The prop.table(table()) command
will then produce a 3 by 4 table. Remove the last row (that contains the

Hans Gardfjell
Dept of Ecology and Environmental science
Umeå University, Sweden

Chris Stubben wrote:

Hi again,

I almost figured this out, but still need some help on the last part.

I can use prop.table to get survival probabilities...

A <- t(prop.table( table(trans\$class, trans\$fate),1) )

rep seed veg
rep 0.5000000 0.0000000 1.0000000
veg 0.5000000 0.6666667 0.0000000

so now I just need to format the matrix. I thought I could create a
matrix of zeroes using size class names,

dev<- c("seed","veg", "rep").

A0<-matrix(numeric(9), nrow=3, dimnames=list(dev,dev) )
seed veg rep
seed 0 0 0
veg 0 0 0
rep 0 0 0

but how do I assign values in A to the corresponding rows and columns in
A0? I hope there is an easy solution that I'm overlooking.

seed veg rep
seed 0 0 0
veg 0.67 0 0.5
rep 0 1 0.5

Thanks,

Chris

Chris Stubben wrote:
>/ Hi,
/>/
/>/ I would like to construct a transition matrix from a data frame with
/>/ annual transitions of marked plants.
/>/
/>/ plant<-c(1:6)
/>/ class<-c("seed","seed", "seed", "veg", "rep", "rep")
/>/
/>/ trans<-data.frame(plant, class, fate)
/>/
/>/ plant class fate
/>/ 2 2 seed veg
/>/ 3 3 seed veg
/>/ 4 4 veg rep
/>/ 5 5 rep rep
/>/ 6 6 rep veg
/>/
/>/ I have been using sql queries to do this, but I would like to construct
/>/ the matrix in R since I plan to resample transitions using
/>/ trans[sample(nrow(trans), 6, replace=T), ]
/>/
/>/ I know I can get the original size vector using table()
/>/
/>/ data.matrix(table(trans\$class))
/>/ [,1]
/>/ rep 2
/>/ seed 3
/>/ veg 1
/>/
/>/
/>/ but I don't know how to get counts of each class-fate combination where
/>/ fate does NOT equal dead
/>/
/>/ seed veg = 2
/>/ veg rep = 1
/>/ rep rep = 1
/>/ rep veg = 1
/>/
/>/
/>/ or how to divide the class-fate count by the original class count in
the
/>/ size vector to get survival probabilities
/>/
/>/ seed veg = 2 / 3 seed = 0.67
/>/ veg rep = 1 / 1 veg = 1
/>/ rep rep = 1 / 2 rep = 0.5
/>/ rep veg = 1 / 2 rep = 0.5
/>/
/>/
/>/ or construct the square matrix with rows and columns in the same
/>/ developmental sequence like dev<- c("seed","veg", "rep").
/>/
/>/ seed veg rep
/>/ seed 0 0 0
/>/ veg 0.67 0 0.5
/>/ rep 0 1 0.5
/>/
/>/ Any help or suggestions would be appreciated.
/>/ Thanks,
/>/
/>/
/>/ Chris Stubben
/>/
/>/
/>/ --
/>/ Los Alamos National Lab
/>/ BioScience Division
/>/ MS M888
/>/ Los Alamos, NM 87545
/>/
/>/
/>

```