hist_exp <- function() { print("scegli la numerosità del campione") n <- scan(what = numeric(),n=1) x <- rexp(n) x_g <- seq(0, 5, .1) y <- exp(-x_g) #distribuzione di probabilità p <- ggplot() + geom_histogram(aes(x = x, y = after_stat(density)), bins = 50, col = "black") + geom_density(aes(x), col = "black") + geom_line(aes(y = y, x = x_g), col = "blue") # pdf(paste0("plot/hist_exp/hist_exp_", n, ".pdf")) print(p) # dev.off() } library(ggplot2) type_dist <- function(dist, n, param) { if (dist == "exp") { x <- rexp(n, param[1]) x_g <- seq(0, max(x), length.out = 100) y <- dexp(x_g, rate = param[1]) } else if (dist == "norm") { x <- rnorm(n, param[1], param[2]) x_g <- seq(min(x), max(x), length.out = 100) y <- dnorm(x_g, mean = param[1], sd = param[2]) } else if (dist == "bino") { x <- rbinom(n, param[1], param[2]) x_g <- seq(0, n, length.out = 100) y <- dbinom(x_g, size = param[1], prob = param[2]) } else if (dist == "nbino") { x <- rnbinom(n, param[1], param[2]) x_g <- seq(0, max(x), length.out = 100) y <- dnbinom(x_g, size = param[1], prob = param[2]) } else if (dist == "pois") { x <- rpois(n, param[1]) x_g <- seq(0, max(x), length.out = 100) y <- dpois(x_g, lambda = param[1]) } else if (dist == "gamma") { x <- rgamma(n, param[1]) x_g <- seq(0, max(x), length.out = 100) y <- dgamma(x_g, shape = param[1]) } else if (dist == "chisq") { x <- rchisq(n, param[1]) x_g <- seq(0, max(x), length.out = 100) y <- dchisq(x_g, df = param[1]) } else if (dist == "t") { x <- rt(n, param[1]) x_g <- seq(min(x), max(x), length.out = 100) y <- dt(x_g, df = param[1]) } return(list(x = x, x_g = x_g, y = y)) } param_line <- function(dist){ if (dist == "exp") { cat("scegli il parametro lambda") param <- scan(what = numeric(), n = 1) } else if (dist == "norm") { cat("scegli i parametri: media, varianza") param <- scan(what = numeric(), n = 2) } else if (dist == "bino") { cat("scegli i parametri: n successi, probabilità") param <- scan(what = numeric(), n = 2) } else if (dist == "nbino") { cat("scegli i parametri: n successi, probabilità") param <- scan(what = numeric(), n = 2) } else if (dist == "pois") { cat("scegli i parametri: n medio eventi") param <- scan(what = numeric(), n = 1) } else if (dist == "gamma") { cat("scegli i parametri: forma, scala") param <- scan(what = numeric(), n = 2) } else if (dist == "chisq") { cat("scegli i parametri: gradi di libertà") param <- scan(what = numeric(), n = 1) } else if (dist == "t") { cat("scegli i parametri: gradi di libertà") param <- scan(what = numeric(), n = 1) } return(param) } gg_visual <- function() { cat("Scegli cosa visualizzare:\n 1.istogramma\n 2.distribuzione quartilica\n 3.QQ plot") visualizzazione <- scan(what = numeric(), n = 1) cat("Scegli la distribuzione: exp, norm, bino, nbino, pois, gamma, chisq, t") dist <- scan(what = character(), n = 1) cat("Scegli la numerosità del campione") n <- scan(what = numeric(), n = 1) param <- param_line(dist) res <- type_dist(dist, n, param) if (visualizzazione == 1) {#istogramma p <- ggplot() + geom_histogram(aes(x = res$x, y = after_stat(density)), bins = 30, col = "black") + labs(title = paste("Distribuzione", dist)) } else if (visualizzazione == 2) {#distribuzione quartilica p <- ggplot() + geom_histogram(aes(x = res$x, y = after_stat(density)), bins = 30, col = "black") + # Punti per la distribuzione campionaria geom_vline(aes(xintercept = quantile(res$x, c(0.25, 0.5, 0.75))), linetype = "dashed", col = "red") + # Linee verticali per i quartili labs(title = paste("Distribuzione", dist)) } else if (visualizzazione == 3) {#QQ plot # Crea un QQ plot qq_data <- data.frame(Theoretical = res$y, Sample = res$x) p <- ggplot(qq_data, aes(sample = Sample)) + geom_qq(distribution = stats::qunif, col = "blue", size = 1) + labs(title = paste("QQ Plot - Distribuzione", dist)) print(p) } else { print("Opzione non valida.") return(NULL) } print(p) return(res) }