楼主: 南冰
96459 299

急:R软件加载程序包tsDyn问题 [推广有奖]

171
iooo 发表于 2012-4-14 19:40:02
epoh 发表于 2012-4-14 19:22
呵呵,iooo兄是行家.
的确是这样.
Transition function
恩,谢谢。没有怎么仔细的去看代码

172
雁茗轩 发表于 2012-4-14 23:43:41
epoh 发表于 2012-4-14 19:37
library(tsDyn)
nan=read.table("cpo.txt", header = TRUE)
y=nan$CP
真的非常感谢您,您是德艺双馨的前辈!我重新安装了一遍R和程序包,可以估计了

173
雁茗轩 发表于 2012-4-15 01:45:58
雁茗轩 发表于 2012-4-14 23:43
真的非常感谢您,您是德艺双馨的前辈!我重新安装了一遍R和程序包,可以估计了
老师您好!我把模型估计出来了,想对残差做个ARCH-LM检验,检验时,总是出现要建立一个varest之类的东西,这个要怎么解决呢,谢谢了!老麻烦您真不好意思……

174
epoh 发表于 2012-4-15 07:27:25
雁茗轩 发表于 2012-4-15 01:45
老师您好!我把模型估计出来了,想对残差做个ARCH-LM检验,检验时,总是出现要建立一个varest之类的东西, ...
library(tsDyn)
nan=read.table("cpo.txt", header = TRUE)
y=nan$CP
x=nan$Oil
mod=lstar(y,m=2,d=1,thVar=x,control=list(maxit=3000))
mod
names(mod)
#[1] "str"      "coefficients"   "fitted.values"  "residuals"   "k"            
#[6] "model"    "model.specific"

#########Statistical Tests:
library(fGarch)
# Lagged Series:
     .tslagGarch = function (x, k = 1) {
         ans = NULL
         for (i in k) ans = cbind(ans, .tslag1Garch(x, i))
         indexes = (1:length(ans[, 1]))[!is.na(apply(ans, 1, sum))]
         ans = ans[indexes, ]
         if (length(k) == 1) ans = as.vector(ans)
         ans }
     .tslag1Garch = function (x, k) {
         c(rep(NA, times = k), x[1:(length(x) - k)]) }
     # Statistical Tests:
     cat("\n Residuals Tests:\n")
     r.s = mod$residuals
     ans = NULL
     # Normality Tests:
     jbtest = jarqueberaTest(r.s)@test
     ans = rbind(ans, c(jbtest[1], jbtest[2]))
     if (length(r.s) < 5000) {
         swtest = shapiro.test(r.s)
         if (swtest[2] < 2.6e-16) swtest[2] = 0
         ans = rbind(ans, c(swtest[1], swtest[2]))
     } else {
         ans = rbind(ans, c(NA, NA))
     }
     # Ljung-Box Tests:
     box10 = Box.test(r.s, lag = 10, type = "Ljung-Box")
     box15 = Box.test(r.s, lag = 15, type = "Ljung-Box")
     box20 = Box.test(r.s, lag = 20, type = "Ljung-Box")
     ans = rbind(ans, c(box10[1], box10[3]))
     ans = rbind(ans, c(box15[1], box15[3]))
     ans = rbind(ans, c(box20[1], box20[3]))
     box10 = Box.test(r.s^2, lag = 10, type = "Ljung-Box")
     box15 = Box.test(r.s^2, lag = 15, type = "Ljung-Box")
     box20 = Box.test(r.s^2, lag = 20, type = "Ljung-Box")
     ans = rbind(ans, c(box10[1], box10[3]))
     ans = rbind(ans, c(box15[1], box15[3]))
     ans = rbind(ans, c(box20[1], box20[3]))
     # Ljung-Box Tests - tslag required
     lag.n = 12
     x.s = as.matrix(r.s)^2
     n = nrow(x.s)
     tmp.x = .tslagGarch(x.s[, 1], 1:lag.n)
     tmp.y = x.s[(lag.n + 1):n, 1]
     fit = lm(tmp.y ~ tmp.x)
     stat = (n-lag.n) * summary.lm(fit)$r.squared
     ans = rbind(ans, c(stat, p.value = 1 - pchisq(stat, lag.n)) )
     # Add Names:
     rownames(ans) = c(
         " Jarque-Bera Test   R    Chi^2 ",
         " Shapiro-Wilk Test  R    W     ",
         " Ljung-Box Test     R    Q(10) ",
         " Ljung-Box Test     R    Q(15) ",
         " Ljung-Box Test     R    Q(20) ",
         " Ljung-Box Test     R^2  Q(10) ",
         " Ljung-Box Test     R^2  Q(15) ",
         " Ljung-Box Test     R^2  Q(20) ",
         " LM Arch Test       R    TR^2  ")
     colnames(ans) = c("Statistic", "p-Value")
     print(ans)

                                Statistic p-Value   
Jarque-Bera Test   R    Chi^2  2.378766  0.3044091
Shapiro-Wilk Test  R    W      0.9891328 0.4601097
Ljung-Box Test     R    Q(10)  6.890205  0.735768  
Ljung-Box Test     R    Q(15)  25.69184  0.0413892
Ljung-Box Test     R    Q(20)  33.93066  0.02659757
Ljung-Box Test     R^2  Q(10)  9.998199  0.4406513
Ljung-Box Test     R^2  Q(15)  10.39486  0.7941945
Ljung-Box Test     R^2  Q(20)  13.69091  0.8458153
LM Arch Test       R    TR^2   13.27055  0.3496864
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
ywh19860616 + 5 + 5 + 5 精彩帖子

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

175
雁茗轩 发表于 2012-4-15 10:14:29
epoh 发表于 2012-4-15 07:27
library(tsDyn)
nan=read.table("cpo.txt", header = TRUE)
y=nan$CP
非常感谢您,我好好看看!您真是个好老师……

176
雁茗轩 发表于 2012-4-15 12:29:33
雁茗轩 发表于 2012-4-15 10:14
非常感谢您,我好好看看!您真是个好老师……
老师您好,为什么我自己做的时候,参照您给出来的程序写好,总是出现下面的错误呢:
错误于`rownames<-`(`*tmp*`, value = c(" Jarque-Bera Test   R    Chi^2 ",  :
  'dimnames'的长度[1]必需与陈列范围相等
>      colnames(ans) = c("Statistic", "p-Value")
>      print(ans)
     Statistic p-Value   
[1,] 0.6940057 0.7068033  
[2,] 0.9950685 0.9540148  
[3,] 7.852276  0.6432638  
[4,] 32.20009  0.00604615
[5,] 38.24426  0.008266291
[6,] 5.550793  0.8514875  
[7,] 10.21307  0.8061273  
[8,] 14.72797  0.7917557
而且列名并不显示是哪个统计量?应该出来9个统计量的,这只有8个,是不是我哪个地方开始设置错了?
麻烦您了!

177
epoh 发表于 2012-4-15 14:12:28
雁茗轩 发表于 2012-4-15 12:29
老师您好,为什么我自己做的时候,参照您给出来的程序写好,总是出现下面的错误呢:
错误于`rownames    ...
程序你有修改吧?
请把修改的程序贴出来或贴在短信息

178
雁茗轩 发表于 2012-4-15 14:16:43
epoh 发表于 2012-4-15 14:12
程序你有修改吧?
请把修改的程序贴出来或贴在短信息
数据是我自己估计出来的一个模型,和您上面估计的那个只是换了一个变量而已,样本容量一致,然后在下面的估计中我把残差命名为res,您命名的是r.s.其他的都没有改变,然后出来的结果就是这样子……

> #########Statistical Tests:
> library(fGarch)
> # Lagged Series:
>      .tslagGarch = function (x, k = 1) {
+          ans = NULL
+          for (i in k) ans = cbind(ans, .tslag1Garch(x, i))
+  indexes = (1:length(ans[, 1]))[!is.na(apply(ans, 1, sum))]
+          ans = ans[indexes, ]
+          if (length(k) == 1) ans = as.vector(ans)
+          ans }
>      .tslag1Garch = function (x, k) {
+          c(rep(NA, times = k), x[1:(length(x) - k)]) }
>      # Statistical Tests:
>      cat("\n Residuals Tests:\n")

Residuals Tests:
>      res = mod$residuals
>      ans = NULL
>  # Normality Tests:
>      jbtest = jarqueberaTest(res)@test
>      ans = rbind(ans, c(jbtest[1], jbtest[2]))
>      if (length(res) < 5000) {
+          swtest = shapiro.test(res)
+          if (swtest[2] < 2.6e-16) swtest[2] = 0
+          ans = rbind(ans, c(swtest[1], swtest[2]))
+      } else {
+          ans = rbind(ans, c(NA, NA))
+      }
>      # Ljung-Box Tests:
>      box10 = Box.test(res, lag = 10, type = "Ljung-Box")
>      box15 = Box.test(res, lag = 15, type = "Ljung-Box")
>      box20 = Box.test(res, lag = 20, type = "Ljung-Box")
>      ans = rbind(ans, c(box10[1], box10[3]))
>      ans = rbind(ans, c(box15[1], box15[3]))
>      ans = rbind(ans, c(box20[1], box20[3]))
>      box10 = Box.test(res^2, lag = 10, type = "Ljung-Box")
>      box15 = Box.test(res^2, lag = 15, type = "Ljung-Box")
>      box20 = Box.test(res^2, lag = 20, type = "Ljung-Box")
>      ans = rbind(ans, c(box10[1], box10[3]))
>      ans = rbind(ans, c(box15[1], box15[3]))
>      ans = rbind(ans, c(box20[1], box20[3]))
>      #Ljung-Box Tests - tslag required
>      lag.n = 12
>      x.s = as.matrix(res)^2
>      n = nrow(x.s)
>      tmp.x = .tslagGarch(x.s[, 1], 1:lag.n)
>      tmp.y = x.s[(lag.n + 1):n, 1]
>      fit = lm(tmp.y ~ tmp.x)
>      stat = (n-lag.n) * summary.lm(fit)$resquared
>      ans = rbind(ans, c(stat, p.value = 1 - pchisq(stat, lag.n)) )
> # Add Names:
>      rownames(ans) = c(
+          " Jarque-Bera Test   R    Chi^2 ",
+          " Shapiro-Wilk Test  R    W     ",
+          " Ljung-Box Test     R    Q(10) ",
+          " Ljung-Box Test     R    Q(15) ",
+          " Ljung-Box Test     R    Q(20) ",
+          " Ljung-Box Test     R^2  Q(10) ",
+          " Ljung-Box Test     R^2  Q(15) ",
+          " Ljung-Box Test     R^2  Q(20) ",
+          " LM Arch Test       R    TR^2  ")
错误于`rownames<-`(`*tmp*`, value = c(" Jarque-Bera Test   R    Chi^2 ",  :
  'dimnames'的长度[1]必需与陈列范围相等
>      colnames(ans) = c("Statistic", "p-Value")
>      print(ans)
     Statistic p-Value   
[1,] 0.6940057 0.7068033  
[2,] 0.9950685 0.9540148  
[3,] 7.852276  0.6432638  
[4,] 32.20009  0.00604615
[5,] 38.24426  0.008266291
[6,] 5.550793  0.8514875  
[7,] 10.21307  0.8061273  
[8,] 14.72797  0.7917557

179
epoh 发表于 2012-4-15 14:45:58
雁茗轩 发表于 2012-4-15 14:16
数据是我自己估计出来的一个模型,和您上面估计的那个只是换了一个变量而已,样本容量一致,然后在下面的 ...
nan_res.txt
    nan_res.txt (2.61 KB)
   

180
雁茗轩 发表于 2012-4-15 14:48:30
epoh 发表于 2012-4-15 14:45
nan_res.txt
真是不好意思,太麻烦你了老师!

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

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