0

I have been trying to calculate the number of following events in a month say January, 1950 to form transition probability matrix of Markov chain:

E00 = dry day after dry day
E01 = wet day after dry day
E10 = dry day after wet day
E11 = wet day after wet day

Dry day means rainfall = 0 and wet day means rainfall>0.

How can I calculate these above using R? I have tried my best to write a function with logical statements but cannot. Could anyone please help me?

data:

day,month,year,rainfall
1,01,1950,0.586212158
2,01,1950,1.202758789
3,01,1950,0
4,01,1950,0
5,01,1950,0
6,01,1950,0.183441162
7,01,1950,4.675704956
8,01,1950,9.258003235
9,01,1950,3.425468445
10,01,1950,0
11,01,1950,0
12,01,1950,8.39233E-05
13,01,1950,0
14,01,1950,0
15,01,1950,0.641731262
16,01,1950,9.325141907
17,01,1950,5.588417053
18,01,1950,0.903343201
19,01,1950,3.892127991
20,01,1950,0.133895874
21,01,1950,0.102630615
22,01,1950,5.334564209
23,01,1950,1.017593384
24,01,1950,0
25,01,1950,0.621734619
26,01,1950,0.957794189
27,01,1950,3.131622314
28,01,1950,4.868865967
29,01,1950,0.169281006
30,01,1950,0
31,01,1950,0
3
  • Do you mean "How do I estimate a transition matrix from data like these?"
    – Glen_b
    Sep 16, 2013 at 5:28
  • Comments about programming are better asked on Stack Overflow.
    – user2005253
    Sep 16, 2013 at 6:03
  • Thank you, Glen_b and user2005253. I have got it.
    – PyLabour
    Nov 1, 2013 at 12:18

1 Answer 1

3

One way to do what I assume you're after (it's possible to be briefer):

# assuming the data is read into a data frame called "raindata"
rf <- raindata$rainfall
n <- length(rf)
rainedf <- as.factor(ifelse(rf,0,1))
trf <- table(data.frame(yesterday=rainedf[1:(n-1)],today=rainedf[2:n]))
trf/rowSums(trf)
2
  • Would it be possible to do this also for a higher markov chain? How would it look like then?
    – Developer
    Mar 2, 2017 at 18:04
  • If you mean "higher order" then 1. Yes, it's possible 2. the matrix will be a lot bigger, because you make pairs of rainy-day events as source states, akin to the second matrix here and so the line with table must work with those previous.two.days states in place of yesterday (you'd also want a lot more data to support that estimation).
    – Glen_b
    Mar 3, 2017 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.