Error in as.vector(x, mode) : cannot coerce type 'closure' to vector of type 'any' — when running a nested function

huangapple go评论212阅读模式
英文:

Error in as.vector(x, mode) : cannot coerce type 'closure' to vector of type 'any' -- when running a nested function

问题

问题:运行get_pi_ij()时出现错误:

Error in as.vector(x, mode) : cannot coerce type 'closure' to vector of type 'any'

Called from: as.vector(data)

这个函数的第一步是将生成的alphasbeta_prelims转换为与c匹配的矩阵,以便它们可以一起计算。这是出现问题的地方,我无法找出问题所在。如果我在前面的函数中使用<<-alphasbetas保存到全局环境中,并将它们替换为在有问题的函数中的那些,那么它就可以正常工作。因此,我认为问题可能与我在矩阵创建中调用函数的方式有关。

get_pi_ij <- function() {
  alphas <-
    matrix(get_alpha(),
           nrow = length(get_alpha()),
           ncol = length(get_alpha()),
           byrow = FALSE)
  betas <-
    matrix(get_beta_prelim(),
           nrow = length(get_beta_prelim()),
           ncol = length(get_beta_prelim()),
           byrow = TRUE) 
  pi_ij <- exp(alphas + betas + gamma * c)
  return(pi_ij)
}
get_pi_ij()

我添加了完整的代码,因为它不太长,前面的部分只是定义。这样更容易测试。

一切都正常工作,如预期那样

size <- 18 
gamma <- -0.07 

c <- structure(c(0, 4, 8, 12, 16, 4, 4, 8, 12, 16, 8, 8, 8, 12, 16, 
            12, 12, 16, 16, 4, 0, 4, 8, 12, 8, 4, 4, 8, 12, 12, 8, 8, 12, 
            16, 12, 12, 16, 16, 8, 4, 0, 4, 8, 12, 8, 4, 8, 12, 16, 12, 8, 
            12, 16, 16, 12, 16, 16, 12, 8, 4, 0, 4, 12, 8, 4, 4, 8, 16, 12, 
            8, 8, 12, 16, 12, 12, 16, 16, 12, 8, 4, 0, 16, 12, 8, 4, 4, 16, 
            12, 8, 8, 8, 16, 12, 12, 16, 4, 8, 12, 12, 16, 0, 4, 8, 12, 16, 
            4, 4, 8, 12, 16, 8, 8, 12, 12, 4, 4, 8, 8, 12, 4, 0, 4, 8, 12, 
            8, 4, 4, 8, 12, 8, 8, 12, 12, 8, 4, 4, 4, 8, 8, 4, 0, 4, 8, 12, 
            8, 4, 8, 12, 12, 8, 12, 12, 12, 8, 8, 4, 4, 12, 8, 4, 0, 4, 12, 
            8, 4, 4, 8, 12, 8, 8, 12, 16, 12, 12, 8, 4, 16, 12, 8, 4, 0, 
            16, 12, 8, 4, 4, 12, 8, 8, 12, 8, 12, 16, 16, 16, 4, 8, 12, 12, 
            16, 0, 4, 8, 12, 16, 4, 8, 12, 8, 8, 8, 12, 12, 12, 4, 4, 8, 
            8, 12, 4, 0, 4, 8, 12, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 
            8, 8, 4, 0, 4, 8, 8, 4, 8, 8, 12, 12, 12, 8, 8, 12, 8, 8, 4, 
            4, 12, 8, 4, 0, 4, 8, 4, 4, 8, 16, 16, 16, 12, 8, 16, 12, 12, 
            8, 4, 16, 12, 8, 4, 0, 12, 8, 4, 8, 12, 12, 16, 16, 16, 8, 8, 
            12, 12, 12, 4, 4, 8, 8, 12, 0, 4, 8, 4, 12, 12, 12, 12, 12, 8, 
            8, 8, 8, 8, 8, 4, 4, 4, 8, 4, 0, 4, 4, 16, 16, 16, 12, 12, 12, 
            12, 12, 8, 8, 12, 8, 8, 4, 4, 8, 4, 0, 4, 16, 16, 16, 16, 16, 
            12, 12, 12, 12, 12, 8, 8, 8, 8, 8, 4, 4, 4, 0), 
          .Dim = c(19L, 19L), 
          .Dimnames = list(NULL, c("X1", "X

<details>
<summary>英文:</summary>

Problem: running `get_pi_ij()` gives the error: 

`Error in as.vector(x, mode) : cannot coerce type &#39;closure&#39; to vector of type &#39;any&#39;`

`Called from: as.vector(data)`

The first thing this function does is to make the resulting `alphas` and `beta_prelims` into matrixes that match `c` so that they can be calculated together. This is where something goes wrong, and I have not been able to figure out what. If I use `&lt;&lt;-` to save the `alphas` and `betas` to the global environment in the prior functions for alphas and betas and replace that with those in the faulty function, it works. So I assume it has to do with how I call the functions inside the matrix creation. 

    get_pi_ij &lt;- function() {
      alphas &lt;-
        matrix(get_alpha(),
               nrow = length(get_alpha()),
               ncol = length(get_alpha()),
               byrow = FALSE)
      betas &lt;-
        matrix(get_beta_prelim,
               nrow = length(get_beta_prelim()),
               ncol = length(get_beta_prelim()),
               byrow = TRUE) 
      pi_ij &lt;- exp(alphas + betas + gamma * c)
      return(pi_ij)
    }
    get_pi_ij()

I added the full code cause it&#39;s not too long and the first parts are just definitions. Makes it easier to test it. 

**Everything up to the final function works as it is supposed to**

    size &lt;- 18 
    gamma &lt;- -0.07 

    c &lt;- structure(c(0, 4, 8, 12, 16, 4, 4, 8, 12, 16, 8, 8, 8, 12, 16, 
                12, 12, 16, 16, 4, 0, 4, 8, 12, 8, 4, 4, 8, 12, 12, 8, 8, 12, 
                16, 12, 12, 16, 16, 8, 4, 0, 4, 8, 12, 8, 4, 8, 12, 16, 12, 8, 
                12, 16, 16, 12, 16, 16, 12, 8, 4, 0, 4, 12, 8, 4, 4, 8, 16, 12, 
                8, 8, 12, 16, 12, 12, 16, 16, 12, 8, 4, 0, 16, 12, 8, 4, 4, 16, 
                12, 8, 8, 8, 16, 12, 12, 16, 4, 8, 12, 12, 16, 0, 4, 8, 12, 16, 
                4, 4, 8, 12, 16, 8, 8, 12, 12, 4, 4, 8, 8, 12, 4, 0, 4, 8, 12, 
                8, 4, 4, 8, 12, 8, 8, 12, 12, 8, 4, 4, 4, 8, 8, 4, 0, 4, 8, 12, 
                8, 4, 8, 12, 12, 8, 12, 12, 12, 8, 8, 4, 4, 12, 8, 4, 0, 4, 12, 
                8, 4, 4, 8, 12, 8, 8, 12, 16, 12, 12, 8, 4, 16, 12, 8, 4, 0, 
                16, 12, 8, 4, 4, 12, 8, 8, 12, 8, 12, 16, 16, 16, 4, 8, 12, 12, 
                16, 0, 4, 8, 12, 16, 4, 8, 12, 8, 8, 8, 12, 12, 12, 4, 4, 8, 
                8, 12, 4, 0, 4, 8, 12, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 
                8, 8, 4, 0, 4, 8, 8, 4, 8, 8, 12, 12, 12, 8, 8, 12, 8, 8, 4, 
                4, 12, 8, 4, 0, 4, 8, 4, 4, 8, 16, 16, 16, 12, 8, 16, 12, 12, 
                8, 4, 16, 12, 8, 4, 0, 12, 8, 4, 8, 12, 12, 16, 16, 16, 8, 8, 
                12, 12, 12, 4, 4, 8, 8, 12, 0, 4, 8, 4, 12, 12, 12, 12, 12, 8, 
                8, 8, 8, 8, 8, 4, 4, 4, 8, 4, 0, 4, 4, 16, 16, 16, 12, 12, 12, 
                12, 12, 8, 8, 12, 8, 8, 4, 4, 8, 4, 0, 4, 16, 16, 16, 16, 16, 
                12, 12, 12, 12, 12, 8, 8, 8, 8, 8, 4, 4, 4, 0), 
              .Dim = c(19L, 19L), 
              .Dimnames = list(NULL, c(&quot;X1&quot;, &quot;X2&quot;, &quot;X3&quot;, &quot;X4&quot;, &quot;X5&quot;, 
                                       &quot;X6&quot;, &quot;X7&quot;, &quot;X8&quot;, &quot;X9&quot;, &quot;X10&quot;, 
                                       &quot;X11&quot;, &quot;X12&quot;, &quot;X13&quot;, &quot;X14&quot;, &quot;X15&quot;, 
                                       &quot;X16&quot;, &quot;X17&quot;, &quot;X18&quot;, &quot;X19&quot;)))

    set.seed(12345) 
    h_share &lt;- diff(c(0, sort(runif(size)), 1)) 
    e_share &lt;- diff(c(0, sort(runif(size)), 1)) 

    alpha &lt;- numeric(size + 1)
    beta &lt;- numeric(size + 1)
    
    get_beta_prelim &lt;- function() {
      a_matrix &lt;- exp(alpha + t(c) * gamma)
      beta_prelim &lt;- log(e_share) - log(colSums(a_matrix))
      return(beta_prelim)
    }
    
    get_beta &lt;- function() {
      beta &lt;- get_beta_prelim() - get_beta_prelim()[[1]]
      return(beta)
    }
    
    get_alpha_prelim &lt;- function() {
      b_matrix &lt;- t(exp(beta + t(c) * gamma))
      alpha_prelim &lt;- log(h_share) - log(rowSums(b_matrix))
      return(alpha_prelim)
    }
    
    get_alpha &lt;- function() {
      alpha &lt;- get_alpha_prelim() - get_alpha_prelim()[[1]]
      return(alpha)
    }

    get_pi_ij &lt;- function() {
      alphas &lt;-
        matrix(get_alpha(),
               nrow = length(get_alpha()),
               ncol = length(get_alpha()),
               byrow = FALSE)
      betas &lt;-
        matrix(get_beta_prelim,
               nrow = length(get_beta_prelim()),
               ncol = length(get_beta_prelim()),
               byrow = TRUE) 
      pi_ij &lt;- exp(alphas + betas + gamma * c)
      return(pi_ij)
    }
    get_pi_ij()

</details>


# 答案1
**得分**: 0

```r
get_pi_ij <- function() {
  alphas <-
    matrix(get_alpha(),
           nrow = length(get_alpha()),
           ncol = length(get_alpha()),
           byrow = FALSE)
  betas <-
    matrix(get_beta_prelim(), # your were missing "()"
           nrow = length(get_beta_prelim()),
           ncol = length(get_beta_prelim()),
           byrow = TRUE) 
  pi_ij <- exp(alphas + betas + gamma * c)
  return(pi_ij)
}
英文:
get_pi_ij &lt;- function() {
  alphas &lt;-
    matrix(get_alpha(),
           nrow = length(get_alpha()),
           ncol = length(get_alpha()),
           byrow = FALSE)
  betas &lt;-
    matrix(get_beta_prelim(), # your were missing &quot;()&quot;
           nrow = length(get_beta_prelim()),
           ncol = length(get_beta_prelim()),
           byrow = TRUE) 
  pi_ij &lt;- exp(alphas + betas + gamma * c)
  return(pi_ij)
}

huangapple
  • 本文由 发表于 2023年6月1日 18:49:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定