-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCsims.R
71 lines (69 loc) · 1.28 KB
/
MCsims.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
set.seed(1)
size = 5;
t = vector('numeric')
for (i in 1:1000) {
s <- rnorm(size)
t[i] = sqrt(size) * mean(s) / sd(s)
}
mean(t>2)
# EXE 3
B = 1000;
ps = seq(1/(B+1), 1-1/(B+1), len=B)
theoreticals <- qt(ps, df = size-1)
qqplot(t, theoreticals)
abline(0,1)
# Ans
library(rafalib)
mypar(3,2)
Ns<-seq(5,30,5)
B <- 1000
mypar(3,2)
LIM <- c(-4.5,4.5)
for(N in Ns){
ts <- replicate(B, {
X <- rnorm(N)
sqrt(N)*mean(X)/sd(X)
})
ps <- seq(1/(B+1),1-1/(B+1),len=B)
qqplot(qt(ps,df=N-1),ts,main=N,
xlab="Theoretical",ylab="Observed",
xlim=LIM, ylim=LIM)
abline(0,1)
}
# EXE 4
Ns<-seq(5,30,5)
B <- 1000
mypar(3,2)
LIM <- c(-4.5,4.5)
for(N in Ns){
ts <- replicate(B,{
x <- rnorm(N)
y <- rnorm(N)
t.test(x,y, var.equal = TRUE)$stat
})
ps <- seq(1/(B+1),1-1/(B+1),len=B)
qqplot(qt(ps,df=2*N-2),ts,main=N,
xlab="Theoretical",ylab="Observed",
xlim=LIM, ylim=LIM)
abline(0,1)
}
# EXE 6
N <- 1000
B<- 1000
ts <- replicate(B, {
X <- sample(c(-1,1), N, replace = TRUE)
sqrt(N) * mean(X) / sd(X)
})
ps=seq(1/(B+1), 1-1/(B+1), len=B)
qqplot(qt(ps,N-1), ts, xlim=range(ts))
abline(0,1)
# EXE 7
N <- seq(5,30,5)
B <- 1000
exacts <- replicate(B, {
m <- median(rnorm(N))
sqrt(N) * m / sd()
})
theoretical <- replicate(B, {
rnorm(0, 1/sqrt(N))
})