I have a time series which represents the amount of a certain product sold throughout the year 2018. I am trying to decompose the time series but I get the following error Error in decompose(myzoo) : time series has no or less than 2 periods. This is my code in R
## requiere packages
library(forecast)
library(sweep)
library(timetk)
library(zoo)
## Read the Data
Data <- read.delim("R Project/Dataset/MyData.txt")
## Create a daily Date object
inds <- seq(as.Date("2018-01-01"), as.Date("2018-12-31"), by = "day")
## Create a time series object
myzoo <- zoo(Data, inds)
## print myzoo
print(myzoo)
## plot myzoo
plot(myzoo)
plot(myzoo, xaxt = "n")
Axis(inds, side = 1, at = seq(inds[1], tail(inds, 1) + 60, by = "1 months"), format = "%b %Y")
## Decompose myzoo
composition = decompose(myzoo)
stl(myzoo)
## use auto.arima to choose ARIMA terms
fit <- auto.arima(myzoo)
## forecast for next 60 time points
fore <- forecast(fit, h = 15, level=c(99.5))
## plot it with no x-axis
plot(fore, xaxt = "n")
Axis(inds, side = 1, at = seq(inds[1], tail(inds, 1) + 60, by = "1 months"), format = "%b %Y")
And this is my data (MyData.txt):
X
0
2621
3407
3644
3569
1212
0
0
4473
3885
3671
3641
1453
0
4182
3812
3650
3444
3557
1612
0
4004
3631
3342
3203
3424
1597
0
4280
3644
3642
3696
3793
1753
0
4416
3935
3522
3544
3569
1649
0
3871
3442
3144
3158
3693
1780
0
4322
3682
3499
3279
3485
1716
0
4255
3713
3470
3673
3983
1931
0
4771
3986
3833
3501
3620
1710
0
4407
3799
3654
3332
3693
1780
0
0
4574
4016
3748
3559
1625
0
4548
3726
2780
0
0
122
0
5005
4300
3772
3929
3917
2021
0
4820
4117
3668
3664
3639
1742
0
4473
4151
3844
3499
3736
1838
0
4346
3693
3297
3327
3639
1773
0
4519
0
4352
4079
4143
1970
0
4693
4018
3679
3838
3606
1601
0
0
4289
4011
3742
3710
1781
0
4186
3707
3600
3484
3702
1747
0
4195
3838
3504
3609
3934
1943
0
0
5243
4754
4164
4121
1854
0
0
5173
4518
3875
3889
1904
0
5105
4056
4186
4079
3953
1846
0
4543
4341
4013
2998
4048
1767
0
0
4317
5260
5185
4969
2046
0
5683
5004
4567
4542
4266
2065
0
4357
5281
4830
4510
0
1567
0
5818
4906
4518
4218
4275
2074
0
5005
4645
4543
4558
4574
2129
0
4755
0
4458
3845
3746
1689
0
4285
3476
3447
2959
3470
1584
0
0
4159
3881
3533
3360
1643
0
4152
3748
3329
3112
3303
1790
0
3852
4190
3482
3313
3400
1582
0
4042
3706
3451
3137
3178
1518
0
4077
3754
3429
3369
3307
1467
0
3918
3620
3442
3302
3168
1630
0
3967
3707
3397
3294
3314
1646
0
4196
3812
3478
3111
3113
1411
0
0
3717
3501
3282
3366
1554
0
3737
3428
3028
2960
2977
1513
0
3608
3306
2941
2918
3238
1543
0
0
3959
3678
3367
3237
1024
0
0
4057
3562
3344
3367
1602
0
3784
3581
3395
2948
3009
1446
0
3676
3276
3112
3125
3133
1502
0
4200
4027
3739
3531
3222
2
0
4446
4342
4066
3811
2932
1643
0
4587
4534
4146
3994
3350
1400
0
1248
0
4248
4629
4346
1844
0
168
The zeros represent sales on holidays and Sundays. The purpose of this script is to be able to make forecast.
Thanks in advance.
Can't help you with the software . Perhaps contact the author. You data is better suited to deterministic effects rather than arima memory effects. There are strong monthly effects and even stronger daily effects. along with a host of pulses probably reflecting holiday or promotion effects that are currently omitted from the model.
The Actual/Fit and Forecast should give you motivation to pursue this approach. with statistical summary here
Related
I have a time series which represents the amount of a certain product sold throughout the year 2018 (from 2018/01/01 to 2018/12/31); is it correct to think of a frequency of 7 observations per cycle? and if so, what is my cycle? one week? I try to understand this in order to decompose my time series avoiding the error Error in decompose(tsData) : time series has no or less than 2 periods. This is my R script and my data.
library(forecast)
library(sweep)
library(timetk)
Data <- read.delim("R Project/Dataset/MyData.txt")
DataFrame <- data.frame(Data,
Date = seq(as.Date("2018-01-01"), as.Date("2018-12-31"),
by = "day"))
inds <- seq(as.Date("2018-01-01"), as.Date("2018-12-31"), by = "day")
tsData <- ts(Data, start = c(2018, as.numeric(format(inds[1], "%j"))),
frequency = 365)
print(tsData)
plot(tsData)
Axis(inds, side = 1, at = seq(inds[1], tail(inds, 1) + 60,
by = "1 months"), format = "%b %Y")
comp = decompose(tsData)
#comp = stl(tsData)
plot(comp)
fit <- auto.arima(tsData)
fore <- forecast(fit, h = 15, level = 99.5)
plot(fore, xaxt = "n")
Axis(inds, side = 1, at = seq(inds[1], tail(inds, 1) + 60, by = "1 months"),
format = "%b %Y")
This is MyData.txt file
Daily Data
0
2621
3407
3644
3569
1212
0
0
4473
3885
3671
3641
1453
0
4182
3812
3650
3444
3557
1612
0
4004
3631
3342
3203
3424
1597
0
4280
3644
3642
3696
3793
1753
0
4416
3935
3522
3544
3569
1649
0
3871
3442
3144
3158
3693
1780
0
4322
3682
3499
3279
3485
1716
0
4255
3713
3470
3673
3983
1931
0
4771
3986
3833
3501
3620
1710
0
4407
3799
3654
3332
3693
1780
0
0
4574
4016
3748
3559
1625
0
4548
3726
2780
0
0
122
0
5005
4300
3772
3929
3917
2021
0
4820
4117
3668
3664
3639
1742
0
4473
4151
3844
3499
3736
1838
0
4346
3693
3297
3327
3639
1773
0
4519
0
4352
4079
4143
1970
0
4693
4018
3679
3838
3606
1601
0
0
4289
4011
3742
3710
1781
0
4186
3707
3600
3484
3702
1747
0
4195
3838
3504
3609
3934
1943
0
0
5243
4754
4164
4121
1854
0
0
5173
4518
3875
3889
1904
0
5105
4056
4186
4079
3953
1846
0
4543
4341
4013
2998
4048
1767
0
0
4317
5260
5185
4969
2046
0
5683
5004
4567
4542
4266
2065
0
4357
5281
4830
4510
0
1567
0
5818
4906
4518
4218
4275
2074
0
5005
4645
4543
4558
4574
2129
0
4755
0
4458
3845
3746
1689
0
4285
3476
3447
2959
3470
1584
0
0
4159
3881
3533
3360
1643
0
4152
3748
3329
3112
3303
1790
0
3852
4190
3482
3313
3400
1582
0
4042
3706
3451
3137
3178
1518
0
4077
3754
3429
3369
3307
1467
0
3918
3620
3442
3302
3168
1630
0
3967
3707
3397
3294
3314
1646
0
4196
3812
3478
3111
3113
1411
0
0
3717
3501
3282
3366
1554
0
3737
3428
3028
2960
2977
1513
0
3608
3306
2941
2918
3238
1543
0
0
3959
3678
3367
3237
1024
0
0
4057
3562
3344
3367
1602
0
3784
3581
3395
2948
3009
1446
0
3676
3276
3112
3125
3133
1502
0
4200
4027
3739
3531
3222
2
0
4446
4342
4066
3811
2932
1643
0
4587
4534
4146
3994
3350
1400
0
1248
0
4248
4629
4346
1844
0
168
The frequency = parameter in ts() function indicates the number of observations before pattern repetition. If you set a seasonality of 365 (1 year) with 1 year of data it will have only 1 period and so decompose() tells you: time series has no or less than 2 periods.
As you said "7 observations per cycle", you may want to set frequency equal to 7. Or if you want to analyze year seasonality put more data in tsData.
Just change:
# ....
tsData <- ts(Data, start = c(2018, as.numeric(format(inds[1], "%j"))), frequency = 365)
# ...
to :
# ...
### weekly seasonality
tsData <- ts(Data, start = c(2018, as.numeric(format(inds[1], "%j"))), frequency = 7)
#...
and now decompose works:
comp = decompose(tsData) # NO ERROR
### get the plot
plot(comp)
# ... rest of your code ...
here the plot:
EDIT on your comment:
The X-axis on the plot depends on how you declare the start, please have a look at ts documentation.
If you want to have the 2018 year value you can simply use (see documentation) autoplot() :
# ... rest of code ...
autoplot(tsData)
# ... rest of code ...
that is also highly customizable, if you want to know how to customize the plot (made through ggplot2 package) just have a look at documentation and all the posts on this blog etc.
I have a daily data of sales with zero values (by holidays and sundays) and I want to apply boxCox.lambda() function, but clearly with the zero values this is impossible. Mi options actually are:
1 - Change the zero values by values approaching zero, but I do not know how this can affect my forecast.
Any suggestions I will be grateful.
This is my data:
Data
0
2621
3407
3644
3569
1212
0
0
4473
3885
3671
3641
1453
0
4182
3812
3650
3444
3557
1612
0
4004
3631
3342
3203
3424
1597
0
4280
3644
3642
3696
3793
1753
0
4416
3935
3522
3544
3569
1649
0
3871
3442
3144
3158
3693
1780
0
4322
3682
3499
3279
3485
1716
0
4255
3713
3470
3673
3983
1931
0
4771
3986
3833
3501
3620
1710
0
4407
3799
3654
3332
3693
1780
0
0
4574
4016
3748
3559
1625
0
4548
3726
2780
0
0
122
0
5005
4300
3772
3929
3917
2021
0
4820
4117
3668
3664
3639
1742
0
4473
4151
3844
3499
3736
1838
0
4346
3693
3297
3327
3639
1773
0
4519
0
4352
4079
4143
1970
0
4693
4018
3679
3838
3606
1601
0
0
4289
4011
3742
3710
1781
0
4186
3707
3600
3484
3702
1747
0
4195
3838
3504
3609
3934
1943
0
0
5243
4754
4164
4121
1854
0
0
5173
4518
3875
3889
1904
0
5105
4056
4186
4079
3953
1846
0
4543
4341
4013
2998
4048
1767
0
0
4317
5260
5185
4969
2046
0
5683
5004
4567
4542
4266
2065
0
4357
5281
4830
4510
0
1567
0
5818
4906
4518
4218
4275
2074
0
5005
4645
4543
4558
4574
2129
0
4755
0
4458
3845
3746
1689
0
4285
3476
3447
2959
3470
1584
0
0
4159
3881
3533
3360
1643
0
4152
3748
3329
3112
3303
1790
0
3852
4190
3482
3313
3400
1582
0
4042
3706
3451
3137
3178
1518
0
4077
3754
3429
3369
3307
1467
0
3918
3620
3442
3302
3168
1630
0
3967
3707
3397
3294
3314
1646
0
4196
3812
3478
3111
3113
1411
0
0
3717
3501
3282
3366
1554
0
3737
3428
3028
2960
2977
1513
0
3608
3306
2941
2918
3238
1543
0
0
3959
3678
3367
3237
1024
0
0
4057
3562
3344
3367
1602
0
3784
3581
3395
2948
3009
1446
0
3676
3276
3112
3125
3133
1502
0
4200
4027
3739
3531
3222
2
0
4446
4342
4066
3811
2932
1643
0
4587
4534
4146
3994
3350
1400
0
1248
0
4248
4629
4346
1844
0
168
I'd recommend you just drop all the Sundays from your data. As we know they will alway s be zero there is no point in spending time and effort on forecasting them.
The periodicity is very strong even with them removed, and diagnosing the data by looking at acf plots etc. is much more straight forward.
# Removing every Sunday and creating a ts object of appropriate frequency
x6 <- x[seq_along(x) %% 7 != 0]
x6.ts <- ts(x6, frequency=6)
# Plenty of periodic structure left
par(mfcol=c(2, 1))
sp <- split(x6.ts, (seq_along(x6.ts)-1) %% 6 + 1)
stripchart(sp, vertical=TRUE, col=rainbow(6, alpha=0.2, start=0.97), pch=16,
method="jitter", group.names=c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat"))
plot.default(x6.ts, type="p", pch=16, col=rainbow(6, alpha=0.6, start=0.97))
The we could f.ex apply a SARIMA model
acf(x6.ts, adj=c(0.5))
title("x6.ts", cex.main=0.9)
acf(diff(x6.ts, lag=6))
title("diff(x6.ts, lag=6)", cex.main=0.9)
I see a seasonal random walk there, and once we take the seasonal difference we see that there's at least a couple of seasonal autoregressive components, and maybe a non-seasonal autoregression.
aa6.1 <- arima(x6.ts, order=c(0, 0, 0), seasonal=c(1, 1, 0))
aa6.2 <- arima(x6.ts, order=c(0, 0, 0), seasonal=c(2, 1, 0))
aa6.3 <- arima(x6.ts, order=c(1, 0, 0), seasonal=c(2, 1, 0))
aa6.4 <- arima(x6.ts, order=c(1, 0, 0), seasonal=c(3, 1, 0))
dummy11 <- model.matrix(~ as.factor(seq_along(x6.ts) %% 11))[,2]
aa6.5 <- arima(x6.ts, order=c(1, 0, 0), seasonal=c(3, 1, 0),
xreg=dummy11)
AIC(aa6.1, aa6.2, aa6.3, aa6.4, aa6.5)
# df AIC
# aa6.1 2 5244.846
# aa6.2 3 5195.019
# aa6.3 4 5192.212
# aa6.4 5 5179.310
# aa6.5 6 5164.567
acfr <- function(x){
a <- acf(residuals(x), plot=FALSE)
a$acf[1, 1, 1] <- 0
plot(a, main="", frame.plot=FALSE, ylim=c(-0.2, 0.2))
mod <- paste(paste(names(x$call),
as.character(x$call), sep="=")[-1], collapse=", ")
text(-0.1, 0.19, pos=4, xpd=NA,
paste0("AIC: ", round(x$aic), "\n", "Mod: ", mod))
}
par(mfcol=c(5, 1))
k <- lapply(list(aa6.1, aa6.2, aa6.3, aa6.4, aa6.5), acfr)
Seems like (1 0 0) (3 1 0)[6] does a decent job, but there's a persistent autocorrelation at lag 11. This is an artefact of the removal of Sundays, but we can address it by including an external regressor of dummys.
I have a dataset collected from a spectrometer; the number of spectra can be between 5 and a few hundred. I added a reference spectra to the dataset and plotted the whole lot. To distinguish between the spectra and the ref, I would like to be able to change the color and line type of the ref.
I already tried with scale_linetype_manual and scale_color_manual but did not succeed.
Wavelength EGG5 EGG6 EGG7 EGG8 EGG9 EGG10 Mean
337 516.87 3362 3400 3577 2727 3321 3627 3273.874
338 517.15 3389 3496 3567 2727 3368 3698 3285.288
339 517.44 3370 3479 3590 2728 3410 3711 3291.471
340 517.72 3363 3541 3584 2744 3403 3732 3308.022
341 518.01 3332 3528 3617 2780 3403 3722 3318.277
342 518.29 3350 3547 3610 2779 3413 3748 3321.633
343 518.58 3334 3489 3628 2759 3454 3772 3323.353
344 518.87 3371 3462 3630 2742 3499 3774 3341.572
345 519.15 3368 3494 3666 2761 3494 3747 3357.921
346 519.44 3407 3524 3639 2799 3470 3765 3364.273
347 519.72 3426 3536 3657 2791 3455 3792 3368.371
348 520.01 3449 3512 3640 2771 3462 3812 3380.655
349 520.29 3433 3494 3685 2761 3490 3791 3395.201
350 520.58 3478 3519 3673 2773 3475 3800 3406.921
351 520.86 3481 3518 3717 2798 3482 3802 3413.669
352 521.15 3497 3551 3694 2809 3468 3797 3417.065
353 521.43 3444 3551 3745 2832 3486 3785 3416.489
354 521.72 3447 3579 3710 2814 3516 3820 3422.496
355 522.00 3428 3575 3738 2787 3481 3843 3422.108
356 522.29 3475 3566 3725 2800 3518 3897 3429.982
357 522.57 3472 3577 3726 2832 3491 3894 3427.777
358 522.86 3512 3601 3714 2866 3552 3882 3444.234
359 523.14 3492 3630 3731 2846 3529 3835 3444.288
360 523.43 3515 3609 3744 2856 3567 3821 3455.061
361 523.71 3523 3621 3753 2816 3595 3845 3459.338
362 524.00 3522 3612 3720 2800 3555 3829 3457.784
363 524.28 3527 3641 3731 2791 3552 3861 3466.151
364 524.57 3511 3647 3742 2859 3530 3880 3469.151
365 524.85 3508 3656 3760 2876 3586 3917 3483.457
366 525.14 3526 3629 3745 2858 3609 3896 3475.342
367 525.42 3533 3598 3763 2845 3621 3907 3484.658
368 525.71 3600 3616 3774 2829 3623 3909 3485.237
369 525.99 3587 3633 3815 2861 3603 3933 3496.604
370 526.28 3598 3677 3795 2873 3593 3901 3495.921
371 526.56 3583 3676 3763 2914 3589 3910 3495.032
372 526.85 3586 3672 3719 2921 3588 3887 3491.266
373 527.13 3573 3653 3719 2929 3590 3875 3485.712
374 527.42 3537 3652 3734 2927 3582 3878 3489.273
375 527.70 3567 3681 3761 2938 3607 3880 3495.421
376 527.99 3587 3700 3751 2931 3632 3924 3506.284
377 528.27 3607 3705 3749 2928 3620 3890 3508.827
378 528.56 3588 3708 3736 2899 3631 3890 3504.655
379 528.84 3552 3685 3742 2882 3602 3867 3495.597
380 529.13 3554 3666 3716 2897 3614 3902 3499.396
381 529.41 3513 3618 3742 2877 3571 3934 3498.964
382 529.70 3561 3619 3753 2884 3598 3949 3504.813
383 529.98 3582 3622 3816 2844 3618 3919 3506.935
384 530.26 3625 3667 3805 2868 3613 3897 3510.910
385 530.55 3603 3659 3839 2894 3618 3917 3507.428
386 530.83 3593 3691 3830 2920 3607 3954 3501.018
387 531.12 3613 3672 3833 2909 3631 3966 3503.317
388 531.40 3631 3675 3815 2878 3581 3925 3500.795
389 531.69 3627 3615 3771 2890 3569 3909 3503.964
390 531.97 3614 3633 3765 2865 3590 3893 3501.849
391 532.26 3596 3650 3775 2868 3664 3923 3512.464
392 532.54 3615 3700 3797 2852 3673 3943 3519.813
393 532.82 3584 3704 3781 2873 3591 3952 3517.381
394 533.11 3593 3723 3781 2878 3543 3931 3511.986
395 533.39 3631 3742 3776 2885 3561 3929 3516.270
396 533.68 3642 3739 3823 2900 3608 3935 3523.061
397 533.96 3663 3696 3810 2893 3639 3974 3534.691
398 534.25 3590 3678 3814 2886 3649 3968 3533.288
399 534.53 3584 3677 3821 2866 3657 3988 3535.514
400 534.81 3587 3711 3795 2872 3624 3964 3531.615
401 535.10 3575 3681 3827 2904 3639 3993 3539.518
402 535.38 3554 3683 3763 2928 3634 3959 3549.532
403 535.67 3531 3673 3778 2957 3646 3951 3553.817
404 535.95 3577 3687 3761 2921 3660 3936 3543.496
405 536.24 3630 3662 3828 2940 3679 3966 3550.511
406 536.52 3664 3670 3854 2875 3689 3983 3557.536
407 536.80 3664 3709 3858 2874 3655 3998 3577.245
408 537.09 3644 3778 3835 2893 3680 4017 3580.881
409 537.37 3656 3796 3837 2949 3695 4044 3593.831
410 537.66 3659 3799 3860 2979 3708 4052 3589.629
411 537.94 3679 3786 3892 2956 3693 4042 3587.568
412 538.22 3635 3778 3906 2915 3670 4027 3588.734
413 538.51 3671 3793 3909 2925 3686 4062 3608.561
414 538.79 3700 3791 3861 2920 3687 4121 3621.248
415 539.08 3731 3848 3862 2997 3722 4120 3637.788
416 539.36 3723 3849 3875 3033 3742 4106 3647.777
417 539.64 3739 3868 3942 3075 3769 4072 3657.374
418 539.93 3735 3861 3952 3044 3767 4116 3663.442
419 540.21 3776 3890 4021 3057 3765 4101 3675.629
420 540.50 3765 3910 4030 3033 3785 4102 3691.608
421 540.78 3831 3931 4078 3054 3836 4103 3709.950
422 541.06 3827 3953 4098 3016 3843 4130 3725.504
423 541.35 3871 3969 4112 3025 3872 4163 3744.403
424 541.63 3878 3966 4132 3055 3856 4181 3756.741
425 541.91 3864 3966 4089 3076 3920 4213 3766.216
426 542.20 3842 3999 4107 3098 3935 4246 3780.737
427 542.48 3866 4038 4107 3066 3950 4254 3788.410
428 542.76 3930 4043 4120 3085 3958 4285 3812.446
429 543.05 3970 4069 4137 3096 3935 4272 3838.629
430 543.33 3968 4103 4153 3130 3948 4341 3870.597
431 543.62 3971 4145 4196 3174 3966 4334 3883.356
432 543.90 3980 4171 4238 3192 4048 4384 3901.036
433 544.18 4016 4156 4282 3206 4105 4393 3918.155
434 544.47 4054 4195 4315 3200 4124 4458 3945.201
435 544.75 4123 4221 4347 3241 4131 4490 3976.065
436 545.03 4176 4279 4362 3229 4129 4490 3999.345
437 545.32 4196 4294 4368 3247 4121 4476 4019.050
438 545.60 4197 4336 4376 3242 4138 4525 4036.065
439 545.88 4205 4379 4394 3294 4190 4553 4080.453
440 546.17 4250 4383 4471 3291 4259 4621 4112.651
441 546.45 4324 4413 4489 3328 4306 4622 4146.709
442 546.73 4355 4437 4542 3381 4369 4690 4163.784
443 547.02 4385 4542 4562 3447 4397 4711 4202.871
444 547.30 4441 4560 4605 3453 4484 4790 4231.701
445 547.58 4493 4630 4594 3435 4496 4788 4264.464
446 547.87 4515 4671 4592 3412 4532 4827 4284.018
447 548.15 4518 4726 4666 3461 4496 4808 4310.137
448 548.43 4590 4765 4762 3520 4554 4856 4343.259
449 548.72 4610 4805 4823 3571 4604 4889 4375.237
450 549.00 4653 4848 4850 3564 4670 4916 4403.590
451 549.28 4626 4839 4848 3553 4663 4962 4432.097
452 549.57 4713 4836 4867 3639 4691 5003 4470.795
453 549.85 4768 4859 4896 3689 4714 5068 4510.371
454 550.13 4828 4895 4951 3728 4716 5126 4545.076
455 550.41 4810 4993 4972 3643 4730 5148 4572.414
456 550.70 4836 5075 5021 3698 4762 5176 4607.784
457 550.98 4883 5174 5043 3698 4844 5188 4641.068
458 551.26 4944 5204 5156 3806 4877 5272 4686.183
459 551.55 4994 5241 5183 3795 4912 5349 4717.932
460 551.83 5029 5247 5224 3889 4961 5370 4750.104
461 552.11 5112 5277 5189 3868 5019 5379 4782.029
462 552.40 5134 5343 5196 3906 5050 5415 4823.004
463 552.68 5185 5426 5247 3912 5103 5462 4863.273
464 552.96 5212 5459 5318 3986 5142 5488 4900.011
465 553.24 5284 5462 5384 4000 5240 5544 4945.446
466 553.53 5339 5466 5449 3985 5262 5594 4976.561
467 553.81 5391 5554 5513 3986 5318 5612 5019.626
468 554.09 5415 5640 5570 4006 5319 5612 5045.396
469 554.38 5448 5721 5614 4064 5380 5674 5098.770
470 554.66 5544 5765 5653 4051 5437 5751 5138.752
471 554.94 5585 5793 5634 4089 5494 5821 5176.392
472 555.22 5624 5808 5667 4112 5563 5827 5202.475
473 555.51 5622 5837 5693 4185 5622 5892 5236.281
474 555.79 5648 5845 5760 4214 5672 5918 5265.612
475 556.07 5709 5953 5783 4286 5668 5985 5316.511
476 556.35 5757 6016 5849 4313 5704 5955 5359.302
477 556.64 5797 6109 5927 4322 5776 5991 5412.820
478 556.92 5848 6135 5989 4292 5832 6037 5443.241
479 557.20 5856 6199 6049 4292 5852 6167 5479.432
480 557.48 5949 6274 6125 4354 5838 6226 5515.306
481 557.77 5991 6303 6165 4419 5896 6261 5553.723
482 558.05 6085 6362 6209 4445 5955 6272 5592.842
483 558.33 6091 6392 6258 4475 5999 6315 5629.468
484 558.61 6158 6483 6267 4465 6062 6388 5669.486
485 558.90 6205 6511 6315 4541 6146 6430 5713.281
486 559.18 6244 6546 6314 4565 6237 6515 5755.496
487 559.46 6304 6549 6393 4619 6261 6565 5793.651
488 559.74 6357 6602 6351 4598 6276 6610 5810.910
489 560.03 6423 6661 6353 4623 6281 6660 5840.727
490 560.31 6467 6769 6401 4636 6325 6656 5883.446
491 560.59 6446 6782 6536 4720 6361 6675 5938.773
492 560.87 6508 6876 6651 4722 6408 6648 5980.385
493 561.16 6552 6881 6669 4736 6441 6703 6014.791
494 561.44 6672 6943 6663 4734 6442 6769 6043.910
495 561.72 6757 6985 6649 4743 6507 6820 6085.784
496 562.00 6801 7076 6689 4786 6544 6868 6136.007
497 562.28 6838 7177 6721 4837 6661 6932 6185.385
498 562.57 6862 7240 6782 4935 6727 7017 6229.784
499 562.85 6922 7301 6894 5022 6785 7087 6265.989
500 563.13 7003 7353 6959 5079 6823 7082 6320.410
501 563.41 7064 7414 7001 5081 6841 7169 6374.471
502 563.69 7120 7451 7003 5072 6921 7197 6425.284
503 563.98 7157 7513 7069 5065 6989 7331 6466.151
504 564.26 7241 7588 7096 5074 7043 7369 6511.083
505 564.54 7294 7653 7181 5089 7088 7459 6567.119
506 564.82 7353 7735 7219 5127 7095 7479 6609.558
507 565.10 7430 7764 7311 5179 7175 7515 6649.820
508 565.39 7493 7842 7364 5259 7265 7579 6711.403
509 565.67 7606 7949 7443 5277 7345 7672 6786.032
510 565.95 7662 8057 7549 5332 7442 7773 6872.349
511 566.23 7732 8162 7618 5332 7519 7855 6916.7
AEDDataMelt <- data.frame( melt(AEDPlotData, id.vars = 'Wavelength', variable = 'series'))
Q <- ggplot()+
geom_line(data = subset(AEDDataMelt, series!="Mean"),aes(x=Wavelength, y=value, col=series))+
scale_color_manual(values = brewer.pal(n=(ncol(AEDPlotData)-1),name = "Dark2" ))+
geom_line(data = subset(AEDDataMelt, series=="Mean"),aes(x = Wavelength, y=value,col= series, linetype = series))+
scale_linetype_manual(values = "dash")+
ggplotly(Q)
At this point I get a dashed line but did not succeed in changing the color of the mean reference line. Also the legend changed in format ffrom EGG5 to (EGG5,1).
I don't know about the ggplotly bit, but manually setting a specific colour and linetype for only one of your lines is quite straightforward. Your code seems to be somewhat overcomplicated for your task and you could simply achieve what you stated by manually adding those scales:
ggplot(AEDDataMelt) +
geom_line(aes(Wavelength, value, colour = series, linetype = series)) +
# First six lines are of one type, last line should be different
scale_linetype_manual(values = c(1,1,1,1,1,1,2)) +
# First six lines are from the brewer pallette, last one a custom colour
scale_colour_manual(values = c(brewer.pal(6, "Dark2"), "black"))
Which gave me this plot:
With the help of teunbrand,
I came to the following solution:
AEDDataMelt <- data.frame( melt(AEDPlotData, id.vars = 'Wavelength', variable =
'series'))
MyLineType <- rep.int(1,(length(AEDPlotData[1, 1:(ncol(AEDPlotData)-2)])))
MyLineType <- append(MyLineType,2,after = length(MyLineType))
Q <- ggplot(AEDDataMelt) +
geom_line(aes(Wavelength, value, colour = series, linetype = series)) +
# First n lines are of one type, last line is reference and should be different
scale_linetype_manual(values = MyLineType)+
# First six lines are from the brewer pallette, last one a custom colour
scale_colour_manual(values = c(brewer.pal((length(MyLineType)-1), "Dark2"),
"black"))
ggplotly(Q)
I have the following time series data. Series1 and Series2 are to be compared and to check for any relation between the two series.
My requirements are.
1) How to check whether there is any relation between the series, such that one series is impacting the other after certain days? How can it be established ?
series1 shows similarity to series2 after 40-50 days. but I got a correlation -0.3345712
serdata <- read.csv("Timeseries.csv")
library("graphics")
plot.ts(serdata)
cor(serdata$Series1,serdata$Series2)
2) What method can be used to analyse such data ?
Moving average can be applied or ARIMA to smooth the curve and to check for a fit ? (I am new to time series analysis. Please suggest any other method)
The data is
date Series1 Series2
9/27/2016 5431 4451
9/28/2016 5468 4889
9/29/2016 5160 5002
9/30/2016 5267 5452
10/1/2016 5097 6223
10/2/2016 4749 6593
10/3/2016 5396 4574
10/4/2016 6001 4285
10/5/2016 6266 5323
10/6/2016 6344 4689
10/7/2016 5992 5499
10/8/2016 5147 5852
10/9/2016 4712 4987
10/10/2016 5328 2680
10/11/2016 6171 3128
10/12/2016 6235 2189
10/13/2016 7286 2532
10/14/2016 7230 3296
10/15/2016 7027 5019
10/16/2016 6063 4222
10/17/2016 5579 2482
10/18/2016 7155 2742
10/19/2016 6938 2611
10/20/2016 6805 2248
10/21/2016 6643 3463
10/22/2016 5620 5030
10/23/2016 6260 6164
10/24/2016 5504 4192
10/25/2016 4035 2879
10/26/2016 4054 2333
10/27/2016 6922 2710
10/28/2016 6848 3568
10/29/2016 5598 5415
10/30/2016 5069 5974
10/31/2016 5537 2314
11/1/2016 6264 2334
11/2/2016 7109 3379
11/3/2016 7411 2846
11/4/2016 7314 3183
11/5/2016 6095 4865
11/6/2016 5279 4948
11/7/2016 4295 3159
11/8/2016 4638 2724
11/9/2016 3536 2866
11/10/2016 3600 3600
11/11/2016 2995 5198
11/12/2016 2432 5192
11/13/2016 2516 4482
11/14/2016 2576 0
11/15/2016 3739 3
11/16/2016 3860 3284
11/17/2016 3587 2938
11/18/2016 3155 3710
11/19/2016 3446 4293
11/20/2016 2682 4239
11/21/2016 3198 4086
11/22/2016 3299 3054
11/23/2016 3134 3194
11/24/2016 2819 3033
11/25/2016 2324 3927
11/26/2016 2093 4824
11/27/2016 2493 4685
11/28/2016 3155 3072
11/29/2016 3510 3139
11/30/2016 3517 4363
12/1/2016 3315 2780
12/2/2016 3640 4075
12/3/2016 3186 5207
12/4/2016 2445 5327
12/5/2016 2812 4223
12/6/2016 3321 3179
12/7/2016 3163 3329
12/8/2016 3325 3747
12/9/2016 3007 3534
12/10/2016 2492 4673
12/11/2016 2364 5205
12/12/2016 2986 3600
12/13/2016 3313 4541
12/14/2016 3425 3823
12/15/2016 4385 3770
12/16/2016 3736 4370
12/17/2016 2336 4478
12/18/2016 2211 5396
12/19/2016 2322 5140
12/20/2016 2342 4089
12/21/2016 2262 4231
12/22/2016 2043 5657
12/23/2016 1723 6215
12/24/2016 1408 4211
12/25/2016 1463 0
12/26/2016 2248 5503
12/27/2016 3424 9483
12/28/2016 2925 6956
12/29/2016 2029 8992
12/30/2016 1950 6062
12/31/2016 1838 6326
1/1/2017 1964 7463
1/2/2017 2232 8426
1/3/2017 2480 8084
1/4/2017 2606 7026
1/5/2017 2606 6295
1/6/2017 2693 7179
1/7/2017 2458 5745
1/8/2017 2362 5690
1/9/2017 2767 5761
1/10/2017 2141 6332
1/11/2017 2355 6240
1/12/2017 3000 6710
1/13/2017 2921 5698
1/14/2017 2558 6156
1/15/2017 2407 7415
1/16/2017 2613 5742
1/17/2017 3005 5779
1/18/2017 3128 5784
1/19/2017 2961 5331
1/20/2017 2582 5476
1/21/2017 2191 7710
1/22/2017 2214 7187
1/23/2017 2649 7676
1/24/2017 3065 4742
1/25/2017 3216 5153
1/26/2017 3548 4817
1/27/2017 4316 5976
1/28/2017 4355 6145
1/29/2017 4848 5764
1/30/2017 4376 5305
1/31/2017 3808 4760
2/1/2017 4172 4752
2/2/2017 8098 4527
2/3/2017 7891 5206
2/4/2017 3484 6209
2/5/2017 3625 5729
2/6/2017 4219 7056
2/7/2017 4282 4955
2/8/2017 3982 4185
2/9/2017 3680 4090
2/10/2017 3314 3881
2/11/2017 2985 5280
2/12/2017 3266 6471
2/13/2017 3665 5840
2/14/2017 3892 4530
2/15/2017 3953 3993
2/16/2017 3855 4453
2/17/2017 3511 5570
2/18/2017 3222 7479
2/19/2017 3284 5349
2/20/2017 3615 4098
2/21/2017 3915 5032
2/22/2017 3994 4256
2/23/2017 3765 6215
2/24/2017 3494 4480
2/25/2017 3257 5995
2/26/2017 3399 6412
2/27/2017 3797 5450
2/28/2017 4076 3935
I am trying this example with my data set but it gives me very strange results: Example of Time Series Prediction using Neural Networks in R
Do you have any idea why it is like this?
This is my source code:
require(quantmod)
require(nnet)
require(caret)
series = read.csv("data.csv")
model <- train(y ~ x1+x2 , series, method='nnet', linout=TRUE, trace = FALSE)
series["o"] <- predict(model, series)
plot.ts(series)
write.csv(series, paste(format(Sys.time(), "%Y%m%d%I%p"), "csv", sep = "."))
This is my data set:
3938
1317
4021
10477
9379
7707
9507
4194
2681
3522
5599
5641
6737
7781
2044
1501
6586
4915
5918
6132
9394
2113
935
9729
5236
8815
3169
5888
5722
191
9539
3384
6006
7139
7285
136
1843
5094
3795
5985
5566
3545
965
14
3738
4645
8439
6390
13842
7754
11440
7572
4876
3206
5577
2734
1169
20
5049
6612
2685
7000
6711
4091
26
5383
5516
7185
6118
4484
2178
754
8104
8209
6159
11137
8994
5172
425
8082
5337
5712
7157
6385
3343
4196
5957
8581
3686
0
254
1819
1071
876
3509
2777
1474
4945
3971
21
5466
5509
1316
5653
2775
797
22
5601
6177
5662
5132
6543
1700
4361
6951
7734
3451
5385
6358
6838
19
6460
5813
6839
6335
2105
8
6
9530
1250
5668
5595
6008
2315
1712
8553
5570
5979
4818
6745
5250
43
5727
7416
5888
6270
4931
0
31
6190
11164
5768
7307
5412
2716
35
8391
6054
2796
5081
6646
4597
1978
7570
5909
9581
3571
6740
1702
1080
6719
963
6781
7544
7708
1993
597
2394
5516
12966
723
6528
2476
86
5956
5820
6995
6682
2460
2479
56
7095
7255
6310
9971
3725
5400
452
6018
5803
6673
6098
9476
692
20
7855
11970
10557
5696
7765
3847
47
6020
6037
5684
7089
6372
970
861
3590
7672
3730
10689
9428
1514
2062
6154
5234
6160
5134
879
1079
9164
6338
6687
8195
6351
1123
4216
3759
9372
7782
3143
4773
6993
849
906
6385
7512
8824
8150
12464
7726
8745
13594
6589
6524
2784
0
1785
688
7998
6797
8289
10815
10280
4839
3928
10935
4588
5785
6771
7628
2908
11391
6637
5585
7454
5828
8259
6644
2436
7055
7206
7873
7368
6239
3595
3166
1846
2301
21
1600
2390
1894
1469
9097
8401
2034
3244
8811
2979
20
7808
7698
11031
4556
7149
3745
5563
9673
8149
12158
7043
6273
1855
80
10729
5880
9327
6343
7227
3522
1244
6382
7186
4964
6162
7435
10524
2449
7437
11970
6661
6122
7323
6707
25
2270
5117
6676
5317
7032
7689
4891
8051
5699
4927
11553
6418
2968
11338
7662
9976
5526
14341
4331
10026
1672
5199
4699
7774
7958
7720
2499
10745
19609
15896
5705
6207
7699
2543
32
3642
6307
7491
6236
8644
2121
1448
7838
5434
5945
6074
6962
5441
42
7424
5818
8877
5743
7980
3140
3046
8329
8186
5994
2931
7309
862
145
8141
6252
9536
6213
7150
2718
1687
5000
6068
5918
10652
12257
1505
2421
10518
2368
7341
8137
7997
3437
2009
5468
3947
5836
8567
11039
3726
746
3417
8649
8016
7652
8298
1306
4031
5525
6203
11847
7688
10911
1080
1001
12315
6084
6529
4074
8526
3161
2184
7400
4916
4521
1523
398
1364
925
38
2580
1039
6556
2040
1166
825
7672
7177
6104
7928
6240
1420
1214
10638
10726
2323
6113
8112
2757
3761
6982
5680
7793
8983
8546
1335
817
6136
3778
6639
6548
6120
3648
584
9099
6434
8828
9988
6066
2575
2237
5114
5879
4094
9309
8008
1614
4307
5801
8006
6344
4803
10904
1339
411
8468
6945
5471
8828
4157
1134
1071
5542
2213
5633
9245
2145
4901
39
10430
7941
6189
7985
8296
614
894
6236
1704
4257
7707
8388
1050
855
9352
4801
7088
8466
470
2433
1036
392
2169
84
5316
8339
4272
2617
1840
7254
5999
6178
4563
3370
756
2773
6610
8967
6182
7452
2570
1443
6537
5338
9158
3870
12036
3574
864
10135
5595
8643
2287
9918
2484