Rstan

[edit: escaped code]

hi all
please guide me
i have encountered this error

 write("// Stan model for simple linear regression
+ 
+ data {
+  int < lower = 1 > N; // Sample size
+  vector[N] x; // Predictor
+  vector[N] y; // Outcome
+ }
+ 
+ parameters {
+  real alpha; // Intercept
+  real beta; // Slope (regression coefficients)
+  real < lower = 0 > sigma; // Error SD
+ }
+ 
+ model {
+  y ~ normal(x * beta + alpha, sigma);
+ }
+ 
+ generated quantities {
+  real y_rep[N];
+ 
+  for (n in 1:N) {
+  y_rep[n] = normal_rng(x[n] * beta + alpha, sigma);
+  }
+ 
+ }",
+ 
+ "stan_model2_GQ.stan")
> stan_model2_GQ <- "scripts/users/imyerssmith/CC-Stan-Part-1/stan_model2_GQ.stan"
> fit3 <- stan("stan_model2_GQ.stan", data = stan_data, iter = 1000, chains = 4, cores = 2, thin = 1)
> fit3
Inference for Stan model: stan_model2_GQ.
4 chains, each with iter=1000; warmup=500; thin=1; 
post-warmup draws per chain=500, total post-warmup draws=2000.

                mean se_mean   sd     2.5%      25%      50%      75%    97.5% n_eff Rhat
alpha           2.85    0.00 0.11     2.64     2.77     2.84     2.92     3.07   765 1.00
beta            0.04    0.00 0.05    -0.06     0.00     0.04     0.07     0.13   764 1.00
sigma           1.41    0.00 0.03     1.35     1.39     1.41     1.43     1.47  1052 1.00
y_rep[1]        2.90    0.03 1.44     0.02     1.89     2.90     3.87     5.78  1855 1.00
y_rep[2]        2.90    0.03 1.42     0.18     1.95     2.94     3.84     5.62  1942 1.00
y_rep[3]        2.84    0.03 1.45     0.04     1.85     2.82     3.84     5.63  1903 1.00
y_rep[4]        2.93    0.03 1.37     0.07     2.07     2.93     3.84     5.57  1972 1.00
y_rep[5]        2.88    0.03 1.44     0.04     1.93     2.85     3.84     5.63  1954 1.00
y_rep[6]        2.84    0.03 1.39    -0.01     1.88     2.90     3.79     5.44  1969 1.00
y_rep[7]        2.86    0.03 1.43     0.04     1.92     2.83     3.77     5.74  1887 1.00
y_rep[8]        2.86    0.03 1.40     0.04     1.92     2.87     3.77     5.63  1913 1.00
y_rep[9]        2.88    0.03 1.38     0.13     1.99     2.84     3.80     5.63  1967 1.00
y_rep[10]       2.91    0.03 1.44     0.00     1.94     2.91     3.91     5.68  2047 1.00
y_rep[11]       2.94    0.03 1.41     0.10     2.04     2.94     3.89     5.69  1892 1.00
y_rep[12]       2.85    0.03 1.42     0.09     1.91     2.87     3.81     5.58  1892 1.00
y_rep[13]       2.89    0.03 1.38     0.23     1.99     2.91     3.80     5.51  1874 1.00
y_rep[14]       2.87    0.03 1.37     0.13     1.96     2.89     3.81     5.57  2154 1.00
y_rep[15]       2.85    0.03 1.41     0.10     1.90     2.84     3.78     5.65  1857 1.00
y_rep[16]       2.82    0.03 1.40     0.16     1.88     2.79     3.75     5.50  1986 1.00
y_rep[17]       2.86    0.03 1.42     0.02     1.93     2.86     3.78     5.60  1765 1.00
y_rep[18]       2.85    0.03 1.42     0.03     1.93     2.83     3.77     5.65  2097 1.00
y_rep[19]       2.86    0.03 1.38     0.12     1.99     2.86     3.77     5.65  1923 1.00
y_rep[20]       2.89    0.03 1.42     0.19     1.93     2.91     3.87     5.67  2254 1.00
y_rep[21]       2.87    0.03 1.42     0.06     1.91     2.86     3.82     5.59  1973 1.00
y_rep[22]       2.91    0.03 1.40     0.17     1.95     2.91     3.84     5.65  1949 1.00
y_rep[23]       2.94    0.03 1.44     0.23     1.96     2.91     3.91     5.78  2112 1.00
y_rep[24]       2.97    0.03 1.41     0.21     2.07     3.01     3.92     5.64  1809 1.00
y_rep[25]       2.90    0.03 1.40     0.11     1.95     2.90     3.86     5.64  2033 1.00
y_rep[26]       2.98    0.03 1.45     0.14     2.00     3.00     3.93     5.90  1934 1.00
y_rep[27]       2.90    0.03 1.40     0.24     1.95     2.87     3.88     5.57  1829 1.00
y_rep[28]       2.85    0.03 1.43     0.15     1.91     2.86     3.80     5.64  1969 1.00
y_rep[29]       2.92    0.03 1.40     0.11     1.99     2.93     3.85     5.59  2048 1.00
y_rep[30]       2.91    0.03 1.42     0.12     1.96     2.88     3.86     5.77  1716 1.00
y_rep[31]       2.93    0.03 1.40     0.08     1.98     2.89     3.87     5.62  1865 1.00
y_rep[32]       2.87    0.03 1.41     0.07     1.95     2.89     3.79     5.62  1825 1.00
y_rep[33]       2.90    0.03 1.41     0.18     1.95     2.87     3.80     5.67  1790 1.00
y_rep[34]       2.83    0.03 1.41    -0.07     1.91     2.82     3.81     5.52  2046 1.00
y_rep[35]       2.88    0.03 1.41     0.19     1.93     2.88     3.81     5.65  1941 1.00
y_rep[36]       2.87    0.03 1.43     0.15     1.89     2.88     3.80     5.77  1906 1.00
y_rep[37]       2.86    0.03 1.43     0.00     1.90     2.91     3.82     5.57  2158 1.00
y_rep[38]       2.95    0.03 1.43     0.11     1.98     2.98     3.95     5.75  2002 1.00
y_rep[39]       2.83    0.03 1.42    -0.07     1.93     2.83     3.76     5.74  2050 1.00
y_rep[40]       2.84    0.03 1.44     0.01     1.87     2.85     3.81     5.65  1881 1.00
y_rep[41]       2.90    0.03 1.41     0.13     1.96     2.88     3.85     5.55  2063 1.00
y_rep[42]       2.88    0.03 1.39     0.31     1.91     2.87     3.84     5.58  1964 1.00
y_rep[43]       2.92    0.03 1.38     0.25     1.99     2.91     3.86     5.54  1936 1.00
y_rep[44]       2.93    0.03 1.44     0.14     1.93     2.93     3.91     5.70  1954 1.00
y_rep[45]       2.91    0.03 1.39     0.28     1.95     2.88     3.83     5.65  1921 1.00
y_rep[46]       2.88    0.03 1.46     0.07     1.86     2.88     3.82     5.78  2062 1.00
y_rep[47]       2.88    0.03 1.43     0.00     1.91     2.91     3.85     5.63  2016 1.00
y_rep[48]       2.87    0.03 1.38     0.07     1.94     2.83     3.80     5.55  1889 1.00
y_rep[49]       2.87    0.03 1.38     0.12     1.94     2.89     3.84     5.47  2029 1.00
y_rep[50]       2.86    0.03 1.48     0.02     1.87     2.84     3.86     5.83  1905 1.00
y_rep[51]       2.84    0.03 1.42     0.04     1.92     2.84     3.80     5.64  1836 1.00
y_rep[52]       2.86    0.03 1.39     0.22     1.90     2.89     3.81     5.58  2010 1.00
y_rep[53]       2.91    0.03 1.44     0.18     1.95     2.89     3.89     5.76  1984 1.00
y_rep[54]       2.87    0.03 1.42     0.00     1.92     2.93     3.84     5.57  2009 1.00
y_rep[55]       2.90    0.03 1.39     0.16     1.95     2.91     3.84     5.66  2171 1.00
y_rep[56]       2.93    0.03 1.37     0.30     2.02     2.90     3.90     5.67  1988 1.00
y_rep[57]       2.83    0.03 1.42     0.12     1.80     2.84     3.81     5.63  1982 1.00
y_rep[58]       2.92    0.03 1.44     0.16     1.95     2.90     3.91     5.72  2156 1.00
y_rep[59]       2.86    0.03 1.42     0.06     1.87     2.83     3.86     5.58  2012 1.00
y_rep[60]       2.84    0.03 1.42     0.11     1.91     2.78     3.82     5.53  1979 1.00
y_rep[61]       2.86    0.03 1.38     0.21     1.90     2.85     3.83     5.54  1888 1.00
y_rep[62]       2.86    0.03 1.43    -0.10     1.92     2.90     3.84     5.59  1986 1.00
y_rep[63]       2.94    0.03 1.41     0.08     2.03     2.95     3.85     5.63  2049 1.00
y_rep[64]       2.86    0.03 1.40     0.14     1.92     2.89     3.77     5.60  1913 1.00
y_rep[65]       2.86    0.03 1.44    -0.13     1.83     2.89     3.85     5.64  1833 1.00
y_rep[66]       2.93    0.03 1.43     0.10     2.00     2.87     3.94     5.67  1976 1.00
y_rep[67]       2.88    0.03 1.41     0.07     1.96     2.89     3.80     5.63  2121 1.00
y_rep[68]       2.90    0.03 1.39     0.30     1.93     2.89     3.82     5.68  2034 1.00
y_rep[69]       2.91    0.03 1.42     0.12     1.98     2.92     3.88     5.78  2039 1.00
y_rep[70]       2.90    0.03 1.39     0.19     1.98     2.94     3.79     5.66  2044 1.00
y_rep[71]       2.85    0.03 1.44     0.14     1.89     2.86     3.81     5.63  1926 1.00
y_rep[72]       2.90    0.03 1.42     0.14     1.97     2.92     3.81     5.74  2091 1.00
y_rep[73]       2.89    0.03 1.39     0.16     1.93     2.86     3.86     5.59  1980 1.00
y_rep[74]       2.85    0.03 1.42     0.20     1.91     2.80     3.80     5.65  1716 1.00
y_rep[75]       2.87    0.03 1.41     0.08     1.92     2.89     3.79     5.62  1646 1.00
y_rep[76]       2.92    0.03 1.40     0.24     1.96     2.92     3.90     5.50  1972 1.00
y_rep[77]       2.88    0.03 1.41     0.20     1.90     2.90     3.82     5.59  2046 1.00
y_rep[78]       2.84    0.03 1.40     0.08     1.90     2.85     3.77     5.69  2054 1.00
y_rep[79]       2.93    0.03 1.41     0.22     1.97     2.95     3.87     5.58  2070 1.00
y_rep[80]       2.86    0.03 1.37     0.19     1.92     2.88     3.80     5.57  1970 1.00
y_rep[81]       2.85    0.03 1.42     0.10     1.93     2.84     3.75     5.66  1727 1.00
y_rep[82]       2.92    0.03 1.44     0.02     1.96     2.90     3.89     5.76  2177 1.00
y_rep[83]       2.82    0.03 1.45    -0.10     1.84     2.79     3.79     5.77  1990 1.00
y_rep[84]       2.89    0.03 1.46     0.07     1.93     2.89     3.88     5.76  1786 1.00
y_rep[85]       2.87    0.03 1.44     0.14     1.90     2.88     3.81     5.74  2023 1.00
y_rep[86]       2.87    0.03 1.38     0.11     1.94     2.89     3.80     5.56  1944 1.00
y_rep[87]       2.92    0.03 1.41     0.11     1.99     2.89     3.90     5.61  1845 1.00
y_rep[88]       2.85    0.03 1.40     0.05     1.93     2.87     3.81     5.55  1924 1.00
y_rep[89]       2.91    0.03 1.42     0.14     1.91     2.93     3.93     5.56  2082 1.00
y_rep[90]       2.87    0.03 1.37     0.21     1.90     2.93     3.82     5.44  2115 1.00
y_rep[91]       2.90    0.03 1.41     0.16     1.93     2.92     3.85     5.70  1926 1.00
y_rep[92]       2.85    0.03 1.42     0.11     1.88     2.82     3.83     5.76  2098 1.00
y_rep[93]       2.90    0.03 1.35     0.23     1.98     2.86     3.86     5.53  1786 1.00
y_rep[94]       2.86    0.03 1.40     0.12     1.98     2.83     3.81     5.65  2090 1.00
y_rep[95]       2.85    0.03 1.43     0.06     1.87     2.90     3.79     5.67  2045 1.00
y_rep[96]       2.85    0.03 1.40     0.14     1.87     2.87     3.82     5.59  2096 1.00
y_rep[97]       2.91    0.03 1.42     0.25     1.93     2.91     3.88     5.76  1933 1.00
 [ reached getOption("max.print") -- omitted 1207 rows ]

Samples were drawn using NUTS(diag_e) at Wed Aug 10 02:09:49 2022.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at 
convergence, Rhat=1).

> y_rep <- as.matrix(fit3, pars = "y_rep")

> dim(y_rep)
[1] 2000 1303

> ppc_dens_overlay(y, y_rep[1:200, ])
**Error in `validate_y()`:**
**! 'y' must be a vector or 1D array.**
**Run `rlang::last_error()` to see where the error occurred.**

One thing that might help here (I saw that you are new-ish to the R interface) would be to grab a copy of Statistical Rethinking - A Bayesian Course with Examples in R and Stan
Book - Statistical Rethinking | Richard McElreath
Github - GitHub - rmcelreath/stat_rethinking_2022: Statistical Rethinking course winter 2022

The book serves as an intro to Bayesian modeling but more importantly a gentle introduction to R. This will help you to learn the R interface and address many of your questions. Once through that revisit these questions on the forum.

There is also a python interface for Stan if that is something you are more familiar with.

1 Like

The error you’re getting is that y and y_rep need to be the same size. You’re returning a 2D structure for y_rep with y_rep[1:200, ]. So I think the mistake was the as.matrix part for y_rep. You should just be able to extract the y_rep values as is and pass them in.

See: PPC distributions — PPC-distributions • bayesplot

1 Like

really thanks
@Bob_Carpenter
@Ara_Winter

1 Like