# Enjoy R

# Two functions for normal distributions

### Function *inorm*

*X ~ N (mean = 15, sd = 5)*

You want to know the interval in which a certain percentage (suppose the 80 %) of X values varies.

The following function solves this kind of problem.

```
inorm <- function(p, mean = 0, sd = 1) {
if (p <= 0 | p >= 1)
stop("The interval makes sense with a probability greater than 0 and lower than 1")
LL <- qnorm((1 - p)/2, mean, sd)
UL <- 2 * mean - LL
interv <- c(LL, UL)
names(interv) <- c("Lower Limit", "Upper Limit")
return(interv)
}
```

So, if you apply the function to this special case:

`inorm(0.8, 15, 5)`

`## Lower Limit Upper Limit ## 8.592 21.408`

### Function *prmtnorm*

You have 2 quantiles of a normal distribution and the probability associated to (lower than) each of them.

You want to know the parameters of this distribution, so you are looking for * mean* and

*.*

*standard deviation*```
prmtnorm <- function(quantile1, p1, quantile2, p2) {
if (p1 <= 0 | p2 <= 0 | p1 >= 1 | p2 >= 1)
stop("Both p1 and p2 must belong to ]0,1[")
sdev <- (quantile1 - quantile2)/(qnorm(p1) - qnorm(p2))
if (sdev <= 0)
stop("Wrong input")
MEAN <- quantile1 - sdev * qnorm(p1)
output <- c(MEAN, sdev)
names(output) <- c("mean", "sd")
return(output)
}
```

Example:

If you know that, in a small town, the size of the 10 % of houses is lower than 150 m^{2} and the size of the 30 % of houses is higher than 250 m^{2}, you can easily obtain mean and standard deviation, assuming you are dealing with a normal distribution.

`prmtnorm(quantile1 = 150, p1 = 0.1, quantile2 = 250, p2 = 1 - 0.3)`

`## mean sd ## 220.96 55.37`

Advertisements

- Posted in: R ♦ rstats ♦ statistical software

## Recent Comments