# Preface

Calculus is fundamentally about working with mathematical functions. The word “calculus” comes from the same root as “calculate,” and much of the subject matter of calculus concerns calculations that are done on functions.

This book shows how to do common calculus calculations using R.

R is a language for communicating instructions to a computer (and to people!). The R software is a computer system that understands this language and acts on it.

R is closely associated with statistics, but not with calculus. It turns out that R is an excellent language for doing calculus:

1. R is what the computer scientists call a functional computer language. Here, “functional” is not about “being able to do something with it,” although that’s certainly true. Rather, “functional” means that the language makes it easy to perform calculations on functions, which is just what’s called for in calculus.
2. In the workforce, you are much more likely to be doing statistics and data science than calculus. Using R for calculus allows you to transition seamlessly between working with data (and statistical models) and integrating the results from data into models that let you make use of them.
3. R provides a large ecosystem of programming, authoring, and publication tools.

R functions that carry out calculus are provided by the mosaicCalc package, available on CRAN.

You will be spending a few hours getting started with calculus in R. Why? Isn’t there some simpler way? Isn’t there nice mouse-based software? Why do you need to learn a language?

The answer isn’t about the quality of software or the availability of friendly packages. The answer has to do with the sorts of things you will be doing in mathematics and statistics. In themselves, the individual tasks you will undertake are not necessarily more complicated than, say, changing a word to a bold-face font, something you would do easily with a mouse.

There are three major reasons why mathematical and statistical computing is intrinsically different from the sort of computing you do when word-processing and so on.

1. In mathematics and statistics, there are often multiple inputs to a computation. To illustrate multiple inputs, consider a familiar word-processing computation: finding all the instances of the word “car” in a document and changing them to “automobile”. Easy enough; just use the find feature. But be careful! You might end up with “automobileeful” or “inautomobilecerated” or “automobilecinogenic” instead of careful, incarcerated, or carcinogenic. A second input to the calculation is needed: the set of contexts in which to allow or disallow the change. For instance, allow the change when “car” is preceded by a space and followed by a space or a period or a comma, or an “s” $$+$$ space “cars” $$\rightarrow$$ automobiles"). Things are not so simple as they might seem at first, which is why the find-and-replace feature of word-processors is only partially effective.

2. In mathematics and statistics, the output of one computation often becomes the input to another computation. That’s why math courses spend so much time talking about functions (and “domain” and “range”, etc.). In word processing, whenever you highlight a word and move it or change the font or replace it, you still end up with stuff on which you can perform the same operations: highlighting, moving, font-changing, etc. Not so in math and statistics. The sorts of operations that you will often perform – solving, integration, statistical summaries, etc. – produce a new kind of thing on which you will be performing new kinds of operations. In mathematics and statistics, you create a chain of operations and you need to be able to express the steps in that chain. It’s not a question of having enough buttons to list all the operations, you’ll need combinations of operations — more than could possibly be listed in a menu system.

3. In mathematics and statistics, the end-product is not the only thing of importance. When you write a letter or post to a blog, what counts is the final product, not the changes you made while writing and certainly not the thought process that you went through in composing your words. But in mathematics and statistics, the end-product is the result of a chain of calculations and it’s important that each step in that chain be correct. Therefore, it’s important that each step in the chain be documented and reproducible so that it can be checked, updated, and verified. Often, the chain of calculations becomes a new computation that you might want to apply to a new set of inputs. Expressing your calculations as a language allows you to do this.

If you have ever travelled to a country where you don’t speak the language, you know that you can communicate simple ideas with gestures and pointing and can satisfy the relatively simply expressed needs of eating and hygiene and shelter. But when you want to converse with a person and express rich ideas, you need a shared language.

Many people think that it would be better if other people learned our language, and the natural extension of this is that computers should be taught to speak English. But it turns out that English, or other natural languages, are not set up to be effective at communicating mathematical or statistical ideas. You need to learn a way to do this. The algebraic notation taught in high school is part of the story, but not a complete solution. That’s why you’ll be learning R.

If you’ve ever learned just a little of a foreign language, you’re familiar with the situation where you say something that seems straightforward, but your listener gives you a quizzical look: it doesn’t make sense. You used the wrong verb or the wrong preposition or a word with a slightly different meaning. A relative of mine, visiting me in France, once asked my host about “last year,” or so he intended. He actually said something pretty close but that works out to “elder buttock.” This did not produce the intended reaction.

Similarly in R. At first, you will make elementary mistakes. The computer will respond, like my French host, quizzically. But with practice – just a few hours – you will become fluent and able to express your ideas with confidence and certainty.

As you learn the R language, which will be much, much easier than learning a natural language like French or Chinese or Spanish, you will make mistakes and you will run into frustrating situations. But remember, the reason you are learning it is to be able to express complicated ideas. It’s the nature of mathematics and statistics that’s at the core here. Having a systematic way to express yourself will not only let you use the computer’s power, but will increase your understanding of the mathematical and statistical ideas.