0
votes

I have multivariate data collected during the spring and summer seasons (months: 5,6,7,8), not necessarily daily, and over 30 years. How do I turn it into a time series object to conduct a time series analysis?

I have tried: timeseries <- ts(data, start(2017,5), frequency = 4)

But I didn't know what frequency to have, since it's collected 4 times a year but not as "quarterlies".

This is what the data looks like:

    Year  Month Day   ID     Size      Sex      Temperature
    1 2017     5  13  033     54.0       M        13.0
    2 2017     5  15  044     53.5       F        11.0
    3 2017     5  16  141     55.8       M        15.7

I figure maybe I will have to add NAs in the data for the months and days that lack collected data. I also don't know how to divide by day AND month per year...

1

1 Answers

0
votes

1) Using the data shown in the Note at the end (slightly changed from the question) create a zoo series from Temperature whose times are year + (month-5)/4 reducing multiple values in a month by using mean and then convert all that to ts.

library(zoo)
toYearMon <- function(x) x[[1]] + (x[[2]] - 5)/4
z <- read.zoo(DF[c("Year", "Month", "Day", "Temperature")], index = 1:3, 
  FUN = toYearMon, aggregate = mean)
as.ts(z)
##      Qtr1 Qtr2
## 2017 12.0 15.7

ts will think the 4 months are quarters but hopefully you can live with that. Alternately use z.

2) Another possibility, since there are 123 days from May1st to August31st inclusive, is to create a time variable that is year + 0 for May1st, year + 1/123 for May2nd, ..., year + 122/123 for Aug31st.

toDate <- function(x) as.Date(paste(x[[1]], x[[2]], x[[3]], sep = "-"))

sinceMay1 <- function(x) {
  d <- toDate(x)
  may1 <- toDate(data.frame(x[[1]], 5, 1))
  x[[1]] + as.numeric(d - may1) / 123
}

zsm <- read.zoo(DF[c("Year", "Month", "Day", "Temperature")], index = 1:3,
  FUN = sinceMay1)
frequency(zsm) <- 123

and now we can use zsm or as.ts(zsm)

3) another possibility if its good enough to use 1, 2, 3, ... for the times is

ts(DF$Temperature)

4) We can create a zoo series like this where toDate is from above:

read.zoo(DF[c("Year", "Month", "Day", "Temperature")], index = 1:3,
  FUN = toDate)

Note

We changed the last Month to 6.

Lines <- "Year  Month Day   ID     Size      Sex      Temperature
    1 2017     5  13  033     54.0       M        13.0
    2 2017     5  15  044     53.5       F        11.0
    3 2017     6  16  141     55.8       M        15.7"
DF <- read.table(text = Lines)