Anyone able to help me speed up some code:
n = seq_len(ncol(mat)) # seq 1 to ncol(mat)
sym.pr<-outer(n,n,Vectorize(function(a,b) {
return(adf.test(LinReg(mat[,c(a,b)]),k=0,alternative="stationary")$p.value)
}))
Where mat
is an NxM
matrix of N
observation and M
objects, e.g:
Obj1 Obj2 Obj3
1 . . .
2 . . .
3 . . .
LinReg
is defined as:
# Performs linear regression via OLS
LinReg=function(vals) {
# regression analysis
# force intercept c at y=0
regline<-lm(vals[,1]~as.matrix(vals[,2:ncol(vals)])+0)
# return spread (residuals)
return(as.matrix(regline$residuals))
}
Basically I am performing a regression analysis (OLS) on every combination of Objects (i.e. Obj1, Obj2
and Obj2,Obj3
and Obj1, Obj3
) in mat
, then using the adf.test
function from the tseries
package and storing the p-value
. The end result sym.pr
is a symmetric matrix of all p-values
(but actually it's not 100% symmetric, see here for more info), nevertheless it will suffice.
With the above code, on a 600x300
matrix (600 observations and 300 objects), it takes about 15 minutes..
I thought of maybe only calculating the upper triangle of the symmetric matrix, but not sure how to go about doing it.
Any ideas?
Thanks.