-
Notifications
You must be signed in to change notification settings - Fork 75
/
exercise_script_with_solutions.R
549 lines (327 loc) · 14.1 KB
/
exercise_script_with_solutions.R
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
###############################################################################################################
# This R script contains #
# - the examples shown in the step-by-step workshop pages #
# (for you to run line-by-line to observe the outcome, or to modify and play with) #
# - space called 'YOUR TURN' for you to write your own code to answer the exercises from the workshop pages #
# - a possible solution to those exercises #
###############################################################################################################
#~~~~~~~~~ Random Numbers Generators and sampling theory -----
# sample
## x is a sequence
x <- 1:10
x
?sample # default: replace = FALSE
sample(x)
sample(x, replace = TRUE)
sample(letters, size = 10)
sample(x, size = 100, replace = TRUE)
## x is a vector of combined values
x <- c(1,5,8)
x
sample(x, size = 6, replace = TRUE)
# YOUR TURN: generate random numbers
## Sample 100 values between 3 and 103 with replacement
### possible solutions ###
x <- 3:103
sample(x, 100, replace = TRUE)
##########################
# random number generator drawing from specific distributions
?runif # runif(n, min, max)
?rpois # rpois(n, lambda)
?rnorm # rnorm(n, mean, sd)
?rbinom # rbinom(n, prob)
# YOUR TURN: generate random numbers
## Draw 100 values from a normal distribution with a mean of 0 and a sd of 1
### possible solutions ###
rnorm(n = 100, mean = 0, sd = 1)
rnorm(100) # if you sample from a normal distribution with a mean of 0 and a sd of 1, you do not need to provide them, they are the defaults
rnorm(100,0,1) # you do not need to label the arguments if you provide them in their default order, but you should provide names if they overwrite default values
##########################
## Draw 50 values from a normal distribution with a mean of 10 and sd of 5
### possible solutions ###
rnorm(sd = 5, mean = 10, n = 50) # if you label your arguments you can put them in whatever order you want!
rnrom(50, mean = 10, sd = 5) # using the rules above: non-default arguments in order, followed by named arguments using names
##########################
## Draw 1000 values from a poisson distribution with a lambda of 50
### possible solutions ###
rpois(n = 1000, lambda = 50)
rpois(1000,50)
##########################
## Draw 30 values from a uniform distribution between 0 and 10
### possible solutions ###
runif(n = 30, min = 0, max = 10)
runif(30, max = 10) # we can omit min = 0 because that's the default
##########################
# repeat
?replicate # replicate(n, expression)
rnorm(10)
mean(rnorm(10))
replicate(10,rnorm(10))
replicate(10, mean(rnorm(100)))
hist(replicate(10, mean(rnorm(100))))
# YOUR TURN: generate random numbers, repeat, and plot
## Replicate 1000 times the mean of 10 values drawn from a uniform distribution between 0 and 10
### possible solutions ###
replicate(1000, mean(runif(10, max = 10)))
hist(replicate(1000, mean(runif(10, max = 10))))
##########################
## Replicate 100 times the mean of 50 values drawn from a normal distribution of mean 10 and standard deviation 5
### possible solutions ###
replicate(100, mean(rnorm(50, mean = 10, sd = 5)))
hist(replicate(100, mean(rnorm(50, mean = 10, sd = 5))))
##########################
# set seed
hist(replicate(100, mean(rnorm(10))))
hist(replicate(100, mean(rnorm(10))))
hist(replicate(100, mean(rnorm(10))))
set.seed(10)
hist(replicate(100, mean(rnorm(10))))
set.seed(10)
hist(replicate(100, mean(rnorm(10))))
# defining sample size within a replication (n) and the number of simulation/repeats/replication (nrep)
## single random sample of normal distribution N(0,1) with n = 10
set.seed(10)
x <- rnorm(10) # mean = 0, sd = 1 are the defaults
hist(x, breaks = 10, col = "grey", xlim = c(-4,4))
abline(v = 0, col = "red", lty = 2, lwd = 2)
abline(v = mean(x), col = "blue", lwd = 2)
par(xpd = TRUE) # turn off clipping of legend
# where a function has a long list of arguments, we can put them on a new line each
legend(
0.9,
y = 1.5,
legend = c("mean(x)", "0"),
lty = c(1, 2),
col = c("blue","red")
)
## 24 sims of same distribution N(0,1) with n = 10
set.seed(10)
x24 <- replicate(24, rnorm(10))
par(mfrow = c(3,8), mar = c(0,0,0,0))
# apply is complicated because it takes a function as one of its arguments
x24Plot <- apply(
x24,
2,
function(x) {
# for fairly simple functions with lots of arguments, we sometimes just cram them all on the same line.
# it's not great practice but it stops the script getting super long when dealing with graphical objects
hist(x, col = "grey", xlim = c(-5,5), ylim = c(0,7), breaks = c(-5:5),
main = "", ylab = "", xlab = "", xaxt = "n", yaxt = "n")
abline(v = mean(x), col = "blue", lwd = 2)
abline(v = 0, col = "red", lty = 2, lwd = 2)
}
)
## distribution of means and sd from 24 sims N(0,1) with n = 10
par(mfrow = c(1,2), mar = c(5,5,1,1))
hist(apply(x24, 2, mean), main = "Mean", col = "grey", xlim = c(-1,1))
abline(v = 0, col = "red", lty = 2, lwd = 2)
hist(apply(x24, 2, sd), main = "SD",col = "grey", xlim = c(0.6,1.4))
abline(v = 1, col = "red", lty = 2, lwd = 2)
## 24 sims of same distribution N(0,1) with n = 1000
set.seed(10)
x24b <- replicate(24, rnorm(1000))
par(mfrow = c(3,8), mar = c(0,0,0,0))
x24bPlot <- apply(
x24b,
2,
function(x){
hist(x, col = "grey", xlim = c(-5,5), ylim = c(0,500), breaks = c(-5:5),
main = "", ylab = "", xlab = "", xaxt = "n", yaxt = "n")
abline(v = mean(x), col = "blue", lwd = 2)
abline(v = 0, col = "red", lty = 2, lwd = 2)
}
)
## distribution of means and SDs from 24 sims N(0,1) with n = 1000
par(mfrow = c(1,2), mar = c(5,5,1,1))
hist(apply(x24b, 2, mean), main = "Mean",col = "grey", xlim = c(-1,1))
abline(v = 0, col = "red", lty = 2, lwd = 2)
hist(apply(x24b, 2, sd), main = "SD",col = "grey", xlim = c(0.6,1.4))
abline(v = 1, col = "red", lty = 2, lwd = 2)
## distribution of means and SDs from 1000 sims N(0,1) with n = 10
set.seed(10)
x1000 <- replicate(1000, rnorm(10))
par(mfrow = c(1,2), mar = c(5,5,1,1))
hist(apply(x1000, 2, mean), main = "Mean",col = "grey")
abline(v = 0, col = "red", lty = 2, lwd = 2)
hist(apply(x1000, 2, sd), main = "SD",col = "grey")
abline(v = 1, col = "red", lty = 2, lwd = 2)
#~~~~~~~~~ Functions -----
# writing a function
## function syntax:
## AwesomeFunctionName <- function(argument1, argument2,…){
## do stuff here
## }
## The last thing that appears in the 'do stuff here' section is the function's
## "return value"
# YOUR TURN: write a function that takes input "nrep", replicates '(mean(rnorm(100)))'
# nrep times, and draws a histogram of the results
### possible solutions ###
#### step 1: the action
mean(rnorm(100))
#### step 2: replicate the action 1000 times
replicate(1000, mean(rnorm(100)))
#### step 3: plot the outcome of those simulations
hist(replicate(1000, mean(rnorm(100))))
#### step 4: replicate the action nrep time, with nrep defined outside the function
nrep <- 1000
replicate(nrep, mean(rnorm(100)))
#### step 5: wrap it in a function:
histrnorm100 <- function(nrep){
hist(replicate(nrep, mean(rnorm(100))))
}
#### step 6: check that the function works
histrnorm100(9)
histrnorm100(1000)
##########################
# YOUR TURN: modify your function
## to draw a histogram of nrep mean(rnorm(n)), where n is another input
### possible solutions ###
#### step 4: define parameters outside the function
nrep <- 100
n <- 10
replicate(nrep, mean(rnorm(n)))
#### step 5: wrap the action in a function:
histrnorm_n <- function(nrep, n){
hist(replicate(nrep, mean(rnorm(n))))
}
#### step 6: check that the function works
histrnorm_n(10,10)
histrnorm_n(10,100)
histrnorm_n(100,100)
histrnorm_n(1000,100)
##########################
#~~~~~~~~~ Simulating no effect and check alpha -----
# YOUR TURN: draw from the same normal distribution twice
## and see if the sample differ from each other
## will they differ significantly in 5% of the nrep?
### Figure out how to do a t.test in R
### Generate two vectors of 10 values drawn from N(0,1) and compare them with a t test
### Figure out how to extract the p-value from that object (HINT use `str` or `names`)
### Write a function simT that generates two vectors of n random normals, compare them with a t test and return the p-value
### Repeat with nrep = 20 and draw a histogram for n = 10
### Repeat with nrep = 100 and draw a histogram for n = 10
### possible solutions ###
#### Figure out how to do a t.test in R
?t.test
#### Generate two vectors of 10 N(0,1)
x1 <- rnorm(10,0,1)
x2 <- rnorm(10,0,1)
#### Compare them with a t test
t.test(x1,x2)
#### extract p value
str(t.test(x1,x2))
t.test(x1,x2)$p.value
#### write function
simT <- function(n){
x1 <- rnorm(n,0,1)
x2 <- rnorm(n,0,1)
t.test(x1, x2)$p.value
}
#### test function
simT(50)
#### repeat function for n = 10 and for different nrep and plot
par(mfrow = c(1,2))
simTRep <- replicate(20, simT(10))
hist(simTRep, breaks = 21, col = c('red',rep('grey',20)),
main = "nrep = 20, n = 10", xlab = "pvalue")
simTRep2 <- replicate(100, simT(10))
hist(simTRep2, breaks = 21, col = c('red',rep('grey',20)),
main = "nrep = 100, n = 10", xlab = "pvalue")
##########################
#### repeat function for nrep = 1000 and various n
par(mfrow = c(1,2))
simTRep <- replicate(1000, simT(10))
hist(simTRep, breaks = 21, col = c('red',rep('grey',20)),
main = "nrep = 1000, n = 10", xlab = "pvalue")
simTRep2 <- replicate(1000, simT(100))
hist(simTRep2, breaks = 21, col = c('red',rep('grey',20)),
main = "nrep = 1000, n = 100", xlab = "pvalue")
#~~~~~~~~~ Simulating an effect and check power -----
# we can calculate the power of a t.test for a given sample size using:
power.t.test(n = NULL, delta = 0.5, sd = 1, sig.level = 0.05, power = 0.8)
# the required sample size is 64 per group.
# YOUR TURN: Use your simulation skills to work out the power of a t-test for a given sample size through simulation.
## Write a function which:
### 1. Draws n values from a random normal distribution with mean 1, and another n values from a normal distribution with mean 2
### 2. Compares the means of these two samples with a t.test and extracts the p.value
## Then, use that function to replicate the function 1000 times using the parameters used in the power calculation above (that used the power.t.test function).
## Calculate the proportion of p-values that are <0.05
### possible solution ###
#### write new function
simT2 <- function(n, m1, m2) {
##### n is sample size per group, m1 is mean of group 1, m2 is mean of group 2
x1 <- rnorm(n, m1)
x2 <- rnorm(n, m2)
t.test(x1, x2)$p.value
}
##### repeat the function 1000 times
##### note that we are using a difference of 0.5 between means to match the "delta"
##### used in the power calculation
set.seed(100)
p <- replicate(1000, simT2(n = 64, m1 = 0, m2 = 0.5))
#### plot the results
par(mfrow = c(1,1))
hist(p, breaks = 21, col = c('red',rep('grey',20)),
main = "nrep = 1000, n = 64, delta = 0.5", xlab = 'pvalue')
#### calculate the proportion "significant"
prop.table(table(p < 0.05))
##########################
#### power is the probability that the test correctly rejects the null. Since we
#### know the population paramaters (as we set them in our simulation), we know
#### that there really is a difference, and the null should be rejected. The power
#### is therefore the proportion of p.values <0.05
# compare that to calculating the "power" parameter using the function below with all the other parameters provided (including n)
power.t.test(n = 64, delta = 0.5, sd = 1) # the results are similar
#~~~~~~~~~ Simulating for a preregistration -----
# YOUR TURN:
## Try to make a dataset that looks like this, using the
## functions `data.frame()`, `sample()`, and `rnorm()`
# smoking_status lung_cancer sex age
# 1 Yes No M 12.67918
# 2 Yes Yes F 23.71397
# 3 No No M 28.87786
# 4 Yes No F 28.99327
# 5 Yes Yes F 30.41415
# 6 No No M 44.60615
#### possible solution ###
set.seed(1234)
N <- 10 # size of your dataset
yes_no <- c("Yes", "No") # some options for the outcomes in the data
# the dataframe() function let's us create a data frame
# before the " = " is the column name. After " = " is the contents of the columm
df <- data.frame(
smoking_status = sample(yes_no, size = N, replace = T),
lung_cancer = sample(yes_no, size = N, replace = T),
sex = sample(c("M", "F"), size = N, replace = T),
age = rnorm(N, 30, sd = 10)
)
# run the parts after the " = " alone to make sure you understand what's going on
sample(c("M", "F"), size = N, replace = T)
head(df)
##########################
# YOUR TURN:
## Run a logistic regression on the data with lung cancer as the outcome and
## adjusting for the other variables.
## You could try something like:
## glm(lung_cancer ~ smoking_status, sex, age, family = binomial(link = "logit"), data = df)
## Why doesn't it work? Try to trouble shoot and get the code to work!
## HINT: are the variables the correct data type?
## HINT: once the model works, use summary() to look at the results
#### possible solution ###
# This mysterious error message appears because the categorical variables
# need to be factors for the model to run. So we are already learning
# something about how the data need to be that you might not have known
# before trying to run the code on a simulated dataset.
# You can convert the relevant variables to factors
df[1:3] <- lapply(df[1:3], as.factor)
# You could also do this one variable at a time:
# df$smoking_status <- as.factor(df$smoking_status)
# Now we can rerun the model:
m1 <- glm(
lung_cancer ~ smoking_status + sex + age,
family = binomial(link = "logit"),
data = df
)
# and look at the results
summary(m1)
##########################