A Basic bivariate regression model

108 views
Skip to first unread message

krishna mohan

unread,
Oct 10, 2016, 12:10:28 AM10/10/16
to Stan users mailing list
All

To learn Stan and R Stan, I began by setting up a basic bivariate regression model. I got the following errors. Can some one help me identify the source of the errors?
I'm going to include my code here.

First the "stan" file called "media.stan"

data {
int<lower=0> N;  //have 100 simulated data points
vector[N] x;       // one predictor
vector[N] y;       // single dependent
}
parameters {
real alpha;
real beta;
real<lower=0> sigma;
}
model {
y ~ normal(alpha + beta * x, sigma);
}

My "R" files called "media.R" is as follows:

library(rstan)

setwd("C:/Users/Documents/Techniques/HBayes/RStan/example/")

mdata <- read.csv("media1.csv", header=TRUE)

head(mdata, n=3)

fit1 <- stan(
  file = "media.stan",    # Stan program
  data = mdata,           # named list of data
  chains = 4,             # number of Markov chains
  warmup = 1000,          # number of warmup iterations per chain
  iter = 3000,            # total number of iterations per chain
  cores = 1,              # number of cores (using 1 just for the vignette)
  refresh = 1000          # show progress every 'refresh' iterations
  )

Now for the R session errors I got:

In file included from C:/Users/Documents/R/win-library/3.3/BH/include/boost/multi_array/base.hpp:28:0,

                 from C:/Users/Documents/R/win-library/3.3/BH/include/boost/multi_array.hpp:21,

                 from C:/Users/Documents/R/win-library/3.3/BH/include/boost/numeric/odeint/util/multi_array_adaption.hpp:29,

                 from C:/Users/Documents/R/win-library/3.3/BH/include/boost/numeric/odeint.hpp:61,

                 from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math/prim/arr/functor/integrate_ode_rk45.hpp:13,

                 from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math/prim/arr.hpp:36,

                 from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math/prim/mat.hpp:235,

                 from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/mat.hpp:9,

                 from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math.hpp:4,

                 from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/src/stan/model/model_header.hpp:4,

                 from file136859821533.cpp:8:

C:/Users/Documents/R/win-library/3.3/BH/include/boost/multi_array/concept_checks.hpp: In static member function 'static void boost::multi_array_concepts::detail::idgen_helper<N>::call(Array&, const IdxGen&, Call_Type)':

C:/Users/Documents/R/win-library/3.3/BH/include/boost/multi_array/concept_checks.hpp:42:43: warning: typedef 'index_range' locally defined but not used [-Wunused-local-typedefs]

       typedef typename Array::index_range index_range;

                                           ^

C:/Users/Documents/R/win-library/3.3/BH/include/boost/multi_array/concept_checks.hpp:43:37: warning: typedef 'index' locally defined but not used [-Wunused-local-typedefs]

       typedef typename Array::index index;

                                     ^

C:/Users/Documents/R/win-library/3.3/BH/include/boost/multi_array/concept_checks.hpp: In static member function 'static void boost::multi_array_concepts::detail::idgen_helper<0ull>::call(Array&, const IdxGen&, Call_Type)':

C:/Users/Documents/R/win-library/3.3/BH/include/boost/multi_array/concept_checks.hpp:53:43: warning: typedef 'index_range' locally defined but not used [-Wunused-local-typedefs]

       typedef typename Array::index_range index_range;

                                           ^

C:/Users/Documents/R/win-library/3.3/BH/include/boost/multi_array/concept_checks.hpp:54:37: warning: typedef 'index' locally defined but not used [-Wunused-local-typedefs]

       typedef typename Array::index index;

                                     ^

In file included from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core.hpp:42:0,

                 from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/mat.hpp:4,

                 from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math.hpp:4,

                 from C:/Users/Documents/R/win-library/3.3/StanHeaders/include/src/stan/model/model_header.hpp:4,

                 from file136859821533.cpp:8:

C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp: At global scope:

C:/Users/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:17: warning: 'void stan::math::set_zero_all_adjoints()' defined but not used [-Wunused-function]

     static void set_zero_all_adjoints() {

                 ^

Error in eval(substitute(expr), envir, enclos) :

  variable does not exist; processing stage=data initialization; variable name=N; base type=int

In addition: Warning message:

In readLines(file, warn = TRUE) :

  incomplete final line found on 'C:\Users\Documents\Techniques\HBayes\RStan\example\media.stan'

failed to create the sampler; sampling not done

Bob Carpenter

unread,
Oct 10, 2016, 12:22:51 AM10/10/16
to stan-...@googlegroups.com
From this:

variable does not exist; processing stage=data initialization; variable name=N; base type=int

it looks like you're not supplying the value of N. Makes sense if
your mdata comes from a read.csv. Stan requires all the variables
declared as data to be defined in the data list.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

krishna mohan

unread,
Oct 10, 2016, 2:31:39 PM10/10/16
to Stan users mailing list
Thanks Bob for your feedback. I have gone back and corrected my and it ran correctly. I got huge help from one of pdf files concerning the use of R and Stan (from Andrew Gelman's book portion - Appendix C). 


In the 8 schools example, the data to be read in is saved in a CSV file and read from it. The code subsequently shows how to get the different variables and string the list together. That allowed me to understand your comment better and correctly read the data and conduct the analysis. I was able to get additional information from these forums on how to use additional packages such as coda and ggmcmc to examine the stan model output.

I'm going to create a tutorial so beginners such as I can use it. Thank you for this forum and for your feedback.

Krishna

Bob Carpenter

unread,
Oct 10, 2016, 3:58:13 PM10/10/16
to stan-...@googlegroups.com
Check out our documentation tab on mc-stan.org.

There is RStan doc and vignettes, tutorials, and several
case studies linked that show how to do things in RStan
specifically.

- Bob
Reply all
Reply to author
Forward
0 new messages