---
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r , include=FALSE}
library(mosaic) # load the mosaic package for later use
library(jpeg)

```

# Exam exercise for Module 4: Low pass filter

```{r, fig.width=8, echo=FALSE, fig.align='center'}
url <- "https://asta.math.aau.dk/static-files/asta/img/Lab-opstilling.jpg"
z <- tempfile()
download.file(url, z, mode = "wb")
grid::grid.raster(jpeg::readJPEG(z))
invisible(file.remove(z))
```
\begin{center}{\begin{large}{\bf Setup in lab.}\end{large}}\end{center}


### First order low pass filter

```{r, fig.width=4, echo=FALSE, fig.align='center'}
url <- "https://asta.math.aau.dk/static-files/asta/img/RC-circuit.jpg"
z <- tempfile()
download.file(url, z, mode = "wb")
grid::grid.raster(jpeg::readJPEG(z))
invisible(file.remove(z))
```

We shall study data associated with the displayed circuit.
The fundamental characteristics associated with the circuit is:
\newcommand{\mRt}{\mbox{R_true}}
\newcommand{\mCt}{\mbox{C_true}}
\newcommand{\mft}{\mbox{f_c_true}}


- $R$ is the resistance measured in Ohm
- $C$ is the capacitance measured in Farad
- $f_c$ is the 3dB cut-off frequency measured in Hertz


In theory the relation between these quantities is
$$f_c=\frac{1}{2\pi RC}$$

### Data

Peter Koch has spent quite some time in the lab producing data. Peter has measured $R$, $C$ and $f_c$ for different combinations of nominal $R$ and $C$ values. 

Four resistors were used with nominal $R$ values:

- 1, 1.1, 4.75 and 5.9 kOhm

Seven capacitors were used with nominal $C$ values:

- 47, 56, 68, 330, 470, 560 and 680 nF

For each component, the actual $R$ or $C$ value has been measured once. Moreover, for each combination of capacitor and resistor, the frequency has been measured, that is,
in total $4x7=28$ measurements of frequency were made.

First load the data:

```{r }
load(url("https://asta.math.aau.dk/datasets?file=RC_data.RData"))
head(RC_data)
```

On a logarithmic scale the model is:

- `ln(fc_true)=-ln(2*pi)-ln(R_true)-ln(C_true)`

where "true" refers to the exact value of the variable for the components tested.

What we are actually measuring is

- `ln(fc_measured)=ln(fc_true)+fc_error`
- `ln(R_measured)=ln(R_true)+R_error`
- `ln(C_measured)=ln(C_true)+C_error`

1) Show that:

- `log(fc_measured)=log_fc_predict+R_error+C_error+fc_error`

where 

- `log_fc_predict= -log(2*pi)-log(R_measured)-log(C_measured)`

2) Argue that if the meters have no systematic errors, then 
 `(log_fc_predict,log(fc_measured))` should vary around the identity line.

- Calculate and plot these points. You can use the code below to add the identity line to the plot.

```{r}
# gf_point(...) %>% #plot the points
# gf_abline(slope=~1,intercept=~0) #adds the identity line
```

- Argue that the plot calls for a linear calibration of `log(fc_measured)`.

3) Fit a simple linear regression of `log(fc_measured)` on `log_fc_predict`. Argue that

- the intercept is significantly different from zero.
- the slope is significantly different from one.

This shows that the meters must have systematic errors.

4) In the light of your conclusions do a calibration of `log(fc_measured)` and call it `log_fc_corrected`.

- Make a plot of `(log_fc_predict,log_fc_corrected)`

5) The data version of the RC-model is now

- `log_fc_corrected=-log(2*pi)-log(R_measured)-log(C_measured)+error`

Fit a multiple regression of `log_fc_corrected` on `log(R_measured)` and `log(C_measured)` and test the hypotheses

- intercept is equal to  `-log(2*pi)`.
- slope of `log(R_measured)`  is equal to -1.
- slope of  `log(C_measured)` is equal to -1.

6) If the fitted model is called `fit`, then the residuals is obtained by `resid(fit)`. 
Investigate whether the residuals follow a normal distribution  using

  - a `qqnorm` plot of the residuals
  - Geary's test
  - Godness-of-fit test with 5 bins
  - Shapiro-Wilks test

What is your conclusion?
