Skip to content

Instantly share code, notes, and snippets.

@mages
Last active September 26, 2016 06:25
Show Gist options
  • Save mages/2160bc4d4fb73f3f52f79fdbb923b0af to your computer and use it in GitHub Desktop.
Save mages/2160bc4d4fb73f3f52f79fdbb923b0af to your computer and use it in GitHub Desktop.

Revisions

  1. mages revised this gist Sep 26, 2016. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions NormalDistribution.R
    Original file line number Diff line number Diff line change
    @@ -20,12 +20,12 @@ print(fit, probs=c(0.025, 0.5, 0.975))
    ## post-warmup draws per chain=500, total post-warmup draws=2000.
    ##
    ## mean se_mean sd 2.5% 50% 97.5% n_eff Rhat
    ## mu 4.21 0.01 0.17 3.85 4.21 4.54 631 1
    ## sigma 1.82 0.00 0.13 1.57 1.81 2.10 1808 1
    ## y_ppc 4.20 0.04 1.85 0.69 4.20 7.83 1992 1
    ## lp__ -200.50 0.03 1.02 -203.36 -200.18 -199.53 873 1
    ## mu 4.22 0.01 0.17 3.88 4.22 4.53 771 1
    ## sigma 1.82 0.00 0.13 1.57 1.81 2.10 2000 1
    ## y_ppc 4.22 0.04 1.86 0.70 4.21 7.88 2000 1
    ## lp__ -200.32 0.03 0.97 -203.02 -200.02 -199.41 1016 1
    ##
    ## Samples were drawn using NUTS(diag_e) at Sun Sep 25 20:32:45 2016.
    ## Samples were drawn using NUTS(diag_e) at Mon Sep 26 07:25:09 2016.
    ## 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).
  2. mages revised this gist Sep 25, 2016. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions NormalDistribution.R
    Original file line number Diff line number Diff line change
    @@ -8,10 +8,9 @@ summary(y)
    ## Min. 1st Qu. Median Mean 3rd Qu. Max.
    ## -0.4294 3.0120 4.2280 4.2180 5.3830 8.8030

    ret <- stanc(file="NormaLDistribution.stan")
    ret_sm <- stan_model(stanc_ret = ret)
    fit <- sampling(ret_sm, warmup=100, iter=600,
    seed=1,
    ret <- stanc(file="NormaLDistribution.stan") # Check Stan file
    ret_sm <- stan_model(stanc_ret = ret) # Compile Stan code
    fit <- sampling(ret_sm, warmup=100, iter=600, seed=1,
    data=list(y, N=length(y)))
    stan_trace(fit, inc_warmup = TRUE)
    stan_hist(fit)
  3. mages revised this gist Sep 25, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion NormalDistribution.R
    Original file line number Diff line number Diff line change
    @@ -29,4 +29,9 @@ print(fit, probs=c(0.025, 0.5, 0.975))
    ## Samples were drawn using NUTS(diag_e) at Sun Sep 25 20:32:45 2016.
    ## 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).
    ## convergence, Rhat=1).
    summary(extract(fit, "y_ppc")[["y_ppc"]])
    ## Min. 1st Qu. Median Mean 3rd Qu. Max.
    ## -1.483 2.959 4.213 4.222 5.451 10.540
    plot(ecdf(y), main="Posterior predictive check")
    lines(ecdf(extract(fit, "y_ppc")[["y_ppc"]]), col="#B2001D")
  4. mages revised this gist Sep 25, 2016. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions NormalDistribution.R
    Original file line number Diff line number Diff line change
    @@ -15,18 +15,18 @@ fit <- sampling(ret_sm, warmup=100, iter=600,
    data=list(y, N=length(y)))
    stan_trace(fit, inc_warmup = TRUE)
    stan_hist(fit)
    print(fit, probs=c(0.25, 0.5, 0.75))
    print(fit, probs=c(0.025, 0.5, 0.975))
    ## Inference for Stan model: NormaLDistribution.
    ## 4 chains, each with iter=600; warmup=100; thin=1;
    ## post-warmup draws per chain=500, total post-warmup draws=2000.
    ##
    ## mean se_mean sd 25% 50% 75% n_eff Rhat
    ## mu 4.21 0.01 0.17 4.11 4.21 4.33 631 1
    ## sigma 1.82 0.00 0.13 1.72 1.81 1.90 1808 1
    ## y_ppc 4.20 0.04 1.85 2.92 4.20 5.42 1992 1
    ## lp__ -200.50 0.03 1.02 -200.92 -200.18 -199.77 873 1
    ## mean se_mean sd 2.5% 50% 97.5% n_eff Rhat
    ## mu 4.21 0.01 0.17 3.85 4.21 4.54 631 1
    ## sigma 1.82 0.00 0.13 1.57 1.81 2.10 1808 1
    ## y_ppc 4.20 0.04 1.85 0.69 4.20 7.83 1992 1
    ## lp__ -200.50 0.03 1.02 -203.36 -200.18 -199.53 873 1
    ##
    ## Samples were drawn using NUTS(diag_e) at Sun Sep 25 20:16:49 2016.
    ## Samples were drawn using NUTS(diag_e) at Sun Sep 25 20:32:45 2016.
    ## 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).
  5. mages created this gist Sep 25, 2016.
    32 changes: 32 additions & 0 deletions NormalDistribution.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    library(rstan)
    library(MASS)
    set.seed(1)
    y <- rnorm(100, 4, 2)
    truehist(y, col="#B2001D")
    lines(density(y), col="skyblue", lwd=2)
    summary(y)
    ## Min. 1st Qu. Median Mean 3rd Qu. Max.
    ## -0.4294 3.0120 4.2280 4.2180 5.3830 8.8030

    ret <- stanc(file="NormaLDistribution.stan")
    ret_sm <- stan_model(stanc_ret = ret)
    fit <- sampling(ret_sm, warmup=100, iter=600,
    seed=1,
    data=list(y, N=length(y)))
    stan_trace(fit, inc_warmup = TRUE)
    stan_hist(fit)
    print(fit, probs=c(0.25, 0.5, 0.75))
    ## Inference for Stan model: NormaLDistribution.
    ## 4 chains, each with iter=600; warmup=100; thin=1;
    ## post-warmup draws per chain=500, total post-warmup draws=2000.
    ##
    ## mean se_mean sd 25% 50% 75% n_eff Rhat
    ## mu 4.21 0.01 0.17 4.11 4.21 4.33 631 1
    ## sigma 1.82 0.00 0.13 1.72 1.81 1.90 1808 1
    ## y_ppc 4.20 0.04 1.85 2.92 4.20 5.42 1992 1
    ## lp__ -200.50 0.03 1.02 -200.92 -200.18 -199.77 873 1
    ##
    ## Samples were drawn using NUTS(diag_e) at Sun Sep 25 20:16:49 2016.
    ## 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).