[prev in list] [next in list] [prev in thread] [next in thread]
List: r-sig-finance
Subject: Re: [R-SIG-Finance] apply Function Problem
From: G See <gsee000 () gmail ! com>
Date: 2011-10-31 16:21:42
Message-ID: CA+xi=qaq1xh_-ZG74xLoPTFv2MJLJjUjBkpAnbfZQqmH68mq2A () mail ! gmail ! com
[Download RAW message or body]
Hi Josh,
I do not see how you were able to get the "individual column testing" to
work with the code you supplied (even if I make up my own data for your
`data` object and assign your 'function(x)' to StdTS -- please follow the
posting guide and give reproducible code).
Your immediate problem is that the first value that `returns` gives is NA.
So, the first time through the for loop, you are trying to multiply 100 *
(1 + NA) which is NA.
FYI, `returns` calculates log returns by default
here are a few functions to look at
?lag
?quantmod:::Lag
?cumsum
?cumprod
Here is how you could create some example data which would be the first
step towards making your code reproducible.
1> A <- xts(9+cumsum(rnorm(1:10)), Sys.Date()-10:1)
1> B <- xts(50+cumsum(rnorm(1:10)), Sys.Date()-10:1)
1> mydata <- cbind(A,B)
1> colnames(mydata) <- c("A","B")
1> mydata
A B
2011-10-21 9.779622 50.81187
2011-10-22 11.235407 53.00871
2011-10-23 10.591079 55.05790
2011-10-24 9.037941 56.69034
2011-10-25 7.440232 56.94461
2011-10-26 9.245329 57.43580
2011-10-27 8.763682 57.11172
2011-10-28 9.384062 55.44967
2011-10-29 9.996185 57.21740
2011-10-30 9.833874 57.24320
Garrett
On Mon, Oct 31, 2011 at 3:11 AM, JOSH CHIEN <joshchien@yahoo.com> wrote:
> Hi All,
> About "apply" function, I need your guys' help to figure out my problem.
>
> below is my example.
> > data
> GMT
> A B
> 2011-02-24 47.3 87.9
> 2011-02-25 47.4 88.1
> 2011-03-01 48.1 88.6
>
> I want to do standardized process to compare both series.
> Like,
> > StdTS(data[,1])
> GMT
> A
> 2011-02-24 100.0000
> 2011-02-25 100.2112
> 2011-03-01 101.6803
> > StdTS(data[,2])
> GMT
> B
> 2011-02-24 100.0000
> 2011-02-25 100.2273
> 2011-03-01 100.7945
>
>
> I code a function.
>
> function(x) {
>
>
> require(fBasics)
>
>
> x.ret <- returns(x)
>
>
> x[1] <- 100
>
> for(i in 2:length(x)){
>
> x[i] <- x[i-1]*(1+x.ret[i-1])
>
> }
>
> x
>
> }
>
> Individual column testing is well as above.
> But, I use "apply(data,2,StdTS)"
>
> > apply(data,2,StdTS)
> GMT
> A B
> 2011-02-24 100 100
> 2011-02-25 NA NA
> 2011-03-01 NA NA
>
>
> I don't know why ?
>
> Anyone can help me ?
>
> Sincerely,
> Josh
> [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-SIG-Finance@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>
[[alternative HTML version deleted]]
_______________________________________________
R-SIG-Finance@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic