Consider how I fit a Markov chain to my data with R:
library(markovchain)
library(dplyr)
library(ggplot2)
library(data.table)
#Data
A<-structure(c("sunny", "sunny", "sunny", "sunny", "sunny", "sunny",
"rain", "cloudy", "rain", "cloudy", "sunny", "cloudy", "cloudy",
"cloudy", "cloudy", "sunny", "sunny", "sunny", "sunny", "rain",
"sunny", "rain", "sunny", "sunny", "rain", "cloudy", "rain",
"sunny", "sunny", "cloudy", "rain", "cloudy", "rain", "sunny",
"rain", "rain", "rain", "sunny", "cloudy", "cloudy", "cloudy",
"cloudy", "cloudy", "cloudy", "sunny", "cloudy", "rain", "rain",
"cloudy", "cloudy", "sunny", "sunny", "cloudy", "cloudy", "cloudy"
), .Dim = c(5L, 11L), .Dimnames = list(NULL, c("time1", "time2",
"time3", "time4", "time5", "time6", "time7", "time8", "time9",
"time10", "time11")))
#estimate transition matrix
B<-markovchainFit(data=A,name="weather")
mcWeather<-B$estimate
##### Do the forecasting over time and find uncertainty due to small sampling size
KKK<-list()
for(j in 1:10000){
LL<-list()
for(i in 1:5){
LL[[i]]<-data.frame(cat=rmarkovchain(n = 10, object = mcWeather, t0 = "sunny",include.t0 = TRUE),index=i,time=1:11)
}
LLL<-rbindlist(LL)
KKK[[j]]<-LLL %>% group_by(time,cat) %>% summarize(freq=n()/i)
KKK[[j]]$perm=j
}
KOO<-rbindlist(KKK)
KKX<-KOO %>% group_by(time,cat) %>% summarize(mean=mean(freq),lq=quantile(freq,0.025),up=quantile(freq,0.975))
# Plot results
ggplot(KKX,aes(x=time,y=mean,color=cat))+geom_line()+ geom_ribbon(aes(ymin=lq, ymax=up),color="grey",alpha=0.3)+facet_wrap(~cat)
Some more detail to the code: I have 5 individuals that show a sequence of states over time, which can be expressed as a Markov chain.
I fit a Markov chain model to my data to obtain my transition matrix. With this I can now forecast the expected probabilities or expected distribution of my states over time. E.g., via:
W0<-t(as.matrix(c("cloudy"=0,"rainy"=0,"sunny"=1))) #start category sunny
for (time in 1:10){
W0 * (B$estimate ^ time)
}
But if I repeated my experiment with another 5 individuals I would not necessarily observe my expected distribution of the states over time, because this can be seen as 5 random draws of my Markov chain. These are not enough samples to hit the expected distribution perfectly. With this simulation I try to account for that by 10000 times draw sequences for 5 individuals and calculate the uncertainty of the distribution of my states over time. With this I can account for the uncertainty due to small sampling size (low number of individuals) and better compare different experiments all based on 5 individuals.
So with this code I have to some extent accounted for uncertainty of the small sampling size of 5. (See how confidence increases when changing to for(i in 1:5000){..
right? Or is this way wrong already?)
Now my question is: Does my estimated transition matrix—the one I used for the simulation above—not already have some uncertainty? Each entry of the transition matrix is estimated from very few observations (sequences of 5 individuals) as well. I saw the function markovchainFit() includes confidence interval estimates for the matrix entries, but I don't know how to link and combine this to the uncertainty estimation I already have done, so that in the end I get a 'global' estimation of the uncertainty in the forecast including (1) the uncertainty in the probability estimates of the transition matrix I fitted to my data and (2) the uncertainty I simulate above due to small sample size.