r - Running multiple regressions is there a smart way to vectorize -
i have written believe semi-quick ols-regression function
ols32 <- function (y, x,ridge=1.1) { xrd<-crossprod(x) xry<-crossprod(x, y) diag(xrd)<-ridge*diag(xrd) solve(xrd,xry) }
now want apply following
(vapply(1:la, function(j) me %*% ols32((nza[,j]),(cbind(nzaf1[,j],nzaf2[,j],nza[,-j],momf))) [(la+2):(2*la+1)],fun.value=0))
where nza,nzaf1,nzaf2 , momf 500x50 matrixes , la=50 , me vector of length 50. do regression use beta-coefficients momf multiply me.
nza.mat<-matrix(rnorm(500*200),ncol=200) nza<-nza.mat[,1:50] nzaf2<-nza.mat[,101:150] momf<-nza.mat[,151:200] nzaf1<-nza.mat[,51:100] me<-nza.mat[500,151:200]
is there imediate way of making things faster or need use someting rcppeigen? tks p
so came faster way of solving rewriting ols-function calculates 2 crossproducts once whole matrix. new function looks this:
ols.quick <- function (y, x, me) { la<-dim(y)[2] xx.cross<-crossprod(x) xy.cross<-crossprod(x, y) diag(xx.cross)<-ridge*diag(xx.cross) betas<-sapply(1:la, function(j){ idx<-c((1:la)[-j],la+j,2*la+j,(3*la+1):(4*la)); solve(xx.cross[idx,idx],xy.cross[idx,j])},simplify=t) me%*%betas[(la+2):(2*la+1),] }
where y=nza (500x50) , x=cbind(nza,nzaf1,nzaf2,momf) (500x200) solves problem 3.5 times faster.
microbenchmark(ols.quick(nza,nza.mat,me), vapply(1:la, function(j) me%*%ols32(nza[,j],(cbind(nzaf1[,j],nzaf2[,j],nza[,-j],momf))) [(la+2): (lb+2)],fun.value=0)) min lq median uq max neval 66.30495 67.71903 68.57001 70.17742 77.80069 100 251.59395 255.43306 258.35041 262.85742 296.51313 100
i suppose gain speed parlapply parallel package havet looked yet.
Comments
Post a Comment