expanding.lm.df: no frills rolling linear regression

Description Usage Arguments Value Author(s) See Also Examples

Description

a no frills rolling version of lm. designed to return only the coefficients and nothing else. (and potentially some basic stats in a future version)

Usage

1
expanding.lm.df(panel, right.hand.side, left.hand.sides, min.rows)

Arguments

panel

a dataframe containing the relevant vectors

right.hand.side

a string referencing the column containing the observation vector of length n.

left.hand.sides

a vector of strings referencing the vectors to use as the design matrix (n * p).

min.rows

required number of rows of observations before rolling lm can start.

Value

a matrix of the coefficients over the expanding window.

Author(s)

Whit Armstrong

See Also

lm the standard lm method.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
do.running.lm <- function(panel, right.hand.side, left.hand.sides, min.obs=30L) {
    NR <- nrow(panel)
    ans <- matrix(NA, NR - min.obs + 1, length(left.hand.sides))
    formula <- paste(right.hand.side,paste(c("0",left.hand.sides),collapse="+"),sep="~")
    for(i in min.obs:NR) {
        ans[i-min.obs+1,] <- as.vector(lm(formula=formula,data=panel[1:i,])$coefficients)
    }
    ans
}

NC <- 10
NR <- 1e4
A <- as.data.frame(asofdate=1:NR,matrix(rnorm(NR*NC),nrow=NR,ncol=NC))
colnames(A) <- letters[1:NC]
flm.time <- system.time(ans <- expanding.lm.df(panel=A, right.hand.side="a",left.hand.sides=c("b","c","d","e")))
lm.time <- system.time(lm.ans <- do.running.lm(panel=A, right.hand.side="a",left.hand.sides=c("b","c","d","e")))
cat("all.equal:", all.equal(ans,lm.ans),"\n")
bmark.times <- rbind(flm.time,
                     lm.time,
                     lm.time/flm.time)
print(bmark.times)

armstrtw/fast.lm documentation built on May 10, 2019, 1:42 p.m.