t-test single

This commit is contained in:
Jakub Adamski 2022-05-09 12:01:06 +02:00
parent 1896c3cc83
commit 11f948c114
2 changed files with 52 additions and 0 deletions

View File

@ -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')

View File

@ -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)
}