楼主: 南冰
95828 299

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

51
南冰 发表于 2011-3-20 14:53:45
前辈您好!我在R中估计STAR模型时出现了下面的提示:
> xx<-star(x,m=3,d=2,control=list(maxit=3000))
Using default threshold variable: thDelay=0
Testing linearity...   p-Value =  6.281377e-08
The series is nonlinear. Incremental building procedure:
Building a 2 regime STAR.
Using default threshold variable: thDelay=0
Performing grid search for starting values...
Starting values fixed: gamma =  40 , th =  10.80275 ; SSE =  576.5637
Optimization algorithm converged
Optimized values fixed for regime 2  : gamma =  40.00044 , th =  10.76126
Testing for addition of regime 3.
  Estimating gradient matrix...
  Done. Computing the test statistic...
  Done. Regime 3 is needed (p-Value =  0.01466867 ).
Adding regime  3 .
Fixing good starting values for regime  3 ...
错误于if (cost <= bestCost) { : 需要TRUE/FALSE值的地方不可以用缺少值
此外: 警告信息:
In rbind(object$model.specific$phi1, rnorm(3)) :
  number of columns of result is not a multiple of vector length (arg 2)
这是不是因为数据本身的原因出现的呢?如果不是的话怎么修正呢?谢谢!
2# epoh
一直怀有一个梦想,希望在不久的将来能读个博士,做做学术搞搞研究,饱尝学术的艰辛

52
epoh 发表于 2011-3-21 15:11:46
Nonlinear autoregressive time series models in R
    [url]http://cran.r-project.org/web/packages/tsDyn/index.html[/url]
m: embedding dimension
d: the time delay  
s: the forecasting steps

thDelay: stands for 'delta',
         and must be an integer number between 0 and m-1.
         see page 7.(3.2 SETAR models)

mTh: stands for 'beta',
     and takes the form of a vector of real coefficients of length m.
     see page 7.(3.2 SETAR models)

mL,mH:
If not specified, mL and mH defaults to m. One can decide also only to select a
few values between 1:mL and 1:mH. This is possible using mL and mH. Hence to
have a first regime with lag 1 and 3, the second with all 3, would be: mL=c(1,3)
and mH=1:3.

phi1,phi2: see page 7.(3.2 SETAR models) formula(4)
            or page 9.(3.3 LSTAR models) generalization of SETAR

Q2:
  错误于if (cost <= bestCost)....
  方便的话上传数据,以利判断.
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
flyboy + 5 + 5 + 5 对论坛有贡献

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

53
南冰 发表于 2011-3-21 16:34:44
十分感谢,看到你在线真的很高兴,问题急需解决!下面是程序,附件里是数据。
> getwd()
[1] "d:/我的文档"
> sqr <- read.table("sqr.txt", header = TRUE);
> x <-sqr$sqr
> library(tsDyn)
>
> mod1.star <- star(x, m=4,d=1,thDelay=0,noRegimes=3,sig=0.1,control=list(maxit=3000))
Testing linearity...   p-Value =  1.665672e-05
The series is nonlinear. Incremental building procedure:
Building a 2 regime STAR.
Performing grid search for starting values...
Starting values fixed: gamma =  16 , th =  12.40319 ; SSE =  188.9531
Optimization algorithm converged
Optimized values fixed for regime 2  : gamma =  16.00069 , th =  12.39935
Testing for addition of regime 3.
  Estimating gradient matrix...
  Done. Computing the test statistic...
  Done. Regime 3 is needed (p-Value =  0.07807366 ).
Adding regime  3 .
Fixing good starting values for regime  3 ...
错误于if (cost <= bestCost) { : 需要TRUE/FALSE值的地方不可以用缺少值
此外: 警告信息:
In rbind(object$model.specific$phi1, rnorm(3)) :
  number of columns of result is not a multiple of vector length (arg 2)
>


52# epoh
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
epoh + 3 + 3 + 3 补偿

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

一直怀有一个梦想,希望在不久的将来能读个博士,做做学术搞搞研究,饱尝学术的艰辛

54
南冰 发表于 2011-3-21 16:53:05
还有我也遇到了下面这个问题,程序如下,数据在附件里:
> getwd()
[1] "d:/我的文档"
> library(tsDyn)
> svpdx <- read.table("quartc.txt", header = TRUE);
> x <- svpdx$qcpi
> mod1.star <- star(x, m=3,d=1,thDelay=0,noRegimes=3,sig=0.1,control=list(maxit=3000))
Testing linearity...   p-Value =  0.001151816
The series is nonlinear. Incremental building procedure:
Building a 2 regime STAR.
Performing grid search for starting values...
Starting values fixed: gamma =  21 , th =  8.19391 ; SSE =  128.2701
Optimization algorithm converged
Optimized values fixed for regime 2  : gamma =  21.00119 , th =  8.223794
Testing for addition of regime 3.
  Estimating gradient matrix...
  Done. Computing the test statistic...
  Done. Regime 3 is needed (p-Value =  0.01342169 ).
Adding regime  3 .
Fixing good starting values for regime  3 ...
Reordering regimes...
Estimating parameters of regime 3 ...
Optimized values fixed for regime  3 : gamma =  30.76407 , th =  16.5718
Optimization algorithm converged
Optimized linear values:
0.3238569 1.282448 -0.2267579 -0.1405304
-2.391707 1.009585 -1.465678 0.715712
17.98733 -1.548293 1.862583 -1.268843
Ok.
Testing for addition of regime  4 .
  Estimating gradient matrix...
  Computing the test statistic...
Regime  4  is needed (p-Value =  0.006431805 ).
Finished building a MRSTAR with  3  regimes
警告信息:
In rbind(object$model.specific$phi1, rnorm(3)) :
  number of columns of result is not a multiple of vector length (arg 2)
>

52# epoh

quartc数据.txt

650 Bytes

一直怀有一个梦想,希望在不久的将来能读个博士,做做学术搞搞研究,饱尝学术的艰辛

55
南冰 发表于 2011-3-21 17:04:57
谢谢您啊,函数参数的问题我已经解决,我在估计下面的方程时同时出现了前面53楼54楼的错误和警告信息,数据同54楼附件的数据,程序和结果如下:
> library(tsDyn)
> svpdx <- read.table("quartc.txt", header = TRUE);
> x <- svpdx$qcpi
> mod1.star <- star(x, m=4,d=1,thDelay=0,noRegimes=3,sig=0.1,control=list(maxit=3000))
Testing linearity...   p-Value =  0.0003500272
The series is nonlinear. Incremental building procedure:
Building a 2 regime STAR.
Performing grid search for starting values...
Starting values fixed: gamma =  18 , th =  8.22092 ; SSE =  112.1446
Optimization algorithm converged
Optimized values fixed for regime 2  : gamma =  18.00000 , th =  8.24219
Testing for addition of regime 3.
  Estimating gradient matrix...
  Done. Computing the test statistic...
  Done. Regime 3 is needed (p-Value =  0.02941768 ).
Adding regime  3 .
Fixing good starting values for regime  3 ...
错误于if (cost <= bestCost) { : 需要TRUE/FALSE值的地方不可以用缺少值
此外: 警告信息:
In rbind(object$model.specific$phi1, rnorm(3)) :
  number of columns of result is not a multiple of vector length (arg 2)
>

52# epoh
一直怀有一个梦想,希望在不久的将来能读个博士,做做学术搞搞研究,饱尝学术的艰辛

56
epoh 发表于 2011-3-22 20:06:12
修改star.R之function startingValues()
   将原来maxTh <- quantile(as.ts(s_t), .9) # percentil 90 of s_t
   改为  maxTh <- quantile(as.ts(s_t), .75)# percentil 75 of s_t
然后re-build package 'tsDyn'

########
library(tsDyn)
####data sqr
sqr <- read.table("sqr.txt", header = TRUE);
x <-sqr$sqr
mod1.star <- star(x, m=4,d=1,thDelay=0,noRegimes=3,sig=0.1,control=list(maxit=3000))

Testing linearity...   p-Value =  1.665672e-05
The series is nonlinear. Incremental building procedure:
Building a 2 regime STAR.
Performing grid search for starting values...
Starting values fixed: gamma =  16 , th =  12.40319 ; SSE =  188.9531
Optimization algorithm converged
Optimized values fixed for regime 2  : gamma =  16.00069 , th =  12.39935

Testing for addition of regime 3.
  Estimating gradient matrix...
  Done. Computing the test statistic...
  Done. Regime 3 is needed (p-Value =  0.07807366 ).
Adding regime  3 .
Fixing good starting values for regime 3 ...
Reordering regimes...
Estimating parameters of regime 3 ...
Optimized values fixed for regime  3 : gamma =  40.4925 , th =  15.80372
Optimization algorithm converged
Optimized linear values:
0.2746266 1.298432 -0.2259236 -0.04944461 -0.0386298
-2.956231 1.312587 -1.672697 0.4749626 0.1970925
23.57368 -2.076103 1.995213 -0.6978957 -0.7024029
Ok.
Testing for addition of regime  4 .
  Estimating gradient matrix...
  Computing the test statistic...
Regime  4  is NOT accepted (p-Value =  0.1812764 ).

Finished building a MRSTAR with  3  regimes
#################
####data svpdx
svpdx <- read.table("quartc.txt", header = TRUE);
xx <- svpdx$qcpi
mod2.star <- star(xx, m=4,d=1,thDelay=0,noRegimes=3,sig=0.1,control=list(maxit=3000))

Testing linearity...   p-Value =  0.0003500272
The series is nonlinear. Incremental building procedure:
Building a 2 regime STAR.
Performing grid search for starting values...
Starting values fixed: gamma =  18 , th =  8.22092 ; SSE =  112.1446
Optimization algorithm converged
Optimized values fixed for regime 2  : gamma =  18.00000 , th =  8.24219

Testing for addition of regime 3.
  Estimating gradient matrix...
  Done. Computing the test statistic...
  Done. Regime 3 is needed (p-Value =  0.02941768 ).
Adding regime  3 .
Fixing good starting values for regime 3 ...
Reordering regimes...
Estimating parameters of regime 3 ...
Optimized values fixed for regime  3 : gamma =  0.002031225 , th =  2.555149
Optimization algorithm converged
Optimized linear values:
4646656 4672.685 142.4123 -121.2756 36.74252
-66627.73 1708.731 4584.483 -841.5228 -3582.858
-9317491 120.1544 -285.2847 242.0331 -73.32033
Ok.
Testing for addition of regime  4 .
  Estimating gradient matrix...
  Computing the test statistic...
Regime  4  is NOT accepted (p-Value =  0.9999442 ).

Finished building a MRSTAR with  3  regimes
已有 2 人评分学术水平 热心指数 信用等级 收起 理由
南冰 + 5 + 5 + 5 感谢您一直帮助我,让我收益颇丰!
ywh19860616 + 1 + 1 + 1 呵呵,很久没有看到您发言了,热心

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

57
南冰 发表于 2011-3-23 08:26:35
万分感谢,为什么我找不到这个文件呢?“修改star.R之function startingValues()”中的star.R,我在我的整个电脑里搜他都搜不到,谢谢您啊!
本文来自: 人大经济论坛 S-Plus&R专版 版,详细出处参考:[url=http://www.pinggu.org/bbs/viewthread.php?tid=923194&page=6&from^^uid=841068http://www.pinggu.org/bbs/viewthread.php?tid=923194&page=6&from^^uid=841068[b[/url]] 56# epoh
一直怀有一个梦想,希望在不久的将来能读个博士,做做学术搞搞研究,饱尝学术的艰辛

58
epoh 发表于 2011-3-23 09:58:48
底下稍加说明function star()运行过程
虽然noRegimes你设置3,function star()
还是由Build the 2-regime star开始:

object <- star.predefined(x, m=m, noRegimes=2, thVar=thVar,

                         d=d, steps=steps, mTh=mTh, thDelay=thDelay,
                         series=series, trace=trace, control=control)

由于noRegimes=3,所以经过检试之后,会告诉你


Regime 3 is needed or Regime 3 is NOT accepted


如果Regime 3 is needed,就继续执行下列指令


   object <- addRegime(object);


   ("Fixing good starting values for regime")
   object <- startingValues(object, control=control, trace=trace);

   ('Estimating parameters of regime')
   object <- estimateParams(object, control=control, trace=trace);

你的问题出现在startingValues()

   startingValues.star <- function(object, trace=TRUE, ...){


   .......


   # Maximum and minimum values for c


     minTh <- quantile(as.ts(s_t), .1) # percentil 10 of s_t


     maxTh <- quantile(as.ts(s_t), .9) # percentil 90 of s_t


   .......
}

maxTh
太大造成cost产生NA,于是告诉你


错误于if(cost<= bestCost){:需要TRUE/FALSE值的地方不可以用缺少值


这里你可以配合你的数据调整maxTh.


star.R在 tsDyn\R
   http://cran.r-project.org/web/packages/tsDyn/index.html
         Package source:tsDyn_0.7-40.tar.gz


build package 可参考:
   http://www.murdoch-sutherland.com/Rtools/index.html


我把修改后,re-build package tsDyn_0.7-40.zip先提供你使用
   
    tsDyn_0.7-40.zip (1.32 MB)
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
南冰 + 5 + 5 + 5 学术与人品兼备,堪为人师!

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

59
南冰 发表于 2011-3-23 12:05:41
谢谢您啊! 30# epoh
一直怀有一个梦想,希望在不久的将来能读个博士,做做学术搞搞研究,饱尝学术的艰辛

60
南冰 发表于 2011-3-28 20:48:24
前辈您好,我又碰到新问题了,麻烦您指教。如果我选择的AR(p)模型中自回归的项数不是从1到p而是间断性的,比如说自回归中只有AR(1)和AR(3)项的话我该怎么写STAR函数呢?这样说可能不明白,举个例子,如果我想估计Yt=a+a1Y(t-1)+a2Y(t-3)+[b+b1Y(t-1)+b2Y(t-3)]G(.),其中G(.)为logistic function,如果写mod=star(x, m=3,d=1,thDelay=2,noRegimes=3,control=list(maxit=3000))的话估计出来的会把AR(2)项钱的系数估计出来,如果我只想估计AR(1)和AR(3)项的话我该如何写star()函数呢?谢谢您了!

2# epoh
一直怀有一个梦想,希望在不久的将来能读个博士,做做学术搞搞研究,饱尝学术的艰辛

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-15 00:11