【发布时间】:2020-02-12 19:03:12
【问题描述】:
我想通过调用上次迭代中的函数来重新定义我的函数。但是我知道这基本上是一种我不想要的递归方式。举个例子,看下面
for(i in 1:3)
{
...
myfunction<-function(y){myfunction(y)*dnorm(full_x[j],mean=y*full_x[j-1],sd=true_eps)}
...
result[[i]]<-myfunction
}
我想要的是,在步骤 t 中,我定义了一个名为 myfunction 的函数,在步骤 t+1 中,我想通过使用最后一步中的 myfunction 重新定义该函数,以便进行一些动态更新。程序不断给我错误:“错误:C 堆栈使用 7971152 太接近限制”。我想问题在于这种“递归方式”。我试图在每个循环中重新标记该函数,但它不起作用。有没有办法解决这个问题?
**一个具体的例子如下
# loop for calculating the density function
for(j in 2:length(full_x))
{
# define the normal density
trued<-function(y){dnorm(full_x[j],mean=y*full_x[j-1],sd=true_eps)}
# joint distribution
# which is normal density multiplied by a piror
if(j>=3)
{
trued<- function(y){trued(y)**true_density(y)}
}
# integration of the density w.r.t. rho
trueint<-integrate(trued, lower = 0, upper = 1)$value
# density function
true_density<-function(y) {trued(y)/trueint}
# save into list
dyn_density_true[[j-1]]<-true_density
}
我想做的是计算一系列密度函数。在第一步中,它只是一个通过其积分加权的正态密度(即后验密度)。在接下来的步骤中,我需要先使用上一步的密度函数,然后再做同样的练习。
【问题讨论】:
-
你能提供更多关于你想要做什么的细节吗?也许让我们抛出和明确和最小的示例,您可以在其中展示旧的和新的“函数定义”。对我来说,这听起来像是 XY problem。