---
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))
```


A model is wanted for a DC-motor, where the model is to be fitted to data from experiments. A set of three real tests on a DC motor is provided. In each test, input-output data are recorded. 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.

You are asked to obtain the DC motor model from the voltage input to the velocity output. The operating point of the input voltage is 2 V. A linear regression with ARMA noise of the motor at this operating point 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"))
```

- 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)
```

- It should be obvious that the output data is subject to noise (especially in the beginning) and a regularly repeating unknown disturbance. You are free to 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

- Use the cross correlation function to detect the delay between the input and output. 
- 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.
- Check whether the model fits the data by considering the residuals (use a time series plot of the residuals as well as correlogram and histogram) - 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.
- When you have a model that fits the data, interpret the estimated parameters and the corresponding confidence intervals.
