R 返回混合概率密度的函数

R 返回混合概率密度的函数,r,functional-programming,R,Functional Programming,在R中,我想创建一个函数,它执行以下操作: given vectors of means, variances and weights create a function which is a mixture of normal distributions return this function 正态密度的混合物为总和 f(x) = c_1 * N(mu_1,var_1) + ... + c_n * N(mu_n,var_n). 在R中做这件事的简单方法是什么 编辑 我想出了一个非常直截

在R中,我想创建一个函数,它执行以下操作:

given vectors of means, variances and weights
create a function which is a mixture of normal distributions
return this function
正态密度的混合物为总和

f(x) = c_1 * N(mu_1,var_1) + ... + c_n * N(mu_n,var_n). 
在R中做这件事的简单方法是什么

编辑 我想出了一个非常直截了当、也许很幼稚的方法:

gmix <- function(means,vars,weights){
  n <- length(means)

  f<- function(t) { 
    res <- 0;

    for(i in 1:n) {
      res <- res + weights[i]*dnorm(t,mean=means[i],sd=sqrt(vars[i]))
    }
     return(res)
    }
    return(f)
}

gmix有趣的帖子,我当然帮了忙,谢谢。