Reputation: 219
I am trying to learn R to analyze discrete and unevenly spaced time series for my research. The dataframe has columns "t,y,dy", where t is time in units of days and y/dy is the value/value error. I first downloaded my data and plotted it as shown:
ogle <- read_csv("ogle_data/sc49_ogle.csv")
ggplot(ogle, aes(x = t, y = y, ymin = y-dy, ymax = y+dy)) +
geom_pointrange() +
ylim(15.3, 14.3) +
xlim(57500, 59460)
Data ouput is:
dput(head(ogle,20))
structure(c(57557.43506, 57560.41181, 57576.4440799998, 57583.3938199999,
57587.4252200001, 57593.3775300002, 57601.3586300001, 57604.3239899999,
57611.3208099999, 57617.3240499999, 57621.27972, 57624.29385,
57629.31177, 57632.2804299998, 57636.28437, 57639.26076, 57646.23569,
57649.2604999999, 57652.2198200002, 57656.20529, 14.416, 14.403,
14.418, 14.417, 14.399, 14.401, 14.417, 14.415, 14.408, 14.381,
14.406, 14.411, 14.403, 14.401, 14.387, 14.394, 14.403, 14.394,
14.411, 14.404, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003,
0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003,
0.003, 0.003, 0.003, 0.003), .Dim = c(20L, 3L), .Dimnames = list(
NULL, c("t", "y", "dy")), .Tsp = c(1, 1.05205479452055, 365
), class = c("mts", "ts", "matrix"))
This shows the data has some gaps and the time domain for the data ranges from about 57,500 to 59,000. Now, when I convert my data to a time series object I get:
Y <- ts(ogle[,2], start = min(ogle[,1]))
plot(Y, ylim = c(15.5,14.3))
As you can see, my new time series does not have the same span in time as the original. It stops around 57,750 rather than 59,000. It appears that the ts()
function is treating each data point as being equally spaced. How do I fix this such that the time series actually reflects the true data?
Upvotes: 0
Views: 53
Reputation: 2459
A ts()
object in R can only handle regularly spaced observations in time. If you need to use the ts()
class, you will need to complete the dataset by adding in NA
values for the time points that are missing. You might be able to do this with your ogle
object using ogle %>% expand(t = full_seq(t, 1))
before creating your ts()
object. Without your dataset, I can't check if this will work.
An alternative would be to use a time series data structure that allows for gaps (implicit missing values), such as tsibble
.
library(tsibble)
as_tsibble(ogle, index = t)
You can learn more about analysing time series data with tsibbles from the FPP3 online textbook: https://otexts.com/fpp3/
Upvotes: 1