LSTAR:
方法一
- library(tsDyn)
- mod.star <- star(log10(lynx), m=2, d=1, control=list(maxit=3000))
- mod.star
- summary(mod.star)
例1
- library(tsDyn)
- mod.lstar <- lstar(log10(lynx), m=2, d=1, control=list(maxit=3000))
- summary(mod.lstar)
#cpo.txt下载:[url=https://bbs.pinggu.org/thread-923194-16-1.html]
- library(tsDyn)
- nan=read.table("cpo.txt", header = TRUE)
- y=nan$CP
- x=nan$Oil
- mod=lstar(y,m=3,d=1,thVar=x,control=list(maxit=3000))
- summary(mod)
- xxL=cbind(1,mod$str$xx)
- xxH=xxL
- yy=mod$str$yy
- z=mod$model.specific$thVar
- gamma=mod$coefficients[7]
- th=mod$coefficients[8]
- G <- function(y, g, th) plogis(y, th, 1/g)
- calc.lm.t <- function(x)
- {
- Qr <- x$qr
- r<- x$residuals
- p <- x$rank
- p1<- 1L:p
- rss<- sum(r^2)
- n<- NROW(Qr$qr)
- rdf<- n-p
- resvar <- rss/rdf
- R <- chol2inv(Qr$qr[p1,p1,drop=FALSE])
- se<- sqrt(diag(R)*resvar)
- est<- x$coefficients[Qr$pivot[p1]]
- tval <- est/se
- res <- cbind(est = est, se = se, tval = tval)
- res
- }
- lmf <- lm.fit( cbind(xxL, xxH * G(z, gamma, th)), yy)
- calc.lm.t(lmf)
- mod
- names(mod)
- g=mod$coefficients[7]
- th=mod$coefficients[8]
- z=mod$model.specific$thVar
- gfun<-function(y,g,th) 1 / (1 + exp(-g*(y-th)))
- G=gfun(z,g,th)
- par(mfrow=c(2,1))
- plot(G,type = "l", col = "red")
- plot(G,z)
- library(fGarch)
- .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)]) }
- cat("\n Residuals Tests:\n")
- r.s = mod$residuals
- ans = NULL
- 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))
- }
- 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]))
- [hide][hide] 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)[/hide]