Introduction

This Markdown shows examples of using built-in functions in R to answer questions about normally distributed data.

Question (example)

Adult male height (X) follows (approximately) a normal distribution with a mean of 69 inches and a standard deviation of 2.8 inches.

Define parameters:

pop.mean=69
pop.stdev=2.8
test.height=65

First, to understand the problem, I drew a normal curve by hand. I took a picture and saved the image in the same folder as my .Rproj file and my .Rmd (Markdown) document. Here is what I drew:


Next, I drew the plot using R plotting functions. This helped me sanity check my code.

curve(dnorm(x,mean=pop.mean,sd=pop.stdev),
        xlim=c(pop.mean-3*pop.stdev,
               pop.mean+3*pop.stdev),
      ylab="Density",xlab="Height (inches)") 
cord.x=c(pop.mean-3*pop.stdev,
        seq(pop.mean-3*pop.stdev,test.height,0.01),
        test.height) 
cord.y=c(0,
         dnorm(seq(pop.mean-3*pop.stdev,test.height,0.01),
               mean=pop.mean,
               sd=pop.stdev),
         0)
abline(v=pop.mean,col="black")
polygon(cord.x,cord.y,col='skyblue')

The blue are in the plot above represents the fraction of men who are shorter than 65. This is what I want to find.

To find the answer to the question, I used pnorm, which calculates the area to left of the line I drew at test.height.

fraction=pnorm(test.height,sd=pop.stdev,mean=pop.mean)
percentage=100*fraction
rounded_answer=round(percentage,1)

Approximately 7.7% of men are shorter than 65 inches.

To check my answer, I try the inverse function qnorm, which finds the value immediately to right of a given area. If qnorm returns test.height or close to it, I’ll know I used pnorm correctly.

inverse_result=qnorm(fraction,sd=pop.stdev,mean=pop.mean)

The result I got from the inverse function is 65, which is the same as my test height, which was 65.

Conclusion

This Markdown shows how you can: