// Code generated by Stan version 2.21.0

#include <stan/model/model_header.hpp>

namespace model3fb43b754eb3_chicks_namespace {

using std::istream;
using std::string;
using std::stringstream;
using std::vector;
using stan::io::dump;
using stan::math::lgamma;
using stan::model::prob_grad;
using namespace stan::math;

static int current_statement_begin__;

stan::io::program_reader prog_reader__() {
    stan::io::program_reader reader;
    reader.add_event(0, 0, "start", "model3fb43b754eb3_chicks");
    reader.add_event(109, 107, "end", "model3fb43b754eb3_chicks");
    return reader;
}

class model3fb43b754eb3_chicks
  : public stan::model::model_base_crtp<model3fb43b754eb3_chicks> {
private:
        int N;
        int J;
        std::vector<double> weight;
        std::vector<double> Time;
        std::vector<int> Chick;
        std::vector<double> y;
public:
    model3fb43b754eb3_chicks(rstan::io::rlist_ref_var_context& context__,
        std::ostream* pstream__ = 0)
        : model_base_crtp(0) {
        ctor_body(context__, 0, pstream__);
    }

    model3fb43b754eb3_chicks(stan::io::var_context& context__,
        unsigned int random_seed__,
        std::ostream* pstream__ = 0)
        : model_base_crtp(0) {
        ctor_body(context__, random_seed__, pstream__);
    }

    void ctor_body(stan::io::var_context& context__,
                   unsigned int random_seed__,
                   std::ostream* pstream__) {
        typedef double local_scalar_t__;

        boost::ecuyer1988 base_rng__ =
          stan::services::util::create_rng(random_seed__, 0);
        (void) base_rng__;  // suppress unused var warning

        current_statement_begin__ = -1;

        static const char* function__ = "model3fb43b754eb3_chicks_namespace::model3fb43b754eb3_chicks";
        (void) function__;  // dummy to suppress unused var warning
        size_t pos__;
        (void) pos__;  // dummy to suppress unused var warning
        std::vector<int> vals_i__;
        std::vector<double> vals_r__;
        local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
        (void) DUMMY_VAR__;  // suppress unused var warning

        try {
            // initialize data block variables from context__
            current_statement_begin__ = 3;
            context__.validate_dims("data initialization", "N", "int", context__.to_vec());
            N = int(0);
            vals_i__ = context__.vals_i("N");
            pos__ = 0;
            N = vals_i__[pos__++];

            current_statement_begin__ = 4;
            context__.validate_dims("data initialization", "J", "int", context__.to_vec());
            J = int(0);
            vals_i__ = context__.vals_i("J");
            pos__ = 0;
            J = vals_i__[pos__++];

            current_statement_begin__ = 6;
            validate_non_negative_index("weight", "N", N);
            context__.validate_dims("data initialization", "weight", "double", context__.to_vec(N));
            weight = std::vector<double>(N, double(0));
            vals_r__ = context__.vals_r("weight");
            pos__ = 0;
            size_t weight_k_0_max__ = N;
            for (size_t k_0__ = 0; k_0__ < weight_k_0_max__; ++k_0__) {
                weight[k_0__] = vals_r__[pos__++];
            }

            current_statement_begin__ = 7;
            validate_non_negative_index("Time", "N", N);
            context__.validate_dims("data initialization", "Time", "double", context__.to_vec(N));
            Time = std::vector<double>(N, double(0));
            vals_r__ = context__.vals_r("Time");
            pos__ = 0;
            size_t Time_k_0_max__ = N;
            for (size_t k_0__ = 0; k_0__ < Time_k_0_max__; ++k_0__) {
                Time[k_0__] = vals_r__[pos__++];
            }

            current_statement_begin__ = 9;
            validate_non_negative_index("Chick", "N", N);
            context__.validate_dims("data initialization", "Chick", "int", context__.to_vec(N));
            Chick = std::vector<int>(N, int(0));
            vals_i__ = context__.vals_i("Chick");
            pos__ = 0;
            size_t Chick_k_0_max__ = N;
            for (size_t k_0__ = 0; k_0__ < Chick_k_0_max__; ++k_0__) {
                Chick[k_0__] = vals_i__[pos__++];
            }


            // initialize transformed data variables
            current_statement_begin__ = 15;
            validate_non_negative_index("y", "N", N);
            y = std::vector<double>(N, double(0));
            stan::math::fill(y, DUMMY_VAR__);

            // execute transformed data statements
            current_statement_begin__ = 17;
            stan::math::assign(y, stan::math::log(weight));

            // validate transformed data

            // validate, set parameter ranges
            num_params_r__ = 0U;
            param_ranges_i__.clear();
            current_statement_begin__ = 23;
            num_params_r__ += 1;
            current_statement_begin__ = 25;
            num_params_r__ += 1;
            current_statement_begin__ = 27;
            validate_non_negative_index("eta0", "J", J);
            num_params_r__ += (1 * J);
            current_statement_begin__ = 29;
            validate_non_negative_index("eta1", "J", J);
            num_params_r__ += (1 * J);
            current_statement_begin__ = 31;
            num_params_r__ += 1;
            current_statement_begin__ = 33;
            num_params_r__ += 1;
            current_statement_begin__ = 35;
            num_params_r__ += 1;
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
            // Next line prevents compiler griping about no return
            throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
        }
    }

    ~model3fb43b754eb3_chicks() { }


    void transform_inits(const stan::io::var_context& context__,
                         std::vector<int>& params_i__,
                         std::vector<double>& params_r__,
                         std::ostream* pstream__) const {
        typedef double local_scalar_t__;
        stan::io::writer<double> writer__(params_r__, params_i__);
        size_t pos__;
        (void) pos__; // dummy call to supress warning
        std::vector<double> vals_r__;
        std::vector<int> vals_i__;

        current_statement_begin__ = 23;
        if (!(context__.contains_r("b0")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable b0 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("b0");
        pos__ = 0U;
        context__.validate_dims("parameter initialization", "b0", "double", context__.to_vec());
        double b0(0);
        b0 = vals_r__[pos__++];
        try {
            writer__.scalar_unconstrain(b0);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable b0: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 25;
        if (!(context__.contains_r("b1")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable b1 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("b1");
        pos__ = 0U;
        context__.validate_dims("parameter initialization", "b1", "double", context__.to_vec());
        double b1(0);
        b1 = vals_r__[pos__++];
        try {
            writer__.scalar_unconstrain(b1);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable b1: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 27;
        if (!(context__.contains_r("eta0")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable eta0 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("eta0");
        pos__ = 0U;
        validate_non_negative_index("eta0", "J", J);
        context__.validate_dims("parameter initialization", "eta0", "double", context__.to_vec(J));
        std::vector<double> eta0(J, double(0));
        size_t eta0_k_0_max__ = J;
        for (size_t k_0__ = 0; k_0__ < eta0_k_0_max__; ++k_0__) {
            eta0[k_0__] = vals_r__[pos__++];
        }
        size_t eta0_i_0_max__ = J;
        for (size_t i_0__ = 0; i_0__ < eta0_i_0_max__; ++i_0__) {
            try {
                writer__.scalar_unconstrain(eta0[i_0__]);
            } catch (const std::exception& e) {
                stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable eta0: ") + e.what()), current_statement_begin__, prog_reader__());
            }
        }

        current_statement_begin__ = 29;
        if (!(context__.contains_r("eta1")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable eta1 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("eta1");
        pos__ = 0U;
        validate_non_negative_index("eta1", "J", J);
        context__.validate_dims("parameter initialization", "eta1", "double", context__.to_vec(J));
        std::vector<double> eta1(J, double(0));
        size_t eta1_k_0_max__ = J;
        for (size_t k_0__ = 0; k_0__ < eta1_k_0_max__; ++k_0__) {
            eta1[k_0__] = vals_r__[pos__++];
        }
        size_t eta1_i_0_max__ = J;
        for (size_t i_0__ = 0; i_0__ < eta1_i_0_max__; ++i_0__) {
            try {
                writer__.scalar_unconstrain(eta1[i_0__]);
            } catch (const std::exception& e) {
                stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable eta1: ") + e.what()), current_statement_begin__, prog_reader__());
            }
        }

        current_statement_begin__ = 31;
        if (!(context__.contains_r("sigma")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable sigma missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("sigma");
        pos__ = 0U;
        context__.validate_dims("parameter initialization", "sigma", "double", context__.to_vec());
        double sigma(0);
        sigma = vals_r__[pos__++];
        try {
            writer__.scalar_lb_unconstrain(0, sigma);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable sigma: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 33;
        if (!(context__.contains_r("tau0")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable tau0 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("tau0");
        pos__ = 0U;
        context__.validate_dims("parameter initialization", "tau0", "double", context__.to_vec());
        double tau0(0);
        tau0 = vals_r__[pos__++];
        try {
            writer__.scalar_lb_unconstrain(0, tau0);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable tau0: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 35;
        if (!(context__.contains_r("tau1")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable tau1 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("tau1");
        pos__ = 0U;
        context__.validate_dims("parameter initialization", "tau1", "double", context__.to_vec());
        double tau1(0);
        tau1 = vals_r__[pos__++];
        try {
            writer__.scalar_lb_unconstrain(0, tau1);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable tau1: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        params_r__ = writer__.data_r();
        params_i__ = writer__.data_i();
    }

    void transform_inits(const stan::io::var_context& context,
                         Eigen::Matrix<double, Eigen::Dynamic, 1>& params_r,
                         std::ostream* pstream__) const {
      std::vector<double> params_r_vec;
      std::vector<int> params_i_vec;
      transform_inits(context, params_i_vec, params_r_vec, pstream__);
      params_r.resize(params_r_vec.size());
      for (int i = 0; i < params_r.size(); ++i)
        params_r(i) = params_r_vec[i];
    }


    template <bool propto__, bool jacobian__, typename T__>
    T__ log_prob(std::vector<T__>& params_r__,
                 std::vector<int>& params_i__,
                 std::ostream* pstream__ = 0) const {

        typedef T__ local_scalar_t__;

        local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
        (void) DUMMY_VAR__;  // dummy to suppress unused var warning

        T__ lp__(0.0);
        stan::math::accumulator<T__> lp_accum__;
        try {
            stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);

            // model parameters
            current_statement_begin__ = 23;
            local_scalar_t__ b0;
            (void) b0;  // dummy to suppress unused var warning
            if (jacobian__)
                b0 = in__.scalar_constrain(lp__);
            else
                b0 = in__.scalar_constrain();

            current_statement_begin__ = 25;
            local_scalar_t__ b1;
            (void) b1;  // dummy to suppress unused var warning
            if (jacobian__)
                b1 = in__.scalar_constrain(lp__);
            else
                b1 = in__.scalar_constrain();

            current_statement_begin__ = 27;
            std::vector<local_scalar_t__> eta0;
            size_t eta0_d_0_max__ = J;
            eta0.reserve(eta0_d_0_max__);
            for (size_t d_0__ = 0; d_0__ < eta0_d_0_max__; ++d_0__) {
                if (jacobian__)
                    eta0.push_back(in__.scalar_constrain(lp__));
                else
                    eta0.push_back(in__.scalar_constrain());
            }

            current_statement_begin__ = 29;
            std::vector<local_scalar_t__> eta1;
            size_t eta1_d_0_max__ = J;
            eta1.reserve(eta1_d_0_max__);
            for (size_t d_0__ = 0; d_0__ < eta1_d_0_max__; ++d_0__) {
                if (jacobian__)
                    eta1.push_back(in__.scalar_constrain(lp__));
                else
                    eta1.push_back(in__.scalar_constrain());
            }

            current_statement_begin__ = 31;
            local_scalar_t__ sigma;
            (void) sigma;  // dummy to suppress unused var warning
            if (jacobian__)
                sigma = in__.scalar_lb_constrain(0, lp__);
            else
                sigma = in__.scalar_lb_constrain(0);

            current_statement_begin__ = 33;
            local_scalar_t__ tau0;
            (void) tau0;  // dummy to suppress unused var warning
            if (jacobian__)
                tau0 = in__.scalar_lb_constrain(0, lp__);
            else
                tau0 = in__.scalar_lb_constrain(0);

            current_statement_begin__ = 35;
            local_scalar_t__ tau1;
            (void) tau1;  // dummy to suppress unused var warning
            if (jacobian__)
                tau1 = in__.scalar_lb_constrain(0, lp__);
            else
                tau1 = in__.scalar_lb_constrain(0);

            // transformed parameters
            current_statement_begin__ = 41;
            validate_non_negative_index("mu", "N", N);
            std::vector<local_scalar_t__> mu(N, local_scalar_t__(0));
            stan::math::initialize(mu, DUMMY_VAR__);
            stan::math::fill(mu, DUMMY_VAR__);

            current_statement_begin__ = 43;
            validate_non_negative_index("a0", "J", J);
            std::vector<local_scalar_t__> a0(J, local_scalar_t__(0));
            stan::math::initialize(a0, DUMMY_VAR__);
            stan::math::fill(a0, DUMMY_VAR__);

            current_statement_begin__ = 45;
            validate_non_negative_index("a1", "J", J);
            std::vector<local_scalar_t__> a1(J, local_scalar_t__(0));
            stan::math::initialize(a1, DUMMY_VAR__);
            stan::math::fill(a1, DUMMY_VAR__);

            // transformed parameters block statements
            current_statement_begin__ = 47;
            for (int j = 1; j <= J; ++j) {

                current_statement_begin__ = 49;
                stan::model::assign(a0, 
                            stan::model::cons_list(stan::model::index_uni(j), stan::model::nil_index_list()), 
                            (b0 + get_base1(eta0, j, "eta0", 1)), 
                            "assigning variable a0");
                current_statement_begin__ = 51;
                stan::model::assign(a1, 
                            stan::model::cons_list(stan::model::index_uni(j), stan::model::nil_index_list()), 
                            (b1 + get_base1(eta1, j, "eta1", 1)), 
                            "assigning variable a1");
            }
            current_statement_begin__ = 55;
            for (int i = 1; i <= N; ++i) {

                current_statement_begin__ = 57;
                stan::model::assign(mu, 
                            stan::model::cons_list(stan::model::index_uni(i), stan::model::nil_index_list()), 
                            (get_base1(a0, get_base1(Chick, i, "Chick", 1), "a0", 1) + (get_base1(a1, get_base1(Chick, i, "Chick", 1), "a1", 1) * get_base1(Time, i, "Time", 1))), 
                            "assigning variable mu");
            }

            // validate transformed parameters
            const char* function__ = "validate transformed params";
            (void) function__;  // dummy to suppress unused var warning

            current_statement_begin__ = 41;
            size_t mu_k_0_max__ = N;
            for (size_t k_0__ = 0; k_0__ < mu_k_0_max__; ++k_0__) {
                if (stan::math::is_uninitialized(mu[k_0__])) {
                    std::stringstream msg__;
                    msg__ << "Undefined transformed parameter: mu" << "[" << k_0__ << "]";
                    stan::lang::rethrow_located(std::runtime_error(std::string("Error initializing variable mu: ") + msg__.str()), current_statement_begin__, prog_reader__());
                }
            }
            current_statement_begin__ = 43;
            size_t a0_k_0_max__ = J;
            for (size_t k_0__ = 0; k_0__ < a0_k_0_max__; ++k_0__) {
                if (stan::math::is_uninitialized(a0[k_0__])) {
                    std::stringstream msg__;
                    msg__ << "Undefined transformed parameter: a0" << "[" << k_0__ << "]";
                    stan::lang::rethrow_located(std::runtime_error(std::string("Error initializing variable a0: ") + msg__.str()), current_statement_begin__, prog_reader__());
                }
            }
            current_statement_begin__ = 45;
            size_t a1_k_0_max__ = J;
            for (size_t k_0__ = 0; k_0__ < a1_k_0_max__; ++k_0__) {
                if (stan::math::is_uninitialized(a1[k_0__])) {
                    std::stringstream msg__;
                    msg__ << "Undefined transformed parameter: a1" << "[" << k_0__ << "]";
                    stan::lang::rethrow_located(std::runtime_error(std::string("Error initializing variable a1: ") + msg__.str()), current_statement_begin__, prog_reader__());
                }
            }

            // model body

            current_statement_begin__ = 67;
            for (int i = 1; i <= N; ++i) {

                current_statement_begin__ = 69;
                lp_accum__.add(normal_log<propto__>(get_base1(y, i, "y", 1), get_base1(mu, i, "mu", 1), sigma));
            }
            current_statement_begin__ = 75;
            for (int j = 1; j <= J; ++j) {

                current_statement_begin__ = 77;
                lp_accum__.add(normal_log<propto__>(get_base1(eta0, j, "eta0", 1), 0, tau0));
                current_statement_begin__ = 79;
                lp_accum__.add(normal_log<propto__>(get_base1(eta1, j, "eta1", 1), 0, tau1));
            }
            current_statement_begin__ = 85;
            lp_accum__.add(normal_log<propto__>(b0, 0, 1e6));
            current_statement_begin__ = 87;
            lp_accum__.add(normal_log<propto__>(b1, 0, 1e6));
            current_statement_begin__ = 89;
            lp_accum__.add(uniform_log<propto__>(sigma, 0, 50));
            current_statement_begin__ = 91;
            lp_accum__.add(uniform_log<propto__>(tau0, 0, 50));
            current_statement_begin__ = 93;
            lp_accum__.add(uniform_log<propto__>(tau1, 0, 50));

        } catch (const std::exception& e) {
            stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
            // Next line prevents compiler griping about no return
            throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
        }

        lp_accum__.add(lp__);
        return lp_accum__.sum();

    } // log_prob()

    template <bool propto, bool jacobian, typename T_>
    T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
               std::ostream* pstream = 0) const {
      std::vector<T_> vec_params_r;
      vec_params_r.reserve(params_r.size());
      for (int i = 0; i < params_r.size(); ++i)
        vec_params_r.push_back(params_r(i));
      std::vector<int> vec_params_i;
      return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
    }


    void get_param_names(std::vector<std::string>& names__) const {
        names__.resize(0);
        names__.push_back("b0");
        names__.push_back("b1");
        names__.push_back("eta0");
        names__.push_back("eta1");
        names__.push_back("sigma");
        names__.push_back("tau0");
        names__.push_back("tau1");
        names__.push_back("mu");
        names__.push_back("a0");
        names__.push_back("a1");
        names__.push_back("yrep");
    }


    void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
        dimss__.resize(0);
        std::vector<size_t> dims__;
        dims__.resize(0);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(J);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(J);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(N);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(J);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(J);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(N);
        dimss__.push_back(dims__);
    }

    template <typename RNG>
    void write_array(RNG& base_rng__,
                     std::vector<double>& params_r__,
                     std::vector<int>& params_i__,
                     std::vector<double>& vars__,
                     bool include_tparams__ = true,
                     bool include_gqs__ = true,
                     std::ostream* pstream__ = 0) const {
        typedef double local_scalar_t__;

        vars__.resize(0);
        stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
        static const char* function__ = "model3fb43b754eb3_chicks_namespace::write_array";
        (void) function__;  // dummy to suppress unused var warning

        // read-transform, write parameters
        double b0 = in__.scalar_constrain();
        vars__.push_back(b0);

        double b1 = in__.scalar_constrain();
        vars__.push_back(b1);

        std::vector<double> eta0;
        size_t eta0_d_0_max__ = J;
        eta0.reserve(eta0_d_0_max__);
        for (size_t d_0__ = 0; d_0__ < eta0_d_0_max__; ++d_0__) {
            eta0.push_back(in__.scalar_constrain());
        }
        size_t eta0_k_0_max__ = J;
        for (size_t k_0__ = 0; k_0__ < eta0_k_0_max__; ++k_0__) {
            vars__.push_back(eta0[k_0__]);
        }

        std::vector<double> eta1;
        size_t eta1_d_0_max__ = J;
        eta1.reserve(eta1_d_0_max__);
        for (size_t d_0__ = 0; d_0__ < eta1_d_0_max__; ++d_0__) {
            eta1.push_back(in__.scalar_constrain());
        }
        size_t eta1_k_0_max__ = J;
        for (size_t k_0__ = 0; k_0__ < eta1_k_0_max__; ++k_0__) {
            vars__.push_back(eta1[k_0__]);
        }

        double sigma = in__.scalar_lb_constrain(0);
        vars__.push_back(sigma);

        double tau0 = in__.scalar_lb_constrain(0);
        vars__.push_back(tau0);

        double tau1 = in__.scalar_lb_constrain(0);
        vars__.push_back(tau1);

        double lp__ = 0.0;
        (void) lp__;  // dummy to suppress unused var warning
        stan::math::accumulator<double> lp_accum__;

        local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
        (void) DUMMY_VAR__;  // suppress unused var warning

        if (!include_tparams__ && !include_gqs__) return;

        try {
            // declare and define transformed parameters
            current_statement_begin__ = 41;
            validate_non_negative_index("mu", "N", N);
            std::vector<double> mu(N, double(0));
            stan::math::initialize(mu, DUMMY_VAR__);
            stan::math::fill(mu, DUMMY_VAR__);

            current_statement_begin__ = 43;
            validate_non_negative_index("a0", "J", J);
            std::vector<double> a0(J, double(0));
            stan::math::initialize(a0, DUMMY_VAR__);
            stan::math::fill(a0, DUMMY_VAR__);

            current_statement_begin__ = 45;
            validate_non_negative_index("a1", "J", J);
            std::vector<double> a1(J, double(0));
            stan::math::initialize(a1, DUMMY_VAR__);
            stan::math::fill(a1, DUMMY_VAR__);

            // do transformed parameters statements
            current_statement_begin__ = 47;
            for (int j = 1; j <= J; ++j) {

                current_statement_begin__ = 49;
                stan::model::assign(a0, 
                            stan::model::cons_list(stan::model::index_uni(j), stan::model::nil_index_list()), 
                            (b0 + get_base1(eta0, j, "eta0", 1)), 
                            "assigning variable a0");
                current_statement_begin__ = 51;
                stan::model::assign(a1, 
                            stan::model::cons_list(stan::model::index_uni(j), stan::model::nil_index_list()), 
                            (b1 + get_base1(eta1, j, "eta1", 1)), 
                            "assigning variable a1");
            }
            current_statement_begin__ = 55;
            for (int i = 1; i <= N; ++i) {

                current_statement_begin__ = 57;
                stan::model::assign(mu, 
                            stan::model::cons_list(stan::model::index_uni(i), stan::model::nil_index_list()), 
                            (get_base1(a0, get_base1(Chick, i, "Chick", 1), "a0", 1) + (get_base1(a1, get_base1(Chick, i, "Chick", 1), "a1", 1) * get_base1(Time, i, "Time", 1))), 
                            "assigning variable mu");
            }

            if (!include_gqs__ && !include_tparams__) return;
            // validate transformed parameters
            const char* function__ = "validate transformed params";
            (void) function__;  // dummy to suppress unused var warning

            // write transformed parameters
            if (include_tparams__) {
                size_t mu_k_0_max__ = N;
                for (size_t k_0__ = 0; k_0__ < mu_k_0_max__; ++k_0__) {
                    vars__.push_back(mu[k_0__]);
                }
                size_t a0_k_0_max__ = J;
                for (size_t k_0__ = 0; k_0__ < a0_k_0_max__; ++k_0__) {
                    vars__.push_back(a0[k_0__]);
                }
                size_t a1_k_0_max__ = J;
                for (size_t k_0__ = 0; k_0__ < a1_k_0_max__; ++k_0__) {
                    vars__.push_back(a1[k_0__]);
                }
            }
            if (!include_gqs__) return;
            // declare and define generated quantities
            current_statement_begin__ = 99;
            validate_non_negative_index("yrep", "N", N);
            std::vector<double> yrep(N, double(0));
            stan::math::initialize(yrep, DUMMY_VAR__);
            stan::math::fill(yrep, DUMMY_VAR__);

            // generated quantities statements
            current_statement_begin__ = 101;
            for (int i = 1; i <= N; ++i) {

                current_statement_begin__ = 103;
                stan::model::assign(yrep, 
                            stan::model::cons_list(stan::model::index_uni(i), stan::model::nil_index_list()), 
                            normal_rng(get_base1(mu, i, "mu", 1), sigma, base_rng__), 
                            "assigning variable yrep");
            }

            // validate, write generated quantities
            current_statement_begin__ = 99;
            size_t yrep_k_0_max__ = N;
            for (size_t k_0__ = 0; k_0__ < yrep_k_0_max__; ++k_0__) {
                vars__.push_back(yrep[k_0__]);
            }

        } catch (const std::exception& e) {
            stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
            // Next line prevents compiler griping about no return
            throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
        }
    }

    template <typename RNG>
    void write_array(RNG& base_rng,
                     Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
                     Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
                     bool include_tparams = true,
                     bool include_gqs = true,
                     std::ostream* pstream = 0) const {
      std::vector<double> params_r_vec(params_r.size());
      for (int i = 0; i < params_r.size(); ++i)
        params_r_vec[i] = params_r(i);
      std::vector<double> vars_vec;
      std::vector<int> params_i_vec;
      write_array(base_rng, params_r_vec, params_i_vec, vars_vec, include_tparams, include_gqs, pstream);
      vars.resize(vars_vec.size());
      for (int i = 0; i < vars.size(); ++i)
        vars(i) = vars_vec[i];
    }

    std::string model_name() const {
        return "model3fb43b754eb3_chicks";
    }


    void constrained_param_names(std::vector<std::string>& param_names__,
                                 bool include_tparams__ = true,
                                 bool include_gqs__ = true) const {
        std::stringstream param_name_stream__;
        param_name_stream__.str(std::string());
        param_name_stream__ << "b0";
        param_names__.push_back(param_name_stream__.str());
        param_name_stream__.str(std::string());
        param_name_stream__ << "b1";
        param_names__.push_back(param_name_stream__.str());
        size_t eta0_k_0_max__ = J;
        for (size_t k_0__ = 0; k_0__ < eta0_k_0_max__; ++k_0__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "eta0" << '.' << k_0__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t eta1_k_0_max__ = J;
        for (size_t k_0__ = 0; k_0__ < eta1_k_0_max__; ++k_0__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "eta1" << '.' << k_0__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        param_name_stream__.str(std::string());
        param_name_stream__ << "sigma";
        param_names__.push_back(param_name_stream__.str());
        param_name_stream__.str(std::string());
        param_name_stream__ << "tau0";
        param_names__.push_back(param_name_stream__.str());
        param_name_stream__.str(std::string());
        param_name_stream__ << "tau1";
        param_names__.push_back(param_name_stream__.str());

        if (!include_gqs__ && !include_tparams__) return;

        if (include_tparams__) {
            size_t mu_k_0_max__ = N;
            for (size_t k_0__ = 0; k_0__ < mu_k_0_max__; ++k_0__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "mu" << '.' << k_0__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            size_t a0_k_0_max__ = J;
            for (size_t k_0__ = 0; k_0__ < a0_k_0_max__; ++k_0__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "a0" << '.' << k_0__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            size_t a1_k_0_max__ = J;
            for (size_t k_0__ = 0; k_0__ < a1_k_0_max__; ++k_0__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "a1" << '.' << k_0__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }

        if (!include_gqs__) return;
        size_t yrep_k_0_max__ = N;
        for (size_t k_0__ = 0; k_0__ < yrep_k_0_max__; ++k_0__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "yrep" << '.' << k_0__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
    }


    void unconstrained_param_names(std::vector<std::string>& param_names__,
                                   bool include_tparams__ = true,
                                   bool include_gqs__ = true) const {
        std::stringstream param_name_stream__;
        param_name_stream__.str(std::string());
        param_name_stream__ << "b0";
        param_names__.push_back(param_name_stream__.str());
        param_name_stream__.str(std::string());
        param_name_stream__ << "b1";
        param_names__.push_back(param_name_stream__.str());
        size_t eta0_k_0_max__ = J;
        for (size_t k_0__ = 0; k_0__ < eta0_k_0_max__; ++k_0__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "eta0" << '.' << k_0__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t eta1_k_0_max__ = J;
        for (size_t k_0__ = 0; k_0__ < eta1_k_0_max__; ++k_0__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "eta1" << '.' << k_0__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        param_name_stream__.str(std::string());
        param_name_stream__ << "sigma";
        param_names__.push_back(param_name_stream__.str());
        param_name_stream__.str(std::string());
        param_name_stream__ << "tau0";
        param_names__.push_back(param_name_stream__.str());
        param_name_stream__.str(std::string());
        param_name_stream__ << "tau1";
        param_names__.push_back(param_name_stream__.str());

        if (!include_gqs__ && !include_tparams__) return;

        if (include_tparams__) {
            size_t mu_k_0_max__ = N;
            for (size_t k_0__ = 0; k_0__ < mu_k_0_max__; ++k_0__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "mu" << '.' << k_0__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            size_t a0_k_0_max__ = J;
            for (size_t k_0__ = 0; k_0__ < a0_k_0_max__; ++k_0__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "a0" << '.' << k_0__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            size_t a1_k_0_max__ = J;
            for (size_t k_0__ = 0; k_0__ < a1_k_0_max__; ++k_0__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "a1" << '.' << k_0__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }

        if (!include_gqs__) return;
        size_t yrep_k_0_max__ = N;
        for (size_t k_0__ = 0; k_0__ < yrep_k_0_max__; ++k_0__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "yrep" << '.' << k_0__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
    }

}; // model

}  // namespace

typedef model3fb43b754eb3_chicks_namespace::model3fb43b754eb3_chicks stan_model;

#ifndef USING_R

stan::model::model_base& new_model(
        stan::io::var_context& data_context,
        unsigned int seed,
        std::ostream* msg_stream) {
  stan_model* m = new stan_model(data_context, seed, msg_stream);
  return *m;
}

#endif