# Chapter 3 Parameters and functions

## 3.1 Parameters versus variables

Why there’s not really a difference.

Newton’s distinction between a, b, c, and x, y, z.

## 3.2 Parameters of modeling functions

Give the parameterizations of exponentials, sines, power laws …

The idea is to make the arguments to the mathematical functions dimensionless.

Parameters and logarithms – You can take the log of anything you like. The units show up as a constant

## 3.3 Polynomials and parameters

Each parameter has its own dimension

## 3.4 Parameters and makeFun()

Describe how makeFun() works here.3

## 3.5 Functions without parameters: splines and smoothers

EXPLAIN hyper-parameter. It’s a number that governs the shape of the function, but it can be set arbitrarily and still match the data. Hyper-parameters are not set directly by the data.

Mathematical models attempt to capture patterns in the real world. This is useful because the models can be more easily studied and manipulated than the world itself. One of the most important uses of functions is to reproduce or capture or model the patterns that appear in data.

Sometimes, the choice of a particular form of function — exponential or power-law, say — is motivated by an understanding of the processes involved in the pattern that the function is being used to model. But other times, all that’s called for is a function that follows the data and that has other desirable properties, for example is smooth or increases steadily.

“Smoothers” and “splines” are two kinds of general-purpose functions that can capture patterns in data, but for which there is no simple algebraic form. Creating such functions is remarkably easy, so long as you can free yourself from the idea that functions must always have formulas.

Smoothers and splines are defined not by algebraic forms and parameters, but by data and algorithms. To illustrate, consider some simple data. The data set Loblolly contains 84 measurements of the age and height of loblolly pines.

gf_point(height ~ age, data=datasets::Loblolly)

Several three-year old pines of very similar height were measured and tracked over time: age five, age ten, and so on. The trees differ from one another, but they are all pretty similar and show a simple pattern: linear growth at first which seems to low down over time.

It might be interesting to speculate about what sort of algebraic function the loblolly pines growth follows, but any such function is just a model. For many purposes, measuring how the growth rate changes as the trees age, all that’s needed is a smooth function that looks like the data. Let’s consider two:

1. A “cubic spline”, which follows the groups of data points and curves smoothly and gracefully.
f1 <- spliner(height ~ age, data = datasets::Loblolly)
1. A “linear interpolant”, which connects the groups of data points with straight lines.
f2 <- connector(height ~ age, data = datasets::Loblolly)

The definitions of these functions may seem strange at first — they are entirely defined by the data: no parameters! Nonetheless, they are genuine functions and can be worked with like other functions. For example, you can put in an input and get an output:

f1(age = 8)
## [1] 20.68193
f2(age = 8)
## [1] 20.54729

You can graph them:

graphFun(f1(age) ~ age, agelim = range(0, 30)) %>%
graphFun(f2(age) ~ age, color="red", agelim = range(0, 30)) %>%
gf_point(height ~ age, data = datasets::Loblolly)

You can even “solve” them, for instance finding the age at which the height will be 35 feet:

findZeros(f1(age) - 35 ~ age, xlim=range(0,30))
##       age
## 1 12.6905
findZeros(f2(age) - 35 ~ age, xlim=range(0,30))
##    age
## 1 12.9

In all respects, these are perfectly ordinary functions. All respects but one: there is no simple formula for them. You’ll notice this if you ever try to look at the computer-language definition of the functions:

f2
## function (age)
## {
##     x = get(fnames[2])
##     if (connect)
##         SF(x)
##     else SF(x, deriv = deriv)
## }
## <environment: 0x7fe527c48180>

There’s almost nothing here to tell the reader what the body of the function is doing. The definition refers to the data itself which has been stored in an “environment.” These are computer-age functions, not functions from the age of algebra.

As you can see, the spline and linear connector functions are quite similar, except for the range of inputs outside of the range of the data. Within the range of the data, however, both types of functions go exactly through the center of each age-group.

Splines and connectors are not always what you will want, especially when the data are not divided into discrete groups, as with the loblolly pine data. For instance, the trees.csv data set is measurements of the volume, girth, and height of black cherry trees. The trees were felled for their wood, and the interest in making the measurements was to help estimate how much usable volume of wood can be gotten from a tree, based on the girth (that is, circumference) and height. This would be useful, for instance, in estimating how much money a tree is worth. However, unlike the loblolly pine data, the black cherry data does not involve trees falling nicely into defined groups.

Cherry <- datasets::trees
gf_point(Volume ~ Girth, data = Cherry)

It’s easy enough to make a spline or a linear connector:

g1 = spliner(Volume ~ Girth, data = Cherry)
g2 = connector(Volume ~ Girth, data = Cherry)
graphFun(g1(x) ~ x, x = range(8,18)) %>%
graphFun(g2(x) ~ x, color ="red") %>%
gf_point(Volume ~ Girth, data = Cherry) %>%
gf_labs(x = "Girth (inches)")

The two functions both follow the data … but a bit too faithfully! Each of the functions insists on going through every data point. (The one exception is the two points with girth of 13 inches. There’s no function that can go through both of the points with girth 13, so the functions split the difference and go through the average of the two points.)

The up-and-down wiggling is of the functions is hard to believe. For such situations, where you have reason to believe that a smooth function is more appropriate than one with lots of ups-and-downs, a different type of function is appropriate: a smoother.

g3 <- smoother(Volume ~ Girth, data = Cherry, span=1.5)
graphFun(g3(x) ~ x, x=range(8,18)) %>%
gf_point(Volume~Girth, data=Cherry) %>%
gf_labs(x = "Girth (inches)")

Smoothers are well named: they construct a smooth function that goes close to the data. You have some control over how smooth the function should be. The hyper-parameter span governs this:

g4 <- smoother(Volume ~ Girth, data=Cherry, span=1.0)
graphFun(g4(x) ~ x, x = range(8,18)) %>%
gf_point(Volume~Girth, data = Cherry) %>%
gf_labs(x = "Girth (inches)", y = "Wood volume")

Of course, often you will want to capture relationships where there is more than one variable as the input. Smoothers do this very nicely; just specify which variables are to be the inputs.

g5 <- smoother(Volume ~ Girth+Height, data = Cherry, span = 1.0)
graphFun(g5(g,h) ~ g & h, g = range(8, 22), h=range(60, 90)) %>%
gf_point(Height ~ Girth, data = Cherry,  fill = NA) %>%
gf_labs(x = "Girth (inches)", y = "Height (ft)", title = "Volume (ft^3)")
## Loading required package: directlabels

When you make a smoother or a spline or a linear connector, remember these rules:

1. You need a data frame that contains the data.
2. You use the formula with the variable you want as the output of the function on the left side of the tilde, and the input variables on the right side.
3. The function that is created will have input names that match the variables you specified as inputs. (For the present, only smoother will accept more than one input variable.)
4. The smoothness of a smoother function can be set by thespan argument. A span of 1.0 is typically pretty smooth.The de fault is 0.5.
5. When creating a spline, you have the option of declaring monotonic=TRUE. This will arrange things to avoid extraneous bumps in data that shows a steady upward pattern or a steady downward pattern.

When you want to plot out a function, you need of course to choose a range for the input values. It’s often sensible to select a range that corresponds to the data on which the function is based. You can find this with the range() command, e.g.

range(Cherry\$Height)
## [1] 63 87

### 3.5.1 Exercises

#### 3.5.1.1 Exercise 1

The datasets::Orange data set

Orange <- datasets::Orange
names(Orange)
## [1] "Tree"          "age"           "circumference"

contains data on the circumference and age of trees.

1. Construct a smoother of circumference (in cm) as a function of age (in days), with span=1. What’s the value of the function for age 1000 days? {127.41,128.32,130.28,132.08,133.12,138.20}

2. Construct a smoother of circumference versus age with span=10. What’s the value for age 1000 days? {127.04, 127.41,128.32,130.28,132.08,133.12,138.20}

3. How does the shape of the graph of the span=10 smoother differ from that of the span=1 smoother?

1. The span=10 smoother is always above the span=1 smoother.
2. The span=10 smoother is always below the span=1 smoother.
3. The span=10 smoother is straighter than the span=1 smoother.

ANSWER:

Orange <- datasets::Orange
f1 <-smoother(circumference ~ age, data = Orange, span=1)
f1(1000)
##        1
## 132.0825
f2 <- smoother(circumference ~ age, data = Orange, span=10)
f2(1000)
##       1
## 127.407
graphFun(f1(age)~age, agelim=range(100,1600)) %>%
graphFun(f2(age)~age, color="red")

The span=10 smoother is straighter than the span=1 smoother.

#### 3.5.1.2 Exercise 2

The data gives in datasets::trees gives wood volume (in cubic feet) of cherry trees along with height (in feet) and girth (in inches — even though the name “Girth” is used, it’s really a diameter).

Construct a smoother of two variables for the cherry data with span=1. (Warning: Be careful to match the capitalization of the names).

What’s the value when height is 50 feet and “Girth” (that is, diameter) is 15 inches? {24.4,25.5,26.6,27.7,28.8}

ANSWER:

f = smoother(Volume~Girth+Height, data=datasets::trees,
span=1)
f(Girth=15,Height=50)
##        1
## 25.49214

#### 3.5.1.3 Exercise 3

The data set BodyFat.csv contains several body measurements made on a group of 252 men. You’re going to look at theses variables:

• Weight — the total body weight in pounds.
• BodyFat — the percentage of total weight that is fat.
• Abdomen — the circumference at the waist in cm.

You can read in the data like this:

BodyFat <- read.csv("http://www.mosaic-web.org/go/datasets/BodyFat.csv")
1. Construct a smoother of BodyFat as a function of Weight with span=1. What is the value of this function for Weight=150? {13.46,14.36,16.34}

What’s the general shape of the function?

1. {Sloping up and curved downwards}
2. Sloping down and curved upwards
3. Straight line sloping downwards
1. Construct a smoother of BodyFat as a function of Abdomen with span=1.

1. What is the value of this function for Abdomen=100? {23.34,23.85,24.43,25.23,26.74,27.25}
2. What’s the general shape of the function?
1. Sloping up and curved downwards
2. Sloping down and curved upwards
3. Straight line sloping downwards
2. Construct a smoother of BodyFat as a function of both Abdomen and Weight with span=1.
1. What is the value of this function for Abdomen=100 and Weight=150? {23.34,23.85,24.43,25.23,26.74,27.25}

ANSWER

f = smoother( BodyFat ~ Abdomen + Weight, data=BodyFat, span = 1)
f(Abdomen = 100, Weight = 150)
##        1
## 27.24956
1. Make a contour plot of your smoother function. Using the plot, answer this question: How does body fat percentage change when increasing weight from 140 to 240 pounds, but holding abdominal circumference constant at 90c m?
1. Body fat percentage doesn’t depend on weight.
2. Body fat percentage goes down with increasing weight.
3. Body fat percentage goes up with increasing weight.

ANSWER:

graphFun(f(Abdomen, Weight) ~ Abdomen & Weight,
Abdominlim = range(70,150),
Weightlim = range(100,250))

1. Pick up on the footnote for experts in chapter 1, explain how the parameters are carried along with the function and not variables in the global environment. In terms of the computer language, we don’t distinguish between parameters and variables.