1

请帮助并为幼稚的问题感到抱歉。我有:

> head (g)
  bo_m          dax             bo_m_lag
1 -0.040131270  0.001842860      0.032612438
2  0.112425025 -0.018043681     -0.040131270
3 -0.078987920 -0.009463752      0.112425025
4 -0.011990692  0.020144077     -0.078987920
5 -0.005279136  0.013360796     -0.011990692
6  0.055994660 -0.004568196     -0.005279136

我需要使用 window=51 运行滚动窗口回归。所以我做了:

> library ("zoo")
> dolm <- function(x) coefficients (lm(bo_m ~ ., data = as.data.frame(x), model=TRUE))
> res <- rollapplyr (g, 51, dolm, by.column = FALSE)

> head(res)
      Intercept       dax        bo_m_lag
[1,] 0.01695970 0.1548448       -0.3846233
[2,] 0.01752946 0.1381642       -0.3781836
[3,] 0.01300192 0.3028419       -0.3549629
[4,] 0.01415457 0.2012890       -0.3341618
[5,] 0.01601414 0.2639384       -0.3486120
[6,] 0.01684621 0.2733759       -0.3482895

> summary (res)
   Intercept              dax           bo_m_lag  
 Min.   :-0.030258   Min.   :-0.71994   Min.   :-0.64239  
 1st Qu.:-0.001953   1st Qu.:-0.05464   1st Qu.:-0.40524  
 Median : 0.003330   Median : 0.26794   Median :-0.35061  
 Mean   : 0.004174   Mean   : 0.35349   Mean   :-0.32037  
 3rd Qu.: 0.014527   3rd Qu.: 0.90471   3rd Qu.:-0.26593  
 Max.   : 0.030142   Max.   : 1.30679   Max.   : 0.01971  

问题:如何计算滚动窗口回归以获得 t 和 p 值?

非常感谢你,罗马

4

1 回答 1

0

创建g并运行一个修改dolm就可以了:

Lines <- "  bo_m          dax             bo_m_lag
 -0.040131270  0.001842860      0.032612438
  0.112425025 -0.018043681     -0.040131270
 -0.078987920 -0.009463752      0.112425025
 -0.011990692  0.020144077     -0.078987920
 -0.005279136  0.013360796     -0.011990692
  0.055994660 -0.004568196     -0.005279136
"

library(zoo)
g <- read.table(text = Lines,  header = TRUE)
dolm <- function(x) {
  co <- coef(summary(lm(bo_m ~., as.data.frame(x))))
  c(Est = co[, 1], SE = co[, 2], t = co[, 3], P = co[, 4])
}
r <- rollapplyr(g, 4, dolm, by.column = FALSE)

更新:更正和简化。

于 2013-06-11T21:29:54.077 回答