1

I am attempting to use knitr trough Rstudio to document a model that save a text string to a *txt file.

When doing so, i get this R markdown error message:

*Error in parse(text = x, srcfile = src) : <text>:2:24: unexpected 
INCOMPLETE_STRING 14: var.m <- 1/tau.m # between-trial variance 15: 
Calls: <Anonymous> ... <Anonymous> -> parse_all -> parse_all.character -> parse*

Anyone know to fix this?

This string works fine:

  Modelstring.baseline = " Text goes here "

This string works fine:

Modelstring.baseline =  "

# Binomial likelihood, logit link, MTC
# Fixed effect model
#CV mortality

model{                                                                          # *** PROGRAM STARTS
  for(i in 1:ns){                                                                 # LOOP THROUGH STUDIES
    mu[i] ~ dnorm(0,.0001)                                                          # vague priors for all trial baselines
    for (k in 1:na[i]) {                                                            # LOOP THROUGH ARMS
      r[i,k] ~ dbin(p[i,k],n[i,k])                                                    # binomial likelihood
      logit(p[i,k]) <- mu[i] + d[t[i,k]]-d[t[i,1]]                                    # model for linear predictor
      rhat[i,k] <- p[i,k] * n[i,k]                                                    # expected value of the numerators
      dev[i,k] <- 2 * (r[i,k] * (log(r[i,k])-log(rhat[i,k]))                          # Deviance contribution
                       + (n[i,k]-r[i,k]) * (log(n[i,k]-r[i,k]) - log(n[i,k]-rhat[i,k])))
    }
    resdev[i] <- sum(dev[i,1:na[i]])                                                # summed residual deviance contribution for this trial
  }
  totresdev <- sum(resdev[])                                                          # Total Residual Deviance
  d[1]<- 0                                                                                # treatment effect is zero for reference treatment
  for (k in 2:nt)  { d[k] ~ dnorm(0,.0001) }                                      # vague priors for treatment effects
 "

Whiles this string generate a parser error:

Modelstring.baseline = "

model{                                                                        # *** PROGRAM STARTS
  for (i in 1:ns)
    {                                                                   # LOOP THROUGH STUDIES
      r[i] ~ dbin(p[i],n[i])                                            # Likelihood
      logit(p[i]) <- mu[i]                                              # Log-odds of response
      mu[i] ~ dnorm(m,tau.m)                                          # Random effects model
    }

  mu.new ~ dnorm(m,tau.m)                                           # predictive dist. (log-odds)
  m ~ dnorm(0,.0001)                                                    # vague prior for mean
  var.m <- 1/tau.m                                                    # between-trial variance

#---Non-informative prior
  #tau.m <- pow(sd.m,-2)                                            
  #sd.m ~ dunif(0,5)

#---Vaguely informative prior
  #tau.m ~ dgamma(0.001,.001)                                           
  #sd.m ~ pow(tau.m,-0.5)

#---Informative prior R.M Turner et al LN(-3.95, 1.79)
  tau.m <- 1/tausq
  tausq ~ dlnorm(-3.95, 0.31) #0.31 = 1/(1.79*1.79)
} 
" 
8
  • Did you notice the ' after writeLines?
    – CL.
    Mar 1, 2016 at 15:00
  • Thank you for pointing that out! The ' was an artifact of the SO edit and was not in the original code. Mar 1, 2016 at 15:08
  • Then your problem is not reproducible. Please turn your code into a minimal reproducible example.
    – CL.
    Mar 1, 2016 at 15:13
  • Modelstring has an uppercase M when you create it, lowercase when you write it. Other than that, it compiles fine
    – scoa
    Mar 1, 2016 at 16:54
  • 1
    Sorry but this is still not reproducible. Or do you see an error when copying the string presented in the question in a RMD document? (I don't.) Please add a complete but minimal document that produces the error.
    – CL.
    Mar 2, 2016 at 9:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.