df <- data.frame(c(6.2411, 6.2415, 6.2454, 6.2456, 6.2437, 6.2429))
names(df) <- "price"
rownames(df) <- c("2012-11-01", "2012-11-02", "2012-11-05", "2012-11-06", "2012-11-07", "2012-11-08")
df
# price
# 2012-11-01 6.2411
# 2012-11-02 6.2415
# 2012-11-05 6.2454
# 2012-11-06 6.2456
# 2012-11-07 6.2437
# 2012-11-08 6.2429
You are trying to assign irregular days. Hence, lubridate
may not be recite to your problem. When you use df[,1]
for your further operations that needs a ts
object, df[,1]
will be automatically coerced to this class. You can perform any analysis you wish with df[,1]
.
Please note that:
The observation points are non-equally (unevenly/irregularly) spaced (national holidays etc. may result in the difference in the number of observations). You may thought the intersections when multiple series are in consideration along with price. You should neglect the effect of the irregularity in such a case since the spacing of the observations would be the same for many observations, and therefore not so highly irregular and also consider the fact that transforming the data into equally spaced observations using linear interpolation can introduce a number of significant and hard to quantify biases (See: Scholes and Williams).
M. Scholes and J. Williams, “Estimating betas from nonsynchronous data”, Journal of Financial Economics 5: 309–327, 1977.
Generalization over Excel2016:
Assume you have thousands of dates and price data in Excel file (DatePrice.xlsx):
A B
1 Date Price
2 2012-11-01 6.2411
3 2012-11-02 6.2415
...
Then, do the following:
library(readxl)
# Use the path returned from getwd() function that is R's working directory
df <- as.data.frame(read_excel("C://Users//User//Documents//Revolution//DatePrice.xlsx"))
names(df) <- c("date","price")
rownames(df) <- df[,1]
df[,1] <- NULL
df
Again, df[,1]
will be the time series that will be used in coercing operations in any further analysis. For example;
An exemplary analysis of how to proceed with the solution:
price <- df[,1]
plot(ts(price)); abline(a=mean(ts(price)), b=0) # graphically, price~ I(1)
#Stationarity analysis (even with 6 obs, it produces results!)
library(fUnitRoots); unitrootTest(price) # formally, price~ I(1) p=0.6889
plot(diff(ts(price), differences=1)) # graphically, Delta(price) ~ I(0)
unitrootTest(diff(ts(price), differences=1)) # formally, Delta(price) ~ I(0) p=1e-04<0.05