Part 1: Simulation Exercise

Overview

In this project I investigate the exponential distribution and compare it with the Central Limit Theorem. In particular I show:

  • The mean and variance and overall distribution of the simulated means are very similar to their theoretical counterparts.

Simulations

set.seed(5)
lambda = 0.2
num_exp = 40

sim_means = NULL
for (i in 1 : 1000) sim_means = c(sim_means, mean(rexp(num_exp, lambda)))

Sample Mean versus Theoretical Mean

# Simulated sample mean
mean_sim_means = mean(sim_means)

# Theoretical mean (1/lambda = 1/0.2 = 5)
teo_sim_means = 1/lambda

# Histogram of simulated sample means
hist(sim_means)

# Difference between simulated sample mean and theoretical mean
abs(mean_sim_means-teo_sim_means)
## [1] 0.04305303

Sample mean variance versus Theoretical mean variance

# Simulated sample mean variance
mean_sim_mean_var = var(sim_means)

# Theoretical mean variance
teo_sim_mean_var = (lambda * sqrt(num_exp))^-2

# Difference between simulated sample variance and theoretical variance
abs(mean_sim_mean_var-teo_sim_mean_var)
## [1] 0.02239529

Distribution

library(ggplot2)
ggplot(data.frame(y=sim_means), aes(x=y)) + 
  geom_histogram(aes(y=..density..), binwidth=0.2) +
  stat_function(fun=dnorm, args=list(mean=lambda^-1, sd=(lambda*sqrt(num_exp))^-1),size= 2) +
  labs(title="Plot of the Simulations", x="Simulation Mean")

Part 2: Basic inferential data analysis

Overview

Basic descriptive analysis suggest that tooth length is higher as dosage increases. Differences by supp only appear to be significant when dosage equals 0.5 or 1 mg/day. Statistical inference based on t-tests support these claims.

library(datasets)
data(ToothGrowth)
str(ToothGrowth)
## 'data.frame':    60 obs. of  3 variables:
##  $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
##  $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
##  $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
head(ToothGrowth)
##    len supp dose
## 1  4.2   VC  0.5
## 2 11.5   VC  0.5
## 3  7.3   VC  0.5
## 4  5.8   VC  0.5
## 5  6.4   VC  0.5
## 6 10.0   VC  0.5
summary(ToothGrowth)
##       len        supp         dose      
##  Min.   : 4.20   OJ:30   Min.   :0.500  
##  1st Qu.:13.07   VC:30   1st Qu.:0.500  
##  Median :19.25           Median :1.000  
##  Mean   :18.81           Mean   :1.167  
##  3rd Qu.:25.27           3rd Qu.:2.000  
##  Max.   :33.90           Max.   :2.000
ggplot(ToothGrowth, aes(x=factor(dose), y=len)) + 
  geom_boxplot(aes(fill = supp)) +
  labs(title="Guinea pig tooth length by dosage for each type of supplement", 
    x="Dose (mg/day)",
    y="Tooth Length")

Confidence intervals to compare tooth growth by supp and dose.

# Comparison between tooth length by supp for low levels of dose -> No empirical support for the null hypothesis of equal means
t.test(len ~ supp, data = subset(ToothGrowth, dose != 2))$p.value
## [1] 0.00423861
# Comparison between tooth length by supp for high levels of dose -> We cannot reject the null hypothesis of equal means
t.test(len ~ supp, data = subset(ToothGrowth, dose == 2))$p.value
## [1] 0.9638516
# Comparison between tooth length for dosage equal to 0.5 and 1 -> No empirical support for the null hypothesis of equal means
t.test(len ~ factor(dose), data = subset(ToothGrowth, dose == 0.5 | dose == 1))$p.value
## [1] 1.268301e-07
# Comparison between tooth length for dosage equal to 1 and 2 -> No empirical support for the null hypothesis of equal means
t.test(len ~ factor(dose), data = subset(ToothGrowth, dose == 1 | dose == 2))$p.value
## [1] 1.90643e-05