We often model temperature change as an exponential process: the object temperature approaches the ambient temperature (room temperature) exponentially.

Stan Wagon examined the temperature of a cup of water over time. Heated initially to near boiling, it gradually cooled to room temperature.

```
water = fetchData("CoolingWater")
```

```
## Data CoolingWater found in package.
```

```
xyplot(temp ~ time, data = water)
```

You can see that the function is roughly exponential and can estimate the ambient temperature directly from the graph, as well as the initial temperature near 100 C.

To check how close the function is to being exponential, we can find the parameters of an exponential function that are a close match and plot the fitted function along with the data. Note that the guess for the numerical value of the exponential time constant \(k\) is based on an eye-balled half-life of 40 minutes.

```
mod = fitModel(temp ~ A + B * exp(-k * time), data = water, start = list(A = 70,
B = 30, k = log(2)/40))
```

The `start`

list gives some guessed values for the parameters. Having such a guess makes it easier for the computer to find the optimal parameters. Here they are, along with a new plot:

```
mod
```

```
## function (time, ..., transform = identity)
## return(transform(predict(model, newdata = data.frame(time = time),
## ...)))
## <environment: 0x10550ed48>
## attr(,"coefficients")
## A B k
## 27.00446 62.13545 0.02096
```

```
xyplot(temp ~ time, data = water)
plotFun(mod(time) ~ time, add = TRUE, col = "red")
```

```
## function (time)
## mod(time)
```

- What's the ambient temperature according to the model?
- How would describe the quality of the fit of the model?

Plotting the difference between the data and the model can highlight deficiencies in the model:

```
xyplot(I(temp - mod(time)) ~ time, data = water, ylab = "Difference from model")
plotFun(0 ~ time, add = TRUE, col = "red")
```

```
## function (time)
## 0
```

If the model were right on, the points would fall on the zero line. They differ systematically, which suggests that there is a systematic process that isn't being captured with the model.

What about the process of pouring nearly boiling water in a mug might not produce an exactly exponential form?

The failure of the model to capture the rapid temperature change at early times suggests that there is a fast cooling process that lasts for only a little while. Perhaps the water is cooling in two different ways. We can try out a sum of exponentials to model this.

```
mod2 = fitModel(temp ~ A + B * exp(-k * time) + C * exp(-kfast * time), data = water,
start = list(A = 70, B = 30, k = log(2)/40, kfast = log(2)/5))
mod2
```

```
## function (time, ..., transform = identity)
## return(transform(predict(model, newdata = data.frame(time = time),
## ...)))
## <environment: 0x106229d90>
## attr(,"coefficients")
## A B k kfast C
## 23.58737 48.06105 0.01351 0.08165 25.20925
```

```
xyplot(temp ~ time, data = water)
plotFun(mod2(time) ~ time, add = TRUE, col = "red")
```

```
## function (time)
## mod2(time)
```

```
xyplot(I(temp - mod2(time)) ~ time, data = water, ylab = "Difference from model")
plotFun(0 ~ time, add = TRUE, col = "red")
```

```
## function (time)
## 0
```

- What's the ambient temperature according to the sum-of-exponentials model?
- In what ways is the sum-of-exponentials model better? In what ways worse?
- How would you quantify the quality of the fit?
- How would you decide whether the improved fit is worthwhile? Propose a method.
- If you were to add in a third exponential, what would you choose as the half-life?