Getting started and need some help

Operating System: Windows 7
Rstudio version: 1.1.383
R version 3.4.2

I have followed this https://github.com/stan-dev/rstan/wiki/Installing-RStan-on-Windows
and got the value 10 at the end, so I assume I am ok on the install part.

I then opened Rstudio, saved a new script under test.stan
Below is the beginning of the script (I took it from a paper just to test how to run):

library(“rstan”)
// Set up the true parameter values
a = c(.8, 1)
b = c(2, .1)
sigma = .2
// Simulate data
x = (1:1000)/100
N = length(x)
ypred = a[1]*exp(-b[1]*x) + a[2]*exp(-b[2]x)
y = ypred
exp(rnorm(N, 0, sigma))

I ran those lines and got this error:

PARSER EXPECTED: whitespace to end of file.
FOUND AT line 1:
library(“rstan”)
// Set up the true parameter values
a = c(.8, 1)
b = c(2, .1)
sigma = .2
// Simulate data
x = (1:1000)/100
N = length(x)
ypred = a[1]*exp(-b[1]*x) + a[2]*exp(-b[2]x)
y = ypred
exp(rnorm(N, 0, sigma))

Error in stanc(model_code = paste(program, collapse = “\n”), model_name = model_cppname, :
failed to parse Stan model ‘test’ due to the above error.

I am not sure what is wrong, any help would be greatly appreciated.

If I understand correctly, the contents of your test.stan file are as follows:

library(“rstan”)
// Set up the true parameter values
a = c(.8, 1)
b = c(2, .1)
sigma = .2
// Simulate data
x = (1:1000)/100
N = length(x)
ypred = a[1]*exp(-b[1]*x) + a[2]*exp(-b[2]x)
y = ypredexp(rnorm(N, 0, sigma))

That’s not Stan syntax, that’s R syntax.

Thanks for the reply

I just run this part again in RStudio with .R extension

Set up the true parameter values

a <- c(.8, 1)
b <- c(2, .1)
sigma <- .2

Simulate data

x <- (1:1000)/100
N <- length(x)
ypred <- a[1]*exp(-b[1]*x) + a[2]*exp(-b[2]x)
y <- ypred
exp(rnorm(N, 0, sigma))

**And no errors **

and then I ran the below as test.stan inside Rstudio

Fit the model

library(“rstan”)
fit <- stan(“exponentials.stan”, data=list(N=N, x=x, y=y), iter=1000, chains=4)
print(fit, pars=c(“a”, “b”, “sigma”))

And received this error.

PARSER EXPECTED: whitespace to end of file.
FOUND AT line 1:
library(“rstan”)
fit <- stan(“exponentials.stan”, data=list(N=N, x=x, y=y), iter=1000, chains=4)
print(fit, pars=c(“a”, “b”, “sigma”))

Error in stanc(model_code = paste(program, collapse = “\n”), model_name = model_cppname, :
failed to parse Stan model ‘test’ due to the above error.

I would like to run it in Rstudio

First, this isn’t Stan code:

library(“rstan”)
fit <- stan(“exponentials.stan”, data=list(N=N, x=x, y=y), iter=1000, chains=4)
print(fit, pars=c(“a”, “b”, “sigma”))

It’s still R code. Do you have a file called “exponentials.stan”? If so, what does it look like?

Judging from the R code that you’ve posted, you’ve been looking at this: http://www.stat.columbia.edu/~gelman/research/published/stan_jebs_2.pdf

It also looks like you’ve confused the R code and the Stan code in that paper. The two are very different. You’ll probably need to slow down and read the paper more carefully.

1 Like