R Vectorization Acceleration for a Square Matrix

Anyone who can 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 matis a matrix of NxMobjects of observation Nand M, for example:

    Obj1 Obj2 Obj3
1      .    .    .
2      .    .    .    
3      .    .    .

LinReg 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 perform regression analysis (OLS) for each combination of objects (i.e., Obj1, Obj2and Obj2,Obj3and Obj1, Obj3) in mat, and then using the function adf.testfrom the package tseriesand saving p-value. The end result sym.pris a symmetric matrix of all p-values(but in fact it is not 100% symmetrical, see here for more information ), however, this will be enough.

With the above code on the matrix 600x300(600 observations and 300 objects), it takes about 15 minutes.

, , , .

?

.

+4
1

mdf <- data.frame( x1 = rnorm(5), x2 = rnorm(5), x3 = rnorm(5) )

. , , mdf[c(i,j)] mdf[c(j,i)]. combn, .

pairs <- as.data.frame( t( combn( colnames( mdf  ),2 ) ) )
pairs
  V1 V2
1 x1 x2
2 x1 x3
3 x2 x3

( t.test ):

pairs[["p.value"]] <- apply( pairs, 1, function( i ){
  t.test( mdf[i] )[["p.value"]]
})
pairs
  V1 V2   p.value
1 x1 x2 0.5943814
2 x1 x3 0.7833293
3 x2 x3 0.6760846

p.values ​​ ( ), :

library(reshape2)
acast( pairs, V1 ~ V2 )
          x2        x3
x1 0.5943814 0.7833293
x2        NA 0.6760846
+2

Source: https://habr.com/ru/post/1525177/


All Articles