Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Statistics / hw2.r
blob: 03daba86fde006e3e7cc073056bb8d88ac7319b3 (plain) (blame)
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
ex1 <- function(ns = 1:6 * 10) {
  check_null_hyp <- function(n, iters = 10000) {
    accepts <- sum(sapply(1:iters, function(i) {
      W1 <- rexp(n, 1)
      W2 <- rexp(n, 1)
      X <- W1 + W2
      return (shapiro.test(X)$p.value > 0.05)
    }))
    return (accepts / iters)
  }

  ys <- sapply(ns, check_null_hyp)
  
  plot(ns, ys, type="l", xlab="n", ylab="Съотношение на вярност на Шапиро-Уилк")
  text(ns + 1, ys + 0.01, ys)
}

ex1()

ex2 <- function(greens = 2, whites = 3, yellows = 5, ns = c(50, 100, 200, 500)) {
  balls <- append(append(rep("green", greens), rep("white", whites)), rep("yellow", yellows))
  
  check_hyp <- function(n, iters=10000) {
    accepts <- sum(sapply(1:iters, function(i) {
      sampled_greens <- sum(sapply(1:n, function(s) {
        return (sample(balls, 1, replace=TRUE) == "green")
      }))
      
      return (prop.test(sampled_greens, n, 1/5)$p.value > 0.05)
    }))
    
    return (accepts / iters)
  }
  
  ys <- sapply(ns, check_hyp)
  plot(ns, ys, type="l", xlab="n", ylab="Съотношение на вярност на заключението")
  text(ns, ys, ys)
}

ex2()