R Tutorials

Extract Numbers from Strings in R

The functions parse_integer(), parse_double(), and parse_number() from the readr library transform a character vector into a numeric vector. Here’s an example that compares these 3 functions: Exercises 1. Extract the number 1000000 from “1 000 000” Not all characters in this string can be transformed into an integer (since we have white spaces), so we …

Extract Numbers from Strings in R Read More »

How to Deal with Violation of Normality of Errors in R

Linear regression assumes that error terms are normally distributed. This is especially important when we are using linear regression for prediction purposes and our sample size is small (see: Understand Linear Regression Assumptions). When the normality of errors assumption is violated, try: Let’s create some data to demonstrate these methods: Output: So we see that …

How to Deal with Violation of Normality of Errors in R Read More »

How to Deal with Heteroscedasticity in Regression in R

Linear regression assumes that the dispersion of data points around the regression line is constant. We can deal with violation of this assumption (i.e. with heteroscedasticity) by: Let’s create some heteroscedastic data to demonstrate these methods: Output: The residuals vs fitted values plot shows a fan shape, which is evidence of heteroscedasticity. (For more information, …

How to Deal with Heteroscedasticity in Regression in R Read More »

How to Deal with Violation of the Linearity Assumption in R

The most important assumption of linear regression is that the relationship between each predictor and the outcome is linear. When the linearity assumption is violated, try: Let’s create some non-linear data to demonstrate these methods: The residuals vs fitted values plot shows a curved relationship, therefore, the linearity assumption is violated. Solution #1: Adding a quadratic …

How to Deal with Violation of the Linearity Assumption in R Read More »

How to Run and Interpret a Logistic Regression Model in R

In this tutorial, we are going to run a logistic regression using the Titanic dataset available in R: 1. Logistic regression equation The formula \(Survived \sim Age\) corresponds to the logistic regression equation: \(\log(\frac{P}{1 – P}) = \beta_0 + \beta_1 Age\) Where \(P\) is the probability of having the outcome, i.e. the probability of surviving. …

How to Run and Interpret a Logistic Regression Model in R Read More »

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: …

Solve a Polynomial in R Read More »

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 …

How to Solve an Equation in R Read More »