Stewart’s Keeling example model, section 1.2 (p. 25)

Thoughts about this example:
1. If all you’ve got is a hammer, everything looks like a nail.
2. Don’t fit a banana with a straight line.	

On the other hand, by page 27, Stewart suggests why this model is probably a bad idea....

Andy Long
Spring, 2018

Here’s the data: 
notice that I’ve shifted year, so that it runs from 0 to 28, from 1980. This makes the parameters of the model a little easier to interpret.

years is a list, as is co2; data is the set of ordered pairs:

years={0,2,4,6,8,10,12,14,16,18,20,22,24,26,28}
co2={338.7,341.2,344.4,347.2,351.5,354.2,356.3,358.6,362.4,366.5,369.4,373.2,377.5,381.9,385.6}
data=Table[{years[[i]],co2[[i]]},{i,15}]

The linear fit: for this we use the LinearModelFit command, and we’re looking for a linear model (just “x” in the second place):

lm=LinearModelFit[data,x,x,ConfidenceLevel->.95]
(* we can interpret the parameters: the constant is the estimated value of CO2 in 1980; the slope is the rate of change per year, i.e. about 1.65 parts-per-million (ppm) per year. *)

p1=ListPlot[data];
p2=Plot[lm[x],{x,0,28}];
Show[p1,p2,Frame->True]
(* What does the linear model predict for 2015=1980+35? *)
lm[35]

(* have a look at the residuals, to see how we're doing: *)
ListPlot[lm["FitResiduals"], Filling->Axis]
(* there seems to be a trend to the residuals -- a pattern -- which means that our model isn't capturing everything important. *)

lm["ParameterConfidenceIntervals"] (* how did we do? *)
lm["RSquared"]

Solve[lm[x]==450,x] (* So this model suggests that we'll hit 450 ppm of CO2 in 2048.0576....*)

The quadratic fit (notice that we’re still using LinearModelFit, however: the model is linear in the parameters):

qm=LinearModelFit[data,{x,x^2},x]
Show[ListPlot[data],Plot[qm[x],{x,0,28}],Frame->True]
(* What does the quadratic model predict for 2015? *)
qm[35]

(* Let's have a look at the residuals : *)
ListPlot[qm["FitResiduals"], Filling->Axis]

qm["ParameterConfidenceIntervals"] (* how did we do? *)

Solve[qm[x]==450,x] (* So this model suggests that we'll hit 450 ppm of CO2 in 2034.8818....*)

qm["ParameterConfidenceIntervals"] (* how did we do? *)
qm["RSquared"]