Whitespace

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Whitespace

Miguel Angel Hombrados Herrera
Hello


Ive been working on a stan program in Rstudio. Im kind of new on this, so probably my question is trivial, However I was not able to find information about this.

The error Im getting when I run my stan code is:


PARSER EXPECTED: whitespace to end of file.
FOUND AT line 2:


The code is:

iter=500
alphamcmc=matrix(0,ncol=J,nrow=iter)
betamcmc=NULL
mu_alpha=NULL
sigma_alpha_2=NULL
sigma_y_2=NULL
#set initial values
alphamcmc[1,]=rep(mean(y),J)
betamcmc[1]=70
mu_alpha[1]=mean(y)
sigma_alpha_2[1]=300
sigma_y_2[1]=350
#mcmc iteration
for(m in 2:iter){
#update alpha vector
for(j in 1:J){
sj=sum(source==j)
var=1/(sj/sigma_y_2[m-1]+1/sigma_alpha_2[m-1])
temp=0
for(i in 1:N){temp=temp+1*(source[i]==j)*(y[i]-betamcmc[m-1]*x[i])}
#sum up (y_i-beta x_i ) for those belonging to group j
mean=var*(temp/sigma_y_2[m-1]+mu_alpha[m-1]/sigma_alpha_2[m-1])
alphamcmc[m,j]=rnorm(1,mean,sqrt(var))
}
#update beta
var=sigma_alpha_2[m-1]/(sum(x^2))
mean=sum(x%*%(y-alphamcmc[m,source])/sum(sum(x^2)))
betamcmc[m]=rnorm(1,mean,sqrt(var))
#update mu_alpha
#update sigma_alpha_2
sigma_alpha_2[m]=rinvgamma(1,shape=J/2,rate=sum((alphamcmc[m,]
-mu_alpha[m])^2/2))
#update sigma_y_2
sigma_y_2[m]=rinvgamma(1,shape=N/2,rate=sum((y-alphamcmc[m,source]-
betamcmc[m]*x)^2/2))
}
nburn=200
apply(alphamcmc[(nburn+1):iter,],2,mean)
apply(betamcmc[(nburn+1):iter],2,mean)

Ive been searching for wrong spaces or tabulations, but I was not able to find anything.

I really would appreciate your help.
Thaknk you.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Whitespace

Jim Lemon-4
Hi Miguel,
You don't seem to have defined "J" before line 2. Maybe that is the problem.

Jim


On Mon, May 1, 2017 at 2:57 PM, Miguel Angel Hombrados Herrera
<[hidden email]> wrote:

> Hello
>
>
> Ive been working on a stan program in Rstudio. Im kind of new on this, so probably my question is trivial, However I was not able to find information about this.
>
> The error Im getting when I run my stan code is:
>
>
> PARSER EXPECTED: whitespace to end of file.
> FOUND AT line 2:
>
>
> The code is:
>
> iter=500
> alphamcmc=matrix(0,ncol=J,nrow=iter)
> betamcmc=NULL
> mu_alpha=NULL
> sigma_alpha_2=NULL
> sigma_y_2=NULL
> #set initial values
> alphamcmc[1,]=rep(mean(y),J)
> betamcmc[1]=70
> mu_alpha[1]=mean(y)
> sigma_alpha_2[1]=300
> sigma_y_2[1]=350
> #mcmc iteration
> for(m in 2:iter){
> #update alpha vector
> for(j in 1:J){
> sj=sum(source==j)
> var=1/(sj/sigma_y_2[m-1]+1/sigma_alpha_2[m-1])
> temp=0
> for(i in 1:N){temp=temp+1*(source[i]==j)*(y[i]-betamcmc[m-1]*x[i])}
> #sum up (y_i-beta x_i ) for those belonging to group j
> mean=var*(temp/sigma_y_2[m-1]+mu_alpha[m-1]/sigma_alpha_2[m-1])
> alphamcmc[m,j]=rnorm(1,mean,sqrt(var))
> }
> #update beta
> var=sigma_alpha_2[m-1]/(sum(x^2))
> mean=sum(x%*%(y-alphamcmc[m,source])/sum(sum(x^2)))
> betamcmc[m]=rnorm(1,mean,sqrt(var))
> #update mu_alpha
> #update sigma_alpha_2
> sigma_alpha_2[m]=rinvgamma(1,shape=J/2,rate=sum((alphamcmc[m,]
> -mu_alpha[m])^2/2))
> #update sigma_y_2
> sigma_y_2[m]=rinvgamma(1,shape=N/2,rate=sum((y-alphamcmc[m,source]-
> betamcmc[m]*x)^2/2))
> }
> nburn=200
> apply(alphamcmc[(nburn+1):iter,],2,mean)
> apply(betamcmc[(nburn+1):iter],2,mean)
>
> Ive been searching for wrong spaces or tabulations, but I was not able to find anything.
>
> I really would appreciate your help.
> Thaknk you.
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Whitespace

barry rowlingson
In reply to this post by Miguel Angel Hombrados Herrera
That looks like an error from Stan parsing your stan file, but the
code you put in the message doesn't look like Stan code, it looks like
R code, so maybe you've tried to parse your R code with Stan....

There's no mention of stan in the code either, like the rstan package,
so somehow you've got a Stan error message from some R code without
calling stan... Okay, I'm well confused now...





On Mon, May 1, 2017 at 5:57 AM, Miguel Angel Hombrados Herrera
<[hidden email]> wrote:

> Hello
>
>
> Ive been working on a stan program in Rstudio. Im kind of new on this, so probably my question is trivial, However I was not able to find information about this.
>
> The error Im getting when I run my stan code is:
>
>
> PARSER EXPECTED: whitespace to end of file.
> FOUND AT line 2:
>
>
> The code is:
>
> iter=500
> alphamcmc=matrix(0,ncol=J,nrow=iter)
> betamcmc=NULL
> mu_alpha=NULL
> sigma_alpha_2=NULL
> sigma_y_2=NULL
> #set initial values
> alphamcmc[1,]=rep(mean(y),J)
> betamcmc[1]=70
> mu_alpha[1]=mean(y)
> sigma_alpha_2[1]=300
> sigma_y_2[1]=350
> #mcmc iteration
> for(m in 2:iter){
> #update alpha vector
> for(j in 1:J){
> sj=sum(source==j)
> var=1/(sj/sigma_y_2[m-1]+1/sigma_alpha_2[m-1])
> temp=0
> for(i in 1:N){temp=temp+1*(source[i]==j)*(y[i]-betamcmc[m-1]*x[i])}
> #sum up (y_i-beta x_i ) for those belonging to group j
> mean=var*(temp/sigma_y_2[m-1]+mu_alpha[m-1]/sigma_alpha_2[m-1])
> alphamcmc[m,j]=rnorm(1,mean,sqrt(var))
> }
> #update beta
> var=sigma_alpha_2[m-1]/(sum(x^2))
> mean=sum(x%*%(y-alphamcmc[m,source])/sum(sum(x^2)))
> betamcmc[m]=rnorm(1,mean,sqrt(var))
> #update mu_alpha
> #update sigma_alpha_2
> sigma_alpha_2[m]=rinvgamma(1,shape=J/2,rate=sum((alphamcmc[m,]
> -mu_alpha[m])^2/2))
> #update sigma_y_2
> sigma_y_2[m]=rinvgamma(1,shape=N/2,rate=sum((y-alphamcmc[m,source]-
> betamcmc[m]*x)^2/2))
> }
> nburn=200
> apply(alphamcmc[(nburn+1):iter,],2,mean)
> apply(betamcmc[(nburn+1):iter],2,mean)
>
> Ive been searching for wrong spaces or tabulations, but I was not able to find anything.
>
> I really would appreciate your help.
> Thaknk you.
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.