Rstan not recognizing data names read from file

I run Stan via rstan in RStudio on a Dell Latitude running Windows 7. This morning while modifying code for a Stan program that otherwise had compiled and run fine, RStudio crashed and automatically restarted, after running the modified program again and crashing RStudio again, rstan no longer compiled or ran previous programs. The error I received was “The procedure entry point R_ContinueUnwind cound not be located in the dynamic link library R.dll.”
I eventually was able to correct this by uninstalling R,Rstudio, Rtools, and rstan, and installing the newest versions. Stan programs I had previously created that hard-coded the data in the rstan .R file ran, but NOT any programs that read the data from an external data file. For columnar numeric data such as for simple linear regression problems, I usually do this by putting the data in .txt file and read it in in the .R file using “FileName<-read.table(filename.txt”, header = TRUE), then assign labels to the data columns using “Column1<-FileName$Column1name”.
Example .txt, .R, and .stan files are attached. When I run the .R file in RStudio I receive the following error message:
Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
Exception: variable does not exist; processing stage=data initialization;
variable name=N; base type=int (in ‘model20308db6833_LWTest’ at line 4)

failed to create the sampler; sampling not done

I am unsure how to proceed to resolve this issue.

Here are the files I ran to test this:

LWTest.R
#Regress wieght on length data:
LW<-read.table(“Pops1LW.txt”, header = TRUE)
N<-nrow(LW)
L<-LW$Len
W<-LW$weight

stanDat <- list(L , W , N )

#sample posterior:
LWTestfit <- stan(file=“LWTest.stan”, data = stanDat, iter = 100, chains = 1)

LWTest.stan:
//LW1H.stan length-weight regression for one population
// NG started Jan 1 2018
data {
int<lower=1>N; //number of data points in each population sample
real L[N];
real W[N];
}
transformed data {
real alpha;
alpha = -11.10;
}
parameters {
real <lower=2> beta; //population-specific slope parameters
real <lower=0> sigma; //population-specific process error of the weight-length relationship.
}
model {
// priors
beta ~ uniform (2.8, 3.2);
sigma ~ uniform(0.001, 1.00);
// likelihood:
for (i in 1:N) {
W[i] ~ normal(alpha + L[i]*beta, sigma);
}

}

Pops1LW.txt:

Len weight
5.3570 4.4493
4.7500 2.8234
5.1255 3.5002
5.0519 3.5995
4.6194 2.2938
4.9449 3.0728
4.7830 2.6920
5.2746 4.1916
4.9225 3.4967
5.0963 3.9187
4.7865 2.6104
5.1489 4.0971
4.8794 3.0868
5.1845 3.8905
5.2079 4.0358
5.2459 4.0537
5.0374 3.4587
4.7010 2.6471
4.8479 3.0641
5.3452 4.5033
4.7731 2.7738
5.2938 4.0921
5.1035 3.7342
5.3915 4.6585
4.6948 2.5292
5.0312 3.5568
4.7256 2.6420
5.3726 4.4105
4.6107 2.2664
5.2626 4.0440

This needs to be a named list, so

stanDat <- list(L = L, W = W, N = N)
1 Like

That appears to have solved it. Thanks again!