This is a guide to help you get started with using RStudio. We will be using RStudio in this class to model and visualize functions in order to work with data and apply concepts from calculus. The programming language used in RStudio is called R and is used by professional statisticians and mathematicians.
In this course, we use Quarto Markdown files, which have the extension .qmd. A Quarto document combines R code, mathematical notation (\(\LaTeX\)), and written text (Markdown) all in one file.
1.1 First Log-in
If you have no experience with RStudio, I highly recommend only using the Macalester RStudio server, located at https://rstudio.macalester.edu/. You will sign in with your Macalester username and password.
If you see this page, you can click “New Session”.
Once you open the RStudio window, you will see something like this: There are a few main areas of the RStudio window:
In the top left, you will find the edit toolbar and the tabs for your open files. You will work primarily in the source file. You want to make sure your file type is Quarto. You can switch file types on the bottom right of the Source panel. The output of your code will show up under each code block.
In the bottom left, we have the console and command line. It is a great place to test code and serves as a basic calculator. Code you write here will not be saved. The output of your code and any error messages will also show up here.
If something is not working, try saving your files, then quitting and restarting by clicking Quit Session (orange power button at the top right).
This panel keeps track of your work. The environment tab shows all the variables you have named in the session (including data, functions, and lists) and gives you a preview of them. The history tab tracks the pieces of code you run.
The Files tab shows you all the folders and files you have saved. This is where you will go to upload and export files. You will want to organize your files to be able to access them easily later. The Packages tab shows you which packages are available and loaded into RStudio. The Help tab explains how functions work and what type of parameters you need to give them.
1.2 Working with a Quarto File
In this class, we’ll be working with code a lot: writing it, running it, analyzing it, and commenting on it. We’ll also be going back to code we’ve already used and tweaking it to solve new but related questions. For all of these reasons, we’re going to use RMarkdown notebooks.
The notebook will run large blocks of code at a time and show the output right next to the code. You can also add comments to your code to make a note of what it does so that you can look back on it later.
You can render your file by clicking on the Render button near the top left of the RStudio window, near your file tabs.
1.2.1 Open or Upload a New File
To open a new document, you can go to File > New File > Quarto Document or click the New File icon (paper with a green plus sign) under Files.
To upload an activity file, you will first download to activity .qmd file from Moodle. Then, in the bottom right panel, go to Files > Upload to upload the file. Click ‘Choose File’ and select the file you would like to upload. Make sure to move the file into an appropriate folder by checking the box next to the file name and clicking the gear icon, then Move.
1.2.2 Starting Your File
At the beginning of a new document, include the document information and settings provided in the activity instructions. You generally do not need to change these settings included in an activity file. You should, however, replace "Your Name" with your name and give your file a reasonable title and file name.
The very start of your file should look something like this:
Registered S3 method overwritten by 'mosaic':
method from
fortify.SpatialPolygonsDataFrame ggplot2
The 'mosaic' package masks several functions from core packages in order to add
additional features. The original behavior of these functions should not be affected by this.
Attaching package: 'mosaic'
The following objects are masked from 'package:dplyr':
count, do, tally
The following object is masked from 'package:Matrix':
mean
The following object is masked from 'package:ggplot2':
stat
The following objects are masked from 'package:stats':
binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
quantile, sd, t.test, var
The following objects are masked from 'package:base':
max, mean, min, prod, range, sample, sum
library("mosaicCalc")
Attaching package: 'mosaicCalc'
The following object is masked from 'package:stats':
D
You only need one setup chunk. The packages must be loaded before you use their functions.
1.2.4 Using Markdown to Format Text
The regular text uses Markdown, which has simple commands to create formatting such has sections, bold, italic, links, itemized lists, and much more.
Paragraphs: You can type regularly to write a paragraph. Skipping a line starts a new paragraph. But if you do not skip a line then it is part of the same paragraph
Headings: Use one hashtag # in front of your text to get a heading. Use two hashtags ## in front of your text to get a subheading. You can keep adding hashtags to get further subheadings.
Bold and Italics: You get italics by surrounding your text with asterisks, like * text *. You get bold text by surrounding your text with double asterisks, like this: ** text ** If you use three asterisks, you will get italicized and bolded text.
1.2.5 Using R to Run Code
Your R code must be inside a code block to run properly. When you render your .qmd file, the code blocks will be executed in the order they’re written. You can run an individual block without rendering the entire file by clicking on the green arrow in the upper right corner of that code block.
To add a code block to your file, you can do one of two things:
Click on the green +C at the top of the Source panel. Choose the R option.
Type the following into the source file:
```{r}(code goes here)```
1.3 Saving and Formatting Your Activity
Before submitting your activity, make sure your file is saved and formatted correctly.
Change your name at the top of the activity. Replace "Your Name" with your actual name wherever it appears at the beginning of the document.
Give your files meaningful names. For example, use RActivity1-YourName.qmd for your Quarto file.
Save all of your Quarto files in a dedicated folder for this class. Keep your .qmd file in a course folder so you can find it and continue working on it later.
Check your work before submitting. Make sure you have answered all questions, included your R code, and written explanations wherever requested.
1.4 Submitting Your Activity to Moodle
You will submit the .html file, not the .qmd file, to Moodle. The ` file is the finished version of your activity, including your written answers, code, and plots.
Follow these steps in RStudio.
Save your work. Click the Save icon or select File > Save.
Render your document. Click the Render button near the top of the RStudio editor pane, where you write your Quarto document. Wait for the rendering process to finish. If errors appear, fix them before continuing.
Open the Files pane. In the lower-right pane of RStudio, click the Files tab and navigate to the folder where you stored your .qmd file. After rendering, The .html file is saved in the same folder as your .qmd file.
Download the file. Check the box to the left of your .html file. Then, click the gear icon and More > Export. Download the file to your computer.
Check your .html file before submitting. Open it and make sure your name, written answers, code, and plots appear correctly. Check that all questions are answered.
Once you have rendered and downloaded your .html file, submit it on Moodle. Make sure that the file name follows to appropriate format.
2 Troubleshooting
Common errors and what they usually mean:
Nothing ran, and the console shows +: You may have an unclosed parenthesis, bracket, or quotation mark. Complete the expression or press Escape to cancel it.
Unexpected symbol: You may have forgotten a multiplication sign (*) or included an invalid character.
Object not found: Check that you created the object first and spelled its name correctly. R is case-sensitive.
Could not find function: Check that the package containing the function is loaded.
Non-numeric argument to mathematical function: Check that your input is numeric and not text or a factor.
Missing values (NA): Check the data for missing values and consider whether the function you are using can handle them.
Error in fitModel(): Check your formula, variable names, starting values, and whether the model is appropriate for the data.
Error in makeFun(): Make sure the variables after ~ match the variables in the expression before it.
Quarto rendering fails: Check that your code runs, your packages are installed, and your document is properly formatted.
3 Functions and Expressions
3.1 Basic Calculations
You can run basic calculations in an R block:
8+13
[1] 21
7*9
[1] 63
sin(pi/2)
[1] 1
sqrt(2)
[1] 1.414214
(2.25)^3
[1] 11.39062
8*cos(pi/6)
[1] 6.928203
3.2 Defining Variables
To define a variable, we use the following syntax:
x <-5
The arrow tells RStudio to give the variable x the value of 5. Then we can ask RStudio what value is stored in x:
x
[1] 5
3.3 Vectors (Lists) and Sequences
Use c() to combine values into a vector (aka a list) and seq() to create a sequence.
Notes: - Always use * for multiplication. - The variable on both sides of ~ must match (e.g., x^2 ~ x, not x^2 ~ t).
You can evaluate functions at specific input values:
f(-10)
[1] 56
g(0)
[1] -0.5
P(6)
[1] 22.40845
Q(-1/2)
[1] 12.13376
Reminders:
Use * for multiplication: 3*x, not 3x.
Use ^ for exponents: x^2.
The variables in the expression must match the inputs after ~.
R distinguishes uppercase and lowercase letters: x and X are different objects.
3.5 Common Mathematical Functions
Mathematical expression
R command
\(x^2\)
x^2
\(4x^2-7x+3\)
4*x^2 - 7*x + 3
\(\sqrt{x}\)
sqrt(x) or x^(1/2)
\(\sqrt[3]{x}\)
x^(1/3)
\(\sin(x)\)
sin(x)
\(\cos(x)\)
cos(x)
\(e^x\)
exp(x)
\(\ln(x)\)
log(x)
\(\log_{10}(x)\)
log10(x)
\(\log_b(x)\)
log(x, base = b)
\(|x|\)
abs(x)
R uses radians for trigonometric functions.
4 Single-Variable Functions
4.1 Plotting a Function with plotFun()
Use plotFun() to plot a function of one variable. You can specify the horizontal range using xlim. You can also change the color with col and the line width with lwd.
Access the variable var from the data set called dataset
diff()
Calculate consecutive differences
log()
Calculate natural logarithms
round()
Round numerical values
print()
Display a message or object
coef()
Extract fitted model coefficients
6.2mosaicData Package Dataset List
Here is a list of some of the built-in data sets, their descriptions, and suggested variables from the mosaicData package:
Dataset
Description
Suggested Variables
CPS85
Census information from 1985
exper (# years of experience) wage (US dollars per hour) educ (# years of education)
Dimes
Weight of dimes
year (year dime was minted) mass (weight of dime in grams)
Galton
Heights of adult children and their parents
father (father’s height in inches) mother (mother’s height in inches) height (child’s height in inches)
GoosePermits
Hunters offered money for their goose permits
bid (amount offered for permit in US dollars) keep (# hunters who kept permit) sell (# hunter who sold permit)
KidsFeet
Length and width of fourth graders’ feet
length (in centimeters) width (in centimeters)
RailTrail
Weather and number of trail walkers in Florence, MA in 2005
hightemp (daily high temperature in degrees Fahrenheit) lowtemp (daily low temperature in degrees Fahrenheit) avgtemp (daily average temperature in degrees Fahrenheit) precip (amount of precipitation in inches) volume (number of trail users)
SAT
SAT data 1994-1995
expend (expenditure per student in thousands of US dollars) salary (average salary of teachers in thousands of US dollars) verbal (average verbal SAT score) math (average math SAT score) sat (average total SAT score)
SaratogaHouses
Data on houses in Saratoga County, NY in 2006
price (US dollars) livingArea (square feet)
SnowGR
Snowfall data in Grand Rapids, MI going back to 1893
SeasonStart (year) Total (inches of snow for entire season Jul-Jun)
SwimRecords
World records for men and women 1905-2004
time (in seconds) year (year record was set)
Utilities
Data on utilities bills
temp (average temperature in degrees Fahrenheit) kwh (electricity usage in kilo-watt hours) ccf (gas usage in hundreds of cubic feet (CCF)) totalbill (in US dollars) gasbill (in US dollars) elecbill (in US dollars)
6.3 Loading a Data Set
Use data() to load a built-in data set. The head command lets you see the first few rows of a data set.
MODEL_EXPRESSION describes the general function with parameters in terms of the input variable.
data identifies the data frame you are using (the table of data points).
The line coef(f_model) displays the parameter values estimated by R.
Here’s an example that fits an exponential function using the fact that \(y = Ce^{kx}\) is equivalent to a linear function \(\ln(y) = \ln(C) + kx\) in terms of the inputs and the log of the outputs.
There are also functions in R that can find the exact values of derivatives and partial derivatives.
To find an exact derivative at a point, we can use the D(x) function. You can see the formula of the derivative and use it to evaluate the derivative at various values of x.
f <-makeFun(x^2~ x)df_dx <-D(f(x) ~ x)df_dx
function (x)
2 * x
df_dx(2)
[1] 4
Here is how you can use the same function D() to find partial derivatives:
g <-makeFun(x^2+3*y^3~ x & y)# partial derivative in the x-directiondg_dx <-D(g(x,y) ~ x)dg_dx
function (x, y)
2 * x
dg_dx(2,3)
[1] 4
# partial derivative in the y-directiondg_dy <-D(g(x,y) ~ y)dg_dy
function (x = 5, y)
9 * y^2
dg_dy(2,3)
[1] 81
7.3 Gradient Vector
You can create a gradient vector by putting your partial derivatives in a function and combining them using the cbind() function:
g <-makeFun(x^2+3*y^3~ x & y)dg_dx <-D(g(x,y) ~ x)dg_dy <-D(g(x,y) ~ y)grad_g <-makeFun(cbind(dx =dg_dx(x, y), dy =dg_dy(x, y)) ~ x & y)grad_g(2,3)
dx dy
[1,] 4 81
7.4 Gradient Search
Use gradient search by moving a small amount in the direction of the gradient:
Single-variable version:
f <-makeFun(x^2~ x)df_dx <-D(f(x) ~ x)x <-0.25# initialize x (your first guess)alpha <-0.1# how much you want to move in the direction of the gradienttotal_steps <-5# how many times you want to step# this for loop is set to run for as many times as you set iterations to# if iterations = 10, then the loop will run 10 times # the final line inside the for loop prints:# the iteration number, the current value of x, the current value of the derivativefor (n in1:total_steps) { derivative =df_dx(x) x <- x - alpha * derivativeprint(cbind(step = n, x=round(x,2), deriv =round(derivative,2)))}
step x deriv
[1,] 1 0.2 0.5
step x deriv
[1,] 2 0.16 0.4
step x deriv
[1,] 3 0.13 0.32
step x deriv
[1,] 4 0.1 0.26
step x deriv
[1,] 5 0.08 0.2
Adjust the initial values and the number of iterations until the derivative deriv is close to 0.
g <-makeFun(x^2+ y^2~ x & y)dg_dx <-D(g(x,y) ~ x)dg_dy <-D(g(x,y) ~ y)x <-1# initialize xy <-2# initialize yalpha <0.1
[1] FALSE
total_steps =10for (n in1:total_steps) { grad_x <-dg_dx(x,y) grad_y <-dg_dy(x,y) x <- x - alpha * grad_x y <- y - alpha * grad_yprint(cbind(step = n, x=round(x,2), y=round(x,2), grad_x =round(grad_x,2), grad_y =round(grad_y,2)))}
step x y grad_x grad_y
[1,] 1 0.8 0.8 2 4
step x y grad_x grad_y
[1,] 2 0.64 0.64 1.6 3.2
step x y grad_x grad_y
[1,] 3 0.51 0.51 1.28 2.56
step x y grad_x grad_y
[1,] 4 0.41 0.41 1.02 2.05
step x y grad_x grad_y
[1,] 5 0.33 0.33 0.82 1.64
step x y grad_x grad_y
[1,] 6 0.26 0.26 0.66 1.31
step x y grad_x grad_y
[1,] 7 0.21 0.21 0.52 1.05
step x y grad_x grad_y
[1,] 8 0.17 0.17 0.42 0.84
step x y grad_x grad_y
[1,] 9 0.13 0.13 0.34 0.67
step x y grad_x grad_y
[1,] 10 0.11 0.11 0.27 0.54
Adjust the initial values and the number of iterations until both partial derivatives grad_x and grad_y are close to 0.
8 Optimization
8.1 Constrained Optimization
Plot contours and constraints together to visualize constrained optimization problems:
P <-makeFun(x^(0.2) * y^(0.8) ~ x & y) # objective functionQ <-makeFun(4*x +3*y ~ x & y) # constraint functioncontour_plot(P(x,y) ~ x & y, domain(x=0:80, y=0:80), contours_at =seq(20,80,5), # adding contours between 20 and 80 in increments of 5skip=0) |>contour_plot(Q(x,y) ~ x & y, contour_color="black", contours_at =c(200), # constraint (contour line) of constraint function at z = 200lwd =2)
Scale for colour is already present.
Adding another scale for colour, which will replace the existing scale.
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
Zoom in for a better estimate and to approximate the Lagrange multiplier by changing the constraint by a small amount.
9 Integration
Approximate definite integrals by Riemann sums over an interval [a,b]:
f <-makeFun(x +sin(x^2) ~ x)# define start point and end point of interval [a,b]a <-5b <-10n <-100# number of rectanglesbase <- (b - a) / n # length of base of rectangle# Left Riemann Sumleft_points <-seq(from = a, to = b - base, by = base)left_heights <-f(left_points) # calculate the heightsleft_areas <- base * left_heightsleft_sum <-sum(left_areas)left_sum
[1] 37.44168
# Right Riemann Sumright_points <-seq(from = a + base, to = b, by = base)right_heights <-f(right_points) # calculate the heightsright_areas <- base * right_heightsright_sum <-sum(right_areas)right_sum
[1] 37.67298
# Midpoint Riemann Summid_points <-seq(from = a+base/2, to = b - base/2, by = base)mid_heights <-f(mid_points) # calculate the heightsmid_areas <- base * mid_heightsmid_sum <-sum(mid_areas)mid_sum
[1] 37.55494
Increase the number of rectangles for better accuracy.
Compute the true definite integral over an interval:
f <-makeFun(x +sin(x^2) ~ x)Integrate(f(x) ~ x, domain(x=5:10)) # integrate f over the interval defined by x=a:b
Loading required namespace: cubature
[1] 37.55575
Find the antiderivative of a function:
f <-makeFun(x +sin(x^2) ~ x)int_f <-antiD(f(x) ~ x)int_f(5)
[1] 13.02792
10 Differential Equations
10.1 Slope Fields and Trajectories
Plot slope fields with vectorfield_plot() and solution trajectories with traj_plot():
vectorfield_plot( t ~1, P ~6-2* P, domain(t=-8:8, P=-8:8))
diff_eq <-makeODE(dP ~6-2* P)soln <-integrateODE(diff_eq, domain(t=0:5), P =6)
Solution containing functions P(t).
traj_plot(P(t) ~ t, soln, nt =5)
Plot vector field + multiple trajectories:
diff_eq <-makeODE(dP ~6-2* P)soln1 <-integrateODE(diff_eq, domain(t=0:5), P =6)
Solution containing functions P(t).
soln2 <-integrateODE(diff_eq, domain(t=0:5), P =2)
Use vectorfield_plot and makeODE to build SIR models:
a <-0.001# infection rateb <-0.2# removal ratevectorfield_plot(S ~-a * S * I, I ~ a * S * I - b * I,domain(S =0:800, I =0:100),transform =function(L) L^0.01)
SIR_diff_eq <-makeODE(dS ~-a * S * I, dI ~ a * S * I - b * I)SIR_soln <-integrateODE(SIR_diff_eq, domain(t =0:80), S =800, I =5)