如何使用rollapply访问t统计数据和p值?

时间:2022-09-03 19:32:34

Please help and sorry for naive question. I have:

请帮助和抱歉幼稚的问题。我有:

> 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

I need to run rolling-window regression with window=51. So I did:

我需要使用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  

The questions: how can I calculate rolling-window regression to have t and p values?

问题:我如何计算滚动窗口的回归有t和p值?

thank you very much, roman

非常感谢,罗曼。

1 个解决方案

#1


0  

Create g and run a revised dolm on it:

创建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)

UPDATE: Corrections and simplification.

更新:修改和简化。

#1


0  

Create g and run a revised dolm on it:

创建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)

UPDATE: Corrections and simplification.

更新:修改和简化。