LoginSignup
1

More than 1 year has passed since last update.

posted at

updated at

macOS(Catalina)にRstanをインストールする

はじめに

MacOS(Catalina)におけるRStanの入れ方をまとめました。

環境

macOS Catalina 10.15.7
R version 3.6.1 (2019-07-05)
RStudio version 1.1.456

事前準備

  • V8をインストール

    ターミナルを起動し、v8をインストール。
    他にも足りないものがあるかもしれないが、現在の環境ではこれだけ。

    brew install v8
    
  • gfortranのインストール

    githubのページからgfortran-10.2-Catalina.dmgをダウンロードし、右クリックでこのアプリケーションで開く-インストーラ(デフォルト)から手順に従ってインストールをする。これを入れないと次のR toolchainをうまくインストールできない。

  • R toolchainのインストール

    githubのページからmacos-rtool-4.0.0.pkgをダウンロードし、gfortranのインストールと同じ手順でインストール。

  • PCの再起動をする

RStanのインストール

  • RStudioを開きます。コンソールで以下を実行

C++ toolchainをインストール。

install.packages("Rcpp", repos = "https://rcppcore.github.io/drat")
# improve the estimation speed of the mode
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars")
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS += -O3 -mtune=native -arch x86_64 -ftemplate-depth-256",
    file = M, sep = "\n", append = FALSE)
  • RStanが既に入っている場合への対策として、以下をコンソールで実行
remove.packages("rstan")
if (file.exists(".RData")) file.remove(".RData")
  • RStudioを再起動
  • 次をコンソールで実行

インストール時の設定をする。

Sys.setenv(MAKEFLAGS = "-j4") # four cores used
  • RStanをインストール
install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)

RStanの動作確認

  • RStanをインポート
library(rstan)

無事に入っていればrstanのVersionが3行目で表示される

参考文献

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
1