-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path05-statistical_inference.R
More file actions
172 lines (152 loc) · 6.79 KB
/
05-statistical_inference.R
File metadata and controls
172 lines (152 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# The following code is taken from the fifth chapter of the online script, which provides more detailed explanations:
# https://imsmwu.github.io/MRDA2020/introduction-to-statistical-inference.html
#-------------------------------------------------------------------#
#---------------------Install missing packages----------------------#
#-------------------------------------------------------------------#
req_pacakges <- c("ggplot2","psych","tidyverse","ggplot2")
req_pacakges <- req_pacakges[!req_pacakges %in% installed.packages()]
lapply(req_pacakges, install.packages)
# Useful options setting that prevents R from using scientific notation on numeric values
options(scipen = 999, digits = 4)
#-------------------------------------------------------------------#
#-----------------Sampling from a known population------------------#
#-------------------------------------------------------------------#
# Generate population data
## ------------------------------------------------------------------------
set.seed(321)
music_listening_population <- data.frame(hours = rnorm(n = 25000, mean = 50, sd = 10))
# Compute descriptives for population
psych::describe(music_listening_population$hours)
# Create histogram
library(ggplot2)
ggplot(music_listening_population) +
geom_histogram(aes(hours), bins = 50, fill = 'white', color = 'black') +
geom_vline(xintercept = mean(music_listening_population$hours),size=1) +
labs(title = "Histogram of listening times in population",
y = 'Number of students',
x = 'Hours') +
theme_bw()
# Take a random sample of 100 students from the population
## ------------------------------------------------------------------------
n <- 100 #sample size
student_sample <- sample(1:25000, size = n, replace = FALSE)
music_listening_sample <- data.frame(hours = music_listening_population[student_sample,"hours"])
# Compute descriptives for sample
psych::describe(music_listening_sample)
# Create histogram
ggplot(music_listening_sample) +
geom_histogram(aes(x = hours), bins = 30, fill='white', color='black') +
geom_vline(xintercept = mean(music_listening_population$hours),size=1,color="red") +
geom_vline(xintercept = mean(music_listening_sample$hours),size=1) +
labs(title = "Histogram of listening times in sample",
y = 'Number of students',
x = 'Hours') +
theme_bw()
# Repeated sampling and the sampling distribution
## ------------------------------------------------------------------------
set.seed(12345)
samples <- 20000 #number of samples
n <- 100 #set sample size
means <- matrix(NA, nrow = samples)
for (i in 1:samples){
student_sample <- sample(1:25000, size = n, replace = FALSE)
means[i,] <- mean(music_listening_population[student_sample,"hours"])
}
means <- data.frame(means = means)
# Compute mean and standard error
mean(means$means) #mean
sd(means$means) #standard error of the mean
# Create histogram
ggplot(means) +
geom_histogram(aes(x = means), bins = 30, fill='white', color='black') +
labs(title = "Histogram of mean listening times (sampling distribution)",
y = 'Number of samples',
x = 'Hours') +
theme_bw()
#-------------------------------------------------------------------#
#-----------------------Central limit theorem-----------------------#
#-------------------------------------------------------------------#
# Generate population data
## ------------------------------------------------------------------------
set.seed(321)
music_listening_population_2 <- data.frame(hours = rgamma(25000, shape = 2, scale = 10))
# Compute descriptives for population
psych::describe(music_listening_population_2)
# Create histogram
ggplot(music_listening_population_2) +
geom_histogram(aes(hours), bins = 50, fill = 'white', color = 'black') +
geom_vline(xintercept = mean(music_listening_population_2$hours),size=1) +
labs(title = "Histogram of listening times in population",
y = 'Number of students',
x = 'Hours') +
theme_bw()
# Take a random sample of 100 students from the population
## ------------------------------------------------------------------------
n <- 100 #sample size
student_sample <- sample(1:25000, size = n, replace = FALSE)
music_listening_sample <- data.frame(hours = music_listening_population_2[student_sample,"hours"])
# Compute descriptives for sample
psych::describe(music_listening_sample)
# Create histogram
ggplot(music_listening_sample) +
geom_histogram(aes(x = hours), bins = 30, fill='white', color='black') +
geom_vline(xintercept = mean(music_listening_population_2$hours),size=1,color="red") +
geom_vline(xintercept = mean(music_listening_sample$hours),size=1) +
labs(title = "Histogram of listening times in sample",
y = 'Number of students',
x = 'Hours') +
theme_bw()
# Repeated sampling and the sampling distribution
## ------------------------------------------------------------------------
set.seed(12345)
samples <- 20000 #number of samples
n <- 100 #set sample size
means <- matrix(NA, nrow = samples)
for (i in 1:samples){
student_sample <- sample(1:25000, size = n, replace = FALSE)
means[i,] <- mean(music_listening_population_2[student_sample,"hours"])
}
means <- data.frame(means = means)
# Compute mean and standard error
mean(means$means) #mean
sd(means$means) #standard error of the mean
sd(music_listening_population_2$hours)/sqrt(n) #also the standard error of the mean
# Create histogram
ggplot(means) +
geom_histogram(aes(x = means), bins = 30, fill='white', color='black') +
labs(title = "Histogram of mean listening times (sampling distribution)",
y = 'Number of samples',
x = 'Hours') +
theme_bw()
#-------------------------------------------------------------------#
#-----------------------Confidence intervals------------------------#
#-------------------------------------------------------------------#
# Draw random sample
## ------------------------------------------------------------------------
set.seed(6789)
n <- 100
student_sample <- sample(1:25000, size = n, replace = FALSE)
music_listening_sample <- data.frame(hours = music_listening_population_2[student_sample,"hours"])
# Compute descriptives for sample
psych::describe(music_listening_sample)
# Create histogram
ggplot(music_listening_sample) +
geom_histogram(aes(x = hours), bins = 30, fill='white', color='black') +
labs(title = "Histogram of listening times in sample",
y = 'Number of students',
x = 'Hours') +
theme_bw()
# Compute mean, critical z-score, and standard error
## ------------------------------------------------------------------------
mean <- mean(music_listening_sample$hours)
mean
se <- sd(music_listening_sample$hours)/sqrt(n)
se
z_crit <- qnorm(0.975)
z_crit
# Compute 95% confidence interval
## ------------------------------------------------------------------------
ci_lower <- mean - z_crit*se
ci_upper <- mean + z_crit*se
ci_lower
ci_upper