statistics/distribucion_normal.R

99 lines
3.3 KiB
R
Raw Normal View History

2022-09-03 18:54:56 -04:00
result <- dbSendQuery(conn, consulta_frames)
data <- fetch(result)
get_variance <- function(dataset = vector(), mean = 0) {
sum <- 0
for (item in dataset) {
sum <- sum + ((item - mean) ^ 2)
}
variance <- sum / length(dataset)
return(variance)
}
get_standard_deviation <- function(variance = 0) {
return(round(sqrt(variance)))
}
dnorm_plot <- function(filename = "dnorm", dataset = vector(), title = "Title", xlab = "X Label") {
mean <- round(mean(dataset))
standard_deviation <- get_standard_deviation(get_variance(dataset, mean))
possibility <- dnorm(dataset, mean, standard_deviation)
png(file = filename)
plot(dataset,
possibility,
xlab = xlab,
ylab = "Probability Density",
col = "blue",
main = title)
abline(v = c(mean, mean - standard_deviation, mean + standard_deviation),
col = c("green", "red", "red"),
lty = c(2, 2, 2))
legend("topright",
legend = c(round(mean, 2), standard_deviation),
col = c("green", "red"),
lty = 2,
lwd = 1,
box.lty = 1)
}
pnorm_less <- function(dataset = vector(), value = 0, word = "") {
mean <- mean(dataset)
standard_deviation <- get_standard_deviation(get_variance(dataset, mean))
probability_text <- "The probability of getting less than"
probability_text <- paste(probability_text, value)
probability_text <- paste(probability_text, word)
probability_text <- paste(probability_text, "is")
probability_text <- paste(probability_text, round(pnorm(value, mean, standard_deviation) * 100, 2))
probability_text <- paste(probability_text, "%", sep = "")
print(probability_text)
}
pnorm_more <- function(dataset = vector(), value = 0, word = "") {
mean <- mean(dataset)
standard_deviation <- get_standard_deviation(get_variance(dataset, mean))
probability_text <- "The probability of getting more than"
probability_text <- paste(probability_text, value)
probability_text <- paste(probability_text, word)
probability_text <- paste(probability_text, "is")
probability_text <- paste(probability_text, round(pnorm(value, mean, standard_deviation, lower.tail = FALSE) * 100, 2))
probability_text <- paste(probability_text, "%", sep = "")
print(probability_text)
}
pnorm_between <- function(dataset = vector(), min = 0, max = 0, word = "") {
mean <- mean(dataset)
standard_deviation <- get_standard_deviation(get_variance(dataset, mean))
probability_text <- "The probability of getting between"
probability_text <- paste(probability_text, min)
probability_text <- paste(probability_text, "and")
probability_text <- paste(probability_text, max)
probability_text <- paste(probability_text, "coins is")
probability_text <- paste(probability_text, round((pnorm(max, mean, standard_deviation) - pnorm(min, mean, standard_deviation)) * 100, 2))
probability_text <- paste(probability_text, "%", sep = "")
print(probability_text)
}
dnorm_plot("out/dnorm_coins.png", sort(data[, 1]), "Coins Normal Distribution", xlab = "Coins")
dnorm_plot("out/dnorm_time.png", sort(data[, 2]), "Time Normal Distribution", xlab = "Time (s)")
pnorm_less(sort(data[, 1]), 5, "coins")
pnorm_more(sort(data[, 1]), 5, "coins")
pnorm_between(sort(data[, 1]), 8, 10, "coins")