roll_lm: Rolling Linear Models

Description Usage Arguments Value Examples

View source: R/roll.R

Description

A function for computing the rolling and expanding linear models of time-series data.

Usage

1
2
3
roll_lm(x, y, width, weights = rep(1, width), intercept = TRUE,
  min_obs = width, complete_obs = TRUE, na_restore = FALSE,
  online = TRUE)

Arguments

x

vector or matrix. Rows are observations and columns are the independent variables.

y

vector or matrix. Rows are observations and columns are the dependent variables.

width

integer. Window size.

weights

vector. Weights for each observation within a window.

intercept

logical. Either TRUE to include or FALSE to remove the intercept.

min_obs

integer. Minimum number of observations required to have a value within a window, otherwise result is NA.

complete_obs

logical. If TRUE then rows containing any missing values are removed, if FALSE then pairwise is used.

na_restore

logical. Should missing values be restored?

online

logical. Process observations using an online algorithm.

Value

A list containing the following components:

coefficients

A list of objects with the rolling and expanding coefficients for each y. An object is the same class and dimension (with an added column for the intercept) as x.

r.squared

A list of objects with the rolling and expanding r-squareds for each y. An object is the same class as x.

std.error

A list of objects with the rolling and expanding standard errors for each y. An object is the same class and dimension (with an added column for the intercept) as x.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
n <- 15
x <- rnorm(n)
y <- rnorm(n)
weights <- 0.9 ^ (n:1)

# rolling regressions with complete windows
roll_lm(x, y, width = 5)

# rolling regressions with partial windows
roll_lm(x, y, width = 5, min_obs = 1)

# expanding regressions with partial windows
roll_lm(x, y, width = n, min_obs = 1)

# expanding regressions with partial windows and weights
roll_lm(x, y, width = n, min_obs = 1, weights = weights)

Example output

$coefficients
      (Intercept)          x1
 [1,]          NA          NA
 [2,]          NA          NA
 [3,]          NA          NA
 [4,]          NA          NA
 [5,]  0.48181967 -0.03210633
 [6,]  0.58380388 -0.28772883
 [7,]  0.58702575 -0.31186857
 [8,]  0.53768064 -0.21984641
 [9,]  0.18798353  0.28007195
[10,]  0.35163296  0.48258804
[11,]  0.25210781  0.53096163
[12,]  0.38315680  1.23390648
[13,] -0.23445004  0.25370529
[14,] -0.09791133 -0.21606419
[15,] -0.72736585 -0.06127493

$r.squared
        R-squared
 [1,]          NA
 [2,]          NA
 [3,]          NA
 [4,]          NA
 [5,] 0.006488938
 [6,] 0.271411214
 [7,] 0.377999253
 [8,] 0.215331765
 [9,] 0.085920350
[10,] 0.134303546
[11,] 0.164759550
[12,] 0.178815996
[13,] 0.031826221
[14,] 0.015762763
[15,] 0.013247295

$std.error
      (Intercept)        x1
 [1,]          NA        NA
 [2,]          NA        NA
 [3,]          NA        NA
 [4,]          NA        NA
 [5,]   0.2027786 0.2293663
 [6,]   0.1736307 0.2721762
 [7,]   0.1796377 0.2309729
 [8,]   0.1892132 0.2422968
 [9,]   0.4379829 0.5274156
[10,]   0.5528433 0.7073836
[11,]   0.5637318 0.6902127
[12,]   0.9268909 1.5266472
[13,]   0.6906703 0.8078914
[14,]   0.6750679 0.9857252
[15,]   0.3096515 0.3053253

$coefficients
      (Intercept)          x1
 [1,]          NA          NA
 [2,]  0.27305901  0.03898481
 [3,]  0.23268484  0.02074292
 [4,]  0.30735442  0.10851360
 [5,]  0.48181967 -0.03210633
 [6,]  0.58380388 -0.28772883
 [7,]  0.58702575 -0.31186857
 [8,]  0.53768064 -0.21984641
 [9,]  0.18798353  0.28007195
[10,]  0.35163296  0.48258804
[11,]  0.25210781  0.53096163
[12,]  0.38315680  1.23390648
[13,] -0.23445004  0.25370529
[14,] -0.09791133 -0.21606419
[15,] -0.72736585 -0.06127493

$r.squared
        R-squared
 [1,]          NA
 [2,] 1.000000000
 [3,] 0.119143159
 [4,] 0.356475230
 [5,] 0.006488938
 [6,] 0.271411214
 [7,] 0.377999253
 [8,] 0.215331765
 [9,] 0.085920350
[10,] 0.134303546
[11,] 0.164759550
[12,] 0.178815996
[13,] 0.031826221
[14,] 0.015762763
[15,] 0.013247295

$std.error
      (Intercept)         x1
 [1,]          NA         NA
 [2,]          NA         NA
 [3,]  0.05144435 0.05640117
 [4,]  0.09648270 0.10309489
 [5,]  0.20277859 0.22936629
 [6,]  0.17363072 0.27217617
 [7,]  0.17963772 0.23097287
 [8,]  0.18921318 0.24229683
 [9,]  0.43798287 0.52741562
[10,]  0.55284335 0.70738363
[11,]  0.56373180 0.69021274
[12,]  0.92689091 1.52664717
[13,]  0.69067033 0.80789145
[14,]  0.67506788 0.98572524
[15,]  0.30965147 0.30532527

$coefficients
      (Intercept)          x1
 [1,]          NA          NA
 [2,]  0.27305901  0.03898481
 [3,]  0.23268484  0.02074292
 [4,]  0.30735442  0.10851360
 [5,]  0.48181967 -0.03210633
 [6,]  0.46937863 -0.02850295
 [7,]  0.43416160 -0.09055689
 [8,]  0.39877941 -0.07215886
 [9,]  0.22554281  0.20647138
[10,]  0.40109307  0.19719202
[11,]  0.35252164  0.23524932
[12,]  0.20274443  0.21851776
[13,]  0.14066242  0.09656704
[14,]  0.08476906  0.07244389
[15,]  0.01930207 -0.10275539

$r.squared
        R-squared
 [1,]          NA
 [2,] 1.000000000
 [3,] 0.119143159
 [4,] 0.356475230
 [5,] 0.006488938
 [6,] 0.005137248
 [7,] 0.060483920
 [8,] 0.037108825
 [9,] 0.094678500
[10,] 0.045742995
[11,] 0.062841017
[12,] 0.037429765
[13,] 0.009158892
[14,] 0.004918882
[15,] 0.011027957

$std.error
      (Intercept)         x1
 [1,]          NA         NA
 [2,]          NA         NA
 [3,]  0.05144435 0.05640117
 [4,]  0.09648270 0.10309489
 [5,]  0.20277859 0.22936629
 [6,]  0.16121519 0.19832442
 [7,]  0.14094790 0.15961315
 [8,]  0.12493089 0.15005969
 [9,]  0.21208155 0.24131625
[10,]  0.26569342 0.31843055
[11,]  0.24669515 0.30282530
[12,]  0.27334722 0.35042446
[13,]  0.25475290 0.30283993
[14,]  0.24238344 0.29744535
[15,]  0.24244217 0.26988416

$coefficients
       (Intercept)          x1
 [1,]           NA          NA
 [2,]  0.273059011  0.03898481
 [3,]  0.229640192  0.02048184
 [4,]  0.308857222  0.12159357
 [5,]  0.529244023 -0.07249779
 [6,]  0.501263001 -0.06274834
 [7,]  0.461275933 -0.14238899
 [8,]  0.401941498 -0.10635594
 [9,]  0.154299205  0.30648235
[10,]  0.437029559  0.30384378
[11,]  0.365785877  0.36190493
[12,]  0.108951599  0.30705733
[13,]  0.007216511  0.09344320
[14,] -0.075366009  0.06269046
[15,] -0.150697011 -0.18713920

$r.squared
        R-squared
 [1,]          NA
 [2,] 1.000000000
 [3,] 0.104592070
 [4,] 0.372765443
 [5,] 0.028132131
 [6,] 0.021049972
 [7,] 0.132402681
 [8,] 0.071622634
 [9,] 0.161290269
[10,] 0.072772400
[11,] 0.099919320
[12,] 0.046410956
[13,] 0.006861132
[14,] 0.002953586
[15,] 0.033867942

$std.error
      (Intercept)         x1
 [1,]          NA         NA
 [2,]          NA         NA
 [3,]  0.05248646 0.05992804
 [4,]  0.10203536 0.11153028
 [5,]  0.21040018 0.24601773
 [6,]  0.16398263 0.21395713
 [7,]  0.14221308 0.16300544
 [8,]  0.12532103 0.15632314
 [9,]  0.23139830 0.26415462
[10,]  0.30942927 0.38345568
[11,]  0.28175006 0.36206724
[12,]  0.31780851 0.44013898
[13,]  0.28479542 0.33896781
[14,]  0.26328192 0.33250160
[15,]  0.26323060 0.27721514

roll documentation built on July 13, 2020, 5:09 p.m.