Implementation of the R language of the grey System model GM (a)

Source: Internet
Author: User
Tags abs numeric

1. Modeling

# #建立灰色模型GM the corresponding function

# #x表示原始数据数列, K indicates the number of data

Gm11<-function (x,k)
{
n<-length (x)
x1<-numeric (n);
For (i in 1:n)   # #一次累加
{
x1[i]<-sum (x[1:i]);
}
B<-numeric (n)
m<-n-1 for
(j in 1:m)
{
b[j+1]<-(0.5*x1[j+1]+0.5*x1[j])   # #紧邻均值生成
}
Yn=t (t (x[2:n))                   # #构造Yn矩阵
B<-matrix (1,nrow=n-1,ncol=2)      
b[,1]<-t (t (-b[2:n]))              # #构造B矩阵
A<-solve (t (b)%*%b)%*%t (b)%*%yn;   # #使用最小二乘法求得灰参数a, u
a<-a[1];
u<-a[2];
X2<-numeric (k);
x2[1]<-x[1];
For (i in 1:k-1)
{
x2[1+i]= (x[1]-u/a) *exp (-a*i) +u/a;
}
X2=c (0,X2);
Y=diff (x2);                     # #累减生成, get the predicted data series
y
}



# #x1原始数据数列, X2 is the predictive data series

X1<-x

X2<-GM11 (X,length (x))


# #检验模型精度

Acc<-function (x1,x2)
{
     n<-length (x1);
     sum1=0;
     For (k in 2:n-1)
     {
            sum1<-sum1+ (x1[k]-x1[1]);
     }
     s1<-sum1+0.5* (x1[n]-x1[1]);
     sum2=0;
     For (k in 2:n-1)
     {
            sum2<-sum2+ (x2[k]-x2[1]);
     }
     s2<-sum2+0.5* (x2[n]-x2[1]);
     Abs1<-abs (S1)
     abs2<-abs (S2)
     abs12<-abs (S1-S2)
     ee<-(1+ABS1+ABS2)/(1+ABS1+ABS2+ABS12)
     ee
}



2, Application: Forecast 2013 and 2014 the national scale above express enterprise income


# #x数列是2008年-2012 National Express Enterprise Income data (Source: National Post Office, Unit: billion)


> x<-c (408.40,479.00,574.60,758.00,1055.30)   
> Gm11 (x,7)
[1]  408.4000  443.1355  585.3243  773.1370 1021.2131 1348.8894 1781.7069
> X1<-x
> X2<-gm11 (x,length (x))
> ACC (X1,X2)
[1] 0.9851449



forecast Results: 2013, 2014, the National Express enterprise income of more than 134.9 billion yuan, 178.2 billion yuan

The gray absolute correlation degree is 0.9851449, namely the correlation degree is the first level, the forecast precision is excellent




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.