1) Import data file.
lynx.column.txt or lynx.row.txt
2) Convert data.frame to a vector (or a matrix for the case of multivariate TS)
data = as.vector(lynx.column[,1])
data = as.vector(lynx.row[1,])
3)Create TS
Basic functions :
1. rts() - Defines a univariate or multivariate regularly spaced time series
USAGE:
rts(x = NA, start = 1, deltat = 1, frequency = 1, end = <see below>, units = NULL, names = NULL, eps = .Options$ts.eps)
EXAMPLES:
lynx.rts = rts(x = data, start = c(1990, 2), frequency=12) #deltat = 1/12 = 1 month
x <- rts(rnorm(100), start = c(1953, 4), frequency = 12)
is.rts(x)
lynx.rts <- as.rts(lynx)
corn.rts <- rts(cbind(corn.rain, corn.yield), start = 1890,
units = "years", names = c("rain", "yield"))
2. its() - Constructs a univariate or multivariate time series with arbitrary sampling time intervals.
USAGE:
its(x, times, units = NULL, names = NULL)
is.its(x)
EXAMPLES:
whitenoise <- matrix(rnorm(50), ncol = 2)
obs.times <- sort(runif(25, 0, 10)) # make up some observation times
x <- its(whitenoise, times = obs.times, names = c("error1","error2"))
is.its(x)
birthdays <- dates(c("01/02/78", "10/21/81", "06/23/86",
"01/19/90", "06/29/92"))
ages <- c(15, 11, 7, 3, 1)
its(ages, times = birthdays)
2. cts() - Defines a regular univariate or multivariate time series object with calendar dates associated with the observations
USAGE:
cts(x, start = dates("01/01/60"), units = "years",
k.units = 1, frequency = 1, names = NULL)
is.cts(x)
4. timeSeries() - Construct a timeSeries object from positions and data, or return an empty timeSeries
USAGE:
timeSeries()
timeSeries(data, positions., units., from=timeCalendar(d=1,m=1,y=1960),
by="days", k.by=1, align.by=F, week.align=NULL)
EXAMPLES:
timeSeries()
timeSeries(data= data.frame(x = 11:20, y = 21:30), from="1/1/98" )
5. signalSeries() - Construct a signalSeries object from positions and data, or return an empty signalSeries object.
USAGE:
signalSeries()
signalSeries(data, positions., units, units.position, from=1, by=1)
EXAMPLES:
signalSeries(data = data.frame(x = 11:20), from = 1, by = 1)
4) Plotting TS :
plot.timeSeries()
USAGE:
plot.timeSeries(x, ..., main=x@title, ylab=x@units,
top.ticks=F, right.ticks=F, reference.grid=T,
plot.type="lines",
merge.args=list(pos="union", how="interp"),
x.axis.args=list(), y.axis.args=list(),
plot.args=list(), log.axes="c", complex.convert=Mod,
frame=sys.nframe())
EXAMPLES:
djia1 <-djia[positions(djia)>=timeDate("09/01/87") &
positions(djia)<=timeDate("11/01/87"), 1:4]
plot.timeSeries(djia1, plot.type="hloc")
plot.timeSeries(djia1, plot.type="lines")
or just
plot(djia1, type=”l”) # may take timeSeries object (TS or SS)
5) Analysing TS :
1. diff() - Returns a time series or other object which is the result of differencing the input.
USAGE:
diff(x, lag=1, differences=1)
2. acf() - Estimates and displays autocovariance, autocorrelation or partial autocorrelation functions.
USAGE:
acf(x, lag.max=<see below>, type="correlation", plot=T)
EXAMPLES:
acf.lynx <- acf(lynx, 36, "correlation")
corn.rts <- rts(cbind(corn.rain, corn.yield), start=1890,
units="years", names=c("rain", "yield") )