Time Series Forecasting using R programming

To load data set from the package “fma”, use the commands:

{`>data (package=fma)
This instantly loads the pre existing data sets in the package.
To load the data set regarding US consumption and income, use the command:
>data (usconsumption)
To print the data:
>usconsumption`}
Time series Forecasting and Decomposition using R image 1

Now, form a time series from the data using the command:

{`>ustimeseries <- ts (usconsumption, frequency=4, start=1970)
Here, the variable name is “ustimeseries”, frequency is 4(because it is quarterly data), and start=1 (since the data starts from 1970)

Now, print the series:
>ustimeseries`}
Time series Forecasting and Decomposition using R image 2

Fit a time series regression model by using the function tslm. Use the command:

{`>fit <- tslm (consumption~income, data=usconsumption)

Draw a scatter plot of time series by using the command:
>plot (consumption~income, data=usconsumption)

Fit the line of best fit for time series by the command:
>abline (fit)`}
Time series Forecasting and Decomposition using R image 3

For scenario based forecasting using R, use the command:

{`> f <- forecast (fit, newdata=data.frame(income=c(-1,1)))

To plot the forecasted value, use the following R command to generate the graph of forecasts from linear regression model:
> plot (f)`}
Time series Forecasting and Decomposition using R image 4

Learn R programming for Statistical Analysis

citation generator
citaion generator
make money online