尝试使用优化函数优化为for循环编写的函数

时间:2019-08-04 03:51:57

标签: r

我已经使用for循环模仿了holtwinter函数,现在我正在尝试优化alpha,beta和gamma的值。但是,当我运行优化函数时,它返回的是我作为起点传递的相同值。我是R的新手,所以寻求帮助。

我将提供我编写的代码和我正在使用的数据表。

我尝试按照给定的格式进行优化,但无法获得预期的结果

sol3<-function(par){
  bal_data<- read.csv("C:/Gitaish_bkp/gitaish/Oracle/ittemar/kpmg docs/hw/bal_data.csv", header=TRUE,sep=",",stringsAsFactors =FALSE)
  final_trend<-data.frame(u= numeric(0))
  #alpha<-par[1]
  #beta<-par[2]
  #gamma<-par[3]
  for (i in 5:11){
    data_cur<-as.numeric(bal_data$data[i])
    data_u_prev<-as.numeric(bal_data$u[i-1])
    data_v_prev<-as.numeric(bal_data$v[i-1])
    bal_data$u[i]<-(x[1]*data_cur/bal_data$forecast[i-4]+(1-x[1])*(data_u_prev+data_v_prev))
    data_u_cur<-as.numeric(bal_data$u[i])
    bal_data$v[i]<-(x[2]*(data_u_cur-data_u_prev)+(1-x[2])*data_v_prev)
    data_v_cur<-as.numeric(bal_data$v[i])
    bal_data$forecast[i]<-(x[3]*(bal_data$data[i]/data_u_cur)+(1-x[3])*bal_data$forecast[i-4])
    bal_data$PV[i]<-(data_u_prev+data_v_prev)*bal_data$forecast[i-4]
    i<-i+1
    dt<-sum(bal_data$data)
    pred<-sum(bal_data$pv)
   return(dt^2-pred^2)
  }
}

x<-c(0.9,0.9,0.9)
result<-optim(x,sol3)

> result$par
[1] 0.9 0.9 0.9

下面是我用来优化代码的数据。

data                 u          v     forecast
11551714.57                   1.0506142
10860713.81                   0.9877685
10989780.35                   0.9995069
10578597.92  10995202   0     0.9621104
10343260.81             0   
10481946.15         
10144200.01         
10486943.26         
10701326.12         
10530507.92         
10633318.04

2 个答案:

答案 0 :(得分:0)

抱歉,我只是尝试了一些选项,实际上代码发布在下面,其中包含par变量。

gttt1<-function(par){
  bal_data<- read.csv("C:/Gitaish_bkp/gitaish/Oracle/ittemar/kpmg docs/hw/bal_data.csv", header=TRUE,sep=",",stringsAsFactors =FALSE)
  final_trend<-data.frame(u= numeric(0))
  #alpha<-0.5
  #beta<-0
  #gamma<-0.76
  for (i in 5:11){
    data_cur<-as.numeric(bal_data$data[i])
    data_u_prev<-as.numeric(bal_data$u[i-1])
    data_v_prev<-as.numeric(bal_data$v[i-1])
    bal_data$u[i]<-(par[1]*data_cur/bal_data$forecast[i-4]+(1-par[1])*(data_u_prev+data_v_prev))
    data_u_cur<-as.numeric(bal_data$u[i])
    bal_data$v[i]<-(par[2]*(data_u_cur-data_u_prev)+(1-par[2])*data_v_prev)
    data_v_cur<-as.numeric(bal_data$v[i])
    bal_data$forecast[i]<-(par[3]*(bal_data$data[i]/data_u_cur)+(1-par[3])*bal_data$forecast[i-4])
    bal_data$PV[i]<-(data_u_prev+data_v_prev)*bal_data$forecast[i-4]
    i<-i+1
    dt<-sum(bal_data$data)
    pred<-sum(bal_data$pv)
    return(dt^2-pred^2)
  }
}

result<-optim(par=c(0,0,0),gttt1)
result$par
[1] 0 0 0

答案 1 :(得分:0)

输入数据帧的输出输出

structure(list(row_values = 1:11, cumm_days = c(NA, 28L, 57L, 
89L, 119L, 148L, 180L, 209L, 237L, 270L, 301L), Days_between = c(NA, 
28L, 29L, 32L, 30L, 29L, 32L, 29L, 28L, 33L, 31L), Dates = c("22-08-2017", 
"19-09-2017", "18-10-2017", "19-11-2017", "19-12-2017", "17-01-2018", 
"18-02-2018", "19-03-2018", "16-04-2018", "19-05-2018", "19-06-2018"
), data = c(11551714.57, 10860713.81, 10989780.35, 10578597.92, 
10343260.81, 10481946.15, 10144200.01, 10486943.26, 10701326.12, 
10530507.92, 10633318.04), u = c(NA, NA, NA, 10995202, NA, NA, 
NA, NA, NA, NA, NA), v = c(NA, NA, NA, 0L, 0L, NA, NA, NA, NA, 
NA, NA), forecast = c(1.0506142, 0.9877685, 0.9995069, 0.9621104, 
NA, NA, NA, NA, NA, NA, NA), PV = c(NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA), alpha = c(0.330281472, 0.330281472, 0.330281472, 
0.330281472, 0.330281472, 0.330281472, 0.330281472, 0.330281472, 
0.330281472, 0.330281472, 0.330281472), beta = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), gamma = c(0.529463573, 0.529463573, 
0.529463573, 0.529463573, 0.529463573, 0.529463573, 0.529463573, 
0.529463573, 0.529463573, 0.529463573, 0.529463573), To_be_used = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, 
-11L))

输出数据帧的Dput输出

list(par = c(0, 0, 0), value = 13759831687347294, counts = c(function = 4L, gradient = NA), convergence = 0L, message = NULL)