---
output:
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = TRUE)
```

# Exam exercise for Module 4: A model for a DC-motor

```{r, fig.width=3, echo=FALSE, fig.align='center'}
# was ![](https://asta.math.aau.dk/static-files/asta/img/dcmotor.png)
url <- "https://asta.math.aau.dk/static-files/asta/img/dcmotor.png"
z <- tempfile()
download.file(url, z, mode = "wb")
grid::grid.raster(png::readPNG(z))
invisible(file.remove(z))
```

```{r, fig.width=10, echo=FALSE, fig.align='center'}
# was ![](https://asta.math.aau.dk/static-files/asta/img/dcmotor.png)
#url <- "https://asta.math.aau.dk/static-files/asta/img/dcmotor.png"
#z <- tempfile()
#download.file(url, z, mode = "wb")
#grid::grid.raster(png::readPNG(z))
#invisible(file.remove(z))
```


The purpose of this exam question is to provide a model for how the velocity of a DC-motor depends on the input voltage. For this, data was collected from an experiment. Input-output data are recorded in the data set below. The first column is the input voltage (V) and the second column is the motor velocity (rad/s). The sampling time is 0.01 s. A linear regression with ARMA noise should be obtained.

## Viewing and cleaning data

- Load the data into R:
```{r}
dat = as.ts(read.table("https://asta.math.aau.dk/datasets?file=dcmotor.txt"),
            colnames=c("Voltage","Velocity"))
```

1. Start by plotting the data (both input and output). You may want to zoom in on a part to get a better view.
```{r}
ts.plot(dat,col=1:2); legend("topright",legend=c("Voltage","Velocity"),col=1:2,lty=1)
ts.plot(dat[500:1000,],col=1:2); legend("topright",legend=c("Voltage","Velocity"),col=1:2,lty=1)
```

2. It should be obvious that the output data is subject to noise (especially in the beginning) and a regularly repeating unknown disturbance. Choose a part of the data that sounds reasonable for obtaining the model. For example, here is a part of the data with the first 500 measurements removed, and all measurements outside the interval [3,7] removed.

```{r}
dat1 = dat[500:6001,]  # removing start
dat1[dat1[,2]<3|dat1[,2]>7,2]=NA # exchanging velocity outliers outside interval [3,7] with NA
ts.plot(dat1[1:500,],col=1:2); legend("topright",legend=c("Voltage","Velocity"),col=1:2,lty=1)
```

## A linear regression model with ARMA noise

3. Use the cross-correlation function to detect the delay between the input and output. 
  - Explain the cross-correlation function.

4. Fit a linear regression model with ARMA(p,q) noise for the output using the input as exogenous variable (include the delay using the lag() function in R). You may choose the orders p and q used in the ARMA model. You should use multiple models with different orders and then compare them using AIC, picking the preferred model (if you get a warning message, try using the option optim.control=list(maxit = 1000)).

5. Check whether the chosen model fits the data by considering the residuals.
  - Use a time series plot of the residuals as well as a correlogram (NA values can be handled in the acf by using the option na.action=na.pass).
  - Explain what you expect to see if your model is correct. 
  - If the model does not fit well you may consider changing the order of the ARMA noise considered to see if you can get a model that fits better.

6. When you have a model that fits the data, write out the model and interpret the estimated parameters.

7. Make a confidence interval for the slope of the regression.