楼主: oliyiyi
5681 62

Bayesian Statistics with Stan [推广有奖]

31
hyq2003 发表于 2019-1-12 10:10:46 |只看作者 |坛友微信交流群
感谢分享

使用道具

32
oliyiyi 发表于 2019-1-12 11:28:25 |只看作者 |坛友微信交流群
Be sure that your Stan programs ends in a blank line without any characters including spaces and comments.

In this Stan program, we let theta be a transformation of mu, eta, and tau instead of declaring theta in the parameters block, which allows the sampler will run more efficiently (see detailed explanation). We can prepare the data (which typically is a named list) in R with:

使用道具

33
oliyiyi 发表于 2019-1-12 11:28:50 |只看作者 |坛友微信交流群
schools_dat <- list(J = 8,
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

使用道具

34
oliyiyi 发表于 2019-1-12 11:30:21 |只看作者 |坛友微信交流群
schools_dat <- list(J = 8,
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

使用道具

35
oliyiyi 发表于 2019-1-12 11:30:37 |只看作者 |坛友微信交流群
And we can get a fit with the following R command. Note that the argument to file = should point to where the file is on your file system unless you have put it in the working directory of R in which case the below will work.

使用道具

36
oliyiyi 发表于 2019-1-12 11:31:02 |只看作者 |坛友微信交流群
fit <- stan(file = '8schools.stan', data = schools_dat)

使用道具

37
oliyiyi 发表于 2019-1-12 11:31:27 |只看作者 |坛友微信交流群
The object fit, returned from function stan is an S4 object of class stanfit. Methods such as print, plot, and pairs are associated with the fitted result so we can use the following code to check out the results in fit. print provides a summary for the parameter of the model as well as the log-posterior with name lp__ (see the following example output). For more methods and details of class stanfit, see the help of class stanfit.

使用道具

38
oliyiyi 发表于 2019-1-12 11:31:57 |只看作者 |坛友微信交流群
S4 object

使用道具

39
oliyiyi 发表于 2019-1-12 11:33:19 |只看作者 |坛友微信交流群
In S3, you can turn any object into an object of a particular class just by setting the class attribute.

使用道具

40
oliyiyi 发表于 2019-1-12 11:34:41 |只看作者 |坛友微信交流群
The S4 approach differs from the S3 approach to creating a class in that it is a more rigid definition. The idea is that an object is created using the setClass command. The command takes a number of options. Many of the options are not required, but we make use of several of the optional arguments because they represent good practices with respect to object oriented programming.

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 07:22