Open1

Rの知見

DKDK

randomForestパッケージのインストール方法

概要

M1 Pro Mac(OS Sonoma)にRのパッケージrandomForestをインストールしようとしたところ、失敗したので解決策をメモ

結論

gfortranをうまく見つけられていないので、新たにファイルでパスを指定してあげることで解決できる

エラーコード(R Studioのコンソール)

> install.packages("randomForest")
Warning in install.packages :
  unable to access index for repository https://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.1:
  cannot open URL 'https://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.1/PACKAGES'
Package which is only available in source form, and may need compilation of C/C++/Fortran: ‘randomForest’
Do you want to attempt to install these from sources? (Yes/no/cancel) yes
installing the source package ‘randomForest’

trying URL 'https://cran.rstudio.com/src/contrib/randomForest_4.7-1.1.tar.gz'
Content type 'application/x-gzip' length 80886 bytes (78 KB)
==================================================
downloaded 78 KB

* installing *source* package ‘randomForest’ ...
** package ‘randomForest’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c classTree.c -o classTree.o
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c init.c -o init.o
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c regTree.c -o regTree.o
regTree.c:36:17: warning: variable 'sumOfSquares' set but not used [-Wunused-but-set-variable]
  double label, sumOfSquares, nodeMean, decsplit, ubest, sumOfNodeData;
                ^
1 warning generated.
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c regrf.c -o regrf.o
regrf.c:60:55: warning: unused variable 'ktmp' [-Wunused-variable]
    int k, m, mr, n, nOOB, j, jout, idx, ntest, last, ktmp, nPerm,
                                                      ^
regrf.c:55:25: warning: variable 'averrb' set but not used [-Wunused-but-set-variable]
    double errts = 0.0, averrb, meanY, meanYts, varY, varYts, r, xrand,
                        ^
regrf.c:60:49: warning: unused variable 'last' [-Wunused-variable]
    int k, m, mr, n, nOOB, j, jout, idx, ntest, last, ktmp, nPerm,
                                                ^
regrf.c:64:15: warning: variable 'nind' set but not used [-Wunused-but-set-variable]
    int *in, *nind, *nodex, *nodexts;
              ^
regrf.c:55:66: warning: unused variable 'xrand' [-Wunused-variable]
    double errts = 0.0, averrb, meanY, meanYts, varY, varYts, r, xrand,
                                                                 ^
5 warnings generated.
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c rf.c -o rf.o
rf.c:98:41: warning: unused variable 'ktmp' [-Wunused-variable]
  int **strata_idx, *strata_size, last, ktmp, nEmpty, ntry;
                                        ^
rf.c:103:4: warning: variable 'tp' set but not used [-Wunused-but-set-variable]
  *tp, *wr, *bestsplitnext, *bestsplit;
   ^
rf.c:88:9: warning: variable 'nimp' set but not used [-Wunused-but-set-variable]
  mimp, nimp, near, nuse, noutall, nrightall, nrightimpall,
        ^
rf.c:97:24: warning: variable 'nind' set but not used [-Wunused-but-set-variable]
  *at, *a, *b, *mind, *nind, *jts, *oobpair, *sampledIndices;
                       ^
rf.c:88:3: warning: variable 'mimp' set but not used [-Wunused-but-set-variable]
  mimp, nimp, near, nuse, noutall, nrightall, nrightimpall,
  ^
rf.c:98:35: warning: unused variable 'last' [-Wunused-variable]
  int **strata_idx, *strata_size, last, ktmp, nEmpty, ntry;
                                  ^
rf.c:443:17: warning: expression result unused [-Wunused-value]
                +                                                               nrightimp[cl[n] - 1]++;
                ^                                                               ~~~~~~~~~~~~~~~~~~~~~~
7 warnings generated.
/opt/R/arm64/bin/gfortran -mtune=native -fno-optimize-sibling-calls  -fPIC  -Wall -g -O2  -c rfsub.f -o rfsub.o
make: /opt/R/arm64/bin/gfortran: No such file or directory
make: *** [rfsub.o] Error 1
ERROR: compilation failed for package ‘randomForest’
* removing ‘/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/randomForest’
Warning in install.packages :
  installation of package ‘randomForest’ had non-zero exit status

The downloaded source packages are in/private/var/folders/rb/sg0_56qx5g9_b6lxttk6t1780000gn/T/Rtmp6rvdjr/downloaded_packages’

解決策

  1. Homebrewでgccをインストール(未インストールの場合)
brew install gcc
  1. インストールしたgccのバージョンを確認(自分の場合は13.2.0)
cd /opt/homebrew/Cellar/gcc
ls

ここで表示されるディレクトリ名をメモしましょう!(以下[version]と表記)

  1. ~/.RディレクトリにMakevarsを作成
/opt/homebrew/Cellar/gcc/[version]/bin/gfortran
F77 = /opt/homebrew/Cellar/gcc/[version]/bin/gfortran
FLIBS = -L/opt/homebrew/Cellar/gcc/[version]/lib/gcc/13
  1. Rにて再度インストールに挑戦
install.packages("randomForest")

参考

Stack Overflow