[This article was first published on R – R-BAR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R’s Inf keyword – Have you’ve ever wondered what to do with it? If so, this is the second in series of posts that explore how we can exploit the keyword’s interesting properties to get the answers we need and improve code robustness. If you want to catch up on the first post where we look at Inf and the cut() function, please see Infamous Inf – Part I

For those unfamiliar with R’s Inf keyword, it is defined as a positive or negative number divided by zero yielding positive or negative infinity, respectively.

c(plus_inf = 1/0, minus_inf = -1/0)

# plus_inf minus_inf
#      Inf      -Inf

Sounds very theoretical. So how we can make practical use of infinity in R? In this first post, we’ll be discussing how Inf can make binning data with cut() a more robust process.

Inf with integrate()

For many of us, Integration ranks low in the fun category, but sometimes its necessary. For example, to get the probability of a normally distributed event between two limits you would need to integrate the normal distribution density function between limits a and b. The functions are detailed below along with their R function equivalent.

$f(x |\mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x - \mu)^2}{2\sigma^2}} \qquad \mathrm{Density\ Function\ (aka.\ dnorm)} \\ P(x |\mu, \sigma^2) = \int\limits_a^b \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x - \mu)^2}{2\sigma^2}} \qquad \mathrm{Proability\ Function \space (aka.\ pnorm)}$

The total area under the Density Function should be 1. So if we integrate dnorm(x) from negative to positive infinity we should get 1. Let’s see

LongDF <- function(x, mu = 0 , sigma = 1){
1/sqrt(2*pi*sigma^2) * exp(-(x-mu)^2/(2*sigma^2))
}

#dnorm and the long function are the same...
#dnorm(0) == LongDF(0)
#TRUE

integrate(dnorm, -Inf,  Inf)

The result:

1 with absolute error < 9.4e-05

As expected the answer is 1. The same result you’d get if you ran.

pnorm(q = Inf, mean = 0, sd = 1)

Which also yields, 1

To leave a comment for the author, please follow the link and comment on their blog: R – R-BAR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update! Subscribe to R-bloggers to receive e-mails with the latest R posts.(You will not see this message again.)

Click here to close (This popup will not appear again)