# R - Time Series Analysis

**time-series object**. It is also a R data object like a vector or data frame.

The time series object is created by using the

**ts()**function.

## Syntax

The basic syntax for**ts()**function in time series analysis is −

timeseries.object.name <- ts(data, start, end, frequency)Following is the description of the parameters used −

**data**is a vector or matrix containing the values used in the time series.**start**specifies the start time for the first observation in time series.**end**specifies the end time for the last observation in time series.**frequency**specifies the number of observations per unit time.

## Example

Consider the annual rainfall details at a place starting from January 2012. We create an R time series object for a period of 12 months and plot it.# Get the data points in form of a R vector. rainfall <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071) # Convert it to a time series object. rainfall.timeseries <- ts(rainfall,start = c(2012,1),frequency = 12) # Print the timeseries data. print(rainfall.timeseries) # Give the chart file a name. png(file = "rainfall.png") # Plot a graph of the time series. plot(rainfall.timeseries) # Save the file. dev.off()When we execute the above code, it produces the following result and chart −

Jan Feb Mar Apr May Jun Jul Aug Sep 2012 799.0 1174.8 865.1 1334.6 635.4 918.5 685.5 998.6 784.2 Oct Nov Dec 2012 985.0 882.8 1071.0The Time series chart −

## Different Time Intervals

The value of the**frequency**parameter in the ts() function decides the time intervals at which the data points are measured. A value of 12 indicates that the time series is for 12 months. Other values and its meaning is as below −

**frequency = 12**pegs the data points for every month of a year.**frequency = 4**pegs the data points for every quarter of a year.**frequency = 6**pegs the data points for every 10 minutes of an hour.**frequency = 24*6**pegs the data points for every 10 minutes of a day.

## Multiple Time Series

We can plot multiple time series in one chart by combining both the series into a matrix.# Get the data points in form of a R vector. rainfall1 <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071) rainfall2 <- c(655,1306.9,1323.4,1172.2,562.2,824,822.4,1265.5,799.6,1105.6,1106.7,1337.8) # Convert them to a matrix. combined.rainfall <- matrix(c(rainfall1,rainfall2),nrow = 12) # Convert it to a time series object. rainfall.timeseries <- ts(combined.rainfall,start = c(2012,1),frequency = 12) # Print the timeseries data. print(rainfall.timeseries) # Give the chart file a name. png(file = "rainfall_combined.png") # Plot a graph of the time series. plot(rainfall.timeseries, main = "Multiple Time Series") # Save the file. dev.off()When we execute the above code, it produces the following result and chart −

Series 1 Series 2 Jan 2012 799.0 655.0 Feb 2012 1174.8 1306.9 Mar 2012 865.1 1323.4 Apr 2012 1334.6 1172.2 May 2012 635.4 562.2 Jun 2012 918.5 824.0 Jul 2012 685.5 822.4 Aug 2012 998.6 1265.5 Sep 2012 784.2 799.6 Oct 2012 985.0 1105.6 Nov 2012 882.8 1106.7 Dec 2012 1071.0 1337.8The Multiple Time series chart −

*Table of contents:*1. R - Overview

2. R - Environment Setup

3. R - Basic Syntax

4. R - Data Types

5. R - Variables

6. R - Operators

7. R - Decision Making

8. R - Loops

9. R - Functions

10. R - Strings

11. R - Vectors

12. R - Matrices

13. R - Arrays

14. R - Factors

15. R - Data Frames

16. R - Packages

17. R - Data Reshaping

18. R - CSV Files

19. R - Excel Files

20. R - Binary Files

21. R - XML Files

22. R - JSON Files

23. R - Web Data

24. R - Database

25. R - Pie Charts

26. R - Bar Charts

27. R - Boxplots

28. R - Histograms

29. R - Line Graphs

30. R - Scatterplots

31. R - Mean, Median and Mode

32. R - Linear Regression

33. R - Multiple Regression

34. R - Logistic Regression

35. R - Normal Distribution

36. R - Binomial Distribution

37. R - Poisson Regression

38. R - Analysis of Covariance

39. R - Time Series Analysis

40. R - Nonlinear Least Square

41. R - Decision Tree

42. R - Random Forest

43. R - Survival Analysis

44. R - Chi Square Tests

## No comments:

## Post a Comment