附录:经典灰色预测模型代码:
x<-c(4643,5853,6876,7847,8724,9425,11045)
x1=NULL
for (i in1:length(x))
{
x1[i]= sum(x[1:i])
}
x2= NULL
for (i in1:(length(x)-1))
{
x2[i]=(x1[i]+x1[i+1])/2
}
y<-x[-1]
z<-x2
lm2= lm(y~z)
a<--(coef(lm2)[2])
b<-coef(lm2)[1]
c= b/a
x;x1;x2;lm2
x5= NULL
x5[1]<-x[1]
for (i in 1:11)#现在是预测五期的,可以改数字
{
x5[i+1]=(x[1]-c)*exp(-a*i)+c
}
x6=NULL
x6<-x5[1]
for (i in1:length(x5))
{
x6[i+1]=x5[i+1]-x5[i]
}
mape=sum(abs((x-x6[1:length(x)])/x))/length(x)
x6;mape
x7<-abs(x6[1:length(x)]-x)
r<-(min(x7)+0.5*max(x7))/(x7+0.5*max(x7))
r1=sum(r)/length(x)
x0=sum(x)/length(x)
s1=sqrt(sum((x-x0)^2)/(length(x)-1))
x70=sum(x7)/length(x)
s2=sqrt(sum((x7-x70)^2)/(length(x)-1))
c1=s2/s1
mape;r1;c1
0.6745*s1;x7
#优化灰色预测模型一代码:
x<-c(4643,5853,6876,7847,8724,9425,11045)
x1=NULL
for (i in1:length(x))
{
x1[i]=sum(x[1:i])
}
x2=NULL
for (i in1:(length(x)-1))
{
x2[i]=sqrt(x1[i]*x1[i+1])
}
y<-x[-1]
z<-x2
lm2=lm(y~z)
a<--(coef(lm2)[2])
b<-coef(lm2)[1]
c=b/a
x;x1;x2;lm2
x5=NULL
x5[1]<-x[1]
for (i in 1:11)
{
x5[i+1]=(x[1]-c)*exp(-a*i)+c
}
x6=NULL
x6<-x5[1]
for (i in1:length(x5))
{
x6[i+1]=x5[i+1]-x5[i]
}
mape=sum(abs((x-x6[1:length(x)])/x))/length(x)
x6;mape
x7<-abs(x6[1:length(x)]-x)
r<-(min(x7)+0.5*max(x7))/(x7+0.5*max(x7))
r1=sum(r)/length(x)
x0=sum(x)/length(x)
s1=sqrt(sum((x-x0)^2)/(length(x)-1))
x70=sum(x7)/length(x)
s2=sqrt(sum((x7-x70)^2)/(length(x)-1))
c1=s2/s1
mape;r1;c1
优化灰色预测模型二代码:
x<-c(4643,5853,6876,7847,8724,9425,11045)
x1=NULL
for (i in1:length(x))
{
x1[i]=sum(x[1:i])
}
x2=NULL
for (i in1:(length(x)-1))
{
x2[i]=0.25*(sqrt(x1[i])+sqrt(x1[i+1]))^2
}
y<-x[-1]
z<-x2
lm2=lm(y~z)
a<--(coef(lm2)[2])
b<-coef(lm2)[1]
c=b/a
x;x1;x2;lm2
x5=NULL
x5[1]<-x[1]
for (i in 1:11)
{
x5[i+1]=(x[1]-c)*exp(-a*i)+c
}
x6=NULL
x6<-x5[1]
for (i in1:length(x5))
{
x6[i+1]=x5[i+1]-x5[i]
}
mape=sum(abs((x-x6[1:length(x)])/x))/length(x)
x6;mape
x7<-abs(x6[1:length(x)]-x)
r<-(min(x7)+0.5*max(x7))/(x7+0.5*max(x7))
r1 =sum(r)/length(x)
x0=sum(x)/length(x)
s1=sqrt(sum((x-x0)^2)/(length(x)-1))
x70=sum(x7)/length(x)
s2=sqrt(sum((x7-x70)^2)/(length(x)-1))
c1=s2/s1
mape;r1;c1
0.6745*s1;x7
x6[1:(length(x)+1)]
优化灰色预测模型三:
x<-c(4643,5853,6876,7847,8724,9425,11045)
x1=NULL
for (i in1:length(x))
{
x1[i]=sum(x[1:i])
}
x2=NULL
for (i in1:(length(x)-1))
{
x2[i]=0.25*(sqrt(x1[i])^2+x1[i+1]^2)
}
y<-x[-1]
z<-x2
lm2=lm(y~z)
a<--(coef(lm2)[2])
b<-coef(lm2)[1]
c=b/a
x;x1;x2;lm2
x5=NULL
x5[1]<-x[1]
for (i in 1:11)
{
x5[i+1]=(x[1]-c)*exp(-a*i)+c
}
x6=NULL
x6<-x5[1]
for (i in1:length(x5))
{
x6[i+1]=x5[i+1]-x5[i]
}
mape=sum(abs((x-x6[1:length(x)])/x))/length(x)
x6;mape
x7<-abs(x6[1:length(x)]-x)
r<-(min(x7)+0.5*max(x7))/(x7+0.5*max(x7))
r1 =sum(r)/length(x)
x0=sum(x)/length(x)
s1=sqrt(sum((x-x0)^2)/(length(x)-1))
x70=sum(x7)/length(x)
s2=sqrt(sum((x7-x70)^2)/(length(x)-1))
c1=s2/s1
mape;r1;c1
0.6745*s1;x7
x6[1:(length(x)+1)]