Skip to content

Multiple regressions with changing dependent variable and time span

3 messages · arun, nooldor

#
Hi,

I was able to read the file after saving it as .csv.? It seems to work without any errors.

dat1<-read.csv("Book2.csv", header=T)
###same as previous

lst1 <- lapply(paste("r",1:334,sep="."),function(x) cbind(dat1[,c(1:3)],dat1[x]))
lst2 <- lapply(lst1,function(x) {colnames(x)[4] <-"r";x} )
?sapply(lst2,function(x) sum(!!rowSums(is.na(x))))
library(zoo)
res1 <- do.call(rbind,lapply(lst2,function(x) rollapply(x,width=32,FUN=function(z) {z1 <- as.data.frame(z); if(!sum(!!rowSums(is.na(z1)))) {l1 <-lm(r~F.1+F.2+F.3,data=z1); c(coef(l1), pval=summary(l1)$coef[,4], rsquare=summary(l1)$r.squared) } else rep(NA,9)},by.column=FALSE,align="right")))
row.names(res1) <- rep(paste("r",1:334,sep="."),each=123)
?dim(res1)
#[1] 41082???? 9

#vif
?library(car)
res2 <- do.call(rbind,lapply(lst2,function(x) rollapply(x,width=32,FUN=function(z) {z1 <- as.data.frame(z); if(!sum(!!rowSums(is.na(z1)))) {l1 <-lm(r~F.1+F.2+F.3,data=z1); vif(l1) } else rep(NA,3)},by.column=FALSE,align="right")))
row.names(res2) <- rep(paste("r",1:334,sep="."),each=123)
dim(res2)
#[1] 41082???? 3

#DW statistic:
?lst3 <- lapply(lst2,function(x) rollapply(x,width=32,FUN=function(z) {z1 <- as.data.frame(z); if(!sum(!!rowSums(is.na(z1)))) {l1 <-lm(r~F.1+F.2+F.3,data=z1); durbinWatsonTest(l1) } else rep(NA,4)},by.column=FALSE,align="right"))
?res3 <- do.call(rbind,lapply(lst3,function(x) x[,-4]))
row.names(res3) <- rep(paste("r",1:334,sep="."),each=123)
?dim(res3)
#[1] 41082???? 3
##ncvTest()
f4 <- function(meanmod, dta, varmod) {
assign(".dta", dta, envir=.GlobalEnv)
assign(".meanmod", meanmod, envir=.GlobalEnv)
m1 <- lm(.meanmod, .dta)
ans <- ncvTest(m1, varmod)
remove(".dta", envir=.GlobalEnv)
remove(".meanmod", envir=.GlobalEnv)
ans
}

?lst4 <- lapply(lst2,function(x) rollapply(x,width=32,FUN=function(z) {z1 <- as.data.frame(z); if(!sum(!!rowSums(is.na(z1)))) {l1 <-f4(r~.,z1) } else NA},by.column=FALSE,align="right"))
names(lst4) <- paste("r",1:334,sep=".") 
length(lst4)
#[1] 334


###jarque.bera.test
library(tseries)
res5 <- do.call(rbind,lapply(lst2,function(x) rollapply(x,width=32,FUN=function(z) {z1 <- as.data.frame(z); if(!sum(!!rowSums(is.na(z1)))) {l1 <-lm(r~F.1+F.2+F.3,data=z1); resid <- residuals(l1); unlist(jarque.bera.test(resid)[1:3]) } else rep(NA,3)},by.column=FALSE,align="right")))
?dim(res5)
#[1] 41082???? 3

A.K.
On Saturday, November 30, 2013 1:44 PM, nooldor <nooldor at gmail.com> wrote:
here is in .xlsx should be easy to open and eventually find&replace commas according to you excel settings (or maybe it will do it automatically)
On 30 November 2013 19:15, arun <smartpink111 at yahoo.com> wrote:
I tried that, but:
#
Hi,
No problem.

In that case, each column will be a list.? For example if I take the first element of `lst2`
dW1 <- rollapply(lst2[[1]],width=32,FUN=function(z) {z1 <- as.data.frame(z); if(!sum(!!rowSums(is.na(z1)))) {l1 <-lm(r~F.1+F.2+F.3,data=z1); durbinWatsonTest(l1,max.lag=3) } else rep(NA,4)},by.column=FALSE,align="right")

?tail(dW1[,1],1)
#[[1]]
#[1] -0.3602936? 0.1975667 -0.1740797


You can store it by:
resdW1 <- do.call(cbind,lapply(seq_len(ncol(dW1)),function(i) do.call(rbind,dW1[,i]))[1:3])


Similarly, for more than one elements (using a subset of lst2- as it takes time)


lst3 <- lapply(lst2[1:2],function(x) rollapply(x,width=32,FUN=function(z) {z1 <- as.data.frame(z); if(!sum(!!rowSums(is.na(z1)))) {l1 <-lm(r~F.1+F.2+F.3,data=z1); durbinWatsonTest(l1,max.lag=3) } else rep(NA,4)},by.column=FALSE,align="right"))

lst3New <- lapply(lst3,function(x) do.call(cbind,lapply(seq_len(ncol(x)),function(i) do.call(rbind,x[,i]))[1:3]))

lst3New <- lapply(lst3New, function(x) {colnames(x) <- paste0(rep(c("r","dw","p"),each=3),1:3); x})

A.K.
On Saturday, November 30, 2013 5:03 PM, nooldor <nooldor at gmail.com> wrote:
Hey!


Yes,
only the D-W test takes so much time, did not check it yet

I checked results (estimates) with manually run regressions (in excel) and they are correct.


I only change the "width" to 31 and "each=123" to 124, cause it should be ((154-31)+1) x 334 = 41416 matrix


with the lag in D-W test I was wondering how to have table when I use durbinWatsonTest(l1,3) - with three lags instead of default 1.

but I can manage it - just need to learn about functions used by you.


Any way: BIG THANK to you!


Best wishes,
T.S.
On 30 November 2013 21:12, arun <smartpink111 at yahoo.com> wrote:
Hi,