2

我正在编写一些由同事汇编的代码,并expose_functions()brms. 当我得到错误时(一旦我确定我没有粗心大意的东西),我开始了一个新的 R 会话,确保我的所有包等都已更新,然后再次尝试 - 同样的错误。

在这一点上,我去了小插图(这里)并运行了该代码 - 错误完全重复了。

以下是小插图的相关部分:

# Data
data("cbpp", package = "lme4")

# Custom family definition
beta_binomial2 <- custom_family(
        "beta_binomial2", dpars = c("mu", "phi"),
        links = c("logit", "log"), lb = c(NA, 0),
        type = "int", vars = "vint1[n]")

# Stan function definition
stan_funs <- "
        real beta_binomial2_lpmf(int y, real mu, real phi, int T) {
                return beta_binomial_lpmf(y | T, mu * phi, (1 - mu) * phi);
        }
        int beta_binomial2_rng(real mu, real phi, int T) {
                return beta_binomial_rng(T, mu * phi, (1 - mu) * phi);
        }
        "

# Stan variables
stanvars <- stanvar(scode = stan_funs, block = "functions")

# Model fit
fit2 <- brm(
        incidence | vint(size) ~ period + (1|herd), 
        data = cbpp, 
        family = beta_binomial2, 
        stanvars = stanvars
)

# Expose functions
expose_functions(fit2, vectorize = TRUE)

这些都不是原始代码 - 它直接取自小插图。当我运行它时,我生成了 32 个警告和一个错误:

Error: $ operator is invalid for atomic vectors

这与我运行其他代码时发生的情况完全相同。我确定是我而不是代码,但我看不出我在哪里出错了。

以防万一它可以帮助任何人找到解决方案,这里是各种警告的列表(仅限唯一警告)。它们中的大多数看起来“无害”(基于),但有些可能不是。

In file included from file656664715164.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/4.1/Resources/library/RcppEigen/include/RcppEigen.h:25:
In file included from /Library/Frameworks/R.framework/Versions/4.1/Resources/library/RcppEigen/include/RcppEigenForward.h:40:
In file included from /Library/Frameworks/R.framework/Versions/4.1/Resources/library/RcppEigen/include/unsupported/Eigen/SparseExtra:51:
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/RcppEigen/include/unsupported/Eigen/../../Eigen/src/Core/util/ReenableStupidWarnings.h:14:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop
                             ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/err/check_matching_dims.hpp:33:8: warning: unused variable 'error' [-Wunused-variable]
  bool error = false;
       ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/err/hmm_check.hpp:33:7: warning: unused variable 'n_transitions' [-Wunused-variable]
  int n_transitions = log_omegas.cols() - 1;
      ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/fun/gp_matern52_cov.hpp:275:10: warning: unused variable 'neg_root_5' [-Wunused-variable]
  double neg_root_5 = -root_5;
         ^
 /Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/fun.hpp:183:
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/fun/log_mix.hpp:86:13: warning: unused variable 'N' [-Wunused-variable]
  const int N = stan::math::size(theta);
            ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/prob/double_exponential_cdf.hpp:82:10: warning: unused variable 'N' [-Wunused-variable]
  size_t N = max_size(y, mu, sigma);
         ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/prob/gaussian_dlm_obs_rng.hpp:98:7: warning: unused variable 'n' [-Wunused-variable]
  int n = G.rows();  // number of states
      ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/prob/hmm_marginal.hpp:26:13: warning: unused variable 'n_states' [-Wunused-variable]
  const int n_states = omegas.rows();
            ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/prob.hpp:315:
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/prob/std_normal_rng.hpp:23:22: warning: unused variable 'function' [-Wunused-variable]
  static const char* function = "std_normal_rng";
                     ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/prob.hpp:335:
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/prob/von_mises_cdf.hpp:72:10: warning: unused variable 'ck' [-Wunused-variable]
  double ck = 50;
         ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/src/stan/io/dump.hpp:778:15: error: no member named 'validate_dims' in namespace 'stan::io'
    stan::io::validate_dims(*this, stage, name, base_type, dims_declared);
    ~~~~~~~~~~^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/src/stan/model/indexing/rvalue.hpp:610:13: warning: unused variable 'cols' [-Wunused-variable]
  const int cols = rvalue_index_size(idxs.tail_.head_, x_ref.cols());
            ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/src/stan/model/indexing/rvalue_varmat.hpp:241:22: warning: unused variable 'x_cols' [-Wunused-variable]
  const Eigen::Index x_cols = x.cols();
                     ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/rev/core/operator_subtraction.hpp:88:43: warning: lambda capture 'b' is not used [-Wunused-lambda-capture]
                            [avi = a.vi_, b](const auto& vi) mutable {
                                          ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/rev/core/operator_minus_equal.hpp:24:16: note: in instantiation of function template specialization 'stan::math::operator-<double, nullptr>' requested here
  vi_ = (*this - b).vi_;
               ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/core/complex_base.hpp:136:9: note: in instantiation of member function 'stan::math::var_value<double, void>::operator-=' requested here
    re_ -= x;
        ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/core/operator_subtraction.hpp:24:5: note: in instantiation of function template specialization 'stan::math::complex_base<stan::math::var_value<double, void> >::operator-=<int>' requested here
  y -= rhs;
    ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/core/operator_subtraction.hpp:55:20: note: in instantiation of function template specialization 'stan::math::internal::complex_subtract<std::__1::complex<stan::math::var>, int>' requested here
  return internal::complex_subtract(x, y);
                   ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/fun/acosh.hpp:105:31: note: in instantiation of function template specialization 'stan::math::operator-<stan::math::var_value<double, void>, int>' requested here
  auto y = log(z + sqrt(z * z - 1));
                              ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/rev/fun/acosh.hpp:94:32: note: in instantiation of function template specialization 'stan::math::internal::complex_acosh<stan::math::var_value<double, void> >' requested here
  return stan::math::internal::complex_acosh(z);
                               ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/rev/core/operator_addition.hpp:84:43: warning: lambda capture 'b' is not used [-Wunused-lambda-capture]
                            [avi = a.vi_, b](const auto& vi) mutable {
                                          ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/rev/fun/grad_inc_beta.hpp:45:43: note: in instantiation of function template specialization 'stan::math::operator+<int, nullptr>' requested here
    grad_2F1(dF1, dF2, a + b, var(1.0), a + 1, z);
                                          ^

/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/rev/core/operator_addition.hpp:84:43: warning: lambda capture 'b' is not used [-Wunused-lambda-capture]
                            [avi = a.vi_, b](const auto& vi) mutable {
                                          ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/prim/fun/trigamma.hpp:63:31: note: in instantiation of function template specialization 'stan::math::operator+<double, nullptr>' requested here
    value = -trigamma_impl(-x + 1.0) + square(pi() / sin(-pi() * x));
                              ^
/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/include/stan/math/rev/fun/trigamma.hpp:23:44: note: in instantiation of function template specialization 'stan::math::trigamma_impl<stan::math::var_value<double, void> >' requested here
inline var trigamma(const var& u) { return trigamma_impl(u); }
4

1 回答 1

1

我将此添加为答案,因为评论太多了。我想帮你解决这个问题。

1) 版本

首先,请比较版本brms和依赖项。我知道你写道你检查了这个,但这些错误很奇怪!比较它们不会有什么坏处。重要的是要注意我有一台 Mac (OS 12.2.1)。

我使用此代码收集信息;这是我的版本。

# brms and it's dependencies
dPkg <- c("brms", "rstan", "ggplot2", "loo", "posterior", "Matrix", "mgcv", "rstantools",
          "bayesplot", "shinystan", "bridgesampling", "glue", "future", "matrixStats", 
          "nleqslv", "nlme", "coda", "abind", "stats", "utils", "parallel", "grDevices", 
          "backports")

cbind(dPkg, purrr::map(dPkg, ~as.character(packageVersion(.x))))
#       dPkg                      
#  [1,] "brms"           "2.16.3" 
#  [2,] "rstan"          "2.21.3" 
#  [3,] "ggplot2"        "3.3.5"  
#  [4,] "loo"            "2.4.1"  
#  [5,] "posterior"      "1.2.0"  
#  [6,] "Matrix"         "1.4.0"  
#  [7,] "mgcv"           "1.8.38" 
#  [8,] "rstantools"     "2.1.1"  
#  [9,] "bayesplot"      "1.8.1"  
# [10,] "shinystan"      "2.5.0"  
# [11,] "bridgesampling" "1.1.2"  
# [12,] "glue"           "1.6.1"  
# [13,] "future"         "1.24.0" 
# [14,] "matrixStats"    "0.61.0" 
# [15,] "nleqslv"        "3.3.2"  
# [16,] "nlme"           "3.1.155"
# [17,] "coda"           "0.19.4" 
# [18,] "abind"          "1.4.5"  
# [19,] "stats"          "4.1.1"  
# [20,] "utils"          "4.1.1"  
# [21,] "parallel"       "4.1.1"  
# [22,] "grDevices"      "4.1.1"  
# [23,] "backports"      "1.4.1"   

2)expose_functionsrstan

我发现这expose_functions是功能的反刍rstan::expose_stan_functions(),所以我去那里寻找答案。

我找到了这个网站。如果您还没有看过它,您可能会发现它非常有见地。但是,它对当前的问题没有帮助。

rstan包中,他们有一个这个函数的简单示例:

library(rstan)
model_code <-
  '
  functions {
    real standard_normal_rng() {
      return normal_rng(0,1);
   }
  }
'
expose_stan_functions(stanc(model_code = model_code))

它也不提供输出(就像我看到的问题一样expose_functions)。我收到一个警告,它需要一个整数向量列表——但它起作用了。对你起作用吗?如果是这样,我们必须回到模型。

我不确定这个功能的真正目的是什么。输出是函数的名称。所以最后一位的输出是:

# [1] "standard_normal_rng"

你的问题的输出是expose_functions在创建时写在代码中的stans_funs。这是原始的输出expose functions

# [1] "beta_binomial2_lpmf" "beta_binomial2_rng"  

当您运行该库rstan时,它会告诉您建议您在脚本中添加一些内容。我听从了建议。

options(mc.cores = parallel::detectCores()) # added as rec by lib rstan
rstan_options(auto_write = T)               # added as rec by lib rstan

3) 回到模型

如果从工作中调用该函数rstan,那么我想知道您从fit1and的输出中得到了什么fit2。我不认为summary()需要添加整个输出,所以我从模型摘要中强调了一些内容:

适合 1

  • 家庭:二项式;链接:mu = logit
  • 平局:4 条链 w/iter = 2K,预热 = 2K,瘦 = 1,预热后 = 4K

适合 2

  • 家庭:beta_binomial2;链接:mu = logit & phi = 身份
  • 抽奖是一样的fit1

至于loo功能,那是从包派生的。loo 等到检查其他部分之后再深入研究它可能会更好,是吗?

于 2022-02-22T16:19:51.417 回答