Skip to content
Snippets Groups Projects
Commit a3b3f4aa authored by Walmes Marques Zeviani's avatar Walmes Marques Zeviani
Browse files

Remove e acrescenta código para:

  - Remove código que usa rpanel, distração para o Curso;
  - Adiciona chunk ao fim com sessionInfo();
  - Remove a parametrização original da Poisson Generalizada, distração.
parent 6cca3321
No related branches found
No related tags found
No related merge requests found
......@@ -185,5 +185,3 @@ cat(format(Sys.time(),
format = "Atualizado em %d de %B de %Y.\n\n"))
sessionInfo()
```
<!------------------------------------------- -->
......@@ -19,7 +19,6 @@ source("_setup.R")
# devtools::load_all("../")
library(lattice)
library(latticeExtra)
library(rpanel)
library(bbmle)
library(corrplot)
library(plyr)
......@@ -56,212 +55,9 @@ $$
- *Note que o espaço paramétrico de $\gamma$ é dependente do
parâmetro $\theta$*.
```{r}
# Função densidade na parametrização original.
dpgnz0 <- function(y, theta, gamma, m = 4) {
if (gamma < 0) {
m <- max(c(m, floor(-theta/gamma)))
if (gamma < max(c(-1, -theta/m))) {
m <- 0
} else {
m <- as.integer(y <= m)
}
} else {
m <- 1
}
z <- theta + gamma * y
k <- lfactorial(y)
# fy <- m * theta * z^(y - 1) * exp(-z)/exp(k)
fy <- m * exp(log(theta) + (y - 1) * log(z) - z - k)
return(fy)
}
# Caso Poisson (gamma == 0).
y <- 0:30
theta <- 10
gamma <- 0
fy <- dpgnz0(y = y, theta = theta, gamma = gamma)
plot(fy ~ y, type = "h", xlab = "y", ylab = "f(y)")
lines(y + 0.3, dpois(y, lambda = theta), type = "h", col = 2)
grid <- expand.grid(lambda = c(2, 8, 15),
alpha = c(-0.05, 0, 0.05))
y <- 0:35
py <- mapply(FUN = dpgnz,
lambda = grid$lambda,
alpha = grid$alpha,
MoreArgs = list(y = y), SIMPLIFY = FALSE)
grid <- cbind(grid[rep(1:nrow(grid), each = length(y)), ],
y = y,
py = unlist(py))
useOuterStrips(xyplot(py ~ y | factor(lambda) + factor(alpha),
ylab = expression(f(y)),
xlab = expression(y),
data = grid, type = "h",
panel = function(x, y, ...) {
m <- sum(x * y)
panel.xyplot(x, y, ...)
panel.abline(v = m, lty = 2)
}),
strip = strip.custom(
strip.names = TRUE,
var.name = expression(lambda == ""),
sep = ""),
strip.left = strip.custom(
strip.names = TRUE,
var.name = expression(alpha == ""),
sep = ""))
```
## Recursos interativos com o `rpanel` ##
```{r, eval=FALSE}
react <- function(panel){
with(panel,
{
m <- THETA/(1 - GAMMA)
s <- sqrt(THETA/(1 - GAMMA)^3)
from <- floor(max(c(0, m - 5 * s)))
to <- ceiling(max(c(YMAX, m + 5 * s)))
y <- from:to
py <- dpgnz0(y = y, theta = THETA, gamma = GAMMA)
if (POIS) {
pz <- dpois(y, lambda = m)
} else {
pz <- 0
}
# Colocar 0 para valores não finitos (-Inf, Inf e NaN) para
# fazer gráfico.
py[!is.finite(py)] <- 0
plot(py ~ y, type = "h",
ylim = c(0, max(c(py, pz))),
xlab = expression(y),
ylab = expression(f(y)))
mtext(side = 3,
text = substitute(sum(f(y)) == s,
list(s = round(sum(py), 5))))
if (EX) {
abline(v = m, col = 2)
}
if (POIS) {
lines(y + 0.3, pz, type = "h", col = 3)
}
})
panel
}
panel <- rp.control(title = "Poisson Generalizada",
size = c(300, 100), YMAX = 150, m = 4)
rp.slider(panel = panel, action = react,
variable = THETA, title = "theta",
from = 0.1, to = 100,
initval = 5, resolution = 0.1,
showvalue = TRUE)
rp.slider(panel = panel, action = react,
variable = GAMMA, title = "gamma",
from = -1, to = 0.99,
initval = 0, resolution = 0.01,
showvalue = TRUE)
rp.checkbox(panel = panel,
variable = EX, action = react, title = "E(Y)",
labels = "Mostrar o valor esperado?")
rp.checkbox(panel = panel,
variable = POIS, action = react, title = "Poisson",
labels = "Adicionar a distribução Poisson?")
rp.do(panel = panel, action = react)
```
## Modelo de Regressão com a Distribuição Poisson Generalizada ##
```{r, eval=FALSE}
# Função densidade na parametrização de modelo de regressão.
MRDCr::dpgnz
react <- function(panel){
with(panel,
{
m <- LAMBDA
s <- sqrt(LAMBDA) * (1 + ALPHA * LAMBDA)
from <- floor(max(c(0, m - 5 * s)))
to <- ceiling(max(c(YMAX, m + 5 * s)))
y <- from:to
py <- dpgnz(y = y, lambda = LAMBDA, alpha = ALPHA)
if (POIS) {
pz <- dpois(y, lambda = m)
} else {
pz <- 0
}
py[!is.finite(py)] <- 0
plot(py ~ y, type = "h",
ylim = c(0, max(c(py, pz))),
xlab = expression(y),
ylab = expression(f(y)))
mtext(side = 3,
text = substitute(sum(f(y)) == s,
list(s = round(sum(py), 5))))
if (EX) {
abline(v = m, col = 2)
}
if (POIS) {
lines(y + 0.3, pz, type = "h", col = 3)
}
})
panel
}
panel <- rp.control(title = "Poisson Generalizada",
size = c(300, 100), YMAX = 150)
rp.slider(panel = panel, action = react,
variable = LAMBDA, title = "lambda",
from = 0.1, to = 100,
initval = 5, resolution = 0.1,
showvalue = TRUE)
rp.slider(panel = panel, action = react,
variable = ALPHA, title = "alpha",
from = -0.1, to = 0.4,
initval = 0, resolution = 0.01,
showvalue = TRUE)
rp.checkbox(panel = panel,
variable = EX, action = react, title = "E(Y)",
labels = "Mostrar o valor esperado?")
rp.checkbox(panel = panel,
variable = POIS, action = react, title = "Poisson",
labels = "Adicionar a distribução Poisson?")
rp.do(panel = panel, action = react)
```
## O Espaço Paramétrico ##
```{r}
#-----------------------------------------------------------------------
# Gráfico do espaço paramétrico de theta x gamma.
fun <- Vectorize(vectorize.args = c("theta", "gamma"),
FUN = function(theta, gamma) {
sum(dpgnz0(y = y, theta = theta, gamma = gamma))
})
grid <- list(theta = seq(1, 50, by = 1),
gamma = seq(-0.5, 1, by = 0.05))
str(grid)
y <- 0:500
my <- max(y)
grid$sum <- with(grid, outer(theta, gamma, fun))
grid <- with(grid,
cbind(expand.grid(theta = theta, gamma = gamma),
data.frame(sum = c(sum))))
levelplot(sum ~ theta + gamma,
data = subset(grid, round(sum, 3) == 1),
col.regions = gray.colors) +
layer(panel.abline(a = 0, b = -1/my)) +
layer(panel.abline(h = 0, lty = 2))
#-----------------------------------------------------------------------
# Gráfico do espaço paramétrico de lambda x alpha.
......@@ -1180,3 +976,11 @@ qqmath(~values | ind, data = rp,
})
```
## Informações da sessão
```{r, echo=FALSE, results="hold"}
cat(format(Sys.time(),
format = "Atualizado em %d de %B de %Y.\n\n"))
sessionInfo()
```
......@@ -150,7 +150,6 @@ uma unidade de $x$.
library(lattice)
library(latticeExtra)
library(grid)
library(rpanel)
library(bbmle)
library(corrplot)
library(plyr)
......@@ -197,68 +196,6 @@ useOuterStrips(xyplot(py ~ y | factor(lambda) + factor(alpha),
sep = ""))
```
## Recursos interativos com o `rpanel` ##
```{r, eval=FALSE}
# Função densidade na parametrização de modelo de regressão.
MRDCr::dgcnt
react <- function(panel){
with(panel,
{
m <- LAMBDA
s <- sqrt(LAMBDA/exp(ALPHA))
y <- 0:max(c(YMAX, ceiling(m + 5 * s)))
py <- dgcnt(y = y, lambda = LAMBDA, alpha = exp(ALPHA))
if (POIS) {
pz <- dpois(y, lambda = m)
} else {
pz <- 0
}
py[!is.finite(py)] <- 0
plot(py ~ y, type = "h",
ylim = c(0, max(c(py, pz))),
xlab = expression(y),
ylab = expression(f(y)))
mtext(side = 3,
text = substitute(sum(f(y)) == s,
list(s = round(sum(py), 5))))
if (EX) {
m <- sum(y * py)
# v <- sum((y - m)^2 * py)
legend("topright", bty = "n",
legend = substitute(E(Y) == mu,
list(mu = m)))
abline(v = m, col = 2)
}
if (POIS) {
lines(y + 0.3, pz, type = "h", col = 3)
}
})
panel
}
panel <- rp.control(title = "Gamma Count",
size = c(300, 100), YMAX = 50)
rp.slider(panel = panel, action = react,
variable = LAMBDA, title = "lambda",
from = 1, to = 0.75 * panel$YMAX,
initval = 5, resolution = 0.1,
showvalue = TRUE)
rp.slider(panel = panel, action = react,
variable = ALPHA, title = "alpha",
from = -5, to = 5,
initval = 0, resolution = 0.02,
showvalue = TRUE)
rp.checkbox(panel = panel,
variable = EX, action = react, title = "E(Y)",
labels = "Mostrar o valor esperado?")
rp.checkbox(panel = panel,
variable = POIS, action = react, title = "Poisson",
labels = "Adicionar a distribução Poisson?")
rp.do(panel = panel, action = react)
```
## Verossimilhança e Estimação ##
```{r}
......@@ -1563,6 +1500,10 @@ colnames(dwl) <- sprintf(paste(levels(db$home)[i], collapse = " %s "),
t(dwl)
```
```{r, eval=FALSE, include=FALSE}
opts_chunk$set(eval = FALSE)
## Informações da sessão
```{r, echo=FALSE, results="hold"}
cat(format(Sys.time(),
format = "Atualizado em %d de %B de %Y.\n\n"))
sessionInfo()
```
......@@ -19,9 +19,6 @@ source("_setup.R")
library(lattice)
library(latticeExtra)
library(grid)
# library(rpanel)
# library(bbmle)
# library(corrplot)
library(plyr)
library(car)
library(doBy)
......@@ -301,3 +298,11 @@ xyplot(fit ~ idade | modelo, groups = resfr, data = pred,
as.layer(xyplot(nap ~ idade, groups = resfr, data = confterm))
```
## Informações da sessão
```{r, echo=FALSE, results="hold"}
cat(format(Sys.time(),
format = "Atualizado em %d de %B de %Y.\n\n"))
sessionInfo()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment