前辈您好!再请教你个问题啊。我boot三区制LSTAR模型的code如下:
#by bootstrapping residuals
library(tsDyn)
svpdx =read.table("quartc.txt", header = TRUE);
x =svpdx$qcpi
mod =star(x, m=5,d=1,thDelay=1,noRegimes=3,control=list(maxit=3000))
phi1=mod$model.specific$phi1[1,]
phi1
phi2=mod$model.specific$phi1[2,]
phi2
phi3=mod$model.specific$phi1[3,]
phi3
re=mod$residuals
mre=(re-mean(re))/sd(re)
n=length(x) #88
n1=length(mre) #83
number=0*seq(1:n1)
B=3
gamma1=c(1:B)
gamma2=c(1:B)
#Transition function
G1 <- function(y1, g1, th1) plogis(y1, th1, 1/(g1))
G2 <- function(y2, g2, th2) plogis(y2, th2, 1/(g2))
for (b in 1:B) {
#bootstrap by resampling the residuals
#sampling with replacement from the original sample
rand=sample(seq(1:n1),n1, replace = TRUE)
rr=mre[rand]
#generate new y
y=c(x[1:5], 6 :n)
for (t in 6:n){
y[t]=t(c(1,y[(t-1):(t-5)]))%*%phi1[1,] + (t(c(1,y[(t-1):(t-5)]))%*%phi1[2,])*G(y[t-2],
phi2[1,1],phi2[1,2]) + (t(c(1,y[(t-1):(t-5)]))%*%phi1[3,])*G(y[t-2], phi2[2,1],phi2[2,2])+rr[t
-5]}
#{y[t]=0.2997076+1.0564187*y[t-1]+0.0616733*y[t-2]+0.0429092*y[t-3]-0.5146651*y[t-4]+0.2624537*y[t-5]+(1.4157232+1.0496377*y[t-1]-1.2715717*y[t-2]-0.5466812*y[t-3]+0.785233*y[t-4]-0.1815548*y[t-5])/(1+exp(-35.0696739*(y[t-2]-5.2725434)))+(12.8977894-1.4625725*y[t-1]+1.5941205*y[t-2]+0.3835062*y[t-3]-1.0377474*y[t-4]-0.0059888*y[t-5])/(1+exp(-42.2771382*( y[t-2]-13.9339929)))+rr[t]}#generate new
y
z=y
rs=star(z, m=5,d=1,thDelay=1,noRegimes=3,trace=FALSE,control=list(maxit=3000))
if (is.null(rs$coefficients[19]) == TRUE) gamma1=NA else
gamma1=rs$coefficients[19]
if (is.null(rs$coefficients[20]) == TRUE) gamma2=NA else
gamma2=rs$coefficients[20]
}
gamma1=gamma1[!is.na(gamma1)] # remove NAs
gamma2=gamma2[!is.na(gamma2)] # remove NAs
length(gamma1)
ength(gamma2)
但是给了我错误提示:
> #by bootstrapping residuals
>
> library(tsDyn)
> svpdx =read.table("quartc.txt", header = TRUE);
> x =svpdx$qcpi
> mod =star(x, m=5,d=1,thDelay=1,noRegimes=3,control=list(maxit=3000))
Testing linearity... p-Value = 0.002852162
The series is nonlinear. Incremental building procedure:
Building a 2 regime STAR.
Performing grid search for starting values...
Starting values fixed: gamma = 24 , th = 14.00072 ; SSE = 88.80465
Optimization algorithm converged
Optimized values fixed for regime 2 : gamma = 23.99999 , th = 13.99887
Testing for addition of regime 3.
Estimating gradient matrix...
Done. Computing the test statistic...
Done. Regime 3 is needed (p-Value = 0.01449991 ).
Adding regime 3 .
Fixing good starting values for regime 3 ...
Reordering regimes...
Estimating parameters of regime 3 ...
Optimized values fixed for regime 3 : gamma = 42.27714 , th = 13.93399
Optimization algorithm converged
Optimized linear values:
0.2997076 1.056419 0.06167327 0.04290917 -0.5146651 0.2624537
1.415723 1.049638 -1.271572 -0.5466812 0.785233 -0.1815548
12.89779 -1.462572 1.594120 0.3835062 -1.037747 -0.005988798
Ok.
Testing for addition of regime 4 .
Estimating gradient matrix...
Computing the test statistic...
Regime 4 is NOT accepted (p-Value = 0.1873368 ).
Finished building a MRSTAR with 3 regimes
>
> phi1=mod$model.specific$phi1[1,]
> phi1
[1] 0.29970762 1.05641871 0.06167327 0.04290917 -0.51466513 0.26245373
>
> phi2=mod$model.specific$phi1[2,]
> phi2
[1] 1.4157232 1.0496377 -1.2715717 -0.5466812 0.7852330 -0.1815548
>
> phi3=mod$model.specific$phi1[3,]
> phi3
[1] 12.897789438 -1.462572488 1.594120467 0.383506197 -1.037747375
[6] -0.005988798
>
> re=mod$residuals
> mre=(re-mean(re))/sd(re)
> n=length(x) #88
> n1=length(mre) #83
> number=0*seq(1:n1)
> B=3
> gamma1=c(1:B)
> gamma2=c(1:B)
>
> #Transition function
> G1 <- function(y1, g1, th1) plogis(y1, th1, 1/(g1))
> G2 <- function(y2, g2, th2) plogis(y2, th2, 1/(g2))
>
>
> for (b in 1:B) {
+ #bootstrap by resampling the residuals
+ #sampling with replacement from the original sample
+ rand=sample(seq(1:n1),n1, replace = TRUE)
+ rr=mre[rand]
+ #generate new y
+ y=c(x[1:5], 6 :n)
+ for (t in 6:n){
+
+ y[t]=t(c(1,y[(t-1):(t-5)]))%*%phi1[1,] + (t(c(1,y[(t-1):(t-5)]))%*%phi1[2,])*G(y[t-2],
+ phi2[1,1],phi2[1,2]) + (t(c(1,y[(t-1):(t-5)]))%*%phi1[3,])*G(y[t-2], phi2[2,1],phi2[2,2])+rr[t
+ -5]}
+
+ #{y[t]=0.2997076+1.0564187*y[t-1]+0.0616733*y[t-2]+0.0429092*y[t-3]-0.5146651*y[t-4]+0.2624537*y[t-5]+(1.4157232+1.0496377*y[t-1]-1.2715717*y[t-2]-0.5466812*y[t-3]+0.785233*y[t-4]-0.1815548*y[t-5])/(1+exp(-35.0696739*(y[t-2]-5.2725434)))+(12.8977894-1.4625725*y[t-1]+1.5941205*y[t-2]+0.3835062*y[t-3]-1.0377474*y[t-4]-0.0059888*y[t-5])/(1+exp(-42.2771382*( y[t-2]-13.9339929)))+rr[t]}#generate new
+ y
+ z=y
+ rs=star(z, m=5,d=1,thDelay=1,noRegimes=3,trace=FALSE,sig=1,control=list(maxit=3000))
+ if (is.null(rs$coefficients[19]) == TRUE) gamma1=NA else
+ gamma1=rs$coefficients[19]
+
+ if (is.null(rs$coefficients[20]) == TRUE) gamma2=NA else
+ gamma2=rs$coefficients[20]
+ }
错误于phi1[1, ] : 量度数目不对
> gamma1=gamma1[!is.na(gamma1)] # remove NAs
> gamma2=gamma2[!is.na(gamma2)] # remove NAs
> length(gamma1) # 66
[1] 3
> ength(gamma2) # 66
麻烦您帮我检查下code,谢谢您了!顺便和您说下,你上次发给我的程序有点小问题,不过我改了后可以运行了,万分感谢啊!
70# epoh


雷达卡

京公网安备 11010802022788号







