Spaces:
Sleeping
Sleeping
File size: 5,646 Bytes
19bc544 874e677 0396fcc 4169d07 19bc544 874e677 19bc544 874e677 364822b 4169d07 364822b 19bc544 364822b 874e677 bb31a53 364822b 19bc544 4169d07 19bc544 874e677 bb31a53 19bc544 4169d07 19bc544 874e677 bb31a53 19bc544 0396fcc 19bc544 364822b 4169d07 364822b 0396fcc 364822b 19bc544 4169d07 19bc544 0396fcc 19bc544 4169d07 19bc544 0396fcc 19bc544 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
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"
)
}
}
})
})
|