[R] Code working but too slow, any idea for how to speed it up ?(no loop in it)

Ravi Varadhan rvaradhan at jhmi.edu
Fri Feb 12 20:11:14 CET 2010


First of all, please send a reproducible code.  Your code is wrong and it does not execute: naming a function called "function" is a bad idea.

Ravi.

____________________________________________________________________

Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University

Ph. (410) 502-2619
email: rvaradhan at jhmi.edu


----- Original Message -----
From: anna <lippelanna24 at hotmail.com>
Date: Friday, February 12, 2010 1:30 pm
Subject: [R] Code working but too slow, any idea for how to speed it up ?(no loop in it)
To: r-help at r-project.org


>  Hello my friends, 
>  here is a code I wrote with no loops on matrix that is taking too 
> long (2
>  seconds and I call him 720 times --> 12 minutes):
>  
>  mat1 and mat2 are both matrix with 103 columns and 164 rows.
>  
>  sequence <- matrix(seq(1 : ncol(mat1)))
>  
>  returns <- apply(sequence, 1, function, mat1= mat1, mat2 = mat2, day 
> = 1)
>  
>  function<- function(mat1, mat2, colNb, day){           
>      mat1<- matrix(mat1[, colNb] )
>      mat2<- matrix(mat2[, colNb])
>      nbDays <- length(mat1)       
>      returns <- abs(mat1[1:(nbDays - day)]) * ((as.ts(lag(mat2,
>  day))/as.ts(mat2))^mat1[1:(nbDays - day)] - 1)
>      return(returns)
>   }
>  
>  I am wondering if the fact of using lag and ts is not slowering down 
> the
>  code, any idea?
>  
>  -----
>  Anna Lippel
>  -- 
>  View this message in context: 
>  Sent from the R help mailing list archive at Nabble.com.
>  
>  ______________________________________________
>  R-help at r-project.org mailing list
>  
>  PLEASE do read the posting guide 
>  and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list