---
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 (NA values can be handled in the ccf as well as the acf by using the option na.action=na.pass). 
  - Explain the cross-correlation function.
  
4.  Fit a linear regression model with AR(1) noise for the output using the input as exogenous variable (adjust for the delay using the lag() function in R). What is the autocorrelation function for the noise process in this model? 

5. Check whether the AR(1) model fits the data by considering the residuals.
  - Use a time series plot of the residuals as well as a correlogram.
  - Explain what you expect to see if your model is correct. 

6. Fit a linear regression model with ARMA(p,q) noise for different choises of p and q. Compare the models using AIC and pick the preferred model (if you get a warning message, try using the option optim.control=list(maxit = 1000)).

7. Check whether the chosen model fits the data by considering the correlogram for the residuals.
  - If the model does not fit well you may consider returning to the previous step and increase the order of the ARMA(p,q) model up to p <= 3 and q <=3 to see if you can get a model that fits better.

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

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

10. Suppose the next value of the voltage is 2.23. Predict the next observation of velocity and make a confidence interval for the prediction.
