Randomly generate some daily data for the first 191 days of the year. Get the pure linear trend data, randomly generated errors have a mean of 0 and a standard deviation of 80.

x <- 1:191
data <- 2*x + rnorm(191, 0, 80)
head(data)
## [1]   62.81333 -105.45041   85.48090  -39.86352  -15.23876  -76.38470

Time Series Function ts()

Approximate a daily time series with Base R ts(), which is not designed for a daily or weekly time series because it needs data at regular time intervals and there are not always 365 days in the year.

Specify the start and end dates and the frequency of the time series. Start at Year 1 as ts() does not process actual years. End at the fraction of the year in terms of the number of days for which the time series extends.

ts_d <- ts(data, start=c(1, 191/365), frequency=365)
head(ts_d) # start date approximates 1
## [1]   62.81333 -105.45041   85.48090  -39.86352  -15.23876  -76.38470

Plot the Base R time series with lessR function Plot(). Remove the points as an option by setting the size parameter to 0.

suppressPackageStartupMessages(library(lessR))
Plot(ts_d, size=0, quiet=TRUE)

Time Series Function xts()

A more general approach to a daily time series is the xts() function from the xts package, which can process irregular time intervals, and so does daily (and weekly) data better than ts(). xts() works from actual dates that you provide instead of having the dates generated from a frequency parameter in conjunction with a start and end date.

Create an xts() time series. Generate a daily date sequence of the first 191 days of the year with the Base R function seq().

start_date <- as.Date("2023-01-01")
end_date <- as.Date("2023-01-01") + 190
dates <- seq(from=start_date, to=end_date, by="day")
head(dates)
## [1] "2023-01-01" "2023-01-02" "2023-01-03" "2023-01-04" "2023-01-05"
## [6] "2023-01-06"

Run the install procedure once. Remove the # sign at the beginning to run.

install.packages("xts")

Use xts() in place of ts() to create the time series.

library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
xts_d <- xts(data, order.by=dates)
head(xts_d)
##                  [,1]
## 2023-01-01   62.81333
## 2023-01-02 -105.45041
## 2023-01-03   85.48090
## 2023-01-04  -39.86352
## 2023-01-05  -15.23876
## 2023-01-06  -76.38470
plot(xts_d)

Forecast

To forecast from an xts() time series with the forecast() function, need to convert to a ts() time series. Convert xts() time series to a ts() time series, which is required by forecast().

xts_dt <- as.ts(xts_d)
head(xts_dt)
## [1]   62.81333 -105.45041   85.48090  -39.86352  -15.23876  -76.38470

Do the forecast for the next month, 30 days.

suppressPackageStartupMessages(library(fpp2))
fit <- tslm(xts_dt ~ trend) # no seasonality in the data
f <- forecast(fit, h=30)
f
##     Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 192       384.3807 277.3780 491.3833 220.2553 548.5061
## 193       386.4080 279.3879 493.4281 222.2559 550.5601
## 194       388.4354 281.3977 495.4731 224.2563 552.6145
## 195       390.4628 283.4074 497.5183 226.2565 554.6692
## 196       392.4902 285.4168 499.5636 228.2563 556.7241
## 197       394.5176 287.4261 501.6091 230.2559 558.7793
## 198       396.5450 289.4351 503.6548 232.2552 560.8347
## 199       398.5724 291.4440 505.7007 234.2543 562.8904
## 200       400.5997 293.4528 507.7467 236.2530 564.9464
## 201       402.6271 295.4613 509.7929 238.2515 567.0027
## 202       404.6545 297.4697 511.8393 240.2498 569.0593
## 203       406.6819 299.4779 513.8859 242.2477 571.1161
## 204       408.7093 301.4859 515.9326 244.2454 573.1732
## 205       410.7367 303.4938 517.9796 246.2428 575.2305
## 206       412.7641 305.5014 520.0267 248.2400 577.2881
## 207       414.7914 307.5089 522.0739 250.2368 579.3460
## 208       416.8188 309.5163 524.1214 252.2334 581.4042
## 209       418.8462 311.5234 526.1690 254.2298 583.4626
## 210       420.8736 313.5304 528.2168 256.2258 585.5214
## 211       422.9010 315.5372 530.2648 258.2216 587.5803
## 212       424.9284 317.5438 532.3130 260.2172 589.6396
## 213       426.9558 319.5502 534.3613 262.2124 591.6991
## 214       428.9831 321.5565 536.4098 264.2074 593.7589
## 215       431.0105 323.5626 538.4585 266.2021 595.8189
## 216       433.0379 325.5685 540.5074 268.1966 597.8793
## 217       435.0653 327.5742 542.5564 270.1907 599.9399
## 218       437.0927 329.5798 544.6056 272.1846 602.0007
## 219       439.1201 331.5851 546.6550 274.1783 604.0619
## 220       441.1475 333.5904 548.7046 276.1717 606.1233
## 221       443.1748 335.5954 550.7543 278.1648 608.1849

Plot the data and the forecast.

autoplot(f, xlab="Date", ylab="Data")