Modeling with Exponential Functions II
Looking ahead
Objectives
- Use a semi-log plot to decide whether an exponential model fits a dataset.
- Given data exhibiting an exponential relationship, approximate the parameters by hand.
- Determine an appropriate horizontal shift to use.
- In R, use
fitModel()to obtain the best-fit exponential model. - Given a semi-log fit , use algebra to write an equivalent model or .
- Evaluate an exponential model at a given input and interpret the answer.
1. Semi-Log Test for an Exponential Model
Objective. Use a semi-log plot to decide whether an exponential model fits a dataset.
The semi-log plot of an exponential model with is a line (Lesson 5 sheet, sections 4 and 5). With a horizontal shift , the line takes the form below.
Theorem 1 (Shifted exponential model and its semi-log line).
The exponential model , with and horizontal shift , has the semi-log line
To convert a semi-log line with back to the exponential model, set
Semi-log test.
- Make the standard plot:
plotPoints(y ~ x). - Check that every output is positive, then make the semi-log plot:
plotPoints(log(y) ~ x). Data with an output of zero or less have no semi-log plot. - If the semi-log plot is close to a nonconstant line and straighter than the standard plot, then an exponential model is reasonable.
Judgment call. When both plots are close to a line and neither is straighter, fit the model as in section 4 and check whether its curve follows the data, with the points scattered on both sides of it.
Nonconstant semi-log line. The semi-log test asks for a nonconstant line. A flat semi-log line has slope . The model is then the constant model , which neither grows nor decays.
Example 1
The Mortgage30YrAnnual dataset gives the average 30-year mortgage rate Rate, in percent, for each Year from 1981 to 2012. Determine whether an exponential model is reasonable for Rate as a function of Year.
ShowHide solution
Apply the semi-log test. Plot the data, then plot the logged rates against Year:
plotPoints(Rate ~ Year, data = Mortgage30YrAnnual)
plotPoints(log(Rate) ~ Year, data = Mortgage30YrAnnual)
R’s log() is the natural logarithm, and the title log(Rate) ~ Year means the points . The 1981 rate, 16.63 percent, plots at height , the orange point. The standard plot drops steeply through the 1980s and flattens after 2000. The semi-log plot falls along a nearly straight line. An exponential model is reasonable, because the semi-log plot is close to a nonconstant line and straighter than the standard plot.
Example 2
The APCalculus dataset gives the number of Advanced Placement Calculus exams taken, Exams, for each Year from 1955 to 2015. Determine whether an exponential model is reasonable for Exams as a function of Year.
ShowHide solution
Plot the data, then plot the logged exam counts against Year:
plotPoints(Exams ~ Year, data = APCalculus)
plotPoints(log(Exams) ~ Year, data = APCalculus)
The semi-log plot climbs steeply through the late 1950s and then bends into a much slower rise. An exponential model is not reasonable, because the semi-log plot bends instead of following one line.
Next: 2. Parameters by HandLesson map
2. Parameters by Hand
Objective. Given data exhibiting an exponential relationship, approximate the parameters by hand.
A line through the first and last points of the semi-log plot approximates the line that the semi-log points lie close to. As in the textbook, the data are taken in order of increasing input. The first data point has the smallest input, and the last data point has the largest.
Fitting an exponential model by hand.
- Set to the smallest input in the data.
With the first data point and the last, compute the slope from the logs of the outputs and the inputs as they are:
- Set , the value of the semi-log line at , the first data point’s input.
- Convert and with the shifted exponential model theorem, and write .
Example 3
A sealed drum of radioactive material gives off radiation. An instrument at the drum’s surface gives a reading rad, in millirem per hour (mrem/hr), that falls with year, the years since sealing.
year | 0 | 2 | 3.5 | 6 | 8 |
rad (mrem/hr) | 950 | 695 | 538 | 361 | 262 |
Approximate an exponential model for rad as a function of year by hand, from the first and last data points.
ShowHide solution
Because the smallest input is 0, the shift is and drops out of the model. Substitute the first and last data points, and , into the formulas for the slope and the intercept:
The slope is the slope of the dashed line through the first and last semi-log points, and , the filled points in the figure. The intercept is the height of that line at . Convert with the shifted exponential model theorem, and :
The model is
The model gives 950 mrem/hr at sealing, with per year.
Example 4
Approximate an exponential model for Rate as a function of Year in Mortgage30YrAnnual by hand, from the first and last data points, and . Plot the model over the data.
ShowHide solution
As in Example 3, with the shift , the smallest input, the slope and the intercept are
The conversion gives
and the model is
In R, store the model at full precision and draw it over the data. The line for m is the slope formula above, and exp(b) is :
m = (log(3.66) - log(16.63))/(2012 - 1981)
b = log(16.63)
f = makeFun(exp(b)*exp(m*(Year - 1981)) ~ Year)
plotPoints(Rate ~ Year, data = Mortgage30YrAnnual)
plotFun(f(Year) ~ Year, add = TRUE, col = "red")
The red curve passes through the first and last data points, and , the orange points, because the hand fit is built from those two points alone. The 30 rates in between fall on both sides of the curve: 14 lie above it and 16 below it.
Next: 3. Horizontal ShiftLesson map
3. Horizontal Shift
Objective. Determine an appropriate horizontal shift to use.
Shift from the smallest input. The horizontal shift of (Lesson 5 sheet, section 2) is set to the smallest input in the data. It comes from the data and is never estimated.
With this shift, is the model’s value at the first data point’s input, because equals at . Without the shift, the constant in front of the exponential is the model’s value at . That input can lie far outside the data.
Unshifted mortgage model. Writing Example 4’s semi-log line without the shift moves the intercept to Year 0:
With , the slope Example 4 stores in R at full precision (typing m prints [1] -0.04883049), and , the new intercept is
and the model is
Both forms describe the same model when and keep full precision. The rounded slope gives about 11.9 at 1981 in place of 16.63. Compute outputs of the unshifted form from the stored and .
The unshifted constant is the model’s rate for Year 0, 1981 years before the data begin, and has no meaning for mortgage rates. The shifted form is used because its constant, , is a rate inside the data: the rate in 1981, the first year recorded.
Example 5
The table gives six rows of the NetherlandsPopulation dataset: the population of the Netherlands, in millions, every 20 years from 1850 to 1950.
Year | 1850 | 1870 | 1890 | 1910 | 1930 | 1950 |
Population | 3.0840 | 3.5950 | 4.5150 | 5.8582 | 7.8250 | 10.0268 |
Determine the horizontal shift for an exponential model of Population as a function of Year fitted to these six rows.
ShowHide solution
The shift is the smallest value of the input Year, , and the model has the form
With this shift, is the model’s population in 1850, the first year in the data.
Example 6
The table gives air pressure, in inches of mercury, at altitudes in thousands of feet.
| Altitude | 0 | 5 | 10 | 15 | 20 | 25 | 30 |
| Pressure | 29.92 | 24.90 | 20.58 | 16.89 | 13.75 | 11.10 | 8.89 |
Determine the horizontal shift for an exponential model of Pressure as a function of Altitude, and state what means for that model.
ShowHide solution
The shift is the smallest value of the input Altitude, , which drops out of the model:
The constant is the model’s pressure in inches of mercury at sea level, altitude 0. That altitude is the first one in the data.
Next: 4. Best-Fit Exponential Model in RLesson map
4. Best-Fit Exponential Model in R
Objective. In R, use fitModel() to obtain the best-fit exponential model.
For an exponential model, the command fitModel() (Lesson 4 sheet, section 3) fits the shifted semi-log line to every semi-log point. The method of section 2 uses only the first and last.
fitModel() for an exponential model.
bfit = fitModel(log(y) ~ m*(x - h) + b, data = D)
coef(bfit)
In this command, y, x, and D are the output, the input, and the dataset. Type the shift in as a number. R estimates and , and coef() prints them. For data typed in with c(), as in Example 8, leave off data = D.
The fitted model function is the R function that fitModel() returns. Given an input , it returns the fitted model’s output , including when the formula is fitted to log(y). In the command above, the fitted model function is bfit.
[2]
Example 7
Find the best-fit exponential model for Rate as a function of Year in Mortgage30YrAnnual, and plot it over the data.
ShowHide solution
The shift is , the smallest Year. Fit the semi-log line in the shifted form and print its slope and intercept:
bfit = fitModel(log(Rate) ~ m*(Year - 1981) + b, data = Mortgage30YrAnnual)
coef(bfit)
The last line prints
m b
-0.03739385 2.64752113
R labels each estimate with its name in the formula: m is the slope and b the intercept of the semi-log line. Convert with and :
and the model is
Plot the fitted model function over the data:
=1.5em plotPoints(Rate ~ Year,
data = Mortgage30YrAnnual)
plotFun(bfit(Year) ~ Year,
add = TRUE, col = "red")
The red curve starts at in 1981, the shift . The curve follows the fall of the data: 15 of the 32 recorded rates lie above it and 17 below it, and the largest gap is 2.511 percentage points, in 1981.
Example 8
The table gives six rows of the NetherlandsPopulation dataset: the population of the Netherlands, in millions, every 20 years from 1850 to 1950.
Year | 1850 | 1870 | 1890 | 1910 | 1930 | 1950 |
Population | 3.0840 | 3.5950 | 4.5150 | 5.8582 | 7.8250 | 10.0268 |
Type the six rows into R as vectors, and find the best-fit exponential model for Population as a function of Year.
ShowHide solution
The shift is , the smallest Year. Type in the data and fit as in Example 7:
Year = c(1850, 1870, 1890, 1910, 1930, 1950)
Population = c(3.0840, 3.5950, 4.5150, 5.8582, 7.8250, 10.0268)
pfit = fitModel(log(Population) ~ m*(Year - 1850) + b)
coef(pfit)
The last line prints
m b
0.01212707 1.06758071
Then
and the model is
Fitted model over a semi-log plot. Draw the log of the model’s output over a semi-log plot:
bfit = fitModel(log(Rate) ~ m*(Year - 1981) + b, data = Mortgage30YrAnnual)
plotPoints(log(Rate) ~ Year, data = Mortgage30YrAnnual)
plotFun(log(bfit(Year)) ~ Year, add = TRUE, col = "red")
Without the log, plotFun(bfit(Year) ~ Year, add = TRUE) draws nothing and prints no error, because the curve lies entirely above the window of the semi-log plot. The model’s rates run from 14.119 down to about 4.4, and the vertical axis of the semi-log plot stops near 2.8.
Errors in the shifted formula. When the * between m and the parentheses is missing, R reports a function m that does not exist:
> bfit = fitModel(log(Rate) ~ m(Year - 1981) + b, data = Mortgage30YrAnnual) Error in m(Year - 1981) : could not find function "m"
A fitModel() command typed without the parentheses, as m*Year - 1981 + b, runs with no error and fits an unshifted line: coef() prints in place of 2.648.
Next: 5. Equivalent Exponential ModelsLesson map
5. Equivalent Exponential Models
Objective. Given a semi-log fit , use algebra to write an equivalent model or .
Exponentiating both sides of the semi-log line undoes the log, because :
Naming and gives .
Unshifted form. Splitting the exponent once more removes the shift:
The unshifted constant is .
Example 9
A dataset has the semi-log line
Write the equivalent exponential model.
ShowHide solution
The conversions and give
The model is
Example 10
Write the semi-log line of the exponential model
ShowHide solution
The conversions and give
The semi-log line is
Example 11
Write the best-fit mortgage model of Example 7,
in the unshifted form .
ShowHide solution
The unshifted constant is with . R computes it from coef(bfit) at full precision:
bfit = fitModel(log(Rate) ~ m*(Year - 1981) + b, data = Mortgage30YrAnnual)
cf = coef(bfit)
exp(cf["b"])*exp(-cf["m"]*1981)
The last line prints 2.094724e+33, under the label b. The model is
Outputs come from the fitted model function bfit, which keeps and at full precision. At 1981, this rounded form gives about 30.811 in place of 14.119,
Early rounding. Rounding to first gives
That constant is less than half the full-precision value. The rounding error in is multiplied by 1981 inside the exponent.
Next: 6. Predictions and InterpretationLesson map
6. Predictions and Interpretation
Objective. Evaluate an exponential model at a given input and interpret the answer.
To evaluate the model at full precision, give the input to the fitted model function: bfit(2015) returns the model’s output for Year 2015. Each prediction is an interpolation or an extrapolation (Lesson 4 sheet, section 3).
Prediction and recorded value. The model’s output is a prediction. The dataset records the value that occurred. State the model’s output as a prediction, with units, and keep it separate from the recorded value.
Example 12
Use the best-fit exponential model for Mortgage30YrAnnual to predict the average 30-year mortgage rate in 2015, in mid-1993, and in 1884. Interpret each answer.
ShowHide solution
Evaluate the fitted model function at each year:
bfit = fitModel(log(Rate) ~ m*(Year - 1981) + b, data = Mortgage30YrAnnual)
bfit(2015)
bfit(1993.5)
bfit(1884)
The last three lines print [1] 3.95955, [1] 8.847174, and [1] 530.9799.
- The model predicts an average rate of 3.960 percent in 2015. The prediction is an extrapolation, because the data stop at 2012.
- The model predicts an average rate of 8.847 percent in mid-1993. The prediction is an interpolation, because 1993.5 lies inside the data’s years, 1981 to 2012.
- The model predicts an average rate of 530.980 percent in 1884. The prediction is an extrapolation, because 1884 lies 97 years before the data begin. The model gives no usable rate that far outside the data, because 530.980 percent is more than 30 times the largest recorded rate, 16.63 percent.
Example 13
Compare the best-fit mortgage model’s output for 2000 with the rate recorded in Mortgage30YrAnnual for 2000.
ShowHide solution
Evaluate the fitted model function at 2000:
bfit = fitModel(log(Rate) ~ m*(Year - 1981) + b, data = Mortgage30YrAnnual)
bfit(2000)
The last line prints [1] 6.93817. The model predicts an average rate of 6.938 percent in 2000. To find the recorded rate, type View(Mortgage30YrAnnual) in the Console. The row for Year 2000 records a rate of 8.06 percent, about 1.122 percentage points above the prediction.
Example 14
A sealed drum of radioactive material gives off radiation. An instrument at the drum’s surface gives a reading rad, in millirem per hour (mrem/hr), that falls with year, the years since sealing. The drum can be shipped once its reading falls below 200 mrem/hr.
year | 0 | 2 | 3.5 | 6 | 8 |
rad (mrem/hr) | 950 | 695 | 538 | 361 | 262 |
Use the best-fit exponential model to predict the reading 10 years after sealing, and interpret the answer.
ShowHide solution
Because the smallest input is 0, the shift is and drops out of the formula given to fitModel(). Type in the data, fit as in Example 8, and evaluate the fitted model function at 10:
year = c(0, 2, 3.5, 6, 8)
rad = c(950, 695, 538, 361, 262)
rfit = fitModel(log(rad) ~ m*year + b)
rfit(10)
The last line prints [1] 189.426. The model predicts a reading of 189.426 mrem/hr 10 years after sealing. By the model, the drum can be shipped at 10 years, because the predicted reading is below the 200 mrem/hr limit. The prediction is an extrapolation beyond the last reading at 8 years.