Matma_AI_cyber/Projekt_1/T-student-multiple.R

50 lines
1.3 KiB
R
Raw Normal View History

2022-05-09 12:34:50 +02:00
t.test_multiple <- function(x, y, m0=0, alternative = "two.sided") {
2022-05-09 12:01:06 +02:00
M1 <- mean(x)
M2 <- mean(y)
n1 <- length(x)
n2 <- length(y)
2022-05-09 12:34:50 +02:00
S <- sqrt((var(x) / n1) + (var(y) / n2))
2022-05-09 12:01:06 +02:00
statistic <- (M1 - M2 - m0) / S
2022-05-09 12:34:50 +02:00
2022-05-09 12:01:06 +02:00
p <- if (alternative == "two.sided") {
2022-05-09 12:34:50 +02:00
2 * pt(q=abs(statistic), df=n1+n2-2, lower.tail=FALSE)
2022-05-09 12:01:06 +02:00
} else if (alternative == "less") {
2022-05-09 12:34:50 +02:00
pt(q=statistic, df=n1+n2-2, lower.tail = TRUE)
2022-05-09 12:01:06 +02:00
} else {
2022-05-09 12:34:50 +02:00
pt(q=statistic, df=n1+n2-2, lower.tail = FALSE)
2022-05-09 12:01:06 +02:00
}
2022-05-09 12:34:50 +02:00
value <- list(mean1 = M1, mean2 = M2, df=n1+n2-2, m0 = m0, statistic = statistic, p.value = p, alternative = alternative)
2022-05-09 12:01:06 +02:00
return(value)
2022-05-09 12:34:50 +02:00
}
2022-05-09 12:37:50 +02:00
# niezależne
load("Bledy.RData")
attach(Bledy)
2022-05-09 12:34:50 +02:00
2022-05-09 17:33:51 +02:00
size_Bledy <- length(Bledy$Blad)
2022-05-09 12:34:50 +02:00
2022-05-24 16:17:00 +02:00
wyniki <- data.frame()
for (i in 1:10){
bootstrappedData_Bledy_Blad <- ourBoot(Bledy$Blad, size_Bledy, size_Bledy)
bootstrappedData_Bledy_Kod <- ourBoot(Bledy$Kod, size_Bledy, size_Bledy)
ttest <- t.test_multiple(x=bootstrappedData_Bledy_Blad, y=bootstrappedData_Bledy_Kod, alternative='less')
wyniki <- rbind(wyniki, ttest)
}
2022-05-09 12:34:50 +02:00
2022-05-24 16:17:00 +02:00
mean(wyniki$mean1)
mean(wyniki$mean2)
mean(wyniki$statistic)
mean(wyniki$p.value)
2022-05-09 17:33:51 +02:00
#dane normalne
#funkcja wbudowana
2022-05-24 16:17:00 +02:00
#t.test(x=Blad, y=Kod,var.equal=T,alternative='less')
2022-05-09 17:33:51 +02:00
# nasza funkcja
2022-05-24 16:17:00 +02:00
#t.test_multiple(x=Blad, y=Kod, alternative='less')
2022-05-09 17:33:51 +02:00
2022-05-09 12:34:50 +02:00