[R] Odp: Calculating Returns : (Extremely sorry for earlier incomplete mail)

Petr PIKAL petr.pikal at precheza.cz
Fri Jan 7 11:52:59 CET 2011


Hi

Your code is quite complicated and I get an error

spot_returns_table <- lapply(1:nrow(trans), function(z) with(trans[z, ], 
spot_trans(currency_trans=trans$currency_transacted)))
Error in if (currency_trans == "USD") { : argument is of length zero

It seems to me that you do not know what is your code doing.

The warnings are from the fact that the currency_trans value you feed to 
spot_trans function is longer than one and if function needs an input of 
only one logical value.

Maybe you could use debug and see what are values of your variables during 
computation but I believe that better is to use more convenient input 
objects together with *apply or aggregate or basic math could be better 
solution.

rate1
      USD    GBP  EURO   CHF    AUD
1  112.05 171.52 42.71 41.50 109.55
2  112.90 168.27 42.68 41.47 102.52
3  110.85 169.03 41.86 42.84 114.91
4  109.63 169.64 44.71 43.44 122.48
5  108.08 169.29 44.14 43.69 122.12
6  111.23 169.47 44.58 42.30 123.96
7  112.49 170.90 41.07 42.05 100.36
8  108.87 168.69 42.23 41.23 110.19
9  109.33 170.90 44.55 42.76 121.58
10 111.88 169.96 41.12 43.79 103.46

log(rate1[-1,]/rate1[-nrow(rate1),])

Is this what you want?

Regards
Petr




r-help-bounces at r-project.org napsal dne 07.01.2011 07:36:28:

> 
> 
> 
> 
> 
> 
> 
> 
> 
> Dear R forum helpers,
> 
> I am extremely sorry for the receipt of my incomplete mail yesterday. 
There 
> was connectivity problem at my end and so I chose to send the mail 
through my 
> cell, only to realize today about the way mail has been transmitted. I 
am 
> again sending my complete mail through regular channel and sincerely 
apologize
> for the inconvenience caused.
> 
> 
> ## Here is my actual mail
> 
> 
> Dear R forum helpers,
> 
> I have following data
> 
> trans <- data.frame(currency = c("EURO", "USD", "USD", "GBP", "USD", 
"AUD"), 
> position_amt = c(10000, 25000, 20000, 15000, 22000, 30000))
> 
> date <- c("12/31/2010", "12/30/2010", "12/29/2010", "12/28/2010", 
"12/27/
> 2010", "12/24/2010", "12/23/2010", "12/22/2010", "12/21/2010", 
"12/20/2010")
> USD  <- c(112.05, 112.9, 110.85, 109.63, 108.08, 111.23, 112.49, 108.87, 
109.33, 111.88)
> GBP  <- c(171.52, 168.27,169.03, 169.64, 169.29, 169.47, 170.9, 168.69, 
170.9, 169.96)
> EURO <- c(42.71, 42.68, 41.86, 44.71, 44.14, 44.58, 41.07, 42.23, 44.55, 
41.12)
> CHF  <- c(41.5, 41.47, 42.84, 43.44, 43.69, 42.3, 42.05, 41.23, 42.76, 
43.79)
> AUD  <- c(109.55, 102.52, 114.91, 122.48, 122.12, 123.96, 100.36, 
110.19, 121.
> 58, 103.46)
> 
> These are the exchange rates and I am trying calculating the returns. I 
am 
> giving only a small portion of actual data as I can't send this as an 
> attachment. I am using function as I want to generalize the code for any 
portfolio. 
> 
> 
> # __________________________________________________
> 
> # My Code
>  
> trans    <- read.table('transactions.csv', header=TRUE, sep=",", 
> na.strings="NA", dec=".", strip.white=TRUE) 
> # reading as table. 
> 
> #currency <- read.table('currency.csv')
> 
> #date     <- currency$date
> #USD = currency$USD
> #GBP = currency$GBP
> #EURO = currency$EURO
> #CHF = currency$CHF
> #AUD = currency$AUD
> 
> # _________________________________________________________
> 
> # CREATION of Function. I am using function as no of transactions is not 
constant. 
> 
> spot_trans = function(currency_trans) 
> 
> {
> 
> if (currency_trans == "USD")     
> {rate = USD}
> 
> # So if I am dealing with TRANSACTION "USD", I am selecting the USD 
exchange rates.    
> 
> if (currency_trans == "GBP")
> {rate = GBP}
> 
> if (currency_trans == "EURO")
> {rate = EURO}
> 
> if (currency_trans == "CHF")
> {rate = CHF}
> 
> if (currency_trans == "AUD")
> {rate = AUD}
> 
> # ________________________________________________
> 
> # CURRENCY Rate RETURNS i.e. lob(todays rate / yesterday rate) and the 
data is
> in descending "Date" order
> 
> currency_rate_returns = NULL
> for (i in 1:(length(rate)-1))   # if there are 10 elements, total no of 
returns = 9
>     
>     {
>     currency_rate_returns[i] = log(rate[i]/rate[i+1])
>     }
> 
> currency_rate_returns
> 
> return(data.frame(returns = currency_rate_returns))
>     
> }
> 
> # _______________________________________________
> 
> spot_returns_table <- lapply(1:nrow(trans), function(z) with(trans[z, ], 

> spot_trans(currency_trans=trans$currency_transacted)))
> 
> spot_returns_table
> 
> This generates the output as given below with 30 warnings. Also, as 
there are 
> six transactions, 6 outputs are generated but the output in all pertains 
only 
> to the first transacations i.e. 6 times returns are generated for the 
first 
> transaction "EURO"
> 
> > warnings()
> Warning messages:
> 1: In if (currency_trans == "USD") { ... :
>   the condition has length > 1 and only the first element will be used
> 2: In if (currency_trans == "GBP") { ... :
>   the condition has length > 1 and only the first element will be used
> 3: In if (currency_trans == "EURO") { ... :
>   the condition has length > 1 and only the first element will be used
> 
> .... and so on
> 
> The output is as given below.
> 
> > spot_returns_table
> [[1]]
>     spot_returns
> 1   0.0007026584
> 2   0.0193997094
> 3  -0.0658664732
> 4   0.0128307894
> 5  -0.0099189271
> 6   0.0820074000
> 7  -0.0278529410
> 8  -0.0534812850
> 9   0.0801175328
> 10 -0.0710983059
> 
> [[2]]
>     spot_returns
> 1   0.0007026584
> 2   0.0193997094
> 3  -0.0658664732
> 4   0.0128307894
> 5  -0.0099189271
> 6   0.0820074000
> 7  -0.0278529410
> 8  -0.0534812850
> 9   0.0801175328
> 10 -0.0710983059
> 
> [[3]]
>     spot_returns
> 1   0.0007026584
> 2   0.0193997094
> 3  -0.0658664732
> 4   0.0128307894
> 5  -0.0099189271
> 6   0.0820074000
> ....................
> ....................  
> 
> and so on.
> 
> Kindly guide as if there is only one transaction i.e. I am dealing with 
only 
> one currency, code runs excellently.
> 
> Thanking in advance and once again apologize for the inconvenience 
caused.
> 
> Amelia Vettori
> 
> 
> 
> 
> 
> 
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list