2015-04-03 19 views
0

加載Rstan問題我已經安裝了RStan R的安裝似乎是成功的,但是當我加載包,並使用它,我不斷收到指示Rstan錯誤消息未安裝:與R中

> source("http://mc-stan.org/rstan/stan.R") 
    > fit <- stan("7b Class.stan", data=list(N=1187, J=J, y=earn, height=height, male=male, edu=ed, cluster=cluster), iter=1000, chains=4) 
    Fejl i packageDescription("rstan")$Date : 
     $ operator is invalid for atomic vectors 
    In addition: Advarselsbesked: 
    In packageDescription("rstan") : no package 'rstan' was found 

我有一個Mac OS.X 10.7.5。我最近將R升級到3.1.3,並使用R-studio作爲界面。 對於安裝Rstan我遵循了這裏的描述: https://github.com/stan-dev/rstan/wiki/RStan-Mac-OS-X-Prerequisite-Installation-Instructions

我已經安裝了X-代碼和命令行工具(蘋果機OS.X獅子)通過蘋果開發者,當我在R檢查是否正確安裝後,我得到這個消息:

> system('clang++ -v') 
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn) 
Target: x86_64-apple-darwin11.4.2 
Thread model: posix 

當我R中安裝斯坦,我得到以下信息:

> #Install STAN - https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started 
> ###Step 1 - Set numbers of processes to uge 
> Sys.setenv(MAKEFLAGS = "-j4") 
> ###Step 2 - Run the script 
> source('http://mc-stan.org/rstan/install.R', echo = TRUE, max.deparse.length = 2000) 

> install_rstan <- function() { 
+ on.exit(Sys.unsetenv("R_MAKEVARS_USER")) 
+ on.exit(Sys.unsetenv("R_MAKEVARS_SITE"), add = TRUE) 
+ 
+ try(remove.packages("rstan"), silent = TRUE) 
+ Sys.setenv(R_MAKEVARS_USER = "foobar") 
+ Sys.setenv(R_MAKEVARS_SITE = "foobar") 
+ install.packages(c("inline", "BH", "RcppEigen")) 
+ install.packages("Rcpp", type = "source") 
+ library(inline) 
+ library(Rcpp) 
+ src <- ' 
+  std::vector<std::string> s; 
+  s.push_back("hello"); 
+  s.push_back("world"); 
+  return Rcpp::wrap(s); 
+ ' 
+ hellofun <- cxxfunction(body = src, includes = '', plugin = 'Rcpp', verbose = FALSE) 
+ test <- try(hellofun()) 
+ if(inherits(test, "try-error")) stop("hello world failed; ask for help on Rcpp list") 
+ 
+ options(repos = c(getOption("repos"), 
+   rstan = "http://rstan.org/repo/")) 
+ install.packages("rstan", type = 'source') 
+ library(rstan) 
+ set_cppo("fast") 
+ if (any(grepl("^darwin", R.version$os, ignore.case = TRUE))) { 
+  cat('\nCC=clang', 'CXX=clang++ -arch x86_64 -ftemplate-depth-256', 
+   file = "~/.R/Makevars", sep = "\n", append = TRUE) 
+ } 
+ return(invisible(NULL)) 
+ } 
> install_rstan() 
Removing package from ‘/Library/Frameworks/R.framework/Versions/3.1/Resources/library’ 
(as ‘lib’ is unspecified) 
Error in remove.packages : there is no package called ‘rstan’ 
trying URL 'http://cran.rstudio.com/bin/macosx/contrib/3.1/inline_0.3.13.tgz' 
Content type 'application/x-gzip' length 78503 bytes (76 KB) 
opened URL 
================================================== 
downloaded 76 KB 

trying URL 'http://cran.rstudio.com/bin/macosx/contrib/3.1/BH_1.55.0-3.tgz' 
Content type 'application/x-gzip' length 7455235 bytes (7.1 MB) 
opened URL 
================================================== 
downloaded 7.1 MB 

trying URL 'http://cran.rstudio.com/bin/macosx/contrib/3.1/RcppEigen_0.3.2.4.0.tgz' 
Content type 'application/x-gzip' length 3335332 bytes (3.2 MB) 
opened URL 
================================================== 
downloaded 3.2 MB 


The downloaded binary packages are in 
    /var/folders/0x/zkhg5kyd7j3199jkj76v9p340000gn/T//Rtmpk2CfrS/downloaded_packages 
Error in install.packages : Updating loaded packages 

Attaching package: ‘inline’ 

The following object is masked from ‘package:Rcpp’: 

    registerPlugin 

trying URL 'http://rstan.org/repo/src/contrib/rstan_2.6.0.tar.gz' 
Content type 'application/x-gzip' length 1423850 bytes (1.4 MB) 
opened URL 
================================================== 
downloaded 1.4 MB 

ERROR: failed to lock directory ‘/Library/Frameworks/R.framework/Versions/3.1/Resources/library’ for modifying 
Try removing ‘/Library/Frameworks/R.framework/Versions/3.1/Resources/library/00LOCK-rstan’ 
Warning in install.packages : 
    installation of package ‘rstan’ had non-zero exit status 

The downloaded source packages are in 
    ‘/private/var/folders/0x/zkhg5kyd7j3199jkj76v9p340000gn/T/Rtmpk2CfrS/downloaded_packages’ 
Fejl i library(rstan) : there is no package called ‘rstan’ 
Called from: top level 

Restarting R session... 

> install.packages("Rcpp", type = "source") 
trying URL 'http://cran.rstudio.com/src/contrib/Rcpp_0.11.5.tar.gz' 
Content type 'application/x-gzip' length 2353791 bytes (2.2 MB) 
opened URL 
================================================== 
downloaded 2.2 MB 

* installing *source* package ‘Rcpp’ ... 
** package ‘Rcpp’ successfully unpacked and MD5 sums checked 
** libs 
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I../inst/include/ -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" -fPIC -mtune=core2 -g -O2 -c Date.cpp -o Date.o 
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I../inst/include/ -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" -fPIC -mtune=core2 -g -O2 -c Module.cpp -o Module.o 
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I../inst/include/ -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" -fPIC -mtune=core2 -g -O2 -c Rcpp_init.cpp -o Rcpp_init.o 
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I../inst/include/ -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" -fPIC -mtune=core2 -g -O2 -c api.cpp -o api.o 
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I../inst/include/ -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" -fPIC -mtune=core2 -g -O2 -c attributes.cpp -o attributes.o 
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I../inst/include/ -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" -fPIC -mtune=core2 -g -O2 -c barrier.cpp -o barrier.o 
llvm-g++-4.2 -arch x86_64 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -L/usr/local/lib -o Rcpp.so Date.o Module.o Rcpp_init.o api.o attributes.o barrier.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation 
installing to /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/libs 
** R 
** inst 
** preparing package for lazy loading 
** help 
*** installing help indices 
** building package indices 
** installing vignettes 
** testing if installed package can be loaded 
* DONE (Rcpp) 

The downloaded source packages are in 
    ‘/private/var/folders/0x/zkhg5kyd7j3199jkj76v9p340000gn/T/RtmpZSUb93/downloaded_packages’ 

然後我rstan閱讀:library(rstan),我得到了我沒有包叫做Rstan。

我希望有人能看出爲什麼Rstan不會爲我跑。

+0

可能重複的[錯誤:無法找到函數...在R](http://stackoverflow.com/questions/7027288/error-could-nould-find-function-in-r) – jaradniemi 2015-04-03 18:56:13

+0

@Keniajin,請不要將內聯代碼添加到不是代碼的東西,比如產品名稱。 – gunr2171 2015-04-03 19:30:47

回答

0

你永遠不會loaded the package。使用

library(rstan) 

將軟件包加載到您當前的R會話中。

+0

嗨jaraniemi。感謝您的回覆,我在帖子中忘記提及,但是我確實加載了該軟件包。我只是收到錯誤消息,說這個軟件包不存在。另請參閱我上面編輯過的帖子。 – 2015-04-07 22:39:16

+1

因此,您的實際問題是「安裝Rstan」,儘管您的標題是「加載Rstan時出現問題」以及您的問題中的語句,例如「我安裝了Rstan」和「安裝似乎成功」。考慮到安裝產生錯誤,您應該沒有理由相信它已經實際安裝。 – jaradniemi 2015-04-08 15:27:20

0

裝入包通過

require("rstan") 

library("rstan") 

使用require(rstan)因爲它試圖從名爲rstan在環境中的物體加載和未能找到這樣的對象失敗。因此你需要使用倒排引號。