Home > database >  compute log return in R
compute log return in R

Time:08-26

I keep getting the same error when I try to compute the log return. What am I doing wrong?

stockindices = read.csv('https://raw.githubusercontent.com/bandcar/Examples/main/stockInd.csv')
prices = stockindices[,-c(1,2)]
n <- length(prices)
logReturn <- log(prices/prices[n-1])

Error in Ops.data.frame(prices, prices[n - 1]) : ‘/’ only defined for equally-sized data frames

CodePudding user response:

Is this what you are after?

library(data.table)
setDT(stockindices)

stockindices[, (names(stockindices)[3:ncol(stockindices)]) :=
             lapply(.SD,
                    function(x) {log(x/lag(x))}
                      ),
             .SDcols = names(stockindices)[3:ncol(stockindices)]]

stockindices
         X       Date           DJX          SPX          HKX          NKX           DAX           UKX
   1:    1 1999-04-01            NA           NA           NA           NA            NA            NA
   2:    2 1999-05-01  0.0137257534  0.013490548  0.008313656 -0.013745764  0.0002950619  0.0133137062
   3:    3 1999-06-01  0.0247974115  0.021898917  0.034064649  0.017656594  0.0354717194  0.0314885188
   4:    4 1999-07-01 -0.0007556571 -0.002053445  0.043946659  0.005043517 -0.0223677759 -0.0077714677
   5:    5 1999-08-01  0.0110067906  0.004212485  0.002720363 -0.010750847  0.0129956430  0.0075112204
  ---                                                                                                 
3974: 3974 2014-03-27 -0.0002926240 -0.001901881 -0.002438122  0.010015874  0.0002783100 -0.0025739728
3975: 3975 2014-03-28  0.0036106141  0.004629513  0.010527665  0.004989280  0.0142850568  0.0041290889
3976: 3976 2014-03-31  0.0082121904  0.007892887  0.003868689  0.008928431 -0.0032680212 -0.0026048244
3977: 3977 2014-01-04  0.0045437718  0.007014659  0.013340228 -0.002420002  0.0049896709  0.0081866100
3978: 3978 2014-02-04  0.0024400710  0.002849261  0.003353165  0.010379298  0.0020439939  0.0009660711

CodePudding user response:

library(tidyverse)

stockindices %>%  
  mutate(across(3:ncol(.), ~ (log(.x / lag(.x)))))

# A tibble: 3,978 x 8
       X Date             DJX      SPX      HKX       NKX       DAX      UKX
   <int> <chr>          <dbl>    <dbl>    <dbl>     <dbl>     <dbl>    <dbl>
 1     1 1999-04-01 NA        NA       NA       NA        NA        NA      
 2     2 1999-05-01  0.0137    0.0135   0.00831 -0.0137    0.000295  0.0133 
 3     3 1999-06-01  0.0248    0.0219   0.0341   0.0177    0.0355    0.0315 
 4     4 1999-07-01 -0.000756 -0.00205  0.0439   0.00504  -0.0224   -0.00777
 5     5 1999-08-01  0.0110    0.00421  0.00272 -0.0108    0.0130    0.00751
 6     6 1999-11-01 -0.00243  -0.00883 -0.00828 -0.00174  -0.0229   -0.0102 
 7     7 1999-12-01 -0.0152   -0.0195   0.00724 -0.000562 -0.0135   -0.00848
 8     8 1999-01-13 -0.0133   -0.00413 -0.0417   0.00319  -0.0530   -0.0309 
 9     9 1999-01-14 -0.0248   -0.0182  -0.00886  0.0247   -0.00387  -0.00512
10    10 1999-01-15  0.0238    0.0253  -0.00351  0         0.00962   0.0205 
# ... with 3,968 more rows
  • Related