Regression: Difference plm and lm and Visualization



Hi everyone

I'm having trouble running a regression. Beneath I created example data for visualization.
Basically I want to do a time-lagged regression of my stock market data. I have columns indicating date, company name, and monthly returns and I want to regress past monthly returns on the current month return. Additionally I want to control for the past 2 years same calendar month returns (SCMR). Now, when I run plm(), it returns

Error in terms.formula(formula) : '.' in formula and 'data' argument invalid

However, with lm it works fine. Therefore, I am not sure, if there is a difference and if there is any problem with my dataset. I was suspecting a problem with the manipulation through paste() or through na.omit(), but I don't know how to check and resolve my suspicion.

Here is the test code:

DATE <- seq(from = as.Date("1981-01-25"), to = as.Date("1985-12-31"), by = 'month')
COMP <- c(replicate(60, "A"), replicate(60, "B"), replicate(60, "C"), replicate(60, "D"),
          replicate(60, "E"), replicate(60, "F"), replicate(60, "G"), replicate(60, "H"))
RET <- rnorm(480)
MARCAP <- rep(c(runif(40, min = 0, max = 500)), each = 12)
RNGVAR <- rep(c(runif(40, min = 0, max = 100)), each = 12)
test1 <- data.table(DATE, COMP, RET, MARCAP, RNGVAR)
test1$MON <- format(as.Date(test1$DATE), "%m")
test1$MONYEAR <- format(as.Date(test1$DATE), "%Y-%m")
test2 <- test1[, SCMR := as.numeric(zoo::rollmeanr(RET, 2, fill = NA)), .(MON, COMP)]
test2[, sapply(24, function(i){paste0('LagT', 1:24)}) := shift(RET, 1:24),
      by = COMP]
test3 <- na.omit(test2)
test4 <- test3[, c(1:3, 8:32)]

testreg1 <- plm(formula = test4$RET ~ . + ., data = test4)
testreg2 <- lm(formula = test4$RET ~ . + ., data = test4)

Also, do you have any ideas on how to visualize the coefficients? I wanted to have a graph with the (time-lag) coefficient on the y-axis and the time (lag) on the x-axis? I tried ggplot2, but I am a bit clueless on how to make it work.

Thanks in advance!


Hey, I tried not using the "." with this:

var <- paste("test3_dt$LagT",1:24,sep="")
form <- paste("test3$RET ~", paste(var, collapse=" + "))

Unfortunately, that doesn't work with plm either as running testreg1 <- plm(form, data = test3), results in:

Error in pFormula(formula) : inherits(object, "formula") is not TRUE