2015-10-18 153 views
0

這是我的data.frame:自迴歸線性迴歸data.frame

data <- matrix(rnorm(10*5),nrow=25) 
    GDP <- data.frame(data) 
    GDP 

       X1   X2 
    1 -0.37000725 2.53311407 
    2 1.54825124 0.15811930 
    3 2.32926402 0.75203918 
    4 1.39942457 -0.42772401 
    5 -0.94124582 -0.73874833 
    6 0.83330085 0.14364736 
    7 0.73488659 -0.71502188 
    8 0.12321817 1.31648567 
    9 1.55536358 -1.57426731 
    10 1.42325808 0.04616108 
    11 -0.35875716 -0.02854382 
    12 -0.49774322 1.41312880 
    13 -1.88498804 0.82919301 
    14 -1.13962628 0.18335208 
    15 -0.45672902 1.33955701 
    16 1.17333357 1.20232913 
    17 -0.32018730 0.87183555 
    18 0.04167326 -0.11642683 
    19 -0.17698318 0.34282848 
    20 2.28473762 -0.98547134 
    21 -0.80361048 1.12771148 
    22 1.23063390 0.22982985 
    23 -0.03444458 0.91857055 
    24 -0.66244086 -0.21407559 
    25 -0.24960018 -2.72181616 

有任何包,可以幫助我做了一個簡單的自迴歸線性迴歸有沒有我在我的數據來創建另一列。幀? 這就是我想要的東西:

X1 = X1(t-1) + X2(t-2)

感謝。

+0

看起來像'dynlm'具有滯後變量根據http://stackoverflow.com/questions/13096787/adding-lagged-variables-to-an-lm-model – jenesaisquoi

回答

2

這裏有幾個方面:

1)DYN

library(dyn) 
Lag <- function(x, k = 1) lag(x, -k) 
dyn$lm(X1 ~ Lag(X1) + Lag(X2) - 1, as.zoo(GDP)) 

,並提供:

Call: 
lm(formula = dyn(X1 ~ Lag(X1) + Lag(X2) - 1), data = as.zoo(GDP)) 

Coefficients: 
Lag(X1) Lag(X2) 
-0.1876 0.0772 

注意,這一切都太工作,但定義Lag,因爲我們沒有上述品牌它看起來有點漂亮。

dyn$lm(X1 ~ lag(X1, -1) + lag(X2, -1) - 1, as.zoo(GDP)) 

2)瓦爾

library(vars) 
VAR(GDP, type = "none") 

,並提供:

VAR Estimation Results: 
======================= 

Estimated coefficients for equation X1: 
======================================= 
Call: 
X1 = X1.l1 + X2.l1 

     X1.l1  X2.l1 
-0.18755204 0.07719922 


Estimated coefficients for equation X2: 
======================================= 
Call: 
X2 = X1.l1 + X2.l1 

    X1.l1  X2.l1 
0.4433822 0.2558610 

,或者如果我們只是想看到的第一個等式:

VAR(GDP, type = "none")[[1]]$X1 

贈送:

Call: 
lm(formula = y ~ -1 + ., data = datamat) 

Coefficients: 
    X1.l1 X2.l1 
-0.1876 0.0772 

3)沒有軟件包

n <- nrow(GDP) 
lm(X1[-1] ~ X1[-n] + X2[-n] - 1, GDP) 

,並提供:

Call: 
lm(formula = X1[-1] ~ X1[-n] + X2[-n] - 1, data = GDP) 

Coefficients: 
X1[-n] X2[-n] 
-0.1876 0.0772 

注意我們使用在上面的例子中國內生產總值以下。

GDP <- 
structure(list(X1 = c(-0.480007101227991, -0.710506834821923, 
-1.4008090378277, 0.234161619712456, 0.0798157911638669, -0.835197270889505, 
0.598254213927639, -1.14352681562672, 1.03688327045929, 0.660297071029499, 
-0.351328818974587, 0.790545641075689, -0.792678099784052, -0.357614703160382, 
0.314291502993829, -0.431642261560374, 0.316918597548564, 0.5209261331865, 
1.0013650951443, 1.05596920913398, -0.753506630185664, -1.4890660967781, 
1.43183749932514, -0.423639570640277, 0.637317561276307), X2 = c(0.474962739361749, 
-2.39846608215569, -0.98006715899912, -0.0271182048898923, 0.0296705736957689, 
-1.24925308595335, -0.893230759394588, 0.241972221010069, -0.431946104440377, 
-0.638101222832251, 0.844712933353179, 0.883298568281938, 0.996083349802754, 
1.89504374477663, -0.148165464503539, 1.15286878557205, -0.425104835813157, 
-1.38572745123415, 1.52226162248381, 1.55272897266444, 1.35700497284096, 
0.389532599186254, 0.256357476163037, 1.29116051537444, -0.440232029770923 
)), .Names = c("X1", "X2"), row.names = c(NA, -25L), class = "data.frame")