Table of Advertising Expenditures, US, in millions of dollars (Crain, et al.). Cited in Mooney and Swift.

Andy Long
Spring, 2020

Years=Table[i,{i,1970,1989}];
Years=Years-1900;
AdExpenditures={19550,20700,23210,24980,26620,27900,33300,37440,43330,48780,53550,60430,66580,75850,87820,94750,102140,109650,118050,123930};
data=Transpose[{Years,AdExpenditures}];
ListPlot[data,Joined->False,PlotLegends->{"Expenditures (Millions)"},PlotLabel->"Ad Expenditures (Crain, et al. -- from Mooney and Swift",AxesLabel->{"Years","Ad Expenditures"}]

lm=LinearModelFit[data,x,x,ConfidenceLevel->.95]
p1=ListPlot[data];
p2=Plot[lm[x],{x,Min[Years],Max[Years]}];
Show[p1,p2,Frame->True]
lm["RSquared"]

Let’s look at an exponential model, obtained by linearization of the problem. We’ll also compare it to the model estimated by non-linear regression.

ty=Log[AdExpenditures];
transdata=Transpose[{Years,ty}];

tm=LinearModelFit[transdata,x,x,ConfidenceLevel->.95]

p1=ListPlot[transdata];
p2=Plot[tm[x],{x,Min[Years],Max[Years]}];
Show[p1,p2,Frame->True]
tm["RSquared"]

{alpha,beta}=tm["BestFitParameters"]
CIs=tm["ParameterConfidenceIntervals"]
{a,b}={Exp[alpha],beta}
Exp[First[CIs]] (* so a is not in the center of the exponentiated CI *)
a

y=AdExpenditures;
ybar=Mean[y];
expModel[x_]:=a Exp[b x]
yhat=expModel[Years];

p1=ListPlot[data];
p2=Plot[expModel[x],{x,Min[Years],Max[Years]}];
Show[p1,p2,Frame->True]
RSquaredTrans=1-(y-yhat).(y-yhat)/((y-ybar).(y-ybar))

pres=ListPlot[Transpose[{Years,y-yhat}]]

Now let’s try a power model:

ty=Log[AdExpenditures];
tx=Log[Years];
loglogdata=Transpose[{tx,ty}];

pm=LinearModelFit[loglogdata,x,x,ConfidenceLevel->.95]

p1=ListPlot[loglogdata];
p2=Plot[pm[x],{x,Min[tx],Max[tx]}];
Show[p1,p2,Frame->True]
pm["RSquared"]

{alpha,beta}=pm["BestFitParameters"]
{a,b}={Exp[alpha],beta}
y=AdExpenditures;
ybar=Mean[y]
powerModel[x_]:=a x^b
yhat=powerModel[Years];

p1=ListPlot[data];
p2=Plot[powerModel[x],{x,Min[Years],Max[Years]}];
Show[p1,p2,Frame->True]
RSquaredPower=1-(y-yhat).(y-yhat)/((y-ybar).(y-ybar))

pres=ListPlot[Transpose[{Years,y-yhat}]]

Here’s one way you might Import the CO2 data: straight off the web.

dat=Import["ftp://aftp.cmdl.noaa.gov/products/trends/co2/co2_mm_mlo.txt","Table"];
DeleteCases[dat,{__String},{-1}];
dat[[96,1]]
(* First data line in the file: *)
Table[dat[[74,i]],{i,1,7}] 
Dimensions[dat]
YearsKeeling=Table[dat[[i,1]],{i,74,815}] ;