问题
`function(trans,initprob,N)'
{
BrokerPosition <- c("BP", "IP", "SP")
mysequence <- character()
firstposition <- sample(BrokerPosition, 1, rep=TRUE, prob=initprob)
mysequence[1] <- firstposition
for (i in 2:N) {
prevposition <- mysequence[i-1]
probabilities <- trans[prevposition,]
BPosition <- sample(BrokerPosition, 1, rep=TRUE, prob=probabilities)
mysequence[i] <- BPosition
}
return(mysequence)
}
This is a function made to simulate Markov chain , but whenever I run it I get the error no 'dimnames' attribute for array ,any idea why is this happening
回答1:
Your array 'mysequence' is character, so R is trying to find the row with rowname matching mysequence[i-1]. If you don't set rownames on 'trans', this will throw your error. Either use a integer value to select a row from trans, or set the rownames on trans, depending on what you are actually trying to do.
来源:https://stackoverflow.com/questions/16243051/error-no-dimnames-attribute-when-trying-to-assign-to-array