Skip to content

Instantly share code, notes, and snippets.

@hannes101
Last active October 13, 2015 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannes101/57d88a126a479eae2029 to your computer and use it in GitHub Desktop.
Save hannes101/57d88a126a479eae2029 to your computer and use it in GitHub Desktop.
library(data.table)
myDt <- cbind(
"Dt1" = as.numeric(ifelse( dt.myVariables[,c("Date"), with=FALSE] > "2008-12-18" &
dt.myVariables[,c("Date"), with=FALSE] < "2010-12-07", 1 , 0)),
"Dt2" =as.numeric(ifelse( dt.myVariables[,c("Date"), with=FALSE] > "2010-12-07", 1 , 0))
)
#Ijt - the indicator dummy which is only 1 on the break date
myIndicationD <- cbind( "IndiD1" = as.numeric(dt.myVariables[, c("Date"), with = FALSE] == "2008-12-19"),
"IndiD2" = as.numeric(dt.myVariables[, c("Date"), with = FALSE] == "2010-12-08"))
#Create trend variable
myTrend <-as.matrix(cbind( "Trend" = as.numeric(1:dt.myVariables[,.N])))
lagmatrix <- function(x,max.lag){
x <- as.matrix(x)
if(is.null(colnames(x))== TRUE){
colnames(x) <- "VarCol0"
}
return.matrix <- embed(c(rep(NA,max.lag),x),max.lag+1)
dimnames(return.matrix)[[2]] <- c(colnames(x)[1, drop = FALSE], paste(colnames(x)[1,drop = FALSE],".l",1:max.lag, sep = ""))
return(return.matrix)
}
myDtTrend <- myDt * as.vector(myTrend)
colnames(myDtTrend) <- paste(colnames(myDt),colnames(myTrend), sep = "*")
dummat <- cbind(myTrend
,lagmatrix(myDt[,c("Dt1"), drop = FALSE], max.lag = mylags)#[,1, drop = FALSE]
,lagmatrix(myDt[,c("Dt2"), drop = FALSE], max.lag = mylags)#[,1, drop = FALSE]
,myDtTrend
,lagmatrix(myIndicationD[,c("IndiD1"), drop = FALSE], max.lag = mylags)
,lagmatrix(myIndicationD[,c("IndiD2"), drop = FALSE], max.lag = mylags)
)
#Replace NA with 0
dummat[is.na(dummat)] <- 0
head(dummat)
Trend Dt1 Dt1.l1 Dt1.l2 Dt1.l3 Dt1.l4 Dt1.l5 Dt2 Dt2.l1 Dt2.l2 Dt2.l3 Dt2.l4 Dt2.l5 Dt1*Trend Dt2*Trend IndiD1 IndiD1.l1 IndiD1.l2
[1,] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[5,] 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[6,] 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
IndiD1.l3 IndiD1.l4 IndiD1.l5 IndiD2 IndiD2.l1 IndiD2.l2 IndiD2.l3 IndiD2.l4 IndiD2.l5
[1,] 0 0 0 0 0 0 0 0 0
[2,] 0 0 0 0 0 0 0 0 0
[3,] 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 0 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment