0
votes

I have data frame "PnL_3.df" of 451 observations and 3 variables that are not regularly placed. First column are dates from 2002 to 2013, second column are numeric values, third column are percentage values in numeric format. I would like to build a time series object for generating a plot and calculations. How can I do this ?

I have tried ts(), but this command works only for regularly spaced data.

class(PnL_3.df) [1] "data.frame" str(PnL_3.df) 'data.frame': 451 obs. of 3 variables: $ V1: chr "2002-06-18" "2002-05-22" "2002-05-23" "2002-10-23" ... $ V2: num -97.7 118.1 83.6 126.9 13.2 ... $ V3: num -0.135 0.274 0.167 0.125 0.014 ...

I would be happy to build a time series object.

1

1 Answers

0
votes

Time series objects in R require a constant frequency of the input data (which should be provided). So one solution, if it makes sense with you data, would be to extrapolate them with a constant time step (see e.g. https://www.rdocumentation.org/packages/stats/versions/3.6.1/topics/approxfun).

Once you have it, you can create time series object for each individual variables. For the second variable of your data frame it would look like:

    myts <- ts(PnL_3.df[,2], start=PnL_3.df[,1], end=PnL_3.df[length(PnL_3.df[,1]),1],
               frequency=frequency_of_data(obs. per year))

See : https://www.statmethods.net/advstats/timeseries.html