Translating Matlab function to R -


i having lot of trouble translating piece of matlab-code r. missing conceptually. matlab in whole is:

nobs = 2000;  x = cumsum([0;randn(nobs,1)/sqrt(nobs)]); k = 1; n = size(x,1);  dx = zeros(n-k,1);  % calculate dx - part having trouble = k : n - 1     sumx = 0;  j = 0 : k     sumx = sumx + (-1)^j*nchoosek(k,j)*x(i-j+1); end     dx(i-k+1) = sumx; end 

i have tried make function, works. function following part of matlab code:

for j = 0 : k     sumx = sumx + (-1)^j*nchoosek(k,j)*x(i-j+1); end   

so far, i've done in r, corresponds first part of trouble code:

function (n,i,k){   sumx <- 0   (j in 0:k){     term <- ((-1)^j)*choose(k,j)*xsub((i-j)/n)     sumx <- sumx + term    }   return(sumx) } 

now works, can't sum across i?

for (k in 1:2){    (i in k:10){     ?????????????????????    } } 

i don't have matlab here. compare results known sequence, rse=c(1:5)/100

nobs = 2000; rse=rnorm(nobs) #rse=c(1:5)/100 x = cumsum(c(0,rse/sqrt((length(rse)))); k = 1; n = length(x); dx = rep(0,n-k); (i in k:(n - 1)){   sumx = 0;   (j in 0:k)     sumx = sumx + (-1)^j*choose(k,j)*x[i-j+1];   dx[i-k+1] = sumx; } plot(dx~x[-(1:k)],col=3) 
> cbind(dx,x=x[-(1:k)],rse)               dx           x  rse [1,] 0.004472136 0.004472136 0.01 [2,] 0.008944272 0.013416408 0.02 [3,] 0.013416408 0.026832816 0.03 [4,] 0.017888544 0.044721360 0.04 [5,] 0.022360680 0.067082039 0.05 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -