expanding.panel.df: no frills expanding window linear regression for panel data.

Description Usage Arguments Value Author(s) See Also Examples

Description

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

a regression is performed for each unique time step in the data set. and the coefficients are returned as a time series.

Usage

1
expanding.panel.df(panel, right.hand.side, left.hand.sides, asofdate.column = "asofdate", min.dates = 5L)

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).

asofdate.column

string referencing the column of panel to use as the dates vector.

min.dates

minimum number of dates required to start the expanding window.

Value

a time series 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
22
23
24
25
26
27
28
29
library(fast.lm)

do.running.lm <- function(panel, right.hand.side, left.hand.sides, asofdate.colname="asofdate",min.dates=5L) {
    dts <- panel[,asofdate.colname]
    unique.dates <- unique(dts)
    NR <- nrow(panel)
    ans <- matrix(NA, length(unique.dates)  - min.dates + 1, length(left.hand.sides))
    formula <- paste(right.hand.side,paste(c("0",left.hand.sides),collapse="+"),sep="~")
    for(i in min.dates:length(unique.dates)) {
        ans[i-min.dates+1,] <- as.vector(lm(formula=formula,data=panel[dts <= unique.dates[i],])$coefficients)
        cat(".")
    }
    cat("\n")
    ans
}

NC <- 5
NR <- 1e3
panels <- 10
asofdate <- as.double(rep(1:(NR/panels),each=panels))
A <- data.frame(asofdate=asofdate,matrix(rnorm(NR*NC),nrow=NR,ncol=NC))
colnames(A) <- c("asofdate",letters[1:NC])
flm.time <- system.time(ans <- expanding.panel.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.