0

我正在尝试编写一个灵活的函数,以便我的代码能够编译。

这是第一个功能:

similarity1 = nimbleFunction(
  run = function(x_clus = double(1), mu = double(1, default = 0), m0 = double(1, default = 0), v= double(1, default = 1), 
                 v0 = double(1, default = 1)){
    x_clus1 = x_clus
    m_star <- (1/v * sum(x_clus1) + 1/v0*m0)/(length(x_clus1)/v + 1/v0)
    v_star <- 1/(length(x_clus1)/v + 1/v0)
    post_ans = dnorm(mu, m_star, sqrt(v_star))

    lk_ans <- dnorm(x_clus1, mu, sqrt(v))
    lk_ans1 = prod(lk_ans)
    prior_ans <- dnorm(mu, m0, sqrt(v0))

    ans1 = prior_ans*lk_ans1/post_ans

    # Get without the last one
    x_clus2 = x_clus[1:(length(x_clus)-1)]
    x_clus2 = x_clus2 - mean(x_clus2)
    m_star2 <- (1/v * sum(x_clus2) + 1/v0*m0)/(length(x_clus2)/v + 1/v0)
    v_star2 <- 1/(length(x_clus2)/v + 1/v0)
    post_ans2 = dnorm(mu, m_star2, sqrt(v_star2))

    lk_ans <- dnorm(x_clus2, mu, sqrt(v))
    lk_ans2 = prod(lk_ans)

    prior_ans2 <- dnorm(mu, m0, sqrt(v0))

    ans2 = prior_ans2*lk_ans2/post_ans2

    ans3 = ans1/ans2
  
    returnType(double(1))
    return(ans3)
  }
)

然后我编译它并测试它。

Csimilarity <- compileNimble(similarity1)
Csimilarity(x_clus = c(1,2,3,4,2), mu = 0, m0 = 0, v = 1, v0 = 1)

这给出了所需的输出:

0.02989395

现在我创建了一个名为 practice 的新函数,它调用了similarity1 函数。

practice = nimbleFunction(
  run = function(x_clus = double(1), mu = double(1), m0 = double(1), v= double(1), v0 = double(1), clusters = double(1), new = double(1) 
                 , save_vector = double(1)
                 ){
    
    
    x_clus1 = x_clus
    sim = nimNumeric(length = 4)
    print(sim)
    print(save_vector)
    
    for(i in 1:3){
      x_clus11 = (x_clus1[clusters == i])
      sim1 = similarity1(x_clus, mu, m0, v , v0)
      print(sim1)
#Here is where it messes up
      save_vector[i] = sim1
    }
    
  
    return(save_vector)
    returnType(double(1))
  }
)

然后我运行它以查看它是否有效。

practice(x_clus = c(1,2,3,4,5,6,1,2,4.5,3.2,2), mu = 0, m0 = 0, v = 1, v0 = 1, clusters = c(1,1,1,2,2,2,3,3,3,4,4), new = 3, c(0,0,0))

并获得所需的输出:

0 0 0 0
0 0 0
0.002776602
0.002776602
0.002776602
[1] 0.002776602 0.002776602 0.002776602

但是当我尝试编译时:

Cpractice = compileNimble(practice, showCompilerOutput = TRUE)

我收到一条错误消息:

Compiling
  [Note] This may take a minute.
  [Note] On some systems there may be some compiler warnings that can be safely ignored.
Error: Failed to create the shared library.
"C:/Rtools/mingw64/bin/"g++ -std=gnu++11  -I"C:/PROGRA~1/R/R-41~1.1/include" -DNDEBUG -DR_NO_REMAP -I"C:\Users\nateh\Documents\R\win-library\4.1\nimble\include" -I"" -I"" -DEIGEN_MPL2_ONLY=1 -Wno-misleading-indentation -Wno-ignored-attributes -Wno-deprecated-declarations         -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c P_49_rcFun_R_GlobalEnv68.cpp -o P_49_rcFun_R_GlobalEnv68.o
P_49_rcFun_R_GlobalEnv68.cpp: In function 'NimArr<1, double> rcFun_R_GlobalEnv68(NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&)':
P_49_rcFun_R_GlobalEnv68.cpp:280:31: error: cannot convert 'NimArr<1, double>' to 'double' in assignment
  ARG8_save_vector_[(i) - 1] = sim1;

如果我尝试将其保存到其中,它不会添加到保存向量甚至“sim”对象中。我真的需要它来迭代 3 次并从函数中输出这 3 次。

请帮忙,我已经为此工作了一个多星期。如果有更简单的方法可以做到这一点,那么我全神贯注。

4

0 回答 0