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

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

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保存到全局环境中,并将它们替换为在有问题的函数中的那些,那么它就可以正常工作。因此,我认为问题可能与我在矩阵创建中调用函数的方式有关。

  1. get_pi_ij <- function() {
  2. alphas <-
  3. matrix(get_alpha(),
  4. nrow = length(get_alpha()),
  5. ncol = length(get_alpha()),
  6. byrow = FALSE)
  7. betas <-
  8. matrix(get_beta_prelim(),
  9. nrow = length(get_beta_prelim()),
  10. ncol = length(get_beta_prelim()),
  11. byrow = TRUE)
  12. pi_ij <- exp(alphas + betas + gamma * c)
  13. return(pi_ij)
  14. }
  15. get_pi_ij()

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

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

  1. size <- 18
  2. gamma <- -0.07
  3. c <- structure(c(0, 4, 8, 12, 16, 4, 4, 8, 12, 16, 8, 8, 8, 12, 16,
  4. 12, 12, 16, 16, 4, 0, 4, 8, 12, 8, 4, 4, 8, 12, 12, 8, 8, 12,
  5. 16, 12, 12, 16, 16, 8, 4, 0, 4, 8, 12, 8, 4, 8, 12, 16, 12, 8,
  6. 12, 16, 16, 12, 16, 16, 12, 8, 4, 0, 4, 12, 8, 4, 4, 8, 16, 12,
  7. 8, 8, 12, 16, 12, 12, 16, 16, 12, 8, 4, 0, 16, 12, 8, 4, 4, 16,
  8. 12, 8, 8, 8, 16, 12, 12, 16, 4, 8, 12, 12, 16, 0, 4, 8, 12, 16,
  9. 4, 4, 8, 12, 16, 8, 8, 12, 12, 4, 4, 8, 8, 12, 4, 0, 4, 8, 12,
  10. 8, 4, 4, 8, 12, 8, 8, 12, 12, 8, 4, 4, 4, 8, 8, 4, 0, 4, 8, 12,
  11. 8, 4, 8, 12, 12, 8, 12, 12, 12, 8, 8, 4, 4, 12, 8, 4, 0, 4, 12,
  12. 8, 4, 4, 8, 12, 8, 8, 12, 16, 12, 12, 8, 4, 16, 12, 8, 4, 0,
  13. 16, 12, 8, 4, 4, 12, 8, 8, 12, 8, 12, 16, 16, 16, 4, 8, 12, 12,
  14. 16, 0, 4, 8, 12, 16, 4, 8, 12, 8, 8, 8, 12, 12, 12, 4, 4, 8,
  15. 8, 12, 4, 0, 4, 8, 12, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4,
  16. 8, 8, 4, 0, 4, 8, 8, 4, 8, 8, 12, 12, 12, 8, 8, 12, 8, 8, 4,
  17. 4, 12, 8, 4, 0, 4, 8, 4, 4, 8, 16, 16, 16, 12, 8, 16, 12, 12,
  18. 8, 4, 16, 12, 8, 4, 0, 12, 8, 4, 8, 12, 12, 16, 16, 16, 8, 8,
  19. 12, 12, 12, 4, 4, 8, 8, 12, 0, 4, 8, 4, 12, 12, 12, 12, 12, 8,
  20. 8, 8, 8, 8, 8, 4, 4, 4, 8, 4, 0, 4, 4, 16, 16, 16, 12, 12, 12,
  21. 12, 12, 8, 8, 12, 8, 8, 4, 4, 8, 4, 0, 4, 16, 16, 16, 16, 16,
  22. 12, 12, 12, 12, 12, 8, 8, 8, 8, 8, 4, 4, 4, 0),
  23. .Dim = c(19L, 19L),
  24. .Dimnames = list(NULL, c("X1", "X
  25. <details>
  26. <summary>英文:</summary>
  27. Problem: running `get_pi_ij()` gives the error:
  28. `Error in as.vector(x, mode) : cannot coerce type &#39;closure&#39; to vector of type &#39;any&#39;`
  29. `Called from: as.vector(data)`
  30. 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.
  31. get_pi_ij &lt;- function() {
  32. alphas &lt;-
  33. matrix(get_alpha(),
  34. nrow = length(get_alpha()),
  35. ncol = length(get_alpha()),
  36. byrow = FALSE)
  37. betas &lt;-
  38. matrix(get_beta_prelim,
  39. nrow = length(get_beta_prelim()),
  40. ncol = length(get_beta_prelim()),
  41. byrow = TRUE)
  42. pi_ij &lt;- exp(alphas + betas + gamma * c)
  43. return(pi_ij)
  44. }
  45. get_pi_ij()
  46. 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.
  47. **Everything up to the final function works as it is supposed to**
  48. size &lt;- 18
  49. gamma &lt;- -0.07
  50. c &lt;- structure(c(0, 4, 8, 12, 16, 4, 4, 8, 12, 16, 8, 8, 8, 12, 16,
  51. 12, 12, 16, 16, 4, 0, 4, 8, 12, 8, 4, 4, 8, 12, 12, 8, 8, 12,
  52. 16, 12, 12, 16, 16, 8, 4, 0, 4, 8, 12, 8, 4, 8, 12, 16, 12, 8,
  53. 12, 16, 16, 12, 16, 16, 12, 8, 4, 0, 4, 12, 8, 4, 4, 8, 16, 12,
  54. 8, 8, 12, 16, 12, 12, 16, 16, 12, 8, 4, 0, 16, 12, 8, 4, 4, 16,
  55. 12, 8, 8, 8, 16, 12, 12, 16, 4, 8, 12, 12, 16, 0, 4, 8, 12, 16,
  56. 4, 4, 8, 12, 16, 8, 8, 12, 12, 4, 4, 8, 8, 12, 4, 0, 4, 8, 12,
  57. 8, 4, 4, 8, 12, 8, 8, 12, 12, 8, 4, 4, 4, 8, 8, 4, 0, 4, 8, 12,
  58. 8, 4, 8, 12, 12, 8, 12, 12, 12, 8, 8, 4, 4, 12, 8, 4, 0, 4, 12,
  59. 8, 4, 4, 8, 12, 8, 8, 12, 16, 12, 12, 8, 4, 16, 12, 8, 4, 0,
  60. 16, 12, 8, 4, 4, 12, 8, 8, 12, 8, 12, 16, 16, 16, 4, 8, 12, 12,
  61. 16, 0, 4, 8, 12, 16, 4, 8, 12, 8, 8, 8, 12, 12, 12, 4, 4, 8,
  62. 8, 12, 4, 0, 4, 8, 12, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4,
  63. 8, 8, 4, 0, 4, 8, 8, 4, 8, 8, 12, 12, 12, 8, 8, 12, 8, 8, 4,
  64. 4, 12, 8, 4, 0, 4, 8, 4, 4, 8, 16, 16, 16, 12, 8, 16, 12, 12,
  65. 8, 4, 16, 12, 8, 4, 0, 12, 8, 4, 8, 12, 12, 16, 16, 16, 8, 8,
  66. 12, 12, 12, 4, 4, 8, 8, 12, 0, 4, 8, 4, 12, 12, 12, 12, 12, 8,
  67. 8, 8, 8, 8, 8, 4, 4, 4, 8, 4, 0, 4, 4, 16, 16, 16, 12, 12, 12,
  68. 12, 12, 8, 8, 12, 8, 8, 4, 4, 8, 4, 0, 4, 16, 16, 16, 16, 16,
  69. 12, 12, 12, 12, 12, 8, 8, 8, 8, 8, 4, 4, 4, 0),
  70. .Dim = c(19L, 19L),
  71. .Dimnames = list(NULL, c(&quot;X1&quot;, &quot;X2&quot;, &quot;X3&quot;, &quot;X4&quot;, &quot;X5&quot;,
  72. &quot;X6&quot;, &quot;X7&quot;, &quot;X8&quot;, &quot;X9&quot;, &quot;X10&quot;,
  73. &quot;X11&quot;, &quot;X12&quot;, &quot;X13&quot;, &quot;X14&quot;, &quot;X15&quot;,
  74. &quot;X16&quot;, &quot;X17&quot;, &quot;X18&quot;, &quot;X19&quot;)))
  75. set.seed(12345)
  76. h_share &lt;- diff(c(0, sort(runif(size)), 1))
  77. e_share &lt;- diff(c(0, sort(runif(size)), 1))
  78. alpha &lt;- numeric(size + 1)
  79. beta &lt;- numeric(size + 1)
  80. get_beta_prelim &lt;- function() {
  81. a_matrix &lt;- exp(alpha + t(c) * gamma)
  82. beta_prelim &lt;- log(e_share) - log(colSums(a_matrix))
  83. return(beta_prelim)
  84. }
  85. get_beta &lt;- function() {
  86. beta &lt;- get_beta_prelim() - get_beta_prelim()[[1]]
  87. return(beta)
  88. }
  89. get_alpha_prelim &lt;- function() {
  90. b_matrix &lt;- t(exp(beta + t(c) * gamma))
  91. alpha_prelim &lt;- log(h_share) - log(rowSums(b_matrix))
  92. return(alpha_prelim)
  93. }
  94. get_alpha &lt;- function() {
  95. alpha &lt;- get_alpha_prelim() - get_alpha_prelim()[[1]]
  96. return(alpha)
  97. }
  98. get_pi_ij &lt;- function() {
  99. alphas &lt;-
  100. matrix(get_alpha(),
  101. nrow = length(get_alpha()),
  102. ncol = length(get_alpha()),
  103. byrow = FALSE)
  104. betas &lt;-
  105. matrix(get_beta_prelim,
  106. nrow = length(get_beta_prelim()),
  107. ncol = length(get_beta_prelim()),
  108. byrow = TRUE)
  109. pi_ij &lt;- exp(alphas + betas + gamma * c)
  110. return(pi_ij)
  111. }
  112. get_pi_ij()
  113. </details>
  114. # 答案1
  115. **得分**: 0
  116. ```r
  117. get_pi_ij <- function() {
  118. alphas <-
  119. matrix(get_alpha(),
  120. nrow = length(get_alpha()),
  121. ncol = length(get_alpha()),
  122. byrow = FALSE)
  123. betas <-
  124. matrix(get_beta_prelim(), # your were missing "()"
  125. nrow = length(get_beta_prelim()),
  126. ncol = length(get_beta_prelim()),
  127. byrow = TRUE)
  128. pi_ij <- exp(alphas + betas + gamma * c)
  129. return(pi_ij)
  130. }
英文:
  1. get_pi_ij &lt;- function() {
  2. alphas &lt;-
  3. matrix(get_alpha(),
  4. nrow = length(get_alpha()),
  5. ncol = length(get_alpha()),
  6. byrow = FALSE)
  7. betas &lt;-
  8. matrix(get_beta_prelim(), # your were missing &quot;()&quot;
  9. nrow = length(get_beta_prelim()),
  10. ncol = length(get_beta_prelim()),
  11. byrow = TRUE)
  12. pi_ij &lt;- exp(alphas + betas + gamma * c)
  13. return(pi_ij)
  14. }

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:

确定