2016-11-18 2 views
2

Rでは、関数ar.ywは分散をどのように見積もりますか?具体的には、番号 "var.pred"はどこから来たのですか?通常のYWの分散の推定や二乗残差の合計をdfで割ったものではないようです(ただし、dfが何であるべきかについての意見は異なりますが、var.predに相当するものはありません) 。そして、はい、私はYWよりも良い方法があることを知っています。 Rが何をしているのか把握しようとしています。ar.ywは分散をどのように推定するのですか

set.seed(82346) 
temp <- arima.sim(n=10, list(ar = 0.5), sd=1) 
fit <- ar(temp, method = "yule-walker", demean = FALSE, aic=FALSE, order.max=1) 

## R's estimate of the sigma squared 
fit$var.pred 
## YW estimate 
sum(temp^2)/10 - fit$ar*sum(temp[2:10]*temp[1:9])/10 
## YW if there was a mean 
sum((temp-mean(temp))^2)/10 - fit$ar*sum((temp[2:10]-mean(temp))*(temp[1:9]-mean(temp)))/10 
## estimate based on residuals, different possible df. 
sum(na.omit(fit$resid^2))/10 
sum(na.omit(fit$resid^2))/9 
sum(na.omit(fit$resid^2))/8 
sum(na.omit(fit$resid^2))/7 
+0

これは予測パッケージの機能ですか?あなたはRパッケージがすべてオープンソースであることを認識していますか? –

+0

@ 42、いいえ、これは "stats"パッケージの一部です。つまり、 "lm"とその他すべての基本統計機能を提供するパッケージと同じです。したがって、たとえそれがオープンソースであっても、妥当な何かをやろうとしていると推測するかもしれない。 – chucky

答えて

1

文書化されていない場合は、コードを読む必要があります。

言う
?ar.yw 

「でar.ywイノベーションの分散行列を近似された係数であり、xの自己相関から計算されます」。それが十分に説明されていない場合、あなたはコードを見てする必要があります。

methods(ar.yw) 
#[1] ar.yw.default* ar.yw.mts*  
#see '?methods' for accessing help and source code 

getAnywhere(ar.yw.default) 
# there are two cases that I see 
x <- as.matrix(x) 
nser <- ncol(x) 
if (nser > 1L) # .... not your situation 
#.... 
else{ 
    r <- as.double(drop(xacf)) 
    z <- .Fortran(C_eureka, as.integer(order.max), r, r, 
     coefs = double(order.max^2), vars = double(order.max), 
     double(order.max)) 
    coefs <- matrix(z$coefs, order.max, order.max) 
    partialacf <- array(diag(coefs), dim = c(order.max, 1L, 
     1L)) 
    var.pred <- c(r[1L], z$vars) 
    #....... 
    order <- if (aic) 
     (0L:order.max)[xaic == 0L] 
    else order.max 
    ar <- if (order) 
     coefs[order, seq_len(order)] 
    else numeric() 
    var.pred <- var.pred[order + 1L] 
    var.pred <- var.pred * n.used/(n.used - (order + 1L)) 

ですから、今C_eurekaのためのFortranコードを見つける必要があります。私はここでそれを見つけると思う:https://svn.r-project.org/R/trunk/src/library/stats/src/eureka.fこれは、私はvar.pred見積もりを返すと思うコードです。私は時系列の人ではなく、問題への適用可能性についてこのプロセスをレビューするのはあなたの責任です。

 subroutine eureka (lr,r,g,f,var,a) 
c 
c  solves Toeplitz matrix equation toep(r)f=g(1+.) 
c  by Levinson's algorithm 
c  a is a workspace of size lr, the number 
c  of equations 
c 
    snipped 
c estimate the innovations variance 
     var(l) = var(l-1) * (1 - f(l,l)*f(l,l)) 
     if (l .eq. lr) return 
     d = 0.0d0 
     q = 0.0d0 
     do 50 i = 1, l 
      k = l-i+2 
      d = d + a(i)*r(k) 
      q = q + f(l,i)*r(k) 
    50  continue 
関連する問題