全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
1381 1
2020-09-22

跟我学Rstudio编译C++文件

经管之家:Do the best economic and management education!
Author: Danielu tulips liu © tulipsliu

以前没注意这个文章的版面分布;

今天特意再次修改, 让运行出来的 shell 文件更加的清晰。

修改日期 , today , 2021 - 10 - 01

其实最近三年很累,一直没有休息, 我真的很想有钱的话,去某个景色迷人的地方旅行一次。

有景色的地方,一定很迷人。

教学开始

参考链接 https://cran.r-project.org/web/packages/GPRMortality/GPRMortality.pdf

Rstduio Make file and C++ code


> fit = GPRMortality(mortality,mean,minYear=1990,maxYear=2015,
+                    nu = 2,rho_ =0.4 ,product = 0.1 ,verbose=TRUE)

TRANSLATING MODEL 'cd2c3fff62fe816545ca31c72f3a930a' FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model 'cd2c3fff62fe816545ca31c72f3a930a'.
COMPILING THE C++ CODE FOR MODEL 'cd2c3fff62fe816545ca31c72f3a930a' NOW.
OS: x86_64, mingw32; rstan: 2.21.2; Rcpp: 1.0.5; inline: 0.3.16 
 >> setting environment variables: 
LOCAL_LIBS =  "D:/Softwares/R-4.0.2/library/rstan/lib/x64/libStanServices.a" -L"D:/Softwares/R-4.0.2/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/tulipsliu/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb
PKG_CPPFLAGS =   -I"C:/Users/tulipsliu/Documents/R/win-library/4.0/Rcpp/include/"  -I"D:/Softwares/R-4.0.2/library/RcppEigen/include/"  -I"D:/Softwares/R-4.0.2/library/RcppEigen/include/unsupported"  -I"D:/Softwares/R-4.0.2/library/BH/include" -I"D:/Softwares/R-4.0.2/library/StanHeaders/include/src/"  -I"D:/Softwares/R-4.0.2/library/StanHeaders/include/"  -I"C:/Users/tulipsliu/Documents/R/win-library/4.0/RcppParallel/include/"  -I"D:/Softwares/R-4.0.2/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "D:/Softwares/R-4.0.2/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y
 >> Program source :

   1 : 
   2 : // includes from the plugin
   3 : // [[Rcpp::plugins(cpp14)]]
   4 : 
   5 : 
   6 : // user includes
   7 : #include <Rcpp.h>
   8 : #include <rstan/io/rlist_ref_var_context.hpp>
   9 : #include <rstan/io/r_ostream.hpp>
  10 : #include <rstan/stan_args.hpp>
  11 : #include <boost/integer/integer_log2.hpp>
  12 : // Code generated by Stan version 2.21.0
  13 : 
  14 : #include <stan/model/model_header.hpp>
  15 : 
  16 : namespace model15d040116c93_cd2c3fff62fe816545ca31c72f3a930a_namespace {
  17 : 
  18 : using std::istream;
  19 : using std::string;
  20 : using std::stringstream;
  21 : using std::vector;
  22 : using stan::io::dump;
  23 : using stan::math::lgamma;
  24 : using stan::model::prob_grad;
  25 : using namespace stan::math;
  26 : 
  27 : static int current_statement_begin__;
  28 : 
  29 : stan::io::program_reader prog_reader__() {
  30 :     stan::io::program_reader reader;
  31 :     reader.add_event(0, 0, "start", "model15d040116c93_cd2c3fff62fe816545ca31c72f3a930a");
  32 :     reader.add_event(38, 36, "end", "model15d040116c93_cd2c3fff62fe816545ca31c72f3a930a");
  33 :     return reader;
  34 : }
  35 : 
  36 : class model15d040116c93_cd2c3fff62fe816545ca31c72f3a930a
  37 :   : public stan::model::model_base_crtp<model15d040116c93_cd2c3fff62fe816545ca31c72f3a930a> {
  38 : private:
  39 :         int myT;
  40 :         vector_d M;
  41 :         double lambda;
  42 :         double nu;
  43 :         int Mx;
  44 :         vector_d Y;
  45 :         std::vector<int> YEAR;
  46 :         vector_d SIGMAy;
  47 :         matrix_d C;
  48 : public:
  49 :     model15d040116c93_cd2c3fff62fe816545ca31c72f3a930a(rstan::io::rlist_ref_var_context& context__,
  50 :         std::ostream* pstream__ = 0)
  51 :         : model_base_crtp(0) {
  52 :         ctor_body(context__, 0, pstream__);
  53 :     }
  54 : 
  55 :     model15d040116c93_cd2c3fff62fe816545ca31c72f3a930a(stan::io::var_context& context__,
  56 :         unsigned int random_seed__,
  57 :         std::ostream* pstream__ = 0)
  58 :         : model_base_crtp(0) {
  59 :         ctor_body(context__, random_seed__, pstream__);
  60 :     }
  61 : 
  62 :     void ctor_body(stan::io::var_context& context__,
  63 :                    unsigned int random_seed__,
  64 :                    std::ostream* pstream__) {
  65 :         typedef double local_scalar_t__;
  66 : 
  67 :         boost::ecuyer1988 base_rng__ =
  68 :           stan::services::util::create_rng(random_seed__, 0);
  69 :         (void) base_rng__;  // suppress unused var warning
  70 : 
  71 :         current_statement_begin__ = -1;
  72 : 
  73 :         static const char* function__ = "model15d040116c93_cd2c3fff62fe816545ca31c72f3a930a_namespace::model15d040116c93_cd2c3fff62fe816545ca31c72f3a930a";
  74 :         (void) function__;  // dummy to suppress unused var warning
  75 :         size_t pos__;
  76 :         (void) pos__;  // dummy to suppress unused var warning
  77 :         std::vector<int> vals_i__;
  78 :         std::vector<double> vals_r__;
  79 :         local_scalar_t__ 
 426 :         try {
 427 :             // declare and define transformed parameters
 428 :             current_statement_begin__ = 19;
 429 :             validate_non_negative_index("L", "myT", myT);
 430 :             validate_non_negative_index("L", "myT", myT);
 431 :             Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> L(myT, myT);
 432 :             stan::math::initialize(L, DUMMY_VAR__);
 433 :             stan::math::fill(L, DUMMY_VAR__);
 434 : 
 435 :             current_statement_begin__ = 20;
 436 :             double d;
 437 :             (void) d;  // dummy to suppress unused var warning
 438 :             stan::math::initialize(d, DUMMY_VAR__);
 439 :             stan::math::fill(d, DUMMY_VAR__);
 440 : 
 441 :             current_statement_begin__ = 21;
 442 :             double fpart3;
 443 :             (void) fpart3;  // dummy to suppress unused var warning
 444 :             stan::math::initialize(fpart3, DUMMY_VAR__);
 445 :             stan::math::fill(fpart3, DUMMY_VAR__);
 446 : 
 447 :             current_statement_begin__ = 22;
 448 :             validate_non_negative_index("f", "myT", myT);
 449 :             Eigen::Matrix<double, Eigen::Dynamic, 1> f(myT);
 450 :             stan::math::initialize(f, DUMMY_VAR__);
 451 :             stan::math::fill(f, DUMMY_VAR__);
 452 : 
 453 :             // do transformed parameters statements
 454 :             current_statement_begin__ = 24;
 455 :             stan::math::assign(L, cholesky_decompose(C));
 456 :             current_statement_begin__ = 25;
 457 :             stan::math::assign(f, add(M, multiply(L, tmpf)));
 458 : 
 459 :             if (!include_gqs__ && !include_tparams__) return;
 460 :             // validate transformed parameters
 461 :             const char* function__ = "validate transformed params";
 462 :             (void) function__;  // dummy to suppress unused var warning
 463 : 
 464 :             // write transformed parameters
 465 :             if (include_tparams__) {
 466 :                 size_t L_j_2_max__ = myT;
 467 :                 size_t L_j_1_max__ = myT;
 468 :                 for (size_t j_2__ = 0; j_2__ < L_j_2_max__; ++j_2__) {
 469 :                     for (size_t j_1__ = 0; j_1__ < L_j_1_max__; ++j_1__) {
 470 :                         vars__.push_back(L(j_1__, j_2__));
 471 :                     }
 472 :                 }
 473 :                 vars__.push_back(d);
 474 :                 vars__.push_back(fpart3);
 475 :                 size_t f_j_1_max__ = myT;
 476 :                 for (size_t j_1__ = 0; j_1__ < f_j_1_max__; ++j_1__) {
 477 :                     vars__.push_back(f(j_1__));
 478 :                 }
 479 :             }
 480 :             if (!include_gqs__) return;
 481 :         } catch (const std::exception& e) {
 482 :             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
 483 :             // Next line prevents compiler griping about no return
 484 :             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
 485 :         }
 486 :     }
 487 : 
 488 :     template <typename RNG>
 489 :     void write_array(RNG& base_rng,
 490 :                      Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
 491 :                      Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
 492 :                      bool include_tparams = true,
 493 :                      bool include_gqs = true,
 494 :                      std::ostream* pstream = 0) const {
 495 :       std::vector<double> params_r_vec(params_r.size());
 496 :       for (int i = 0; i < params_r.size(); ++i)
 497 :         params_r_vec[i] = params_r(i);
 498 :       std::vector<double> vars_vec;
 499 :       std::vector<int> params_i_vec;
 500 :       write_array(base_rng, params_r_vec, params_i_vec, vars_vec, include_tparams, include_gqs, pstream);
 510 : 
 511 :     void constrained_param_names(std::vector<std::string>& param_names__,
 512 :                                  bool include_tparams__ = true,
 513 :                                  bool include_gqs__ = true) const {
 514 :         std::stringstream param_name_stream__;
 515 :         size_t tmpf_j_1_max__ = myT;
 516 :         for (size_t j_1__ = 0; j_1__ < tmpf_j_1_max__; ++j_1__) {
 517 :             param_name_stream__.str(std::string());
 518 :             param_name_stream__ << "tmpf" << '.' << j_1__ + 1;
 519 :             param_names__.push_back(param_name_stream__.str());
 520 :         }
 521 : 
 522 :         if (!include_gqs__ && !include_tparams__) return;
 523 : 
 524 :         if (include_tparams__) {
 525 :             size_t L_j_2_max__ = myT;
 526 :             size_t L_j_1_max__ = myT;
 527 :             for (size_t j_2__ = 0; j_2__ < L_j_2_max__; ++j_2__) {
 528 :                 for (size_t j_1__ = 0; j_1__ < L_j_1_max__; ++j_1__) {
 529 :                     param_name_stream__.str(std::string());
 530 :                     param_name_stream__ << "L" << '.' << j_1__ + 1 << '.' << j_2__ + 1;
 531 :                     param_names__.push_back(param_name_stream__.str());
 532 :                 }
 533 :             }
 534 :             param_name_stream__.str(std::string());
 535 :             param_name_stream__ << "d";
 536 :             param_names__.push_back(param_name_stream__.str());
 537 :             param_name_stream__.str(std::string());
 538 :             param_name_stream__ << "fpart3";
 539 :             param_names__.push_back(param_name_stream__.str());
 540 :             size_t f_j_1_max__ = myT;
 541 :             for (size_t j_1__ = 0; j_1__ < f_j_1_max__; ++j_1__) {
 542 :                 param_name_stream__.str(std::string());
 543 :                 param_name_stream__ << "f" << '.' << j_1__ + 1;
 544 :                 param_names__.push_back(param_name_stream__.str());
 545 :             }
 546 :         }
 547 : 
 548 :         if (!include_gqs__) return;
 549 :     }
 550 : 
 551 : 
 552 :     void unconstrained_param_names(std::vector<std::string>& param_names__,
 553 :                                    bool include_tparams__ = true,
 554 :                                    bool include_gqs__ = true) const {
 555 :         std::stringstream param_name_stream__;
 556 :         size_t tmpf_j_1_max__ = myT;
 557 :         for (size_t j_1__ = 0; j_1__ < tmpf_j_1_max__; ++j_1__) {
 558 :             param_name_stream__.str(std::string());
 559 :             param_name_stream__ << "tmpf" << '.' << j_1__ + 1;
 560 :             param_names__.push_back(param_name_stream__.str());
 561 :         }
 562 : 
 
                  param_names__.push_back(param_name_stream__.str());
 573 :                 }
 574 :             }
 575 :             param_name_stream__.str(std::string());
 576 :             param_name_stream__ << "d";
 577 :             param_names__.push_back(param_name_stream__.str());
 578 :             param_name_stream__.str(std::string());
 579 :             param_name_stream__ << "fpart3";
 580 :             param_names__.push_back(param_name_stream__.str());
 581 :             size_t f_j_1_max__ = myT;
 582 :             for (size_t j_1__ = 0; j_1__ < f_j_1_max__; ++j_1__) {
 583 :                 param_name_stream__.str(std::string());
 584 :                 param_name_stream__ << "f" << '.' << j_1__ + 1;
 585 :                 param_names__.push_back(param_name_stream__.str());
 586 :             }
 587 :         }
 588 : 
 589 :         if (!include_gqs__) return;
 590 :     }
 591 : 
 592 : }; // model
 593 : 
 594 : }  // namespace

 610 : 
 611 : 
 612 : #include <rstan_next/stan_fit.hpp>
 613 : 
 614 : struct stan_model_holder {
 615 :     stan_model_holder(rstan::io::rlist_ref_var_context rcontext,
 616 :                       unsigned int random_seed)
 617 :     : rcontext_(rcontext), random_seed_(random_seed)
 618 :      {
 619 :      }
 620 : 
 621 :    //stan::math::ChainableStack ad_stack;
 622 :    rstan::io::rlist_ref_var_context rcontext_;
 623 :    unsigned int random_seed_;
 624 : };
 625 : 
 626 : Rcpp::XPtr<stan::model::model_base> model_ptr(stan_model_holder* smh) {
 627 :   Rcpp::XPtr<stan::model::model_base> model_instance(new stan_model(smh->rcontext_, smh->random_seed_), true);
 628 :   return model_instance;
 629 : }
 630 : 
 631 : Rcpp::XPtr<rstan::stan_fit_base> fit_ptr(stan_model_holder* smh) {
 632 :   return Rcpp::XPtr<rstan::stan_fit_base>(new rstan::stan_fit(model_ptr(smh), smh->random_seed_), true);
 648 : 
 649 : // declarations
 650 : extern "C" {
 651 : SEXP file15d0544912ed( ) ;
 652 : }
 653 : 
 654 : // definition
 655 : 
 656 : SEXP file15d0544912ed(  ){
 657 :  return Rcpp::wrap("cd2c3fff62fe816545ca31c72f3a930a");
 658 : }
 659 : 
 660 : 
make cmd is
  make -f "D:/SOFTWA~1/R-40~1.2/etc/x64/Makeconf" -f "D:/SOFTWA~1/R-40~1.2/share/make/winshlib.mk" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file15d0544912ed.dll" WIN=64 TCLBIN=64 ob jectS="file15d0544912ed.o"

make would use
"C:/rtools40/mingw64/bin/"g++  -std=gnu++14 -I"D:/SOFTWA~1/R-40~1.2/include" -DNDEBUG   -I"C:/Users/tulipsliu/Documents/R/win-library/4.0/Rcpp/include/"  -I"D:/Softwares/R-4.0.2/library/RcppEigen/include/"  -I"D:/Softwares/R-4.0.2/library/RcppEigen/include/unsupported"  -I"D:/Softwares/R-4.0.2/library/BH/include" -I"D:/Softwares/R-4.0.2/library/StanHeaders/include/src/"  -I"D:/Softwares/R-4.0.2/library/StanHeaders/include/"  -I"C:/Users/tulipsliu/Documents/R/win-library/4.0/RcppParallel/include/"  -I"D:/Softwares/R-4.0.2/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "D:/Softwares/R-4.0.2/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y         -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign -c file15d0544912ed.cpp -o file15d0544912ed.o
if test "zfile15d0544912ed.o" != "z"; then \
  if test -e "file15d0544912ed-win.def"; then \
    echo "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file15d0544912ed.dll file15d0544912ed-win.def file15d0544912ed.o   "D:/Softwares/R-4.0.2/library/rstan/lib/x64/libStanServices.a" -L"D:/Softwares/R-4.0.2/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/tulipsliu/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"D:/SOFTWA~1/R-40~1.2/bin/x64" -lR ; \
    "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file15d0544912ed.dll file15d0544912ed-win.def file15d0544912ed.o   "D:/Softwares/R-4.0.2/library/rstan/lib/x64/libStanServices.a" -L"D:/Softwares/R-4.0.2/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/tulipsliu/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"D:/SOFTWA~1/R-40~1.2/bin/x64" -lR ; \
  
    
    "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file15d0544912ed.dll tmp.def file15d0544912ed.o   "D:/Softwares/R-4.0.2/library/rstan/lib/x64/libStanServices.a" -L"D:/Softwares/R-4.0.2/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/tulipsliu/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"D:/SOFTWA~1/R-40~1.2/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \

教学结束

欢迎以后多交流。 我认为一个能开发 R 工具包以及实现MATLAB 和 FORTRAN 混合建模《动态宏观经济学》(DSGE 建模的)。 才算是有趣的。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2020-9-22 14:36:34
太长了,删除了一半的内容。不过其实没具体讲原理。
$ \sigma_t = \Phi * \beta^2 $
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群