首页 > 解决方案 > 在 R 中使用多个参数进行优化

问题描述

我每个月都有一些数据点:

datapoints = c(0.166247133507398, 0.425481263534677, 0.411408800035613, 0.435293979295827, 
0.423245332292126, 0.346365073582275, 0.425199833179369, 0.432137205696319, 
0.43081430546139, 0.432920308468688, 0.453136718369095, 0.455262858225237, 
0.455296396323467, 0.463279988479843, 0.476385172463429, 0.249218233044765, 
0.471649455997085, 0.478360455837937, 0.476885553827009, 0.478454365885292, 
0.469363359162398, 0.482051012672114, 0.472681867759822, 0.473375139224335, 
0.466772123979772, 0.470227689772718, 0.31968277436218, 0.469224848893103, 
0.462242410659246, 0.460592456233742, 0.47620986576363, 0.479597725173361, 
0.463367143161687, 0.445818446073818, 0.448169906772321, 0.469654391229704, 
0.448246061325598, 0.371469823189851, 0.444850426677065, 0.458648112858549, 
0.447860339537274, 0.459419695588122, 0.460269819008956, 0.47749167939716, 
0.473072390305622, 0.476016821990266, 0.4568557470378, 0.449672268375193, 
0.380564883000597, 0.444441519806569, 0.460583921380099, 0.461494033891503, 
0.469138527283594, 0.45150277521197, 0.46718546076104, 0.477345167601084, 
0.472688714814486, 0.487739306383874, 0.480842942430047, 0.391863568128969, 
0.480438541452507, 0.489438220266693, 0.476177921939346, 0.473645046232311, 
0.465869413362134, 0.455922096430803, 0.461787856186048, 0.467509464988477, 
0.484448645266681, 0.471468467400354, 0.395605537227465, 0.479981910572418, 
0.48446619841535, 0.435164680279421, 0.450079104167341, 0.44282935863006, 
0.459921867384553, 0.432157339753678, 0.457218806281876, 0.50047675208151, 
0.436606373021543, 0.40761740347071, 0.415360136419418, 0.427831358793833, 
0.432500363208447, 0.462355479936914, 0.427092963784101, 0.456745139905428, 
0.457223277757524, 0.459387550114517, 0.490044004170164, 0.436484845722895, 
0.420663824652525, 0.435432613404983, 0.432974547875276, 0.457827421432496, 
0.488379378067953, 0.482342084402802, 0.475817074700216, 0.432648247480694, 
0.396584389592281, 0.449595227767319, 0.457362567053931, 0)
months= c(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, 
-15, -16, -17, -18, -19, -20, -21, -22, -23, -24, -25, -26, -27, 
-28, -29, -30, -31, -32, -33, -34, -35, -36, -37, -38, -39, -40, 
-41, -42, -43, -44, -45, -46, -47, -48, -49, -50, -51, -52, -53, 
-54, -55, -56, -57, -58, -59, -60, -61, -62, -63, -64, -65, -66, 
-67, -68, -69, -70, -71, -72, -73, -74, -75, -76, -77, -78, -79, 
-80, -81, -82, -83, -84, -85, -86, -87, -88, -89, -90, -91, -92, 
-93, -94, -95, -96, -97, -98, -99, -100, -101, -102, -103)

我想在数据点上拟合以下函数来估计 a、b 和 c 参数,以最小化原始数据点(曲线)和拟合数据点之间的平方差之和。

Function -> a*exp((b+c*(-numberofmonths ))/(-numberofmonths))

我尝试在 R 中使用 optimix,如下所示:

library(optimx)

#Create function 
fittedCCF.f<- function(a,b,c){
  optimum<-a*exp((b+c*(-months))/(-months))
  return(optimum)
}

#Run optimisation on function to determine a,b and c parameters
result=optimx(datapoints,fittedCCF.f)

以下错误显示:

Error in optimx.check(par, optcfg$ufn, optcfg$ugr, optcfg$uhess, lower,  : 
  Cannot evaluate function at initial parameters

标签: r

解决方案


首先,你的datapointsmonths向量应该是相同的长度。

length(datapoints)
# 104
length(months)
# 103

因此,为了使计算成为可能,我丢弃了datapoints.

n <- length(datapoints)
datapoints <- datapoints[-n]

然后,我没有使用optimx库,这里是基本 R 函数的示例optim

opt_result <- optim(
    par = c(1, 1, 1)
    , fn = function(p) {
        a <- p[1]; b <- p[2]; c <- p[3]
        sq_err <- sum((datapoints - a*exp((b+c*(-months))/(-months)))^2)/(n-1)
    }
    , method = "L-BFGS-B"
)

opt_result
# $par
# [1]  0.2858336 -0.6021726  0.4767209
# 
# $value
# [1] 0.001354874
# 
# $counts
# function gradient 
# 17       17 
# 
# $convergence
# [1] 0
# 
# $message
# [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"

推荐阅读