Gráfico de la serie de tiempo.
# Veamos el gr?fico de la serie
plot(mytimeseries)

library(TSstudio)
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
ts_plot(mytimeseries, line.mode = "lines+markers") %>%
layout(title = "Serie de tiempo creada con una distribucion Uniforme",yaxis = list(title = " "))
plot.ts(mytimeseries,
main = "US Quarterly Natural Gas Consumption",
ylab = "Billion of Cubic Feet"
)

library(dygraphs)
dygraph(mytimeseries,
main = "US Monthly Total Vehicle Sales",
ylab = "Thousands of Vehicle") %>%
dyRangeSelector()
# Clase
library(forecast)# para tsoutliers
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
class(mytimeseries)
## [1] "ts"
tsoutliers(mytimeseries)
## $index
## integer(0)
##
## $replacements
## numeric(0)
# Tiempos
time(mytimeseries)
## Qtr1 Qtr2 Qtr3 Qtr4
## 1956 1956.00 1956.25 1956.50 1956.75
## 1957 1957.00 1957.25 1957.50 1957.75
## 1958 1958.00 1958.25 1958.50 1958.75
## 1959 1959.00 1959.25 1959.50 1959.75
## 1960 1960.00 1960.25 1960.50 1960.75
## 1961 1961.00 1961.25 1961.50 1961.75
## 1962 1962.00 1962.25 1962.50 1962.75
## 1963 1963.00 1963.25 1963.50 1963.75
## 1964 1964.00 1964.25 1964.50 1964.75
## 1965 1965.00 1965.25 1965.50 1965.75
## 1966 1966.00 1966.25 1966.50 1966.75
## 1967 1967.00 1967.25 1967.50 1967.75
## 1968 1968.00 1968.25
Datos faltantes y outliers en series de tiempo.
### Datos faltantes y Outliers
## Import ts.Rmissing.csv
mydata=read.csv('E:/Rmissing.csv')
head(mydata)
## X mydata
## 1 1 32.80146
## 2 2 42.46548
## 3 3 NA
## 4 4 32.20406
## 5 5 55.55765
## 6 6 33.05086
# Convertir la segunda columna en una serie de tiempo sin especificar frecuencia
myts = ts(mydata$mydata)
myts
## Time Series:
## Start = 1
## End = 250
## Frequency = 1
## [1] 32.801464 42.465485 NA 32.204058 55.557647 33.050864
## [7] 43.401620 37.768318 22.844180 36.428877 28.496485 59.037881
## [13] 36.544163 26.668135 41.325626 28.913199 38.595417 31.341447
## [19] 34.547023 NA 30.499324 49.391323 43.976004 22.162741
## [25] 19.439525 41.892407 30.321857 32.899878 17.686235 10.332791
## [31] 31.612958 40.011275 35.378517 46.167222 26.903207 36.304821
## [37] 23.408770 42.785841 31.919674 37.571226 33.907485 17.698917
## [43] 19.931775 23.971169 999.000000 32.853670 33.012320 47.893249
## [49] 33.961104 40.826518 34.389579 27.210322 41.815827 NA
## [55] 49.711080 37.246486 34.472507 27.554913 37.976930 24.503481
## [61] 33.941547 28.582326 17.945402 40.335543 32.103075 15.609346
## [67] 38.637130 58.877558 42.178769 34.075469 29.208206 20.409934
## [73] 23.682860 49.014566 59.160903 24.994359 37.321672 11.830421
## [79] 49.907975 33.288427 25.900307 34.661099 38.170951 30.246685
## [85] 45.001326 36.082827 38.969588 24.260726 8.619401 33.933167
## [91] 30.158056 32.211135 46.688584 36.399098 27.266510 39.706101
## [97] 48.560701 999.000000 31.011612 33.565184 41.850476 45.780926
## [103] 21.679404 32.340497 55.904896 17.349895 32.994516 36.155426
## [109] 47.089342 33.955275 36.563838 18.773382 28.077605 40.483324
## [115] 41.341771 32.907839 59.604911 20.989279 46.886734 53.931163
## [121] 44.662468 43.125045 25.800244 22.833920 51.397357 34.775922
## [127] 50.922532 36.430258 32.975690 37.659017 48.006323 49.901919
## [133] 20.619643 24.895206 4.682543 17.049461 45.618543 28.288209
## [139] 50.446258 34.983971 38.847283 32.301493 46.044574 21.739473
## [145] 16.457915 36.157602 35.773314 23.368300 34.220736 39.443674
## [151] 26.074044 28.599269 45.410516 NA 30.585004 23.405284
## [157] 36.949438 17.647508 22.991044 40.388899 30.654440 49.261182
## [163] 31.215505 30.462442 41.294423 28.046393 24.925970 23.934094
## [169] 41.690112 46.226476 40.741721 999.000000 26.455048 12.766929
## [175] 38.133315 61.653241 11.474054 41.835335 34.572898 59.921615
## [181] 53.072688 51.889866 43.700888 33.639492 24.988901 27.019376
## [187] 12.774882 21.001551 18.133113 48.563807 64.633075 29.362668
## [193] 45.478245 34.152629 50.573869 43.564419 57.644084 20.785408
## [199] 39.811707 51.854335 33.351763 23.242107 34.351879 28.113792
## [205] 33.952911 35.372668 38.059841 40.818440 45.768335 39.272128
## [211] 999.000000 36.665537 50.282893 34.561040 46.040830 39.936308
## [217] 39.873144 51.126396 2.683472 51.667975 41.336229 43.090450
## [223] 24.686842 52.300908 37.379943 19.043254 43.512121 54.236360
## [229] 33.557160 33.851597 NA 36.149460 32.985037 31.422766
## [235] 36.574851 29.648483 18.290954 38.154075 34.446452 12.037743
## [241] 23.581530 36.968395 50.747174 37.981389 28.693203 32.396009
## [247] 41.325484 30.017571 14.818111 45.403854
Comprobar si hay NAs y outliers.
# Comprobar si hay NAs y outliers
summary(myts)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 2.683 28.078 34.573 50.710 42.465 999.000 5
plot(myts)

Usando zoo para localizar y rellenar valores faltantes.
# Usando zoo para localizar y rellenar valores faltantes
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
myts.NAlocf = na.locf(myts) #LOCF: last observation carried forward (copia la ?ltima observacion antes del NA)
myts.NAlocf
## Time Series:
## Start = 1
## End = 250
## Frequency = 1
## [1] 32.801464 42.465485 42.465485 32.204058 55.557647 33.050864
## [7] 43.401620 37.768318 22.844180 36.428877 28.496485 59.037881
## [13] 36.544163 26.668135 41.325626 28.913199 38.595417 31.341447
## [19] 34.547023 34.547023 30.499324 49.391323 43.976004 22.162741
## [25] 19.439525 41.892407 30.321857 32.899878 17.686235 10.332791
## [31] 31.612958 40.011275 35.378517 46.167222 26.903207 36.304821
## [37] 23.408770 42.785841 31.919674 37.571226 33.907485 17.698917
## [43] 19.931775 23.971169 999.000000 32.853670 33.012320 47.893249
## [49] 33.961104 40.826518 34.389579 27.210322 41.815827 41.815827
## [55] 49.711080 37.246486 34.472507 27.554913 37.976930 24.503481
## [61] 33.941547 28.582326 17.945402 40.335543 32.103075 15.609346
## [67] 38.637130 58.877558 42.178769 34.075469 29.208206 20.409934
## [73] 23.682860 49.014566 59.160903 24.994359 37.321672 11.830421
## [79] 49.907975 33.288427 25.900307 34.661099 38.170951 30.246685
## [85] 45.001326 36.082827 38.969588 24.260726 8.619401 33.933167
## [91] 30.158056 32.211135 46.688584 36.399098 27.266510 39.706101
## [97] 48.560701 999.000000 31.011612 33.565184 41.850476 45.780926
## [103] 21.679404 32.340497 55.904896 17.349895 32.994516 36.155426
## [109] 47.089342 33.955275 36.563838 18.773382 28.077605 40.483324
## [115] 41.341771 32.907839 59.604911 20.989279 46.886734 53.931163
## [121] 44.662468 43.125045 25.800244 22.833920 51.397357 34.775922
## [127] 50.922532 36.430258 32.975690 37.659017 48.006323 49.901919
## [133] 20.619643 24.895206 4.682543 17.049461 45.618543 28.288209
## [139] 50.446258 34.983971 38.847283 32.301493 46.044574 21.739473
## [145] 16.457915 36.157602 35.773314 23.368300 34.220736 39.443674
## [151] 26.074044 28.599269 45.410516 45.410516 30.585004 23.405284
## [157] 36.949438 17.647508 22.991044 40.388899 30.654440 49.261182
## [163] 31.215505 30.462442 41.294423 28.046393 24.925970 23.934094
## [169] 41.690112 46.226476 40.741721 999.000000 26.455048 12.766929
## [175] 38.133315 61.653241 11.474054 41.835335 34.572898 59.921615
## [181] 53.072688 51.889866 43.700888 33.639492 24.988901 27.019376
## [187] 12.774882 21.001551 18.133113 48.563807 64.633075 29.362668
## [193] 45.478245 34.152629 50.573869 43.564419 57.644084 20.785408
## [199] 39.811707 51.854335 33.351763 23.242107 34.351879 28.113792
## [205] 33.952911 35.372668 38.059841 40.818440 45.768335 39.272128
## [211] 999.000000 36.665537 50.282893 34.561040 46.040830 39.936308
## [217] 39.873144 51.126396 2.683472 51.667975 41.336229 43.090450
## [223] 24.686842 52.300908 37.379943 19.043254 43.512121 54.236360
## [229] 33.557160 33.851597 33.851597 36.149460 32.985037 31.422766
## [235] 36.574851 29.648483 18.290954 38.154075 34.446452 12.037743
## [241] 23.581530 36.968395 50.747174 37.981389 28.693203 32.396009
## [247] 41.325484 30.017571 14.818111 45.403854
myts.NAfill = na.fill(myts, 33) #rellena con el valor que le pongamos
head(myts.NAfill)
## Time Series:
## Start = 1
## End = 6
## Frequency = 1
## [1] 32.80146 42.46548 33.00000 32.20406 55.55765 33.05086
Detección automática de outliers con la librería forecast.
library(forecast)
myts1 = tsoutliers(myts)
myts1
## $index
## [1] 45 98 172 211
##
## $replacements
## [1] 28.41242 39.78616 33.59838 37.96883
plot(myts)

# Tambi?n hay un m?todo para NA en el paquete forecast
myts.NAinterp = na.interp(myts) #rellena NA con interpolaci?n
Limpiando NAs y outliers con tsclean del paquete forecast.
# Limpiando NAs y outliers con tsclean del paquete forecast
mytsclean = tsclean(myts)
plot(mytsclean)

summary(mytsclean)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.683 28.157 34.567 35.025 41.830 64.633