TU Wien:Statistik und Wahrscheinlichkeitstheorie UE (Bura)/Übungen 2020W/HW09.6

Aus VoWi
Zur Navigation springen Zur Suche springen

Simulation of test-power[Bearbeiten | Quelltext bearbeiten]

Simulatethetest-powerinthetwo-samplet-test:LetX1,...,Xn,Y1,...,Yn beindependent random variables with Xi ∼ N(0,σ2) and Yi ∼ N(d,σ2) for all i = 1,2,...,n. Let the null hypothesis be H0 : d = 0 and the significance level α = 5%. Simulate the test-power (relative frequency of rejections) for d ∈ {−5, −4.5, −4, . . . , 5} in 1000 simulations each. Use the parameters

  • (a) n=10 and σ=3
  • (b) n=20 and σ=3
  • (c) n=20 and σ=1

for each of which you plot the testpower against d. Comment on your graphic. Hint: You can access the p-value with t.test()$p.value.

Lösungsvorschlag von Friday[Bearbeiten | Quelltext bearbeiten]

--Friday Sa 30 Jan 2021 17:38:50 CET

# Statistics and Probability: HW #9
# Friday 
# Duedate: 07.12.2020
library(rlist)

(par(mfrow=c(3,1)))
create_plot <- function(n, sig, text) {
  d = 1
  alpha = 0.05
  x <- -5:5
  y <- replicate(11, 0)
  
  for (d in x) {
    cnt <- 0
    for (i in 1:1000) {
      d1 <- rnorm(n, 0, sig)
      d2 <- rnorm(n, d, sig)
      p <- t.test(d1,d2)$p.value
      if (p < alpha) {
        cnt <- cnt + 1
      }
    }
    y[d+6] <- cnt/1000
  }
  plot(x,y, main=text, xlab="d value", ylab="testpower")
}

create_plot(10, 3, "6a")
create_plot(20, 3, "6b")
create_plot(20, 1, "6c")

# Of course we almost never reject if d=0 because in that case both datasets 
# should have almost the same mean.
# The farther the means of the both sets diverge the more likly we reject the 
# hypothesis.
# If the sigma gets smaller, the rejection is much more sensitiv to changes in
# the means.