/Introduction-to-Rstan

Introduce Bayesian methods and codes to Rstan

Primary LanguageR

Rstan 在贝叶斯分析中的运用

主要包括 stan 的下载、安装、使用手册以及相应的例子

Rstan 安装

  • 下载安装与 R 版本相对应的的 Rtools。安装过程注意下图中的选项 editpathrtools
  • 退出当前的 R,然后重新启动 R
  • 在 R 中输入命令 Sys.getenv("PATH") 并检查 Rtools 是否安装在指定的路径。如果 Rtools 安装在 c:\\Rtools ,那么你会看到下面的代码和显示
> Sys.getenv('PATH')
[1] "c:\\\\Rtools\\\\bin;c:\\\\Rtools\\\\gcc-4.6.3\\\\bin;...
  • 检查 g++ 能否被 R 读取. 例如:
> system('g++ -v')
Using built-in specs.
COLLECT_GCC=c:\Rtools\GCC-46~1.3\bin\G__~1.EXE
COLLECT_LTO_WRAPPER=c:/rtools/gcc-46~1.3/bin/../libexec/gcc/i686-w64-mingw32/4.6.3/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: /data/gannet/ripley/Sources/mingw-test3/src/gcc/configure --host=i686-w64-mingw32 --build=x86_64-linux-gnu --target=i686-w64-mingw32 --with-sysroot=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/mingw32 --prefix=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/mingw32 --with-gmp=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --with-mpfr=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --with-mpc=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --disable-shared --enable-static --enable-targets=all --enable-languages=c,c++,fortran --enable-libgomp --enable-sjlj-exceptions --enable-fully-dynamic-string --disable-nls --disable-werror --enable-checking=release --disable-win32-registry --disable-rpath --disable-werror CFLAGS='-O2 -mtune=core2 -fomit-frame-pointer' LDFLAGS=
Thread model: win32
gcc version 4.6.3 20111208 (prerelease) (GCC)

> system('where make')
c:\Rtools\bin\make.exe
  • 安装 Rstan 包
# note: omit the 's' in 'https' if you cannot handle https downloads
install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies=TRUE)
  • 或者可以尝试下述代码
# note: replace the '4' with the number of cores you want to devote to the build
Sys.setenv(MAKEFLAGS = "-j4") 
install.packages("rstan", type = "source")

Rstan 在贝叶斯分析中的运用

Rstan 应用

  • 假设伽马分布的均值为15,形状参数(shape)和比率参数(rate)分别为 30 和 2。请模拟 1000 个服从该伽马分布的随机数
  • 基于这组随机数应用 Rstan 估计伽马分布的两个参数
  • 假设 y 服从正态分布,标准差为 2,均值可以表示为三个协变量的线性函数,即 μ=10+0.2x1-0.3x2+0.4*x3
  • 三个协变量都服从标准正态分布
  • 模拟 1000 个因变量和协变量的观察值
  • 基于这组模拟数据应用 Rstan 建立线性回归模型。
  • 假设因变量 y 服从伽马分布,均值可以表示为三个协变量的线性函数,即 μ=5+1.2x1+1.3x2+1.4*x3
  • 三个协变量都服从均值为 0.1 的指数分布
  • 请模拟 1000 个因变量和协变量的观察值
  • 基于这组模拟数据应用Rstan建立回归模型。

本数据是国外一家保险公司私家车业务线的流量三角形数据。该数据给出了55个上三角的已付增量赔款以及各个事故年的已收保费,其中数据分为

在未决赔款准备金评估的传统方法中,链锑法是最主要的评估方法之一,对准备金的预测结果与(过离散)泊松回归模型是等价的

  • 下面应用 Rstan 建立对该数据建立回归模型,并预测下三角的增量赔款,同时给出未决赔款准备金的预测均值和预测分布
  • 将预测结果与链锑法相比较