物价分析
物价分析模块主要分为CPI指数分析预测与翘尾因素分析两个模块,具体实现内容及方式表现如下:
1. CPI指数分析预测
1.1. 数据浏览(数据源及格式)
l 文件类型:txt
l 指标:年月(YYYYMM)、同比CPI指数;
l 指标类型:月度指标;
l 默认起始年份/月份:2012/01(可设置)
年月 | 同比CPI指数 |
201201 |
|
|
|
|
|
图1:CPI指数分析预测--数据原格式
1.2. 建模与分析
1.2.1.工具及算法包
工具:R语言(64位)
算法包(64位):
l tseries
l forecast
1.2.2.代码诠释
library(tseries) #加载算法包tseries
library(forecast) #加载算法包forecast
YEAR <- 2012 #设置初始时间年份
Frequency<- 12 #设置数据类型(月度数据为12,季度数据为4)
p<-1
q<-1
d<-0
P<-2
Q<-0
D<-1
step<-4
data <-read.table('C:/Users/Administrator/Desktop/cpi.txt',head = T)
#读入数据源文件
Data<-ts(data$CPI, freq = Frequency, start = c(YEAR))
#acf(data) #查看自相关图
#pacf(data) #查看偏自相关图
model <-arima(Data,order=c(p,q,d),seasonal=list(order=c(P,Q,D),period= Frequency), include.mean= FALSE, method="ML")
#调用ARIMA模型
#其中参数:
# ① p、q、d、P、Q、D对应的是非季节性参数和季节性参数;
# ② include.mean表示是否带常数项,TRUE为带常数项(默认项,为非差分模型),FALSE为不带常数项。
pred <-forecast(model, h = step, level=c(95))
Fit<- data.frame(pred$fitted) #模型拟合值
Error<-data.frame(model$residuals) #导出残差
#tsdiag(model) #R中残差及相关检验图
Fittable<-cbind(data[,1],data$CPI,Fit,Error) #拟合表
names(Fittable)<-c('Time','CPI','CPIFitted','Residual')
#write.table(Fittable,'C:/Users/Administrator/Desktop/Fittable.txt')
#输出拟合残差结果表
Forecast<-data.frame(cbind(pred$mean,pred$upper,pred$lower))
#预测值、预测值95%上限、预测值95%下限
names(Forecast)<-c('forecast','upper','lower')
lastDt <- data[nrow(data),1]
if(nchar(lastDt)==6){
lastDt <-as.Date(paste(as.character(lastDt),'01'),format='%Y%m%d')
forecastDt<- seq(lastDt,length.out=step+1,by='1 month')
forecastDt<- format(forecastDt,'%Y%m')
monthkey <-as.numeric(forecastDt)
forecast<- data.frame(monthkey=monthkey[2:length(monthkey)],Forecast)
}
if (nchar(lastDt)==4){
lastDt<-as.numeric(lastDt)
forecastDt<- seq(lastDt,lastDt+step,by=1)
Year<- forecastDt
forecast<- data.frame(Year=Year[2:length(Year)],Forecast)
}
write.table(forecast,'C:/Users/Administrator/Desktop/forecast.txt')
#输出预测结果表
1.3. 结果与呈现
l 预测结果表
l 拟合残差表
l 拟合预测结果图
2. 翘尾因素分析
2.1. 数据浏览
l 文件类型:txt
l 指标:年月、居民消费价格指数CPI环比值、居民消费价格指数CPI同比值
l 指标类型:月度指标
时间 | 居民消费价格指数CPI环比 | 居民消费价格指数CPI同比 |
201201 | 1.5 | 4.55 |
|
|
|
图2:翘尾因素分析--数据原格式
2.2. 分析(计算)逻辑
data<-read.table('C:/Users/Administrator/Desktop/qiaowei.txt',head = T)
# 读入数据,Monthkey:年月;CPI_hb:环比CPI;CPI_tb:同比CPI
data$hb2<-data$CPI_hb+100
# 创建变量环比CPI百分数(环比CPI+100)
Data<-data[,c(1,3,4)]
# 创建子集Data(Monthkey,CPI_tb,hb2)
i=1
if(max(data$Monthkey)%%100==12)k=max(data$Monthkey)
k=(max(data$Monthkey)%/%100-1)*100+12
# 取月份为12的最大年份数据
Data<-Data[which(Data$Monthkey<=k),]
# 取子集月份为12的最大年份之前的数据
j=nrow(Data)
# 取数据集行数
rs<-0
for(i in 1:nrow(Data)){
if(i%%12 == 0) rs=0
else rs=Data[i+1,3]
i<-i+1
}
# 滞后一阶取数,12月份数据为0
qw<-0
for(j in nrow(Data):1){
if(data[j,1]%%100 ==12) qw[j]=0
else (
if(data[j,1]%%100 ==11) qw[j]=round(rs[j]-100,3)
else qw[j]=round(rs[j]*(qw[j+1]+100)/100-100,3))
j<-j-1
}
# 翘尾系数计算(上月滞后指数*上月翘尾系数)
Data2<-data[which(data$Monthkey>=data[1,1]+100),c("Monthkey","CPI_tb")]
# 将翘尾系数结果赋值为下一年同月翘尾系数
qw2<-qw[1:nrow(Data2)]
HP<-round(Data2$CPI_tb-qw2,2)
# 新涨价因素计算(本年同比指数-本月翘尾系数)
result<-cbind(Data2,qiaowei=qw2,xzjys=HP)
2.3. 分析结果
l 翘尾计算结果表(年月,CPI_环比值,CPI同比值,翘尾系数,新涨价因素)
l 图形呈现(按年月呈现翘尾系数与新涨价因素趋势变动)