From 11f948c114dfbe9f23eb29baa05a173fc36fb540 Mon Sep 17 00:00:00 2001 From: Jakub Adamski Date: Mon, 9 May 2022 12:01:06 +0200 Subject: [PATCH] t-test single --- Projekt_1/T-student-single.R | 26 ++++++++++++++++++++++++++ Projekt_1/T-studnet-multiple.R | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Projekt_1/T-student-single.R create mode 100644 Projekt_1/T-studnet-multiple.R diff --git a/Projekt_1/T-student-single.R b/Projekt_1/T-student-single.R new file mode 100644 index 0000000..50af074 --- /dev/null +++ b/Projekt_1/T-student-single.R @@ -0,0 +1,26 @@ +t.test_single <- function(x, m, alternative = "two.sided") { + M <- mean(x) + n <- length(x) + statistic <- ((M - m) / sd(x)) * sqrt(n) + + p <- if (alternative == "two.sided") { + 2 * pt(q=abs(statistic), df=length(x)-1, lower.tail=FALSE) + } else if (alternative == "less") { + pt(q=statistic, df=length(x)-1, lower.tail = TRUE) + } else { + pt(q=statistic, df=length(x)-1, lower.tail = FALSE) + } + + value <- list(mean = M, m = m, statistic = statistic, p.value = p, alternative = alternative) + return(value) +} + + +load("Hamulce.RData") +attach(Hamulce) +t.test(Wynik,mu=18.6,alternative='less') +t.test_single(Wynik, m=18.6, alternative='less') + + + + diff --git a/Projekt_1/T-studnet-multiple.R b/Projekt_1/T-studnet-multiple.R new file mode 100644 index 0000000..1d062c3 --- /dev/null +++ b/Projekt_1/T-studnet-multiple.R @@ -0,0 +1,26 @@ +t.test_knownvar <- function(x, y, V1, V2, m0 = 0, alpha = 0.05, alternative = "two.sided") { + M1 <- mean(x) + M2 <- mean(y) + n1 <- length(x) + n2 <- length(y) + sigma1 <- sqrt(V1) + sigma2 <- sqrt(V2) + S <- sqrt((V1 / n1) + (V2 / n2)) + statistic <- (M1 - M2 - m0) / S + p <- if (alternative == "two.sided") { + 2 * pnorm(abs(statistic), lower.tail = FALSE) + } else if (alternative == "less") { + pnorm(statistic, lower.tail = TRUE) + } else { + pnorm(statistic, lower.tail = FALSE) + } + LCL <- (M1 - M2 - S * qnorm(1 - alpha / 2)) + UCL <- (M1 - M2 + S * qnorm(1 - alpha / 2)) + value <- list(mean1 = M1, mean2 = M2, m0 = m0, sigma1 = sigma1, sigma2 = sigma2, S = S, statistic = statistic, p.value = p, LCL = LCL, UCL = UCL, alternative = alternative) + # print(sprintf("P-value = %g",p)) + # print(sprintf("Lower %.2f%% Confidence Limit = %g", + # alpha, LCL)) + # print(sprintf("Upper %.2f%% Confidence Limit = %g", + # alpha, UCL)) + return(value) +} \ No newline at end of file