全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
10648 3
2014-01-10
Dear

The code I copied from http://faculty.chicagobooth.edu/ ... oTS/ch3Rscripts.txt  and output are as follows

Coulld you please help fix the issue such as[Error: could not find function 'backtest']. Thanks
================================================================================
### Chapter 3 ####
### Petro Price
da=read.table("C:/Downloads/ch3data/w-petroprice.txt",header=T)
da1=read.table("C:/Downloads/ch3data/w-gasoline.txt")
pgs=log(da1[,1])
pus=log(da$US)
tdx=c(1:717)/52+1997  # calendar time
par(mfcol=c(2,1))
plot(tdx,pgs,xlab='year',ylab='ln(price)',type='l')
title(main='(a) Gasoline')
plot(tdx,pus,xlab='year',ylab='ln(price)',type='l')
title(main='(b) Crude oil')
dpgs=diff(pgs)
acf(dpgs,lag=20)
pacf(dpgs,lag=20)
m1=ar(diff(pgs),method='mle')
m1$order
t.test(dpgs)
m1=arima(dpgs,order=c(5,0,0),include.mean=F)
m1
m1=arima(dpgs,order=c(5,0,0),include.mean=F,fixed=c(NA,NA,NA,0,NA))
m1
tsdiag(m1,gof=20)
dpus=diff(pus)
m3=lm(dpgs~-1+dpus)
summary(m3)
acf(m3$residuals,lag=20)
pacf(m3$residuals,lag=20)
m4=ar(m3$residuals,method='mle')
m4$order
m4=arima(dpgs,order=c(6,0,0),include.mean=F,xreg=dpus)
m4
m4=arima(dpgs,order=c(5,0,0),include.mean=F,xreg=dpus)
m4
m4=arima(dpgs,order=c(5,0,0),include.mean=F,xreg=dpus,fixed=c(NA,NA,NA,0,NA,NA))
m4
tsdiag(m4,gof=20)
c1=c(NA,NA,NA,0,NA)
pm1=backtest(m1,dpgs,316,1,fixed=c1,inc.mean=F)
c4=c(NA,NA,NA,0,NA,NA)
pm4=backtest(m4,dpgs,316,1,xre=dpus,inc.mean=F,fixed=c4)
tdx=tdx[2:717]
pm4fit=dpgs[317:716]-pm4$error
pm1fit=dpgs[317:716]-pm1$error
plot(tdx[317:716],dpgs[317:716],xlab='year',ylab='growth',type='l')
points(tdx[317:716],pm1fit,pch='*')
plot(tdx[317:716],dpgs[317:716],xlab='year',ylab='growth',type='l')
points(tdx[317:716],pm4fit,pch='*')
m6=lm(dpgs[2:716]~-1+dpus[1:715])
summary(m6)
acf(m6$residuals,lag=20)
pacf(m6$residuals,lag=20)
m7=ar(m6$residuals,method='mle')
m7$order
m7=arima(dpgs[2:716],order=c(9,0,0),include.mean=F,xreg=dpus[1:715])
m7
m7=arima(dpgs[2:716],order=c(9,0,0),include.mean=F,xreg=dpus[1:715],fixed=c(NA,NA,NA,0,NA,0,0,0,NA,NA))
m7
tsdiag(m7,gof=20)
c7=c(NA,NA,NA,0,NA,0,0,0,NA,NA)
pm7=backtest(m7,dpgs[2:716],315,1,xre=dpus[1:715],inc.mean=F,fixed=c7)

======================================================

R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> ### Chapter 3 ####
> ### Petro Price
> da=read.table("C:/Downloads/ch3data/w-petroprice.txt",header=T)
> da1=read.table("C:/Downloads/ch3data/w-gasoline.txt")
> pgs=log(da1[,1])
> pus=log(da$US)
> tdx=c(1:717)/52+1997  # calendar time
> par(mfcol=c(2,1))
> plot(tdx,pgs,xlab='year',ylab='ln(price)',type='l')
> title(main='(a) Gasoline')
> plot(tdx,pus,xlab='year',ylab='ln(price)',type='l')
> title(main='(b) Crude oil')
> dpgs=diff(pgs)
> acf(dpgs,lag=20)
> pacf(dpgs,lag=20)
> m1=ar(diff(pgs),method='mle')
> m1$order
[1] 5
> t.test(dpgs)

        One Sample t-test

data:  dpgs
t = 1.3062, df = 715, p-value = 0.1919
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-0.0005536374  0.0027549956
sample estimates:
  mean of x
0.001100679

> m1=arima(dpgs,order=c(5,0,0),include.mean=F)
> m1

Call:
arima(x = dpgs, order = c(5, 0, 0), include.mean = F)

Coefficients:
         ar1     ar2     ar3      ar4      ar5
      0.5073  0.0788  0.1355  -0.0360  -0.0862
s.e.  0.0372  0.0417  0.0415   0.0417   0.0372

sigma^2 estimated as 0.0003262:  log likelihood = 1857.85,  aic = -3703.71
> m1=arima(dpgs,order=c(5,0,0),include.mean=F,fixed=c(NA,NA,NA,0,NA))
Warning message:
In arima(dpgs, order = c(5, 0, 0), include.mean = F, fixed = c(NA,  :
  some AR parameters were fixed: setting transform.pars = FALSE
> m1

Call:
arima(x = dpgs, order = c(5, 0, 0), include.mean = F, fixed = c(NA, NA, NA,
    0, NA))

Coefficients:
         ar1     ar2     ar3  ar4      ar5
      0.5036  0.0789  0.1220    0  -0.1009
s.e.  0.0370  0.0418  0.0385    0   0.0330

sigma^2 estimated as 0.0003265:  log likelihood = 1857.48,  aic = -3704.96
> tsdiag(m1,gof=20)
> dpus=diff(pus)
> m3=lm(dpgs~-1+dpus)
> summary(m3)

Call:
lm(formula = dpgs ~ -1 + dpus)

Residuals:
      Min        1Q    Median        3Q       Max
-0.076149 -0.008834  0.000365  0.009441  0.151350

Coefficients:
     Estimate Std. Error t value Pr(>|t|)   
dpus  0.28703    0.01507   19.05   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.01839 on 715 degrees of freedom
Multiple R-squared:  0.3366,    Adjusted R-squared:  0.3357
F-statistic: 362.8 on 1 and 715 DF,  p-value: < 2.2e-16

> acf(m3$residuals,lag=20)
> pacf(m3$residuals,lag=20)
> m4=ar(m3$residuals,method='mle')
> m4$order
[1] 6
> m4=arima(dpgs,order=c(6,0,0),include.mean=F,xreg=dpus)
> m4

Call:
arima(x = dpgs, order = c(6, 0, 0), xreg = dpus, include.mean = F)

Coefficients:
         ar1     ar2     ar3     ar4      ar5      ar6    dpus
      0.3953  0.1634  0.0946  0.0297  -0.0873  -0.0525  0.1927
s.e.  0.0389  0.0400  0.0404  0.0405   0.0400   0.0373  0.0136

sigma^2 estimated as 0.0002524:  log likelihood = 1949.61,  aic = -3883.21
> m4=arima(dpgs,order=c(5,0,0),include.mean=F,xreg=dpus)
> m4

Call:
arima(x = dpgs, order = c(5, 0, 0), xreg = dpus, include.mean = F)

Coefficients:
         ar1     ar2     ar3     ar4      ar5    dpus
      0.4022  0.1621  0.0899  0.0209  -0.1086  0.1914
s.e.  0.0387  0.0401  0.0403  0.0400   0.0371  0.0136

sigma^2 estimated as 0.0002531:  log likelihood = 1948.62,  aic = -3883.23
> m4=arima(dpgs,order=c(5,0,0),include.mean=F,xreg=dpus,fixed=c(NA,NA,NA,0,NA,NA))
Warning message:
In arima(dpgs, order = c(5, 0, 0), include.mean = F, xreg = dpus,  :
  some AR parameters were fixed: setting transform.pars = FALSE
> m4

Call:
arima(x = dpgs, order = c(5, 0, 0), xreg = dpus, include.mean = F, fixed = c(NA,
    NA, NA, 0, NA, NA))

Coefficients:
         ar1     ar2     ar3  ar4      ar5    dpus
      0.4037  0.1642  0.0961    0  -0.1014  0.1911
s.e.  0.0386  0.0399  0.0386    0   0.0345  0.0136

sigma^2 estimated as 0.0002532:  log likelihood = 1948.48,  aic = -3884.95
> tsdiag(m4,gof=20)
> c1=c(NA,NA,NA,0,NA)
> pm1=backtest(m1,dpgs,316,1,fixed=c1,inc.mean=F)
Error: could not find function "backtest"
> c4=c(NA,NA,NA,0,NA,NA)
> pm4=backtest(m4,dpgs,316,1,xre=dpus,inc.mean=F,fixed=c4)
Error: could not find function "backtest"
> tdx=tdx[2:717]
> pm4fit=dpgs[317:716]-pm4$error
Error: object 'pm4' not found
> pm1fit=dpgs[317:716]-pm1$error
Error: object 'pm1' not found
> plot(tdx[317:716],dpgs[317:716],xlab='year',ylab='growth',type='l')
> points(tdx[317:716],pm1fit,pch='*')
Error in xy.coords(x, y) : object 'pm1fit' not found
> plot(tdx[317:716],dpgs[317:716],xlab='year',ylab='growth',type='l')
> points(tdx[317:716],pm4fit,pch='*')
Error in xy.coords(x, y) : object 'pm4fit' not found
> m6=lm(dpgs[2:716]~-1+dpus[1:715])
> summary(m6)

Call:
lm(formula = dpgs[2:716] ~ -1 + dpus[1:715])

Residuals:
      Min        1Q    Median        3Q       Max
-0.088318 -0.011145 -0.000011  0.011391  0.161679

Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
dpus[1:715]  0.18560    0.01716   10.81   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.02093 on 714 degrees of freedom
Multiple R-squared:  0.1408,    Adjusted R-squared:  0.1395
F-statistic:   117 on 1 and 714 DF,  p-value: < 2.2e-16

> acf(m6$residuals,lag=20)
> pacf(m6$residuals,lag=20)
> m7=ar(m6$residuals,method='mle')
> m7$order
[1] 9
> m7=arima(dpgs[2:716],order=c(9,0,0),include.mean=F,xreg=dpus[1:715])
> m7

Call:
arima(x = dpgs[2:716], order = c(9, 0, 0), xreg = dpus[1:715], include.mean = F)

Coefficients:
         ar1     ar2     ar3      ar4      ar5      ar6      ar7     ar8
      0.4559  0.0888  0.1679  -0.0468  -0.0653  -0.0195  -0.0362  0.0797
s.e.  0.0425  0.0410  0.0423   0.0415   0.0416   0.0414   0.0410  0.0408
          ar9  dpus[1:715]
      -0.0882       0.0454
s.e.   0.0373       0.0174

sigma^2 estimated as 0.0003204:  log likelihood = 1861.55,  aic = -3701.1
> m7=arima(dpgs[2:716],order=c(9,0,0),include.mean=F,xreg=dpus[1:715],fixed=c(NA,NA,NA,0,NA,0,0,0,NA,NA))
Warning message:
In arima(dpgs[2:716], order = c(9, 0, 0), include.mean = F, xreg = dpus[1:715],  :
  some AR parameters were fixed: setting transform.pars = FALSE
> m7

Call:
arima(x = dpgs[2:716], order = c(9, 0, 0), xreg = dpus[1:715], include.mean = F,
    fixed = c(NA, NA, NA, 0, NA, 0, 0, 0, NA, NA))

Coefficients:
         ar1     ar2     ar3  ar4      ar5  ar6  ar7  ar8      ar9  dpus[1:715]
      0.4544  0.0877  0.1415    0  -0.0830    0    0    0  -0.0640       0.0406
s.e.  0.0427  0.0413  0.0393    0   0.0345    0    0    0   0.0318       0.0176

sigma^2 estimated as 0.000323:  log likelihood = 1858.7,  aic = -3703.4
> tsdiag(m7,gof=20)
> c7=c(NA,NA,NA,0,NA,0,0,0,NA,NA)
> pm7=backtest(m7,dpgs[2:716],315,1,xre=dpus[1:715],inc.mean=F,fixed=c7)
Error: could not find function "backtest"
>



二维码

扫码加我 拉你入群

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

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

全部回复
2014-1-10 08:22:09
看一下原书是不是加载了其他的package
二维码

扫码加我 拉你入群

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

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

2014-1-10 10:43:44
看了下  还真有backtest包


事先library(backtest)或者require(backtest)就行了。。。。前提是安装了backtest包install.packages("backtest")



类型问题可以查查此函数的根源就可以了
二维码

扫码加我 拉你入群

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

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

2014-1-10 11:58:43
多谢诸位啦!
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群