R: Rank method changes my index format from Date to POSIXct - r

I have the following problem. I have a XTS containing a date column and several valuations, which should be ranked (biggest = best rank). So my original XTS is test:
> str(index(Test))
Date[1:235], format: "1995-01-31" "1995-02-28" "1995-03-31" "1995-04-28" "1995-05-31" "1995-06-30" "1995-07-31" ...
Now, my rankValuations function:
rankValuations<-function(ValuationXTS){
#Ranks the xts object asset valuations
#ValuationXTS is a xts time series with asset valuations
#Returns an xts object with ranks (asset with the greatest valuation receives 1)
#The ties parameter results in an ascending ranking. If two share the same rank, the first in the matrix gets the first rank
ranked<-as.xts(t(apply(-ValuationXTS,1,rank, ties.method="first",na.last=TRUE)))
}
After running this my index format has changed to POSIX:
> Test<-rankValuations(Test)
> str(index(Test))
POSIXct[1:235], format: "1995-01-31" "1995-02-28" "1995-03-31" "1995-04-28" "1995-05-31" "1995-06-30" "1995-07-31" ...
And this is a big problem because in the POSIX I have now a timezone. If using later on merge.xts it never matches since the POSIX dates are 1 day prior than in the to be merged with XTS which has a Date index. So how can I stop the rank method of changing Date to POSIX?

Related

Time series and how I can construct a ts object

I have a dataset with 3 variables:
the first is date (example"01/01/2019" )
the second is hour (example:"01:00"), and
the third is a numeric.
I want to construct an object ts, but I don't know how I can do this.The first and second variables are characters.
I want an hour time series

Quantmod - Chop data and constructing matrix of return series

I am having trouble with my R assignment I am working on this semester.
Here is the part that I am tasked with doing that I am confused about:
iv. Download 3 month TBill rate from Fred for the same sample period 01/01/1993 to 12/31/2013.
Useful Hints: You may have to chop the data to match the sample period.
v. Construct a matrix of return series combining Stock, S&P500, and TBill for the sample period.
Useful Hints:
Note that the rownames for the TBill may not match with the other two return series, as the dates do not match, although the month and year matches
You have to construct the row names for each of the series as Year – Month format (e.g. 1993-01) or delete the rownames from T-bill before you can combine all three series into one Return matrix.
You have to convert the Return matrix to a dataframe before you use the lm() function.
I tried this below like I have used getSymbols before for SPY and AAPL but it pulls an entire data set rather than the specific date range. How can I chop the data so it fits the desired date range?
getSymbols('TB3MS', src = 'FRED', from = "1993-01-01", to = "2013-12-31")
Next, how would I go about constructing the matrix of return series combining all of the stocks? Can anyone point me in the right direction?
Filtering an xts object: see examples in the xts documentation ?xts.
# filter 1993 until 2013
TB3MS["1993/2013"]
But these dates are of, because tbills are at the first day of the month, the stock dates are the last day of the month. With the coredata you can extract the tbill data and stick it into the other timeseries if the rows match.
Taking the data example from your previous question, you could do something like this (and I'm creating more steps than needed, you could combine a few statements into one):
# create monthly returns of the spy data and give the column a better name than monthly.returns
spy_returns <- monthlyReturn(SPY)
colnames(spy_returns) <- "SPY_returns"
# filter the tbill data
TB3MS_1993_2013 <- TB3MS["1993/2013"]
# add tbill data to spy data
spy_returns$TB3MS <- coredata(TB3MS_1993_2013)
Merging xts objects can just be done with merge. They will be merged on the dates.
merge(spy_returns, aapl_returns) would combine these two. If you have a lot of tickers, use Reduce (check help and SO on how to use Reduce with merge) but better would be to use the tidyquant package if allowed.

Copy Timestamp of xts object to another Matrix in R

I am having a bit of difficulty properly extracting timestamps from an xts object and putting them into another matrix. Basically, I have an xts object with a timestamp column in a YYYY-MM-DD HH:MM:SS.SSS format, and I want to extract specific times (in order) and put them into a column in another matrix in the exact same format as they are in the xts object. For example, let's say I have an xts object with a timestamp column given as:
For example, let's say the timestamp column for a matrix called mat is given as follows:
2000-01-01 09:05:02.333
2000-01-01 09:06:03.212
2000-01-01 09:06:04.764
2000-01-01 09:07:02.211
Now let's say I want to take the 2nd and 4th times and put them into another matrix (which I'll call mat2), then ideally it should come out like this:
Time
---------------------------
2000-01-01 09:06:03.212
2000-01-01 09:07:02.211
Now, I know that by using the index() function on an xts object you can get the timestamp for that object at a particular index value. However, when I try to do this by writing (for example) mat2[i,"Time"] <- index(mat[i]), then rather than putting the date/time value from mat into mat2 it instead puts a number into the matrix, not a time, and I'm not sure why that happens. Is there a way to copy the timestamp of an xts object and put it into take two different time values from one matrix and put them into two separate columns in a different matrix?

How to merge a daily and an intra-day XTS object?

I am trying to merge a daily XTS object (indexed by POSIXCT, format = "%d/%m/%Y") with an intraday XTS object (indexed by POSIXCT, format = "%d/%m/%Y %H:%M").
The intra day object doesn't have a midnight (00:00) index, but by default the merge creates one and adds the daily variable to that observation.
How can I merge the daily into the intraday, but merge to the nearest index, so I do not create a bunch of 00:00 observations in my data?
Not sure if its the best solution, but I ended up filtering out any indexes that matched in both data
x <- x[!(index(x) %in% index(y))]

Linking characters from one data.frame to other datasets

I have a data.frame with two columns. The first column contains various specific times during a day. The second column contains the animal behavior (behavior period) that I observed at each specific time:
Time; Behavior
10:20; feeding
10:25; feeding
10:30; resting
...
For each of those behavior periods I have an additional dataset (TimeSeries) which contains data about the actual animal movement (output from a movement sensor). Each TimeSeries has about 100 rows:
Time; Var1; Var2
10:20:01; 1345; 5232
10:20:02; 1423; 5271
...
Now I would like to link each TimeSeries with the behavior from the first dataset. So, that R knows that "feeding" is related to the TimeSeries of 10:20 and 10:25 and that "resting" is related to the TimeSeries of 10:30 and so on.
Afterwards I want to use this "knowledge" to calculate mean and sd from each TimeSeries. So I will have all the means and sd's from all TimeSeries for each behavior.
It is not clear whether your times are currently characters, factors, POSIXct, variables, etc. So you should first convert them (possibly in a new column) to a numeric variable, something like the number of seconds since midnight. Functions like strptime, difftime, and as.numeric may help.
Add a column to the first data frame that is just 1:nrow(firstdf). Then add a column to the second dataframe that is computed by the findInterval function:
seconddf$newcol <- findInterval( seconddf$seconds, firstdf$seconds )
Now you can merge the 2 data frames on the new columns and the finer grained times will be associated with the activity from the most recent time.

Resources