我想将 y 的每一列与宽度为 12 的 x 进行回归,并计算 Beta、截距和 R-2 值。我想要至少 12 个观测值,并且对于没有重叠数据的情况,这些观测值是 NA。对于我的输出,我需要三个带有列标题 y1、y2、y3 的矩阵(数据帧),分别包含所有 Beta 值、所有截距值和所有 R-2 值。
实际上,我的 y 矩阵有 2000 多个列,因此我希望尽可能避免循环。
Example:
y <- data.frame(y1 = c(NA,NA,NA, rnorm(50)),
y2 = c(NA, NA, NA, NA, NA, rnorm(48)),
y3 = rnorm(53))
x <- data.frame(x = c(NA, NA,NA, NA, NA, rnorm(48)))
我尝试使用 rollapply:
model_output <- rollapply(data = cbind.data.frame(y, x),
width = 12, FUN = function(a,b) {lm(a ~ b)} ,
by.column = FALSE, align = "right",
fill = NA)
这是一个根本不使用
zoo
滚动功能的选项:
pacman::p_load(tidyverse, broom)
dfs <- bind_cols(y, x) |>
pivot_longer(-x, names_to = "n", values_to = "y") |>
group_split(n) |>
# drop the n column from all of them
map(~select(.x, -n))
map(dfs,
\(df) map_df(seq_len(nrow(df)-n),
\(i) df[i:(i+n), ] |>
filter(!is.na(x), !is.na(y)) |>
lm(formula = y ~ x, data = _) |>
tidy()))
输出:
[[1]]
# A tibble: 82 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 0.0539 0.196 0.274 0.793
2 x 0.374 0.194 1.93 0.101
3 (Intercept) -0.127 0.258 -0.491 0.639
4 x 0.300 0.268 1.12 0.299
5 (Intercept) -0.209 0.263 -0.797 0.448
6 x 0.0919 0.227 0.404 0.696
7 (Intercept) -0.0494 0.270 -0.183 0.859
8 x -0.0729 0.226 -0.322 0.755
9 (Intercept) 0.0193 0.255 0.0754 0.941
10 x -0.0800 0.223 -0.358 0.728
# ℹ 72 more rows
# ℹ Use `print(n = ...)` to see more rows
[[2]]
# A tibble: 82 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 0.0363 0.401 0.0904 0.931
2 x -0.113 0.396 -0.287 0.784
3 (Intercept) 0.0662 0.352 0.188 0.856
4 x -0.101 0.364 -0.278 0.789
5 (Intercept) 0.0000392 0.333 0.000118 1.00
6 x -0.268 0.288 -0.930 0.379
7 (Intercept) -0.0409 0.295 -0.138 0.893
8 x -0.226 0.248 -0.912 0.385
9 (Intercept) 0.0884 0.300 0.294 0.774
10 x -0.239 0.263 -0.911 0.384
# ℹ 72 more rows
# ℹ Use `print(n = ...)` to see more rows
[[3]]
# A tibble: 82 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) -0.622 0.256 -2.43 0.0511
2 x -0.132 0.252 -0.523 0.620
3 (Intercept) -0.555 0.234 -2.37 0.0497
4 x -0.104 0.243 -0.430 0.680
5 (Intercept) -0.533 0.215 -2.48 0.0382
6 x -0.0489 0.186 -0.263 0.799
7 (Intercept) -0.546 0.190 -2.88 0.0182
8 x -0.0353 0.159 -0.222 0.830
9 (Intercept) -0.418 0.218 -1.92 0.0839
10 x -0.0486 0.191 -0.255 0.804
# ℹ 72 more rows
# ℹ Use `print(n = ...)` to see more rows