R Language stan Bayesian inference analysis

Original connection: http://tecdat.cn/?p=6252

 

Stan R,


Stan can be run from a number of statistical software packages. So far, I've been from R to run Stan , first follow the Quick Start Guide install and run all the contents of the instructions.

 

Simple linear regression


The first step is to write files to Stan models. It contains a file linreg.stan:


 
data { int N;  [N] x; vector[N] y; } parameters { real alpha; real beta; real sigma; } model { y ~ normal(  + beta * x, sigma); }

The first portion of the data file is referred to, it declares Stan passed to the scalar, vector and matrix as inputs.

Next, we simulated data can be set by running the following R code, and using our file linreg.stan Stan and to fit the model:


 
set.seed(123) n <- 100 x <- rnorm(n) y <- x+ (n) mydata <- list(N = n, y = y, x= ) fit <- stan(file = 'linreg. ', data = mydata, iter = 1000,   = 4)

When you first install Stan model, the model will be compiled into a delay of several seconds when C ++. However, as Stan developers described, once the compilation model, it can be applied to new datasets without having to repeat the compilation process (a great advantage in the context of the implementation of the simulation study.

In the above code, we ask Stan to run four separate chains, each chain has 1000 iterations. After the run, we can aggregate output in the following ways:


 
fit Inference for Stan model: linreg. 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 -0.10 0.00 0.10 -0.29 -0.16 -0.10 -0.04 0.09 1346 1 beta 0.95 0.00 0.11 0.75 0.88 0.95 1.02 1.17 1467 1 sigma 0.98 0.00 0.07 0.85 0.93 0.98 1.03 1.12 1265 1 lp__ -47.54 0.06 1.24 -50.77 -48.02 -47.24 -46.68 -46.17 503 1 Samples were drawn using NUTS(diag_e) at Mon Jun 08 18:35:58 2015. 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).

For regression slope β, we mean posterior 0.95 (close to the true value of the analog data 1). To form the 95% confidence interval, we simply use 2.5% and 97.5% of percentile rear sampling, this is 0.75 to 1.17.

You can get the number from various other model fitting in. One is a drawing wherein the posterior distribution of the model parameters. To obtain the regression slope, we can do the following:


 
result <- extract(fit) hist(result$beta)

The posterior distribution histogram β

Now let's use the standard ordinary least squares linear model:


 
summary(lm(y~x)) Call: lm(formula = y ~ x) Residuals: Min 1Q Median 3Q Max -1.9073 -0.6835 -0.0875 0.5806 3.2904 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.10280 0.09755 -1.054 0.295 x 0.94753 0.10688 8.865 3.5e-14 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.9707 on 98 degrees of freedom Multiple R-squared: 0.4451, Adjusted R-squared: 0.4394 F-statistic: 78.6 on 1 and 98 DF, p-value: 3.497e-14

This gives us an estimate of the slope of 0.95, an average value posteriori Stan difference of 2 decimals of a standard deviation of 0.11, which is the same posteriori Stan of the SD.

stan and Bayesian inference


Stan interested in exploring and using it to perform Bayesian inference, this is due to the problem of measurement error and missing data. As WinBUGS years ago, authors and others described and illustrated, the Bayesian approach is very natural in solving the problem of different sources of uncertainty, these sources of uncertainty beyond the parameters of uncertainty, such as missing data or with errors measured covariates. In fact, multiple imputation method for missing data is a popular development within the Bayesian paradigm, and in fact can be viewed as an approximation of the full Bayesian analysis.

Thank you for reading this article, you have any questions please leave a comment below!

 

 

Big Data tribe - Chinese professional third-party data service providers to provide customized one-stop data mining and statistical analysis consultancy services
Statistical analysis and data mining consulting services: y0.cn/teradat (Consulting Services, please contact the official website customer service )
Click here to send me a message QQ:3025393450
 
[Service] Scene  
Research projects; 
 
Companies outsourcing; online and offline one training; data collection; academic research; report writing; market research.
[Tribe] big data to provide customized one-stop data mining and statistical analysis consultancy services
[] Big Data Big Data tribal tribe to provide customized one-stop data mining and statistical analysis consultancy services
Share the latest information about big data, data analysis, study a little every day, so we do have the attitude of people together data [] Big Data Big Data tribal tribe to provide customized one-stop data mining and statistical analysis consultancy services
Micro-channel customer service number: lico_9e
QQ exchange group: 186 388 004  Big Data tribe

Welcome to elective our R language data analysis will be mining will know the course!

 

[] Big Data Big Data tribal tribe to provide customized one-stop data mining and statistical analysis consultancy services


Welcome attention to micro-channel public number for more information about data dry!
[] Big Data Big Data tribal tribe to provide customized one-stop data mining and statistical analysis consultancy services

 

 

Thank you for reading this article, you have any questions please leave a comment below!

 

 

Big Data tribe - Chinese professional third-party data service providers to provide customized one-stop data mining and statistical analysis consultancy services
Statistical analysis and data mining consulting services: y0.cn/teradat (Consulting Services, please contact the official website customer service )
Click here to send me a message QQ:3025393450
 
[Service] Scene  
Research projects; 
 
Companies outsourcing; online and offline one training; data collection; academic research; report writing; market research.
[Tribe] big data to provide customized one-stop data mining and statistical analysis consultancy services
[] Big Data Big Data tribal tribe to provide customized one-stop data mining and statistical analysis consultancy services
Share the latest information about big data, data analysis, study a little every day, so we do have the attitude of people together data [] Big Data Big Data tribal tribe to provide customized one-stop data mining and statistical analysis consultancy services
Micro-channel customer service number: lico_9e
QQ exchange group: 186 388 004  Big Data tribe

Welcome to elective our R language data analysis will be mining will know the course!

 

[] Big Data Big Data tribal tribe to provide customized one-stop data mining and statistical analysis consultancy services


Welcome attention to micro-channel public number for more information about data dry!
[] Big Data Big Data tribal tribe to provide customized one-stop data mining and statistical analysis consultancy services

 

Guess you like

Origin www.cnblogs.com/tecdat/p/11459020.html