matrix - Regression in R using vectorization and matrices -



matrix - Regression in R using vectorization and matrices -

i have vectorization q in r using matrices. have 2 cols need regressed against each using indices. info

matrix_senttor = [ ... 0.11 0.95 0.23 0.34 0.67 0.54 0.65 0.95 0.12 0.54 0.45 0.43 ] ; indices_forr = [ ... 1 1 1 2 2 2 ] ;

col1 in matrix info msft , goog (3 rows each) , col2 homecoming benchmark stkindex, on corresponding dates. info in matrix format sent matlab.

i utilize

slope <- by( data.frame(matrix_senttor), indices_forr, fun=function(x) {zyp.sen (x1~x2,data=x) $coeff[2] } ) betasfac <- sapply(slope , function(x) x+0)

i'm using data.frame above not utilize cbind(). if utilize cbind() matlab gives error doesn't understand format of data. i'm running these commands within matlab (http://www.mathworks.com/matlabcentral/fileexchange/5051). can replace zyp (zyp.sen) lm.

by slow here (may because of dataframes?). there improve way it? takes 14secs+ 150k rows of data. can instead utilize matrix-vectorization in r? thanks.

i still think overcomplicating things moving matlab r , back. , passing 150k rows of info must slowing things downwards considerably.

zyp.sen pretty trivial port matlab. here go:

function [intercept, slope, intercepts, slopes, rank, residuals] = zypsen(x, y) % computes thiel-sen estimate of slope vector of data. n = length(x); slopes = arrayfun(@(i) zypslopediff(i, x, y, n), 1:(n - 1), ... 'uniformoutput', false); slopes = [slopes{:}]; sni = isfinite(slopes); slope = median(slopes(sni)); intercepts = y - slope * x; intercept = median(intercepts); rank = 2; residuals = x - slope * y + intercept; end function z = zypslopediff(i, x, y, n) z = (y(1:(n - i)) - y((i + 1):n)) ./ ... (x(1:(n - i)) - x((i + 1):n)); end

i checked using r's example(zyp.sen), , gives same answer.

x = [0 1 2 4 5] y = [6 4 1 8 7] [int, sl, ints, sls, ra, res] = zypsen(x, y)

you should farther checking though, sure.

r matrix data.frame vectorization regression

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -