楼主: wxingch.sysu
7737 14

[问答] 如何用R做time series 的prediction [推广有奖]

  • 1关注
  • 0粉丝

初中生

19%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
93 点
帖子
12
精华
0
在线时间
13 小时
注册时间
2011-1-18
最后登录
2013-7-23

楼主
wxingch.sysu 发表于 2012-2-19 11:35:11 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
本人是R的初学者,想请教几个问题:
现在有1992年至2007年的中国人均GDP的数据,目的是要比较 linear model, log linear model, exponeantial model这三个模型,对2008年的人均GDP做forecast,假设并不知道2008年及以后的数据。
问题一:如何以1992至2005年做in sample,以2005至2007做out of sample,比较实际值和预测值,以选择模型呢?关键是如何写R 命令。目前我写到下面:
# ---- Linear Trend Model -----
y <- read.csv(file="data.csv",head=TRUE,sep=",")#input the data with head
print(y)
tsy <- ts(y, start=1992, frequency=1)
plot.ts(tsy,type="l",col="blue",ylab="gdp per capita",xlab="year") #plot the time series
is.ts(tsy)# test whether it is time series
t=1992:2007
lm1<-lm(tsy~t,na.action=NULL)# linear trend model
                             #-- the na.action statement is to retain time series attributes
summary(lm1)
model.matrix(lm1)  #You can view the model matrix (with the dummy variables) this way:
yhat=fitted(lm1) #fitted value of the model
e=residuals(lm1) #forecast error of the model
tsyhat<-ts(fitted(lm1),start=1992, frequency=1)# make the yhat time series
tse<-ts(e,start=1992,frequency=1)  # make the forecast error time series
ts.plot(tsy,type="p",lwd=2,col="red",ylab="GDP per capita",xlab="Year")
lines(tsyhat)
ts.plot(tse,type="l",lwd=2,col="blue",ylab="Forecast error",xlab="Year")
abline(h=0,col="red")
##do the out of sample prediction
years<-c(2007,2008,2009)
b<-predict(lm1,data.frame(x=2008),se.fit = TRUE, scale = NULL, df = Inf,
        interval = c("none", "confidence", "prediction"),
        level = 0.95, type = c("response", "terms"),
        terms = NULL, na.action = na.pass,
        pred.var = res.var/weights, weights = 1)

但是这样predict出来的东西都是1992至2007的,何做2008年的forecast呢?

问题二:如何画prediction的interval到plot上呢,这个一点都不会了。

十万火急啊,下周就要交report了,坐等高手指教,不胜感激!!!!



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Time Series Prediction predict Series Serie prediction 如何

沙发
myyahoo 发表于 2012-2-19 12:00:46
帮顶

藤椅
wxingch.sysu 发表于 2012-2-19 13:16:47
myyahoo 发表于 2012-2-19 12:00
帮顶
谢谢帮顶!

板凳
吉林小王子 在职认证  发表于 2012-2-19 14:37:25
不知道你为什么加那些默认的参数 ,这样可以啊
b<-predict(lm1,data.frame(t=2008:2012),se.fit = TRUE,interval ='prediction')      
b

KE2{E)87~[8DZ215%TUI7BN.jpg (29.79 KB)

KE2{E)87~[8DZ215%TUI7BN.jpg

报纸
wxingch.sysu 发表于 2012-2-19 14:47:16
吉林小王子 发表于 2012-2-19 14:37
不知道你为什么加那些默认的参数 ,这样可以啊
b
谢谢好心人帮忙!那有什么思路吧predict的interval画出来吗,并且和真实值比较,画在一张图上。万分感谢了!不知道这种东西有没有哪里有模板模仿的,如果要自己想实在是太麻烦了。

还想请教一个问题,用plot画图的时候,怎样控制x轴的单位呢,譬如,我的x轴是年,每次都是5年一个刻度,可是我想一年一个刻度,怎么办呢?请不惜指教!

地板
吉林小王子 在职认证  发表于 2012-2-19 15:09:52
y=(1:16)+rnorm(16)*3

tsy <- ts(y, start=1992, frequency=1)
t=1:16
lm1<-lm(tsy~t,na.action=NULL)# linear trend model
b<-predict(lm1,data.frame(t=17:22),se.fit = TRUE,interval ='prediction') ;b

t=17:22
c=data.frame(b[1])
y1=c(c$fit.lwr, c$fit.upr, c$fit.fit);y1
t1=rep(t,3)
plot(t1,y1,type='p')


QY%O9O7]01U7H1E3Y4[[~~U.jpg (24.48 KB)

QY%O9O7]01U7H1E3Y4[[~~U.jpg

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
kk22boy + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

7
吉林小王子 在职认证  发表于 2012-2-19 15:11:45
做模型的时候,一般时间t变量不写成1995~2005这样,而是写成1~25这样,

8
myyahoo 发表于 2012-2-19 15:22:12
wxingch.sysu 发表于 2012-2-19 13:16
谢谢帮顶!
不客气

9
吉林小王子 在职认证  发表于 2012-2-19 15:36:06
plot(t,c[[1]],xlim=c(10,25),ylim=c(1,30),axes=F)
axis(1,seq(10,25,by=5),seq(10,25,by=5))
这样可以每隔5个画一个刻度,详情请看帮助   ?axis

10
吉林小王子 在职认证  发表于 2012-2-19 15:38:07
]WP5CB]3X`_XH~UQ_W4DE.jpg 画出置信区间
plot(t,c[[1]],xlim=c(10,25),ylim=c(1,30),axes=F)
axis(1,seq(10,25,by=5),seq(10,25,by=5))
axis(2)
points(t,c[[2]],col='red',type='p')
points(t,c[[3]],col='red',type='p')

text(16,16.87999,'fit',col='red')
text(16,7.368425,'lower',col='red')
text(16, 26.39156,'upper',col='red')

QY%O9O7]01U7H1E3Y4[[~~U.jpg (24.48 KB)

QY%O9O7]01U7H1E3Y4[[~~U.jpg

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-4 08:43