ExWALD / server.R
FreddyHernandez's picture
Upload server.R
bb31a53 verified
source("dWALD.R")
source("WALD.R")
source("dExWALD.R")
source("ExWALD.R")
library(shiny)
library(MASS)
library(gamlss)
shinyServer(function(input, output) {
datos_reactivo <- reactive({
req(input$datos)
datos_char <- unlist(strsplit(input$datos, ","))
datos_num <- as.numeric(trimws(datos_char))
datos_num <- datos_num[!is.na(datos_num)]
if(length(datos_num) == 0) {
return(NULL)
} else {
return(datos_num)
}
})
output$histograma <- renderPlot({
datos <- datos_reactivo()
req(datos)
dist_sel <- input$dist
#par(mfrow=c(2, 1))
hist(datos, probability = TRUE, col = "white",
main = paste("Histogram and estimated density assuming", dist_sel),
xlab = "RT")
x_seq <- seq(min(datos), max(datos), length.out = 200)
# Estimation y curva según distribución elegida
if(dist_sel == "Normal") {
fit <- gamlss(datos~1, family=NO)
mu <- coef(fit, what="mu")
sigma <- exp(coef(fit, what="sigma"))
y <- dNO(x_seq, mu = mu, sigma = sigma)
lines(x_seq, y, col = "steelblue2", lwd=3)
legend("topright", bty="n",
legend="Estimated density",
lwd=3, col="steelblue2")
} else if(dist_sel == "exGAUS") {
# Ajuste con fitdistr
fit <- tryCatch({
mod <- gamlss(datos~1, family=exGAUS,
control=gamlss.control(n.cyc=5000, trace=TRUE))
}, error = function(e) NULL)
if(!is.null(fit)) {
mu <- coef(fit, what="mu")
sigma <- exp(coef(fit, what="sigma"))
nu <- exp(coef(fit, what="nu"))
y <- dexGAUS(x_seq, mu=mu, sigma=sigma, nu=nu)
lines(x_seq, y, col = "steelblue2", lwd=3)
legend("topright", bty="n",
legend="Estimated density",
lwd=3, col="steelblue2")
}
} else if(dist_sel == "WALD") {
# Ajuste con fitdistr
fit <- tryCatch({
mod <- gamlss(datos~1, family=WALD,
control=gamlss.control(n.cyc=5000, trace=TRUE))
}, error = function(e) NULL)
if(!is.null(fit)) {
mu <- exp(coef(fit, what="mu"))
sigma <- exp(coef(fit, what="sigma"))
y <- dWALD(x_seq, mu=mu, sigma=sigma)
lines(x_seq, y, col = "steelblue2", lwd=3)
legend("topright", bty="n",
legend="Estimated density",
lwd=3, col="steelblue2")
}
} else if(dist_sel == "ExWALD") {
fit <- tryCatch({
fit <- gamlss(datos~1, family=ExWALD,
control=gamlss.control(n.cyc=5000, trace=TRUE))
}, error = function(e) NULL)
if(!is.null(fit)) {
mu <- exp(coef(fit, what="mu"))
sigma <- exp(coef(fit, what="sigma"))
nu <- exp(coef(fit, what="nu"))
y <- dExWALD(x_seq, mu=mu, sigma=sigma, nu=nu)
lines(x_seq, y, col = "steelblue2", lwd=3)
legend("topright", bty="n",
legend="Estimated density",
lwd=3, col="steelblue2")
}
}
# To plot the residual analysis
#plot(fit)
})
output$parametros <- renderTable({
datos <- datos_reactivo()
req(datos)
dist_sel <- input$dist
if(dist_sel == "Normal") {
mu <- mean(datos)
sigma <- sd(datos)
data.frame(
Parameter = c("μ", "σ",
"Sample mean", "Sample standard deviation"),
Estimation = c(mu, sigma, mean(datos), sd(datos))
)
} else if(dist_sel == "exGAUS") {
fit <- tryCatch({
mod <- gamlss(datos~1, family=exGAUS,
control=gamlss.control(n.cyc=5000, trace=TRUE))
}, error = function(e) NULL)
if(!is.null(fit)) {
mu <- coef(fit, what="mu")
sigma <- exp(coef(fit, what="sigma"))
nu <- exp(coef(fit, what="nu"))
data.frame(
Parameter = c("μ", "σ", "ν",
"Sample mean", "Sample standard deviation"),
Estimation = c(mu, sigma, nu, mean(datos), sd(datos))
)
} else {
data.frame(
Parameter = "Error",
Estimation = "We cannot fit WALD"
)
}
} else if(dist_sel == "WALD") {
fit <- tryCatch({
mod <- gamlss(datos~1, family=WALD,
control=gamlss.control(n.cyc=5000, trace=TRUE))
}, error = function(e) NULL)
if(!is.null(fit)) {
mu <- exp(coef(fit, what="mu"))
sigma <- exp(coef(fit, what="sigma"))
data.frame(
Parameter = c("μ", "σ",
"Sample mean", "Sample standard deviation"),
Estimation = c(mu, sigma, mean(datos), sd(datos))
)
} else {
data.frame(
Parameter = "Error",
Estimation = "We cannot fit WALD"
)
}
} else if(dist_sel == "ExWALD") {
fit <- tryCatch({
fit <- gamlss(datos~1, family=ExWALD,
control=gamlss.control(n.cyc=5000, trace=TRUE))
}, error = function(e) NULL)
if(!is.null(fit)) {
mu <- exp(coef(fit, what="mu"))
sigma <- exp(coef(fit, what="sigma"))
nu <- exp(coef(fit, what="nu"))
data.frame(
Parameter = c("μ", "σ", "ν",
"Sample mean", "Sample standard deviation"),
Estimation = c(mu, sigma, nu, mean(datos), sd(datos))
)
} else {
data.frame(
Parameter = "Error",
Estimation = "We cannot fit ExWALD"
)
}
}
})
})