Description Usage Arguments Value Author(s) See Also Examples
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)
1 | expanding.lm.df(panel, right.hand.side, left.hand.sides, min.rows)
|
panel |
a dataframe containing the relevant vectors |
right.hand.side |
a string referencing the column containing the observation vector of
length |
left.hand.sides |
a vector of strings referencing the vectors to use as the design
matrix ( |
min.rows |
required number of rows of observations before rolling lm can start. |
a matrix of the coefficients over the expanding window.
Whit Armstrong
lm
the standard lm method.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.