### ESD-FYS  -  module 4-1  - exercises



#### Component variation: Capacitor

```{r, fig.width=10, echo=FALSE, fig.align='center', fig.caption="Lot of capacitors with nominal value 220 NF and tolerance 10%"}
url <- "https://asta.math.aau.dk/static-files/asta/img/220nF-10procent.jpg"
z <- tempfile()
download.file(url, z, mode = "wb")
grid::grid.raster(jpeg::readJPEG(z))
invisible(file.remove(z))
```

Picture of a "lot" of capacitors. 

The word lot is used to identify several components produced in a single run. 
A run is a production series limited to a given time interval and fixed production parameters.

Peter Koch has tested 269 of the capacitors in the displayed lot.

* First read in the data:

```{r }
Cap220=read.csv(url("https://asta.math.aau.dk/datasets?file=capacitor_lot_220_nF.txt"))[,1]
```

The capacitors in the lot have a nominal value of 220 nF with a tolerance of 10\%.

* Calculate a variable `ln_error` by the formula
$$\text{ln_error} = \ln(\text{Cap220}/220).$$

* Make a histogram of `ln_error`. Does it look normal?

We would expect the mean `ln_error` to be 0. 

* Do a t-test to evaluate whether the mean value of `ln_error` is significantly different from zero.

The result could be due to the systematic errors of the meter. In Lecture 4.1 we do a linear calibration of `ln_error` to remove these systematic errors. The code below redoes the computation of the parameters for the calibration.

```{r }
load(url("https://asta.math.aau.dk/datasets?file=cap_1pct.RData"))
fit <- lm(log(capacity) ~ log(nomval), data = capDat)
summary(fit)
ab<-coef(fit) # a vector of the form (intercept,slope)
ab
```

* Compute the calibrated values `ln_error_corrected` in the `Cap220` dataset by the formula
$$ \text{ln_error_corrected} = (\text{ln_error} - \hat{\alpha})/\hat{\beta}. $$

* Conclude by a t-test that the mean value of `ln_error_corrected` is still significantly different from zero.

We conclude that the lot has a systematic error which is not due to errors in the meter.

 * Estimate mean and standard deviation of `ln_error_corrected`
and calculate a 3-sigma interval for the lot values.

  * Does it respect the 10% tolerance?

An alternative way to check this is by using the coefficient of variation (CV) and mean of CAP220/220. Note that we have not calibrated the original measurements. Instead we will use that we verified log-normality by the histogram above. 

* Assuming log normality of the measurements, estimate CV and mean of CAP220/220 (see formulas on slide 19 from the lecture). Denote the estimates by `CV` and `M` and calculate
$$M\cdot (1 \pm 3\cdot CV)-1$$
and compare to the 3-sigma interval.

#### Component variation: Resistor


Peter Koch has also tested a lot (250 components) of resistors with nominal value 1 kOhm.

* Read in the data:

```{r }
R1000=read.csv(url("https://asta.math.aau.dk/datasets?file=Ohm.txt"))[,2]
```

* Do a similar analysis, except that we haven't calibrated the meter, so that we can only analyse `ln_error`. (This means that an eventual systematic deviation from zero has two sources: A systematic measurement error and a systematic lot error. )

#### [WMMY] Example 13.7

* Read in the data for the exercise:

```{r }
yield=c(9.7,5.6,8.4,7.9,8.2,7.7,8.1,
  10.4,9.6,7.3,6.8,8.8,9.2,7.6,
  15.9,14.4,8.3,12.8,7.9,11.6,9.8,
  8.6,11.1,10.7,7.6,6.4,5.9,8.1,
  9.7,12.8,8.7,13.4,8.3,11.7,10.7)
batch=factor(rep(1:5,each=7))
data=data.frame(batch=batch,yield=yield)
```

* Redo the analysis of the example in R.
