3.4 Linear Regression

Example 3.8

National defense spending in years after 2004 (0 means year 2004) in billions of dollars.

NatDefData = {{0,455.8},{1,495.3},{2,521.8},{3,551.3},{4,616.1}};

NatDefDataPlot = ListPlot[NatDefData,  PlotStyle->Red, PlotMarkers->{"✶",16},PlotLegends->{"Defense Data",LegendMarkers->Automatic}]

The plot of the points does appear to be reasonably linear.  So lets do some linear regression!

Part 2

Now we use the LinearModelFit command, with the variable t, and with a constant coefficient (the vertical intercept).

NatDefDataBestFitLine = LinearModelFit[NatDefData,{1,t},t]

The linear function is L(t) = 37.66t+452.74, where the input is measured in years since 2004, and the output in billions of $.

NatDefLinePlot=Plot[NatDefDataBestFitLine[t],{t,-1,6}, PlotStyle->{Blue, Thickness[0.004]},PlotLegends->{"Best Fit Line",LegendMarkers->Automatic}];

Show[NatDefDataPlot,NatDefLinePlot, PlotRange->{{-1,5},{0,700}}, AxesOrigin->{0,0}]

Part 3

The slope m=37.66 is measured in billions of $ per year, and thus we expect the amount of money the US spends on national defense each you to increase by approximately $37.66 billion per year.

Part 4

We need to evaluate our function NatDefDataBestFitLine at t=5 which corresponds to year 2009 (5 years after 2004). The value given below is $641.04 billion, which is well short of the $661.0 billion that was actually spent in 2009.

NatDefDataBestFitLine [5]

Exercise 4

FBData = {{0,9.2},{1,10.0},{2,10.8},{3,11.7},{4,12.6},{5,13.2},{6,13.8}};

Part a.

FBDataPlot = ListPlot[FBData,  PlotStyle->Red, PlotMarkers->{"✶",16},PlotLegends->{"FB users (millions)",LegendMarkers->Automatic}]

Part b.

FBfDataBestFitLine = LinearModelFit[FBData,{1,t},t]

FBLinePlot=Plot[FBfDataBestFitLine[t],{t,-1,7}, PlotStyle->{Blue, Thickness[0.004]},PlotLegends->{"Best Fit Line",LegendMarkers->Automatic}];

Show[FBDataPlot,FBLinePlot, AxesOrigin-> {0,5}, PlotRange->{{-1,7},{5,15}}, AxesLabel->{"t (years since 2012)","millions of users"}]

Part c.

The slope of the line is approximately m=0.78, and the units are in millions of people per year.  So one would expect over this time frame that each year there is an increase in approximately 0.78 million people using FaceBook in Australia.

FBfDataBestFitLine[7]

13.8+0.7857

Exercise 11

We will start our year count at 2010, so the x-coordinate will represent t years after 2010, 0 years being 2010.

TourismData = {{0, 59.74},{1,62.33},{2,66.66},{3,69.77}};

Part a.

TourismDataPlot = ListPlot[TourismData,  PlotStyle->Red, PlotMarkers->{"✶",16},PlotLegends->{"Tourists (millions)",LegendMarkers->Automatic}]

Part b.

TourismDataBestFitLine = LinearModelFit[TourismData,{1,t},t]

TourismLinePlot=Plot[TourismDataBestFitLine[t],{t,-1,4}, PlotStyle->{Blue, Thickness[0.004]},PlotLegends->{"Best Fit Line",LegendMarkers->Automatic}];

Show[TourismDataPlot,TourismLinePlot, PlotRange->{{-1,4},{59,75}}, AxesLabel->{"t (years since 2010)","millions of users"}]

Part c.

The slope is m = 3.442 and its units are in millions of tourists per year.  Thus, over the time in question, we would expect that there would be an increase of about 3.142 million tourists from one year to the next.

Part d.

So the estimate by the line of best fit is calculated below to be 73.23 million tourists in 2004, which is well below the actual number of 74.73 million tourists.

TourismDataBestFitLine[4]