I'm quite new to R so this is a problem that I am encountering as a problem.
I currently using a Markov Chain from the 1st iteration to the 45th. I need to use the subsequent n and n+1 iteration in a function like this:
bk<-function(n)((B[(n+1)]-B[n])/(B[n]-0.1818182))
However, I need it such that it only computes it so that N is only and ONLY an integer. I tried using a for loop but it does not seem to work properly.
for (n in 0:45){
bk<-function(n)(B[(n+1)]-B[n])/(B[n]-0.1818182)
if(bk=0)
next
print(bk)
}
The for loop tells me that "Error: unexpected '}' in " }"
(What I want is the function to compute N, IFF N is an integer. When N is not an integer the resulting value is 0, and that is why I am using a for loop to eliminate such values)
This is what B is
B
[1] 1.0000000 0.8000000 0.6600000 0.5560000 0.4764000 0.4145600 0.3661360 0.3280400 0.2979758 0.2741956 0.2553518
[12] 0.2403975 0.2285151 0.2190636 0.2115391 0.2055441 0.2007647 0.1969524 0.1939101 0.1914812 0.1895416 0.1879922
[23] 0.1867542 0.1857649 0.1849741 0.1843419 0.1838365 0.1834324 0.1831093 0.1828509 0.1826442 0.1824789 0.1823467
[34] 0.1822409 0.1821564 0.1820887 0.1820346 0.1819913 0.1819567 0.1819290 0.1819068 0.1818891 0.1818749 0.1818636
[45] 0.1818545 0.1818472
>
##Where B Comes from
B<-c(intialstate1[,2])
The Markov Chain was made using the Markov Chain Package
MCMolFrac<-new("markovchain",
states=c("B","C","D","E"),
transitionMatrix=matrix(data=c(0.8,0.2,0,0,0.1,0.6,0.2,0.1,0,0.2,0.8,0,0,0.4,0,0.6),byrow =TRUE,ncol=4),
name= "Unimolecular Reaction Distribution")
initialstate<-c(1,0,0,0)
fvals<-function(mchain,initialstate,n) {
out<-data.frame()
names(initialstate)<-names(mchain)
for (i in 0:n)
{
iteration<-initialstate*mchain^(i)
out<-rbind(out,iteration)
}
t<-cbind(out, i=seq(0,n))
out<-out[,c(4,1:3)]
return(out)
}
intialstate1<-data.frame(fvals(mchain = MCMolFrac, initialstate= c(1,0,0,0),n=45))
##leadingtoVectorBformationAbove
Essentially, I need to create a discrete time dynamical system. Such that I can make the output into a matrix/vector so I can graph it to look something like
Graph of Final Product, the different colours are different datasets
Thank you