## Maine Dataset

Download all the data from here https://www.springer.com/gp/book/9780387886978 read Maine.dat from ts folder

```
Maine.month <- read.table("ts/Maine.dat",header=TRUE)
head(Maine.month)
```

```
## unemploy
## 1 6.7
## 2 6.7
## 3 6.4
## 4 5.9
## 5 5.2
## 6 4.8
```

So it has one variable “unemploy”

`class(Maine.month);str(Maine.month)`

`## [1] "data.frame"`

```
## 'data.frame': 128 obs. of 1 variable:
## $ unemploy: num 6.7 6.7 6.4 5.9 5.2 4.8 4.8 4 4.2 4.4 ...
```

### Converting a data frame to ts object.

ts function is used to convert a data to a time series object

` Maine.month.ts <- ts(Maine.month$unemploy, start = c(1996, 1), freq = 12)`

Looking at the structure and class

`str(Maine.month.ts)`

`## Time-Series [1:128] from 1996 to 2007: 6.7 6.7 6.4 5.9 5.2 4.8 4.8 4 4.2 4.4 ...`

The following code gives the average employment rate over every month. First we aggregated every year, then divide by 12 to get the average of every month.

```
Maine.annual.ts <- aggregate(Maine.month.ts)/12
Maine.annual.ts
```

```
## Time Series:
## Start = 1996
## End = 2005
## Frequency = 1
## [1] 5.258333 5.125000 4.508333 3.950000 3.275000 3.733333 4.341667
## [8] 4.991667 4.616667 4.841667
```

Lets plot both together

```
layout(1:2)
plot(Maine.month.ts,ylab="Unemployed(%)",main="Maine.Month")
plot(Maine.annual.ts,ylab="unemployed(%)",main="Maine Annual")
```

### Window Function: For getting a sample of the data.

We can calculate the precise percentages in R, using window. This function will extract the part of the time series between specified start and end points. and will sample with an interval equal to frequency if its argument is set to TRUE. So, the below line below gives a time series of February figures.

```
Maine.Feb <- window(Maine.month.ts,start=c(1996,2),freq=TRUE)
Maine.Feb
```

```
## Time Series:
## Start = 1996.083
## End = 2006.083
## Frequency = 1
## [1] 6.7 6.5 5.7 5.0 4.4 4.2 4.9 5.8 5.6 5.8 5.6
```

We can calculate similarly for August

```
Maine.Aug <- window(Maine.month.ts,start=c(1996,8),freq=TRUE)
Maine.Aug
```

```
## Time Series:
## Start = 1996.583
## End = 2006.583
## Frequency = 1
## [1] 4.0 4.0 3.6 3.3 2.5 3.1 3.6 4.3 3.8 4.1 3.9
```

We can see that August Figures are in General Lower than Februrary. so we can calculate how much is Feb more or less as compared to the mean of all the years, similarly for August. This gives a sort of seasonal Index.

```
Feb.ratio <- mean(Maine.Feb)/mean(Maine.month.ts)
Aug.ratio <- mean(Maine.Aug)/mean(Maine.month.ts)
Feb.ratio
```

`## [1] 1.222529`

`Aug.ratio`

`## [1] 0.8163732`

So on an average unemployment in February is 22% more than the average and August is about 18% less than the average.