-
Notifications
You must be signed in to change notification settings - Fork 0
/
seminar02c.rmd
121 lines (97 loc) · 3.25 KB
/
seminar02c.rmd
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
---
title: "seminar02c"
author: "Ali"
date: "Thursday, January 22, 2015"
output:
html_document:
keep_md: yes
---
---
title: "seminar02c"
author: "Ali"
date: "Thursday, January 22, 2015"
output: html_document
---
Exercise:
Generate some data from a different normal distribution, i.e. where the mean is not 0 and/or the standard deviation is not 1. Make the new parameter values extreme, so the data will be obviously different.
```{r}
rnorm(n = 10, sd = 1, mean = 10)
n <- 5
B <- c(5)
x <- matrix(rnorm(n * B, sd = 1, mean = 10), nrow = n)
rownames(x) <- sprintf("obs%02d", 1:n)
colnames(x) <- sprintf("samp%02d", 1:B)
x
```
Exercise:
"Repeat the above for a different distribution."
```{r}
set.seed(10)
rnorm(n = 10, sd = 1, mean = 10)
n <- 5
B <- c(5)
x <- matrix(rnorm(n * B, sd = 1, mean = 10), nrow = n)
rownames(x) <- sprintf("obs%02d", 1:n)
colnames(x) <- sprintf("samp%02d", 1:B)
x
```
Exercise:
Recall the claim that the expected value of the sample mean is the true mean. Compute the average of the 4 sample means we have. Is it (sort of) close the true mean? Feel free to change n or B at any point.
```{r}
means <- colMeans(x)
mean(means)
#answer: yes, close to 10!
```
Exercise:
Recall the Weak Law of Large Numbers said that, as the sample size gets bigger, the distribution of the sample means gets more concentrated around the true mean.
```{r}
#had to look at the source code for this one
B <- 1000
x10 <- matrix(rnorm(10 * B), nrow = 10)
x100 <- matrix(rnorm(100 * B), nrow = 100)
x1000 <- matrix(rnorm(1000 * B), nrow = 1000)
x10000 <- matrix(rnorm(10000 * B), nrow = 10000)
xBar10 <- colMeans(x10)
xBar100 <- colMeans(x100)
xBar1000 <- colMeans(x1000)
xBar10000 <- colMeans(x10000)
xBarSd10 <- sd(colMeans(x10))
xBarSd100 <- sd(colMeans(x100))
xBarSd1000 <- sd(colMeans(x1000))
xBarSd10000 <- sd(colMeans(x10000))
cbind(sampSize = c(10, 100, 1000, 10000),
trueSEM = 1 / sqrt(c(10, 100, 1000, 10000)),
obsSEM = c(xBarSd10, xBarSd100, xBarSd1000, xBarSd10000))
```
Exercise:
Generate a reasonably large sample from some normal distribution (it need not be standard normal!). Pick a threshhold. What is the CDF at that threshhold, i.e. what's the true probability of seeing an observation less than or equal to the threshhold? Use your large sample to compute the observed proportion of observations that are less than threshhold. Are the two numbers sort of close? Hint: If x is a numeric vector, then mean(x <= threshhold) computes the proportion of values less than or equal to threshhold
```{r}
x <- rpois(10, lambda = 5)
ppois(6, lambda = 5)
mean(x <= 6)
#sort of close!
```
Exercise:
Do the same for a variety of sample sizes. Do the two numbers tend to be closer for larger samples?
```{r}
ppois(6, lambda = 5)
x <- rpois(10, lambda = 5)
mean(x <= 6)
x <- rpois(100, lambda = 5)
mean(x <= 6)
x <- rpois(1000, lambda = 5)
mean(x <= 6)
x <- rpois(100000000, lambda = 5)
mean(x <= 6)
#yes, the two numbers tend to be closer with higher n
```
Exercise:
Do the same for a different distribution.
Exercise:
Instead of focusing on tail probabilities, focus on the probability of the observed values falling in an interval.
```{r}
bin <- rbinom(10, size = 100, prob = 0.78)
range <- (mean(bin >= 78) - mean(bin >= 80))
range
pbinom(78, size = 100, prob = .78)
```