E. Plot the distribution in part B (3 if you stick close to class notes, or 1 if you venture out).

This example looks at the normal temperature of a human is known to be 98.6 degrees Fahrenheit. The code below creates a normal distribution with 98.6 being centered at the mean.

Using the density norm (dnorm) function to display the density of observations with a temperature between 99.5 and 100.5.

# Set the mean and standard deviation
mu    <- 98.6
sigma <- 1

# Generate a range of values around the mean
?seq
x <- seq(from       =  mu - 3*sigma,
         to         =  mu + 3*sigma, 
         length.out =  1000
         )

# Calculate the probability density function
pdf <- dnorm(x    = x, 
             mean = mu, 
             sd   = sigma
             )

# Plot the normal distribution

plot(x    = x, 
     y    = pdf,
     type = 'l', 
     col  = 'lightblue', 
     lwd  = 3, 
     xlab = 'Temperature', 
     ylab = 'Density',
     main = 'Normal Distribution with Tempertaure Mean 98.6 Degrees'
     )

abline(v = 98.6, col = 'darkblue', lty = 2)

# Shade the area under the curve between 99.5 and 100.5
x_shade <- seq(from       = 99.5, 
               to         = 100.5, 
               length.out = 100
               )

pdf_shade <- dnorm(x    = x_shade, 
                   mean = mu, 
                   sd   = sigma
                   )

polygon(x      =  c(x_shade, rev(x_shade)),
        y      =  c(pdf_shade, rep(x      = 0, 
                                   times  = length(pdf_shade)
                                  )
                   ), 
        col    = 'darkred', 
        border = NA
        )

Using the same example as before, you can plot a CDF using pnorm.

curve(pnorm(x, mean = mu, sd = sigma), 
      from = mu - 3*sigma,
      to = mu + 3*sigma,
      main = "CDF for Tempertaure Mean 98.6 Degrees",
      ylab = "F(x)")

II. Converge of Distributions:

TASK:

Let’s assume that a hospital’s neurosurgical team performed 150 procedures for in-brain bleeding last year. 20 of these procedures resulted in death within 30 days. If the national proportion for death in these cases is 12% then is there evidence to suggest that your hospital’s proportion of deaths is more extreme than the national proportion?

Binomial Example:

## BINOMIAL EXAMPLE ##

# Number of trials (procedures)
n <- 150

# Probability of success (death rate)
pi <- 0.12

# Generate values for x (number of successes)
x_values <- 0:150

# Calculate the probabilities for each value of x
probs <- dbinom(x = x_values,
           size = n, 
           prob = pi)

bionom <- sum(dbinom(20:150,150,.12))
print(bionom)
## [1] 0.3431109
## PLOTTING BINOMIAL ##
barplot(height = probs, 
        names.arg = x_values, 
        col = "blue", 
        main = "Binomial Distribution", 
        xlab = "Number of Successes", 
        ylab = "Probability"
        )

Poisson Example:

## POISSON EXAMPLE ##
n <- 150
x <- 20
range <- 0:150
pi <- .12
l <- n * pi

pois <- sum(dpois(x=20:150,lambda = l))
print(pois)
## [1] 0.3490839
## PLOTTING POISSON ##

values <- dpois(x = range, lambda = l)

# Plot
plot(x = 0:150, 
     y = values, 
     type = "h", 
     lwd = 2, 
     col = "blue",
     xlab = "Number of Events", 
     ylab = "Probability",
     main = "Poisson Distribution")

The results I get from implementing binomial model (.3431109) and Poisson model (0.3490839) are similar, but are not identical. The reason for this could be that the probability in the binomial distribution remains constant in each trial, compared to the Poisson model does not explicitly consider the probability of success.