Home > Software design >  Unexpected symbol error when trying to run a function
Unexpected symbol error when trying to run a function

Time:11-23

Im trying to run the function below but I get:

Error: unexpected symbol in:
"MMod<-function (Pmat, cycle, n, Init, halfcycle=F, measure, discount=.03)
{cumul<-0 i"


MMod<-function (Pmat, cycle, n, Init, halfcycle=F, measure, discount=.03)
{cumul<-0 i<-1 istate<-Init m<-measure*cycle
if (halfcycle) {cumul<-0.5*(Init%*%m)} while (i <= n)
{   istate<-istate%*%Pmat   imeasure<-istate%*%m   cumul<-cumul imeasure
#print(paste(c(i,round(istate,2),cumul)))   i<-i 1
m<-m*(1-discount*cycle)
}
if (halfcycle) {cumul<-cumul - 0.5*imeasure} return(cumul)} 

then I get more erros but I believe that is the key to run it.

CodePudding user response:

R (among many programming languages) is very particular about this: different expressions must be separated either by a newline or a semi-colon. Try this:

MMod <- function(Pmat, cycle, n, Init, halfcycle = FALSE, measure, discount = 0.03) {
  cumul <- 0
  i <- 1
  istate <- Init
  m <- measure * cycle
  if (halfcycle) {
    cumul <- 0.5 * (Init %*% m)
  }
  while (i <= n) {
    istate <- istate %*% Pmat
    imeasure <- istate %*% m
    cumul <- cumul   imeasure
    #print(paste(c(i,round(istate,2),cumul)))   i<-i 1
    m <- m * (1 - discount * cycle)
  }
  if (halfcycle) {
    cumul <- cumul - 0.5 * imeasure
  }
  return(cumul)
}
  •  Tags:  
  • r
  • Related