# R Tutorials

## Plot Logistic Regression Decision Boundary in R

In this article, we will produce the following R plot that represents the decision boundary of a logistic regression model: Here’s the full code used to generate it: Code explanation First, we create some data (2 continuous variables x1 and x2, and 1 binary variable y) and run a logistic regression: Next, we will create …

## Stepwise (Linear & Logistic) Regression in R

In this article, we will cover: Let’s start by creating some data: To run a stepwise regression, use the stepAIC function from the MASS library. 1. How to run forward stepwise linear regression Output: Call: lm(formula = X1 ~ X4 + X3 + X7, data = dat) Residuals: Min 1Q Median 3Q Max -0.52407 -0.23122 …

## How to Deal with Multicollinearity in R

Multicollinearity occurs when there is a strong linear relationship between 2 or more predictors in a regression model. It is a problem because it increases the standard errors of the regression coefficients, leading to noisy estimates. Let’s simulate some data in R: We have a collinearity problem in our model since our variables’ VIFs (Variance …

## Linear Regression with Interaction in R

Output: Call: lm(formula = Y ~ X + Z + X:Z, data = dat) Residuals: Min 1Q Median 3Q Max -1.00058 -0.25209 0.00766 0.21640 0.89542 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.57717 0.11195 40.885 < 2e-16 *** X 0.44168 0.06551 6.742 3.38e-10 *** Z -1.23932 0.21937 -5.649 8.16e-08 *** X:Z 0.18859 0.03357 5.617 …

## How to Describe/Summarize Numerical Data in R (Example)

Let’s start by creating IQ, a normally distributed numerical variable, with a mean of 100 and a standard deviation of 15, that represents the IQ scores of a sample of 100 participants: Next, we will (1) summarize this variable and (2) describe its distribution. 1. Summary statistics Reminder: The 1st quartile is the 25th percentile, …

## How to Describe/Summarize Categorical Data in R (Example)

Let’s start by creating our own data, consisting of 2 categorical variables: gender and smoking: Next, we will create a frequency table and a bar plot to summarize these data one variable at a time, then we will create a contingency table and a stacked bar plot to describe the relationship between the 2 variables. …

## Modulo Operator (%%) in R: Explained + Practical Examples

The modulo operator (%% in R) returns the remainder of the division of 2 numbers. Here are some examples: 5 %% 2 returns 1, because 2 goes into 5 two times and the remainder is 1 (i.e. 5 = 2 × 2 + 1). 4 %% 2 returns 0, since 4 = 2 × 2 …

## Find the Minimum and Maximum of a Function in R

The function optimize (also spelled optimise) in R returns the minimum or maximum of a function f(x) within a specified interval. It takes as inputs: f: a function. interval: a vector containing the lower and upper bounds of the domain where we want to search for the minimum or maximum. maximum: a logical, where TRUE …

## Write a Function that Returns the nth Fibonacci Number in R

Challenge: Write a function in R that prints the nth Fibonacci number. Reminder: the Fibonacci sequence is: 1, 1, 2, 3, 5, 8, … So, the first Fibonacci number is 1, the second is also 1, and then each subsequent number is the sum of the previous 2 in the sequence. Solution: In this article, …

## Plot a Step Function in Base R and ggplot2

As an example of a step function, we will use the floor function floor(x) that takes a real number x and returns the greatest integer less than or equal to x. Coding the floor function in R: Plotting in base R Output: We can add the data points to the plot with the following code: …

## Solve a Polynomial in R

A polynomial p(x) is an expression of the form: $$p(x) = a_0 + a_1x + a_2x^2 + a_3x^3 + … + a_nx^n$$ Where n is any non-negative integer. Solve a polynomial p(x) in R To solve the equation $$p(x) = 0$$ in R, we can use the function: polyroot. For example, let’s solve the equation: …

## Coding and Plotting a Piecewise Function in R

In this tutorial we are going to code the following function in R: $$f(x) =\begin{cases}-x, & \text{if x < -1} \\x^2, & \text{if x \geq -1}\end{cases}$$ And produce the following plot: Coding the piecewise function f(x) in R Using if else statements While this code is easy to read and understand, it does not support …

## How to Plot a Quadratic Function in R

For the following quadratic function: $$f(x) = x^2 + 2x – 20$$ Here’s the plot that we want to produce: Coding the function f(x) in R A quadratic function is a function of the form: $$ax^2 + bx + c$$, where $$a \neq 0$$. So for $$f(x) = x^2 + 2x – 20$$: a = …

## Find the Line Equation From 2 Points in R

Suppose we want to know the equation of the line that passes through 2 points A and B, such that: Quick solution Output: Call: lm(formula = ys ~ xs) Coefficients: (Intercept) xs 58.833 -2.417 So, the equation of the line that passes through A and B is: $$f(x) = -2.417x + 58.833$$ To get more …

## Writing Custom Functions in R

In this article, you will learn how to write your own functions in R. Specifically, we will cover: How to write a simple function How to write a more complex function How to write an anonymous function How to write a function with an unfixed number of arguments How to write a recursive function 1. …

## How to Solve an Equation in R

In this article, will use the uniroot.all() function from the rootSolve package to find all the solutions of an equation over a given interval (or domain). Input: uniroot.all() takes 2 arguments: a function f and an interval. How it works: Its searches the interval for all possible roots of f. Output: uniroot.all() returns a vector …

## Create and Graph Intervals in R

A quick review of intervals The open interval from a to b, denoted (a, b), consists of all numbers between a and b excluding the endpoints a and b. Open circles in the graph indicate that the endpoints are excluded: The closed interval from a to b, denoted [a, b], consists of all numbers between …

## Working with Sets in R (Tutorial)

A set is an unordered collection of unique elements. It is helpful to keep track of distinct objects. In this tutorial, you will learn how to: Create a set Manipulate sets Work with subsets Apply set operations 1. Create a set 1.1. Create a set from scratch A set can contain different types of elements, …