I have imitated the holtwinter function using a for loop and now i am trying to optimize the value of alpha, beta, gamma. But when i run the optim function it is returning the same value i passed as a starting point. i a, little new to R so seeking help.
i will provide the code i have written and the data sheet i am using.
i have tried optim as per the given format but unable to get the desired result
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
bELOW IS THE data i am using for optimization on which the code is written.
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