简体   繁体   English

在R公式中使用函数

[英]Use a function in R formula

Any help with this would be greatly appreciated. 任何帮助,将不胜感激。 I am optimising parameters of a lognormal distribution so that the proportion of estimates matches a set of target values (distances). 我正在优化对数正态分布的参数,以使估计比例与一组目标值(距离)匹配。 The proportions are calculated using the following functions: 使用以下函数计算比例:

adj_sumifs <- function(sum_array, condition_array, f, m=1){
n <- length(condition_array)
sm = 0
if (n == length(condition_array)){
  fun <- function(x,i){if (f (condition_array[i])){sum_array[i] + x}else{x} }
  sm <- Reduce(fun,1:n,0)
} 
ifelse(m <= 0, sm , sm/m)

} }

and

estimate.inrange <- function(vals,dist,lower,upper,total){
  n <- length(lower)
  if (n == length(upper)){
    sapply(1:n, function(i){ ifelse(i < n ,
                                adj_sumifs(vals,dist, (function(x) x >= lower[i] && x < upper[i]),total) ,
                                adj_sumifs(vals,dist, (function(x) x >= lower[i]) , total)
                             ) }
          )
  }else{
    # for a failure in the process
    as.numeric()
  }
}

And the function I would like to optimise is: 我想优化的功能是:

calculate_Det_ptns <- function(alpha, beta, pxa, low,up, distances, eF){
  temp <- numeric()
  if ( length(pxa) == length(distances) && length(low) == length(up) )
  {
    ln_values <- as.numeric(Map(function(pa,d) eF * pa * dlnorm(d, meanlog = alpha, sdlog = beta),pxa,distances))
    temp <- estimate.inrange (ln_values,distances,low,up, total = sum(ln_values))
  }
  temp
}

Optimisation is done using the Levenberg-Marquardt algorithm 使用Levenberg-Marquardt算法进行优化

lnVals <- nlsLM(target  ~ calculate_Det_ptns(alpha = a,beta = b, pxa = odab,low = low, up = up, distances = dist, eF = expF),
                          start = list(a = mu, b = sd ), 
                          trace = T) 

where up,low and target are extracted from the same data file, e,g, 从相同的数据文件中提取上,下和目标,例如

low, up, target
1,2,0.1
2,3,0.4
3,4,0.6
4,5,0.6
5,6,0.9

while odab and distance are vectors of arbitrary lengths (usually much longer than target,etc). odabdistance是任意长度的向量(通常比目标长得多,等等)。 The process works well when the target file has anout 150 rows, and distances and odab have about 500000 values. 当目标文件有150行,并且distancesodab具有大约500000值时,该过程运行良好。 However, for reasons I cannot fathom, is fails when the target file has about 16 rows. 但是,由于我无法理解的原因,当目标文件具有约16行时,is失败。 The error message is: 错误消息是:

Error in model.frame.default(formula = ~target + odab + low + up + dist) : 
  variable lengths differ (found for 'odab')

which suggests that the function is not being evaluated in the formula. 这表明该函数未在公式中求值。 Can anyone suggest a solution or explanation? 谁能提出解决方案或解释? It is important that the proportions are re-estimated for every new mu and sd. 重要的是要重新估计每个新亩和标准房的比例。

You could try surrounding the function with I(), which will evaluate it as is before evaluating the formula; 您可以尝试用I()包围该函数,该函数将在评估公式之前按原样对其进行评估; however, I could not replicate your problem with the code provided because I am missing some of the referenced objects (a, b, odab, dist, expF, mu, sd) so I could not confirm whether or not this works. 但是,由于我缺少某些引用的对象(a,b,odab,dist,expF,mu,sd),因此我无法使用提供的代码来复制您的问题,因此我无法确认这是否可行。 nVals <- nlsLM(target ~ I(calculate_Det_ptns(alpha = a,beta = b, pxa = odab,low = low, up = up, distances = dist, eF = expF)), start = list(a = mu, b = sd ), trace = T)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM