Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time series (ts) function in R doesn't count correctly

Tags:

r

time-series

When using the ts function in R to create a time series per month, the the totals per time unit do not seem to add up correctly. For example, the value for Jan-2014 should be well over 100, but the output says it's 3 (see below). Does anyone know what's wrong with my code?

Used code:

set.seed(1)
df <- data.frame(date_rec = seq(as.Date("2014-01-01"),as.Date("2015-12-31"), by=1),
                 number = sample(c(1:10),size = 730, replace=TRUE))

df_ts <-  ts(df$number, frequency=12, start=c(2014,1), end=c(2015,12))
df_ts

Result for df_ts

    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2014   3   4   6  10   3   9  10   7   7   1   3   2
2015   7   4   8   5   8  10   4   8  10   3   7   2
like image 468
Joep_S Avatar asked Apr 13 '26 19:04

Joep_S


2 Answers

Another alternative using aggregate before ts:

library(tidyverse)
# Group by month and aggregate the numbers
monthly_data <- aggregate(number ~ year(date_rec) + month(date_rec), data = df, FUN = sum)

# Create a time series object
ts(monthly_data$number, frequency = 12, start = c(2014, 1))

   year(date_rec) month(date_rec) number
1            2014               1    188
2            2015               1    143
3            2014               2    159
4            2015               2    148
5            2014               3    189
6            2015               3    198
7            2014               4    157
8            2015               4    158
9            2014               5    169
10           2015               5    195
11           2014               6    171
12           2015               6    176
13           2014               7    195
14           2015               7    172
15           2014               8    181
16           2015               8    175
17           2014               9    187
18           2015               9    196
19           2014              10    167
20           2015              10    187
21           2014              11    162
22           2015              11    204
23           2014              12    189
24           2015              12    165
like image 194
Isaac Avatar answered Apr 16 '26 11:04

Isaac


Summarising the daily data by month before creating the ts object:

library(tidyverse)

set.seed(1)
df <- data.frame(
  date_rec = seq(as.Date("2014-01-01"), as.Date("2015-12-31"), by = 1),
  number = sample(c(1:10), size = 730, replace = TRUE)
)

df |> 
  mutate(date_rec = round_date(date_rec, unit = "month")) |> 
  summarise(number = sum(number), .by = date_rec) |> 
  ts(frequency = 12, start = c(2014, 1), end = c(2015, 12))
#>          date_rec number
#> Jan 2014    16071     88
#> Feb 2014    16102    196
#> Mar 2014    16130    166
#> Apr 2014    16161    179
#> May 2014    16191    148
#> Jun 2014    16222    160
#> Jul 2014    16252    180
#> Aug 2014    16283    185
#> Sep 2014    16314    155
#> Oct 2014    16344    160
#> Nov 2014    16375    160
#> Dec 2014    16405    164
#> Jan 2015    16436    200
#> Feb 2015    16467    147
#> Mar 2015    16495    157
#> Apr 2015    16526    165
#> May 2015    16556    189
#> Jun 2015    16587    138
#> Jul 2015    16617    150
#> Aug 2015    16648    157
#> Sep 2015    16679    180
#> Oct 2015    16709    192
#> Nov 2015    16740    179
#> Dec 2015    16770    181

Created on 2024-03-18 with reprex v2.1.0

like image 39
Carl Avatar answered Apr 16 '26 10:04

Carl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!