<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">Hello, </span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"> I have a markov chain function in R which I need to apply to every element of a </span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">large Matrix.Following is the function I have in R.</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">Sincs it is running a bit slow (I need to run this function over a large numeber of timesteps)</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">So, I am to used the Rcpp package to write this part of the code in C++ </span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">I can write second function ("simulateMC") in C++ but the "transitionFun" I could not.</span></font></div>
<div><span style="white-space:nowrap;color:rgb(34,34,34);font-family:arial,sans-serif">The main problem for me is the "sample" function of R, which I could not implement in Rcpp.</span></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">Any help will be greatly appreciated.</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"><br></span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">#################################</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">transitionFun = function(stateNum, oldState, probMatrix){</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"> colnames(probMatrix) = as.character(1:stateNum-1)</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"> rownames(probMatrix) = as.character(1:stateNum-1)</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"><br>
</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"> newState = sample(colnames(probMatrix), 1,</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"> prob = probMatrix[as.character(oldState), ])</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"> return(newState)</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">} </span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"><br></span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"><br></span></font></div><div>
<font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">tranMat = matrix(c(0.9, 0.1, 0.1, 0.9), ncol = 2);</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">stateNum = 2;</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">states = c(0, 1);</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">######Original state a 100x100 matrix of 1s and 0s.</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"><br></span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">simulateMc<- function (oldMatrix, stateNum, transMat) {</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">oldMatrix<- matrix(0, 100, 100);</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"><br>
</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">newMatrix<- matrix(NA, nrow(oldMatrix), ncol(oldMatrix));</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"> for (a in 1:nrow(newMatrix)){</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>for(b in 1:ncol(newMatrix)){</font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"> newMatrix[a, b] = transitionFun(stateNum, oldMatrix[a, b], transMat);</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap"> }</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">}</span></font></div><div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">return(newMatrix);</span></font></div>
<div><font color="#222222" face="arial, sans-serif"><span style="white-space:nowrap">}</span></font></div><div><br></div>