add normal distribution

This commit is contained in:
Chris Cromer 2022-09-03 18:54:56 -04:00
parent af71948632
commit 300e2ea349
5 changed files with 114 additions and 2 deletions

2
alai.R
View File

@ -2,4 +2,4 @@ source("config.R")
if (!exists("dbListTables", mode = "function")) source("mysql.R")
dbListTables(conn)
source("distribucion_normal.R")

98
distribucion_normal.R Normal file
View File

@ -0,0 +1,98 @@
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")

16
mysql.R
View File

@ -1,6 +1,6 @@
library(RMySQL)
conn = dbConnect(
conn <- dbConnect(
MySQL(),
user = dbuser,
password = dbpass,
@ -8,3 +8,17 @@ conn = dbConnect(
host = dbhost,
port = dbport
)
consulta_frames <- "
SELECT
f.coins, CAST((f.elapsed_time / 1000) AS INT)
FROM game g
LEFT JOIN player p ON (p.id = g.player_id)
LEFT JOIN frame f ON (f.game_id = g.id)
WHERE (
f.id = (
SELECT id FROM frame WHERE game_id = g.id ORDER BY elapsed_time DESC LIMIT 1
)
)
GROUP BY g.id;
"

BIN
out/dnorm_coins.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
out/dnorm_time.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB