# Enter data x <- c(1.2, 2.4, 1.3, 1.3, 0.9, 1.0, 1.8, 0.8, 4.6, 1.4) ## T-test n <- length(x) # sample size # Compute 'tobs' - the observed test statistic tobs <- (mean(x) - 0) / (sd(x) / sqrt(n)) # Compute the p-value as a tail-probability # in the relevant t-distribution: 2 * (1 - pt(abs(tobs), df = n-1)) ## Den nemme måde: t.test(x, mu = 0) t.test(x) t.test(x, mu = 2) ## her testes H_0: mu = 2. ### Konfidensinterval t.test(x, conf.level = 0.95) ### Højde af studerende x <- c(168,161,167,179,184,166,198,187,191,179) t.test(x, conf.level = 0.95) ## tester H_0 : mu = 0 t.test(x, mu = 180, conf.level = 0.95) ## tester H_0 : mu = 180 #### Simulation N = 10 ## antal obs. K = 500 ## antal gentagelser mu <- 4 sigma <- 3 cis <- numeric(K) for (i in 1:K) cis[i] <- t.test(rnorm(N, mu, sigma), mu = 4)\$p.val ## H0 er sand, altså er p-værdien tilfældigt mellem 0 og 1. hist(cis) mean(cis < 0.05) # På 5% sig. niveau afvises ca. 5% ## Samme, men med t-fordeling og kritisk værdi tv <- numeric(K) for (i in 1:K) tv[i] <- t.test(rnorm(N, mu, sigma), mu = 4)\$statistic ## t-værdi hist(tv) qt(0.975, df = 9) # kritisk værdi for testet mean( abs(tv) > qt(0.975, df = 9)) # Studenterhøjder x <- c(168,161,167,179,184,166,198,187,191,179) xr <- rnorm(100, mean(x), sd(x)) hist(xr, xlab = "Height", main = "", freq = FALSE, ylim = c(0, 0.032)) lines(seq(130, 230, 1), dnorm(seq(130, 230, 1), mean(x), sd(x))) ### QQ-plot qqnorm(x) qqline(x) ## xr <- rnorm(1000, mean(x), sd(x)) qqnorm(xr) qqline(xr) ## Reading in the data radon <- c(2.4, 4.2, 1.8, 2.5, 5.4, 2.2, 4.0, 1.1, 1.5, 5.4, 6.3, 1.9, 1.7, 1.1, 6.6, 3.1, 2.3, 1.4, 2.9, 2.9) ## Histogram and q-q plot of data par(mfrow = c(1,2)) hist(radon) qqnorm(radon) qqline(radon) xr <- rnorm(20) qqnorm(xr) qqline(xr)