Last active
February 8, 2022 18:50
-
-
Save eliardocosta/3de08ece6176654fa7ae07f2afb089d0 to your computer and use it in GitHub Desktop.
Scripts R e conjuntos de dados da disciplina MAE0006 MODELOS DE REGRESSAO.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val_agregado insu_trab insu_capital | |
38372840 424471 2689076 | |
1805427 19895 57997 | |
23736129 206893 2308272 | |
26981983 304055 1376235 | |
217546032 1809756 13554116 | |
19462751 180366 1790751 | |
28972772 224267 1210229 | |
14313157 54455 421064 | |
159921 2029 7188 | |
47289846 471211 2761281 | |
63015125 659379 3540475 | |
1809052 17528 146371 | |
10511786 75414 848220 | |
105324866 963156 5870409 | |
90120459 835083 5832503 | |
39079550 336159 1795976 | |
22826760 246144 1595118 | |
38686340 384484 2503693 | |
69910555 216149 4726625 | |
7856947 82021 415131 | |
21352966 174855 1729116 | |
46044292 355701 2706065 | |
92335528 943298 5294356 | |
48304274 456553 2833525 | |
17207903 267806 1212281 | |
47340157 439427 2404122 | |
2644567 24167 334008 | |
14650080 163637 627806 | |
7290360 59737 522335 | |
9188322 96106 507488 | |
51298516 407076 3295056 | |
20401410 43079 404749 | |
87756129 727177 4260353 | |
101268432 820013 4086558 | |
3556025 34723 184700 | |
124986166 1174540 6301421 | |
20451196 201284 1327353 | |
34808109 257820 1456683 | |
104858322 944998 5896392 | |
6541356 68987 297618 | |
37668126 400317 2500071 | |
4988905 56524 311251 | |
62828100 582241 4126465 | |
172960157 1120382 11588283 | |
15702637 150030 762671 | |
5418786 48134 276293 | |
49166991 425346 2731669 | |
46164427 313279 1945860 | |
9185967 89639 685587 | |
66964978 694628 3902823 | |
2979475 15221 361536 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dados ficticios | |
x2 <- c(2, 3, 4) | |
y <- c(3, 5, 6) | |
plot(x2, y, xlim = c(1.5, 4.5), ylim = c(2, 7), pch = 19) | |
# modelo nulo (null) | |
mod2 <- lm(y ~ 1); mod2 | |
abline(mod2, col = "red", lty = 2, lwd = 2) | |
plot(function(x) dnorm(x, mean(y), sd(y)), 0, 8, | |
xlab = "y", ylab = "Densidade") | |
abline(v = mean(y), col = "red", lty = 2, lwd = 2) | |
points(y, c(0.002, 0.002, 0.002), pch = 19) | |
# modelo saturado (full ou saturated) | |
x3 <- c(0, 0, 1) | |
mod1 <- lm(y ~ x2 + x3); mod1 | |
plot(x, y, xlim = c(1.5, 4.5), ylim = c(2, 7), pch = 19) | |
abline(mod2, col = "red", lty = 2, lwd = 2) # null model | |
segments(0, -1 + 2*0, 3, -1 + 2*3, lty = 2, lwd = 2) | |
segments(3, -2 + 2*3, 6, -2 + 2*6, lty = 2, lwd = 2) | |
# modelo intermediario | |
mod3 <- lm(y ~ x2); mod3 | |
abline(mod3, col = "blue", lty = 2, lwd = 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
frota ano trimestre latitude longitude diaspesca captura cpue | |
Santos 1995 3 23.25 41.75 8.0 1000.0000 125.00000 | |
Santos 1996 1 23.25 41.75 4.0 1000.0000 250.00000 | |
Santos 1999 1 23.25 41.75 5.0 1750.0000 350.00000 | |
Santos 1998 2 23.75 41.25 9.0 1800.0000 200.00000 | |
Ubatuba 1995 1 24.25 44.25 8.0 420.0000 52.50000 | |
Ubatuba 1995 1 24.25 44.25 7.0 315.0000 45.00000 | |
Ubatuba 1995 1 24.25 44.25 8.0 350.0000 43.75000 | |
Ubatuba 1998 2 24.25 45.25 10.0 3500.0000 350.00000 | |
Ubatuba 1998 2 24.25 45.25 7.0 950.0000 135.71429 | |
Ubatuba 1998 2 24.25 45.25 8.0 1000.0000 125.00000 | |
Ubatuba 1998 2 24.25 45.25 7.0 780.0000 111.42857 | |
Ubatuba 1998 3 24.25 45.25 4.0 600.0000 150.00000 | |
Ubatuba 1999 2 24.25 44.25 10.0 820.0000 82.00000 | |
Ubatuba 1999 4 24.25 45.25 10.0 880.0000 88.00000 | |
Santos 1999 1 24.25 44.75 5.0 1750.0000 350.00000 | |
Santos 1998 1 24.75 44.25 12.0 2300.0000 191.66667 | |
Santos 1999 1 24.75 46.25 5.0 2250.0000 450.00000 | |
Santos 1999 2 24.75 46.25 9.0 750.0000 78.94737 | |
Santos 1999 2 24.75 45.75 2.0 106.0000 53.00000 | |
Santos 1999 3 24.75 45.75 4.0 910.0000 227.50000 | |
Santos 1999 4 24.75 46.25 5.5 350.0000 63.63636 | |
Ubatuba 1996 3 25.25 46.25 8.0 1000.0000 125.00000 | |
Ubatuba 1996 3 25.25 46.25 10.0 1000.0000 100.00000 | |
Ubatuba 1996 3 25.25 46.25 9.0 650.0000 72.22222 | |
Ubatuba 1996 3 25.25 46.25 8.0 500.0000 62.50000 | |
Ubatuba 1997 2 25.25 46.25 12.0 1700.0000 141.66667 | |
Ubatuba 1997 2 25.25 46.25 10.0 1000.0000 100.00000 | |
Ubatuba 1997 3 25.25 46.25 12.0 3500.0000 291.66667 | |
Ubatuba 1997 3 25.25 46.25 10.0 2800.0000 280.00000 | |
Ubatuba 1997 3 25.25 46.25 10.0 2500.0000 250.00000 | |
Ubatuba 1997 3 25.25 46.25 10.0 2000.0000 200.00000 | |
Ubatuba 1999 1 25.25 46.25 9.0 400.0000 44.44444 | |
Ubatuba 1999 2 25.25 46.25 10.0 2100.0000 210.00000 | |
Ubatuba 1999 2 25.25 46.25 8.0 420.0000 52.50000 | |
Ubatuba 1999 3 25.25 46.25 10.0 1700.0000 170.00000 | |
Ubatuba 1999 3 25.25 46.25 10.0 1200.0000 120.00000 | |
Ubatuba 1999 4 25.25 46.25 8.0 1000.0000 125.00000 | |
Ubatuba 1999 4 25.25 46.25 10.0 1200.0000 120.00000 | |
Santos 1995 4 25.25 45.75 15.0 1000.0000 66.66667 | |
Santos 1996 1 25.25 46.25 10.0 3000.0000 300.00000 | |
Santos 1996 3 25.25 46.25 3.3 266.6667 80.00000 | |
Santos 1998 1 25.25 46.25 10.0 2200.0000 220.00000 | |
Santos 1998 1 25.25 45.25 14.0 3000.0000 214.28571 | |
Santos 1998 1 25.25 45.25 14.0 2000.0000 142.85714 | |
Santos 1999 1 25.25 45.25 12.0 2700.0000 225.00000 | |
Santos 1999 1 25.25 45.25 12.0 1000.0000 83.33333 | |
Santos 1999 2 25.25 45.25 2.0 106.0000 53.00000 | |
Santos 1999 3 25.25 46.25 1.5 350.0000 233.33333 | |
Santos 1999 3 25.25 44.75 4.0 910.0000 227.50000 | |
Santos 1999 4 25.25 46.25 9.0 1949.0000 216.55556 | |
Santos 1999 4 25.25 44.75 6.0 1000.0000 166.66667 | |
Ubatuba 1996 2 25.75 46.25 6.0 1500.0000 250.00000 | |
Ubatuba 1996 2 25.75 46.25 6.0 850.0000 141.66667 | |
Ubatuba 1996 2 25.75 46.25 12.0 680.0000 56.66667 | |
Ubatuba 1996 2 25.75 46.25 13.0 650.0000 50.00000 | |
Santos 1995 3 25.75 45.75 8.0 2000.0000 250.00000 | |
Santos 1995 3 25.75 46.25 1.0 50.0000 50.00000 | |
Santos 1995 4 25.75 46.25 15.0 2000.0000 133.33333 | |
Santos 1996 3 25.75 46.25 3.3 266.6667 80.00000 | |
Santos 1997 4 25.75 45.75 12.0 3000.0000 250.00000 | |
Santos 1997 4 25.75 46.25 4.0 300.0000 75.00000 | |
Santos 1998 1 25.75 46.25 9.0 1200.0000 133.33333 | |
Santos 1998 4 25.75 46.25 12.0 2000.0000 166.66667 | |
Santos 1999 2 25.75 46.25 9.5 750.0000 78.94737 | |
Ubatuba 1996 2 26.25 46.25 12.0 565.0000 47.08333 | |
Ubatuba 1996 4 26.25 47.25 10.0 1300.0000 130.00000 | |
Ubatuba 1996 4 26.25 47.25 10.0 700.0000 70.00000 | |
Ubatuba 1999 2 26.25 46.25 12.0 3500.0000 291.66667 | |
Ubatuba 1999 2 26.25 46.25 12.0 2800.0000 233.33333 | |
Ubatuba 1999 2 26.25 46.25 10.0 1500.0000 150.00000 | |
Ubatuba 1999 2 26.25 46.25 12.0 1730.0000 144.16667 | |
Santos 1995 2 26.25 46.75 5.0 2000.0000 400.00000 | |
Santos 1995 3 26.25 46.75 13.0 700.0000 53.84615 | |
Santos 1996 3 26.25 46.25 3.3 266.6667 80.00000 | |
Santos 1996 4 26.25 46.25 10.0 2000.0000 200.00000 | |
Santos 1996 4 26.25 46.75 9.0 1000.0000 111.11111 | |
Santos 1997 3 26.25 45.75 12.0 6000.0000 500.00000 | |
Santos 1997 4 26.25 45.75 7.5 1650.0000 220.00000 | |
Santos 1997 4 26.25 46.25 7.5 1650.0000 220.00000 | |
Santos 1997 4 26.25 46.25 4.0 300.0000 75.00000 | |
Santos 1998 2 26.25 46.25 12.0 1800.0000 150.00000 | |
Santos 1998 3 26.25 46.75 5.0 700.0000 140.00000 | |
Santos 1998 4 26.25 46.25 2.0 1200.0000 600.00000 | |
Santos 1999 1 26.25 46.25 5.0 2250.0000 450.00000 | |
Santos 1999 1 26.25 46.25 10.0 3000.0000 300.00000 | |
Santos 1999 1 26.25 46.25 15.0 3000.0000 200.00000 | |
Santos 1999 1 26.25 46.25 18.0 3500.0000 194.44444 | |
Santos 1999 2 26.25 46.25 8.0 3876.0000 484.50000 | |
Santos 1999 2 26.25 46.25 8.0 1351.0000 168.87500 | |
Santos 1999 2 26.25 47.75 6.0 900.0000 150.00000 | |
Santos 1999 2 26.25 47.75 7.5 1000.0000 133.33333 | |
Santos 1999 2 26.25 46.25 2.0 106.0000 53.00000 | |
Ubatuba 1996 4 26.75 47.25 10.0 480.0000 48.00000 | |
Santos 1995 1 26.75 46.25 3.0 1000.0000 333.33333 | |
Santos 1995 1 26.75 47.75 3.0 150.0000 50.00000 | |
Santos 1995 2 26.75 47.25 2.0 400.0000 200.00000 | |
Santos 1995 3 26.75 46.25 10.0 1000.0000 100.00000 | |
Santos 1995 4 26.75 46.25 5.0 1500.0000 300.00000 | |
Santos 1997 2 26.75 47.25 15.0 4000.0000 266.66667 | |
Santos 1997 3 26.75 46.25 12.0 3000.0000 250.00000 | |
Santos 1997 3 26.75 46.25 2.0 400.0000 200.00000 | |
Santos 1997 3 26.75 46.25 5.0 900.0000 180.00000 | |
Santos 1997 3 26.75 46.25 10.0 1500.0000 150.00000 | |
Santos 1997 3 26.75 46.75 14.0 2000.0000 142.85714 | |
Santos 1998 2 26.75 46.75 12.0 2900.0000 241.66667 | |
Santos 1998 3 26.75 47.25 8.0 1000.0000 125.00000 | |
Santos 1998 3 26.75 46.25 7.0 600.0000 85.71429 | |
Santos 1999 1 26.75 46.75 15.0 2300.0000 153.33333 | |
Santos 1999 2 26.75 46.75 8.0 2385.0000 298.12500 | |
Santos 1999 4 26.75 46.75 5.5 1750.0000 318.18182 | |
Santos 1999 4 26.75 46.75 8.0 1900.0000 237.50000 | |
Santos 1999 4 26.75 46.25 5.0 1050.0000 210.00000 | |
Santos 1999 4 26.75 46.75 5.0 1050.0000 210.00000 | |
Santos 1999 4 26.75 46.25 6.0 1000.0000 166.66667 | |
Santos 1999 4 26.75 46.75 7.0 1000.0000 142.85714 | |
Santos 1999 4 26.75 47.25 5.0 700.0000 140.00000 | |
Santos 1997 2 27.25 46.75 8.0 4000.0000 500.00000 | |
Santos 1998 1 27.25 46.75 14.0 1000.0000 71.42857 | |
Santos 1998 2 27.25 47.25 3.0 1000.0000 333.33333 | |
Santos 1998 3 27.25 46.75 13.0 2160.0000 166.15385 | |
Santos 1998 3 27.25 46.75 13.0 1700.0000 130.76923 | |
Santos 1998 4 27.25 46.75 2.0 700.0000 350.00000 | |
Santos 1998 4 27.25 46.75 4.0 1000.0000 250.00000 | |
Santos 1998 4 27.25 47.25 4.0 700.0000 175.00000 | |
Santos 1998 4 27.25 46.75 10.0 1500.0000 150.00000 | |
Santos 1998 4 27.25 46.75 11.0 1500.0000 136.36364 | |
Santos 1998 4 27.25 46.75 8.0 800.0000 100.00000 | |
Santos 1999 2 27.25 47.25 6.0 900.0000 150.00000 | |
Santos 1999 2 27.25 47.25 7.5 1000.0000 133.33333 | |
Santos 1999 3 27.25 48.25 5.5 1340.0000 243.63636 | |
Santos 1999 4 27.25 46.75 12.0 2500.0000 208.33333 | |
Santos 1999 4 27.25 46.75 10.0 2000.0000 200.00000 | |
Santos 1999 4 27.25 46.75 5.0 700.0000 140.00000 | |
Santos 1997 4 27.75 47.25 5.0 1342.0000 268.40000 | |
Santos 1995 2 28.25 47.25 10.0 1500.0000 150.00000 | |
Santos 1995 3 28.25 47.25 10.0 4000.0000 400.00000 | |
Santos 1995 3 28.25 47.25 12.0 2000.0000 166.66667 | |
Santos 1995 4 28.25 47.75 8.0 4500.0000 562.50000 | |
Santos 1995 4 28.25 47.25 4.0 1500.0000 375.00000 | |
Santos 1995 4 28.25 47.25 10.0 3500.0000 350.00000 | |
Santos 1995 4 28.25 47.25 12.0 3500.0000 291.66667 | |
Santos 1996 1 28.25 47.25 9.0 4000.0000 444.44444 | |
Santos 1997 1 28.25 47.25 10.0 1300.0000 130.00000 | |
Santos 1997 2 28.25 47.25 8.0 4500.0000 562.50000 | |
Santos 1997 4 28.25 47.25 12.0 5700.0000 475.00000 | |
Santos 1998 1 28.25 47.25 10.0 4500.0000 450.00000 | |
Santos 1998 3 28.25 47.25 16.0 6500.0000 406.25000 | |
Santos 1998 3 28.25 47.25 12.0 3000.0000 250.00000 | |
Santos 1998 3 28.25 47.25 5.0 500.0000 100.00000 | |
Santos 1999 3 28.25 47.25 12.0 3500.0000 291.66667 | |
Santos 1999 3 28.25 47.25 1.5 350.0000 233.33333 | |
Santos 1999 3 28.25 47.25 9.0 1900.0000 211.11111 | |
Santos 1999 3 28.25 46.75 5.0 800.0000 160.00000 | |
Santos 1999 3 28.25 47.25 5.0 800.0000 160.00000 | |
Santos 1999 3 28.25 47.25 13.0 1300.0000 100.00000 | |
Santos 1999 4 28.25 47.25 10.0 950.0000 95.00000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dobson) | |
data(chronic) | |
head(chronic) | |
# sob H0 | |
theta <- 1.184 | |
r <- chronic$number - theta | |
dots <- function(x,...){ | |
sx <- sort(x) | |
sy <- unlist(lapply(table(sx),seq)) | |
plot(sx, sy, ...) | |
} | |
dots(r[1:26], pch = 20, xlab = "Resíduos", ylab = "Frequência", | |
main = "Cidade") # cidade | |
dots(r[27:49], pch = 20, xlab = "Resíduos", ylab = "Frequência", | |
main = "Rural") # rural | |
# sob H1 | |
theta1 <- 1.423 | |
theta2 <- 0.913 | |
r1 <- chronic$number[1:26] - theta1 | |
r2 <- chronic$number[27:49] - theta2 | |
dots(r1, pch = 20, xlab = "Resíduos", ylab = "Frequência", | |
main = "Cidade") # cidade | |
dots(r2, pch = 20, xlab = "Resíduos", ylab = "Frequência", | |
main = "Rural") # rural |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dobson) | |
data(birthweight) | |
head(birthweight) | |
plot(birthweight$`boys gestational age`, birthweight$`boys weight`, | |
xlab = "Idade (semanas)", ylab = "Peso (gramas)", | |
xlim = c(35, 42), ylim = c(2350, 3500), pch = 20, col = 1) | |
points(birthweight$`girls gestational age`, birthweight$`girls weight`, | |
pch = 20, col = 2) | |
legend("topleft", c("meninos", "meninas"), pch = 20, col = 1:2, bty = "n") | |
# sob H0 ------------------------------------------------------------------ | |
b <- 120.894 | |
a1 <- -1610.283 | |
a2 <- -1773.322 | |
# residuos | |
mu1 <- a1 + b*birthweight$`boys gestational age` | |
mu2 <- a2 + b*birthweight$`girls gestational age` | |
res1 <- birthweight$`boys weight` - mu1 | |
res2 <- birthweight$`girls weight` - mu2 | |
respad1 <- res1/sd(c(res1, res2)) | |
respad2 <- res2/sd(c(res1, res2)) | |
plot(mu1, respad1, xlab = "Valores ajustados (gramas)", | |
ylab = "Resíduos padronizados", xlim = c(2500, 3475), | |
ylim = c(-2, 2), pch = 20, col = 1) | |
points(mu2, respad2, pch = 20, col = 2) | |
abline(h = 0, lty = 2) | |
legend("topright", c("meninos", "meninas"), pch = 20, col = 1:2)#, bty = "n") | |
plot(birthweight$`boys gestational age`, respad1, | |
xlab = "Idade (semanas)", ylab = "Resíduos padronizados", | |
xlim = c(35, 43), ylim = c(-2, 2), pch = 20, col = 1) | |
points(birthweight$`girls gestational age`, respad2, pch = 20, col = 2) | |
abline(h = 0, lty = 2) | |
legend("topright", c("meninos", "meninas"), pch = 20, col = 1:2)#, bty = "n") | |
plot(qnorm(ppoints(24)), sort(c(respad1, respad2)), ylim = c(-2, 2), | |
pch = 20, xlab = "Quantis normal", ylab = "Resíduos padronizados") | |
abline(a = 0, b = 1, lty = 2) | |
# sob H1 ------------------------------------------------------------------ | |
b1 <- 111.983 | |
b2 <- 130.400 | |
a1 <- -1268.672 | |
a2 <- -2141.667 | |
# residuos | |
mu1 <- a1 + b1*birthweight$`boys gestational age` | |
mu2 <- a2 + b2*birthweight$`girls gestational age` | |
res1 <- birthweight$`boys weight` - mu1 | |
res2 <- birthweight$`girls weight` - mu2 | |
respad1 <- res1/sd(c(res1, res2)) | |
respad2 <- res2/sd(c(res1, res2)) | |
plot(mu1, respad1, xlab = "Valores ajustados (gramas)", | |
ylab = "Resíduos padronizados", xlim = c(2500, 3475), | |
ylim = c(-2, 2), pch = 20, col = 1) | |
points(mu2, respad2, pch = 20, col = 2) | |
abline(h = 0, lty = 2) | |
legend("topright", c("meninos", "meninas"), pch = 20, col = 1:2)#, bty = "n") | |
plot(birthweight$`boys gestational age`, respad1, | |
xlab = "Idade (semanas)", ylab = "Resíduos padronizados", | |
xlim = c(35, 43), ylim = c(-2, 2), pch = 20, col = 1) | |
points(birthweight$`girls gestational age`, respad2, pch = 20, col = 2) | |
abline(h = 0, lty = 2) | |
legend("topright", c("meninos", "meninas"), pch = 20, col = 1:2)#, bty = "n") | |
plot(qnorm(ppoints(24)), sort(c(respad1, respad2)), ylim = c(-2, 2), | |
pch = 20, xlab = "Quantis normal", ylab = "Resíduos padronizados") | |
abline(a = 0, b = 1, lty = 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dobson) | |
data(failure) | |
failure | |
hist(failure$lifetimes, xlab = "Tempo até a falha (horas)", | |
ylab = "Frequência", main = "") | |
library(animation) | |
ani.options(interval = 2, nmax = 50) | |
# exemplo | |
newton.method(interact = TRUE) | |
# funcao score | |
newton.method(FUN = function(theta) -49*2/theta + (2/(theta^3))*4794902949, | |
interact = TRUE, rg = c(5E3, 14E3), ylim = c(-0.005, 0.05), | |
ylab = expression(U(theta)), xlab = expression(theta), | |
main = "Newton-Raphson", tol = 1E-5) | |
y <- failure$lifetimes | |
N <- length(failure$lifetimes) | |
# metodo score de Fisher | |
old.the <- 1000 # chute inicial | |
new.the <- old.the/2 + sum(y^2)/(2*N*old.the); new.the | |
while(abs(new.the - old.the) > 0.01) { | |
old.the <- new.the | |
new.the <- old.the/2 + sum(y^2)/(2*N*old.the) | |
print(new.the) | |
} | |
sqrt(sum(y^2)/N) # EMV pela expressao explicita | |
IF <- 4*N/new.the^2; IF # informacao | |
EP <- 1/sqrt(IF); EP # erro padrao | |
# IC 95% | |
new.the + c(-1, 1)*1.96*EP | |
logvero <- function(the){ | |
sum(log(y)) + N*log(2) - 2*N*log(the) - sum(y^2)/the^2 | |
} | |
plot(function(x) logvero(x), 7000, 13000, | |
xlab = expression(theta), ylab = "log-verossimilhança") | |
abline(v = new.the, lty = 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dobson) | |
data("poisson") | |
poisson | |
old.b <- c(1, 2) #valores iniciais | |
X <- model.matrix(~ poisson$x) | |
W <- diag(1/(old.b[1] + old.b[2]*poisson$x)) | |
z <- poisson$y | |
t(X)%*%W%*%X | |
t(X)%*%W%*%z | |
new.b <- solve(t(X)%*%W%*%X)%*%t(X)%*%W%*%z; new.b | |
while(sqrt(sum((new.b - old.b)^2)) > 0.01) { | |
old.b <- new.b | |
W <- diag(1/(old.b[1] + old.b[2]*poisson$x)) | |
new.b <- solve(t(X)%*%W%*%X)%*%t(X)%*%W%*%z | |
print(new.b) | |
} | |
# covariancia de beta | |
solve(t(X)%*%W%*%X) | |
# ajuste pela funcao 'glm' | |
mod <- glm(y ~ x, family = poisson(link = "identity"), data = poisson) | |
summary(mod) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dobson) | |
data("birthweight") # dados pesos | |
#- | |
idade <- c(birthweight$`boys gestational age`, birthweight$`girls gestational age`) | |
peso <- c(birthweight$`boys weight`, birthweight$`girls weight`) | |
sexo <- c(rep(0, 12), rep(1, 12)) | |
dados.birth <- data.frame(idade, peso, sexo) | |
mod <- glm(peso ~ idade + sexo, family = gaussian(link = "identity"), | |
data = dados.birth) | |
summary(mod) | |
plot(birthweight$`boys gestational age`, birthweight$`boys weight`, | |
xlab = "Idade (semanas)", ylab = "Peso (gramas)", | |
xlim = c(35, 42), ylim = c(2350, 3500), pch = 20, col = 1) | |
points(birthweight$`girls gestational age`, birthweight$`girls weight`, | |
pch = 20, col = 2) | |
legend("topleft", c("meninos", "meninas"), pch = 20, col = 1:2, bty = "n") | |
abline(a = mod$coef[1], b = mod$coef[2], lty = 2) | |
abline(a = mod$coef[1] + mod$coef[3], b = mod$coef[2], lty = 2, col = "red") | |
#- | |
data("poisson") | |
mod <- glm(y ~ x, family = poisson(link = "identity"), data = poisson) | |
summary(mod) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#- gráfico 3 funcoes de ligacao | |
plot(function(x) exp(x)/(1 + exp(x)), -4, 4, ylim = c(0, 1), lty = 2, | |
lwd = 2, ylab = expression(pi[i]), xlab = "Componente linear") | |
plot(function(x) pnorm(x), -4, 4, lty = 2, col = "blue", lwd = 2, | |
add = TRUE) | |
plot(function(x) 1 - exp(-exp(x)), -4, 4, lty = 2, col = "red", lwd = 2, | |
add = TRUE) | |
legend("topleft", c("logito", "probito", "c-loglog"), lty = 2, lwd = 2, | |
col = c(1, 4, 2), bty = "n") | |
#- dados besouros | |
y <- c(6, 13, 18, 28, 52, 53, 61, 60) | |
n <- c(59, 60, 62, 56, 63, 59, 62, 60) | |
x <- c(1.6907, 1.7242, 1.7552, 1.7842, 1.8113, 1.8369, 1.8610, 1.8839) | |
plot(x, y/n, xlab = "Dose", ylab = "Proporção mortos", pch = 20) | |
#- ligacao logito | |
besouro <- cbind(y, n - y) | |
mod1 <- glm(besouro ~ x, family = binomial(link = "logit")) | |
summary(mod1) | |
pchisq(mod1$deviance, mod1$df.residual, lower.tail = FALSE) # p-valor para o desvio | |
b <- mod1$coef | |
plot(x, y/n, xlab = "Dose", ylab = "Proporção mortos", ylim = c(0, 1), | |
xlim = c(1.67, 1.90), pch = 20) | |
plot(function(x) exp(b[1] + b[2]*x)/(1 + exp(b[1] + b[2]*x)), 1.5, 2, | |
lty = 2, lwd = 2, add = TRUE) | |
library(MASS) | |
dose.p(mod1) # dose letal 50% | |
dose.p(mod1, p = 0.99) # dose letal 99% | |
# ligacao probito | |
mod2 <- glm(besouro ~ x, family = binomial(link = "probit")) | |
summary(mod2) | |
pchisq(mod2$deviance, mod2$df.residual, lower.tail = FALSE) # p-valor para o desvio | |
b <- mod2$coef | |
plot(function(x) pnorm(b[1] + b[2]*x), 1.5, 2, lty = 2, lwd = 2, | |
col = 4, add = TRUE) | |
# ligacao c-loglog | |
mod3 <- glm(besouro ~ x, family = binomial(link = "cloglog")) | |
summary(mod3) | |
pchisq(mod3$deviance, mod3$df.residual, lower.tail = FALSE) # p-valor para o desvio | |
b <- mod3$coef | |
plot(function(x) 1 - exp(-exp(b[1] + b[2]*x)), 1.5, 2, lty = 2, | |
lwd = 2, col = 2, add = TRUE) | |
dose.p(mod3) # dose letal 50% | |
dose.p(mod3, p = 0.99) # dose letal 99% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#- dados cupons | |
y <- c(30, 45, 70, 100, 137, 166, 176) # cupons usados | |
n <- rep(200, 7) # cupons enviados | |
x <- c(5, 10, 15, 20, 25, 30, 35) # desconto | |
plot(x, y/n, xlab = "Desconto", ylab = "Proporção cupons usados", pch = 20) | |
#- | |
cupons <- cbind(y, n - y) | |
mod1 <- glm(cupons ~ x, family = binomial(link = "logit")) | |
summary(mod1) | |
pchisq(mod1$deviance, mod1$df.residual, lower.tail = FALSE) # p-valor para o desvio | |
#- | |
b <- mod1$coef | |
plot(x, y/n, xlab = "Desconto", ylab = "Proporção cupons usados", | |
ylim = c(0, 1), xlim = c(0, 40), pch = 20) | |
plot(function(x) exp(b[1] + b[2]*x)/(1 + exp(b[1] + b[2]*x)), 0, 40, | |
lty = 2, lwd = 2, add = TRUE) | |
#- | |
library(MASS) | |
dose.p(mod1, p = 0.80) | |
# ------------------------------------------------------------------------- | |
library(aplore3) | |
#- dados | |
data(chdage) | |
head(chdage) | |
View(chdage) | |
#- | |
mod2 <- glm(chd ~ age, family = binomial(), data = chdage) | |
summary(mod2) | |
pchisq(mod2$deviance, mod2$df.residual, lower.tail = FALSE) # p-valor para o desvio | |
#- | |
b <- mod2$coef | |
plot(function(x) exp(b[1] + b[2]*x)/(1 + exp(b[1] + b[2]*x)), 0, 80, | |
lty = 2, lwd = 2, ylab = expression(pi), xlab = "Idade") | |
#- | |
exp(b[2]) # RC | |
#- | |
vcov(mod2) | |
Var <- vcov(mod2)[2, 2] | |
rho <- 0.05 # 95% confianca | |
exp(b[2] + c(-1, 1)*qnorm(1 - rho/2)*sqrt(Var)) # IC para RC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dobson) | |
data("doctors") | |
doctors | |
doctors2 <- cbind(doctors, agecat = rep(1:5, 2)) | |
doctors2 | |
dim(doctors2) # 10 x 5 | |
plot(doctors2$agecat, 1E5*doctors2$deaths/doctors2$`person-years`, | |
col = rep(c(2, 1), each = 5), pch = 20, xlab = "Idade (categoria)", | |
ylab = "Mortes por 100000") | |
legend("topleft", c("não fumante", "fumante"), pch = 20, col = 1:2, bty = "n") | |
mod1 <- glm(deaths ~ agecat + smoking, offset = log(`person-years`/1E5), | |
family = poisson(link = "log"), data = doctors2) | |
summary(mod1) | |
pchisq(mod1$deviance, mod1$df.residual, lower.tail = FALSE) # nao faz sentido neste caso | |
b <- mod1$coef | |
plot(function(x) exp(b[1] + b[2]*x), 0, 5, lty = 2, add = TRUE) | |
plot(function(x) exp(b[1] + b[2]*x + b[3]), 0, 5, lty = 2, col = "red", add = TRUE) | |
exp(b[3]) # risco relativo fumante |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nbacter <- c(175, 108, 95, 82, 71, 50, 49, 31, 28, 17, 16, 11) | |
texpo <- 1:12 | |
plot(texpo, nbacter, xlab = "Tempo de exposição", ylab = "Número bactérias", | |
pch = 20) | |
mod1 <- lm(nbacter ~ texpo) | |
mod1 <- glm(nbacter ~ texpo, family = gaussian()) | |
summary(mod1) | |
b <- mod1$coefficients | |
plot(function(x) b[1] + b[2]*x, 0, 12, lty = 2, lwd = 2, col = "red", | |
add = TRUE) | |
mod2 <- glm(nbacter ~ texpo, family = poisson(link = "identity")) | |
summary(mod2) | |
b <- mod2$coefficients | |
plot(function(x) b[1] + b[2]*x, 0, 12, lty = 2, lwd = 2, | |
col = "green", add = TRUE) | |
mod3 <- glm(nbacter ~ texpo, family = poisson()) | |
summary(mod3) | |
b <- mod3$coefficients | |
plot(function(x) exp(b[1] + b[2]*x), 0, 12, lty = 2, lwd = 2, | |
col = "blue", add = TRUE) | |
legend("topright", c("Normal/identidade", "Poisson/identidade", | |
"Poisson/log"), lty = 2, lwd = 2, | |
col = c("red", "green", "blue"), bty = "n") | |
exp(b[2]) | |
# ------------------------------------------------------------------------- | |
store <- read.csv("~/caminho/para/o/arquivo/store.txt", sep = "") | |
head(store) | |
dim(store) | |
mod1 <- glm(nclientes ~ I(domic/1E3) + I(renda/1E3) + idade + distconc | |
+ distloja, family = poisson(), data = store) | |
summary(mod1) | |
b <- mod1$coefficients | |
exp(b) | |
round(100*(exp(b) - 1), 2) | |
round(100*(exp(5*b[4]) - 1), 2) # covariavel idade domicilio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plot(function(x) dnorm(x, 5, 1), 0, 15, ylab = "") | |
mu <- 5; phi <- 4 | |
plot(function(x) dgamma(x, shape = phi, rate = phi/mu), 0, 15, | |
add = TRUE, col = "blue") | |
turbina <- read.csv("~/caminho/para/o/arquivo/turbina.txt", sep = "") | |
head(turbina) | |
dim(turbina) | |
mean(turbina$tempo); sd(turbina$tempo) | |
plot(density(turbina$tempo), xlab = "Tempo", ylab = "Densidade", main = "") | |
hist(turbina$tempo, prob = TRUE, xlab = "Tempo", ylab = "Densidade", main = "") | |
mod1 <- glm(tempo ~ 1, family = Gamma(link = "identity"), data = turbina) | |
summary(mod1) | |
library(MASS) | |
phi <- gamma.shape(mod1); phi # phi | |
2/sqrt(phi$alpha) # assimetria | |
aggregate(tempo ~ tipo, turbina, mean) | |
aggregate(tempo ~ tipo, turbina, sd) | |
aggregate(tempo ~ tipo, turbina, sd)/aggregate(tempo ~ tipo, turbina, mean) | |
boxplot(split(turbina$tempo, turbina$tipo), xlab = "Tipo", ylab = "Tempo") | |
cdplot(as.factor(turbina$tipo) ~ turbina$tempo, xlab = "Duração", | |
ylab = "Tipo") | |
turbina$tipo <- factor(turbina$tipo) | |
mod2 <- update(mod1, . ~ tipo) | |
summary(mod2) | |
phi <- gamma.shape(mod2); phi # phi | |
2/sqrt(phi$alpha) # assimetria | |
turbina <- data.frame(turbina, tipo2 = turbina$tipo) | |
levels(turbina$tipo2) | |
levels(turbina$tipo2) <- c("1", "2", "1", "1", "5") | |
mod3 <- update(mod2, . ~ tipo2, data = turbina) | |
summary(mod3) | |
phi <- gamma.shape(mod3); phi # phi | |
2/sqrt(phi$alpha) # assimetria |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pesca <- read.csv("~/caminho/para/o/arquivo/pesca.txt", sep = "") | |
head(pesca) | |
dim(pesca) | |
plot(density(pesca$cpue), xlab = "CPUE (kg/dias pesca)", ylab = "Densidade", main = "") | |
aggregate(cpue ~ frota, pesca, mean) | |
aggregate(cpue ~ frota, pesca, sd) | |
aggregate(cpue ~ frota, pesca, length) | |
aggregate(cpue ~ frota, pesca, sd)/aggregate(cpue ~ frota, pesca, mean) | |
boxplot(split(pesca$cpue, pesca$frota), xlab = "Frota", ylab = "CPUE") | |
cdplot(as.factor(pesca$frota) ~ pesca$cpue, xlab = "CPUE", | |
ylab = "Frota") | |
aggregate(cpue ~ ano, pesca, mean) | |
aggregate(cpue ~ ano, pesca, sd) | |
aggregate(cpue ~ ano, pesca, length) | |
aggregate(cpue ~ ano, pesca, sd)/aggregate(cpue ~ ano, pesca, mean) | |
boxplot(split(pesca$cpue, pesca$ano), xlab = "Ano", ylab = "CPUE") | |
aggregate(cpue ~ trimestre, pesca, mean) | |
aggregate(cpue ~ trimestre, pesca, sd) | |
aggregate(cpue ~ trimestre, pesca, length) | |
aggregate(cpue ~ trimestre, pesca, sd)/aggregate(cpue ~ trimestre, pesca, mean) | |
boxplot(split(pesca$cpue, pesca$trimestre), xlab = "Trimestre", ylab = "CPUE") | |
cdplot(as.factor(pesca$trimestre) ~ pesca$cpue, xlab = "CPUE", | |
ylab = "Trimestre") | |
pairs(~ cpue + latitude + longitude, data = pesca, pch = 20, | |
panel = panel.smooth, lty = 2) | |
panel.hist <- function(x, ...) { | |
usr <- par("usr"); on.exit(par(usr)) | |
par(usr = c(usr[1:2], 0, 1.5) ) | |
h <- hist(x, plot = FALSE) | |
breaks <- h$breaks; nB <- length(breaks) | |
y <- h$counts; y <- y/max(y) | |
rect(breaks[-nB], 0, breaks[-1], y, col = "grey", ...) | |
} | |
pairs(~ cpue + latitude + longitude, data = pesca, | |
pch = 20, lower.panel = NULL, upper.panel = panel.smooth, | |
diag.panel = panel.hist, lty = 2) | |
pesca$ano <- factor(pesca$ano) | |
pesca$trimestre <- factor(pesca$trimestre) | |
mod1 <- glm(cpue ~ latitude + longitude + ano + trimestre + frota, | |
family = Gamma(link = "log"), data = pesca) | |
summary(mod1) | |
mod2 <- glm(cpue ~ latitude + ano + trimestre + frota, | |
family = Gamma(link = "log"), data = pesca) | |
summary(mod2) | |
mod3 <- glm(cpue ~ latitude + frota, | |
family = Gamma(link = "log"), data = pesca) | |
summary(mod3) | |
library(MASS) | |
phi <- gamma.shape(mod3); phi # phi | |
2/sqrt(phi$alpha) # assimetria | |
# ------------------------------------------------------------------------- | |
econo <- read.csv("~/caminho/para/o/arquivo/gujarati.txt", sep = "") | |
head(econo) | |
dim(econo) | |
plot(density(econo$val_agregado/1E8), xlab = "Valor agregado (10^8)", ylab = "Densidade", main = "") | |
hist(econo$val_agregado/1E8, prob = TRUE, xlab = "Valor agregado (10^8)", ylab = "Densidade", main = "") | |
mod1 <- glm(val_agregado ~ I(log(insu_trab)) + I(log(insu_capital)), | |
family = Gamma(link = "log"), data = econo) | |
summary(mod1) | |
library(MASS) | |
phi <- gamma.shape(mod1); phi # phi | |
2/sqrt(phi$alpha) # assimetria | |
betas <- coef(mod1)[2] + coef(mod1)[3]; betas | |
V <- vcov(mod1) | |
var.betas <- V[2, 2] + V[3, 3] + 2*V[2, 3]; var.betas | |
# IC 95% | |
betas + c(-1, 1)*qnorm(1 - 0.05/2)*sqrt(var.betas) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------------------- | |
# Slides 1, pg. 156 | |
#- Dados | |
library(dobson) | |
data("birthweight") | |
idade <- c(birthweight$`boys gestational age`, birthweight$`girls gestational age`) | |
peso <- c(birthweight$`boys weight`, birthweight$`girls weight`) | |
sexo <- c(rep(0, 12), rep(1, 12)) | |
dados.birth <- data.frame(idade, peso, sexo) | |
#- Ajuste | |
mod <- glm(peso ~ idade + sexo, family = gaussian(link = "identity"), | |
data = dados.birth) | |
summary(mod) | |
#- Diagnostico | |
library(glmtoolbox) | |
res <- residuals2(mod, standardized = TRUE) # res vs. valores ajustados | |
plot(res) # res vs. index | |
abline(h = 0, lty = 2) | |
acf(res) # autocorrelacao res | |
eta <- mod$linear.predictors # eta ajustado | |
plot(eta, res) # res vs. eta ajustado | |
abline(h = 0, lty = 2) | |
plot(dados.birth$idade, res) # res vs. covariavel | |
abline(h = 0, lty = 2) | |
z <- resid(mod, type = "working") + eta # z ajustado | |
plot(z, eta) # z vs. eta, ambos ajustados | |
abline(0, 1) | |
envelope(mod, standardized = TRUE) # QQ-plot com envelope simulado 95% com resíduo quantilico | |
envelope(mod, standardized = TRUE, rep = 1E3, conf = 0.99, | |
type = "deviance") | |
# outros tipos de residuos | |
res <- residuals2(mod, standardized = TRUE, type = "pearson") | |
res <- residuals2(mod, standardized = TRUE, type = "deviance") | |
# pontos influentes | |
p <- ncol(model.matrix(mod)) | |
n <- nrow(model.matrix(mod)) | |
cutoff <- qf(0.5, p, n - p); cutoff | |
cooks <- cooks.distance(mod) # distancia de Cook | |
plot(cooks, type = "h", ylim = c(0, max(cutoff, max(cooks)))) | |
abline(h = cutoff, lty = 2) | |
# ------------------------------------------------------------------------- | |
# Slides 2, pg. 4 | |
#- Dados besouros | |
y <- c(6, 13, 18, 28, 52, 53, 61, 60) | |
n <- c(59, 60, 62, 56, 63, 59, 62, 60) | |
x <- c(1.6907, 1.7242, 1.7552, 1.7842, 1.8113, 1.8369, 1.8610, 1.8839) | |
plot(x, y/n, xlab = "Dose", ylab = "Proporção mortos", pch = 20) | |
#- Ajuste com ligacao logito | |
besouro <- cbind(y, n - y) | |
mod1 <- glm(besouro ~ x, family = binomial(link = "logit")) | |
summary(mod1) | |
# Diagnostico | |
res <- residuals2(mod1, standardized = TRUE) | |
plot(res) | |
abline(h = 0, lty = 2) | |
acf(res) | |
eta <- mod1$linear.predictors | |
plot(eta, res) | |
abline(h = 0, lty = 2) | |
plot(x, res) | |
abline(h = 0, lty = 2) | |
z <- resid(mod1, type = "working") + eta | |
plot(z, eta, main = "logito") | |
abline(0, 1) | |
envelope(mod1, standardized = TRUE) | |
p <- ncol(model.matrix(mod1)) | |
n <- nrow(model.matrix(mod1)) | |
cutoff <- qf(0.5, p, n - p); cutoff | |
cooks <- cooks.distance(mod1) # distancia de Cook | |
plot(cooks, type = "h", ylim = c(0, max(cutoff, max(cooks)))) | |
abline(h = cutoff, lty = 2) | |
#- Ajuste com ligacao probito | |
mod2 <- glm(besouro ~ x, family = binomial(link = "probit")) | |
summary(mod2) | |
# Diagnostico | |
res <- residuals2(mod2, standardized = TRUE) | |
plot(res) | |
abline(h = 0, lty = 2) | |
acf(res) | |
eta <- mod2$linear.predictors | |
plot(eta, res) | |
abline(h = 0, lty = 2) | |
plot(x, res) | |
abline(h = 0, lty = 2) | |
z <- resid(mod2, type = "working") + eta | |
plot(z, eta, main = "probit") | |
abline(0, 1) | |
envelope(mod2, standardized = TRUE) | |
p <- ncol(model.matrix(mod2)) | |
n <- nrow(model.matrix(mod2)) | |
cutoff <- qf(0.5, p, n - p); cutoff | |
cooks <- cooks.distance(mod2) # distancia de Cook | |
plot(cooks, type = "h", ylim = c(0, max(cutoff, max(cooks)))) | |
abline(h = cutoff, lty = 2) | |
identify(cooks, n = 1) | |
mod2.omit1 <- update(mod2, subset = c(-1)) # ajuste sem a observacao 1 | |
coef(mod2) | |
coef(mod2.omit1) | |
#- Ajuste com ligacao c-loglog | |
mod3 <- glm(besouro ~ x, family = binomial(link = "cloglog")) | |
summary(mod3) | |
# Diagnostico | |
res <- residuals2(mod3, standardized = TRUE) | |
plot(res) | |
abline(h = 0, lty = 2) | |
acf(res) | |
eta <- mod3$linear.predictors | |
plot(eta, res) | |
abline(h = 0, lty = 2) | |
plot(x, res) | |
abline(h = 0, lty = 2) | |
z <- resid(mod3, type = "working") + eta | |
plot(z, eta, main = "cloglog") | |
abline(0, 1) | |
envelope(mod3, standardized = TRUE) | |
p <- ncol(model.matrix(mod3)) | |
n <- nrow(model.matrix(mod3)) | |
cutoff <- qf(0.5, p, n - p); cutoff | |
cooks <- cooks.distance(mod3) # distancia de Cook | |
plot(cooks, type = "h", ylim = c(0, max(cutoff, max(cooks)))) | |
abline(h = cutoff, lty = 2) | |
# ------------------------------------------------------------------------- | |
# Slides 2, pg. 31 | |
#- Dados | |
library(aplore3) | |
data(chdage) | |
head(chdage) | |
View(chdage) | |
#- Ajuste | |
mod1 <- glm(chd ~ age, family = binomial(link = "logit"), data = chdage) | |
summary(mod1) | |
#- Diagnostico | |
res <- residuals2(mod1, standardized = TRUE) | |
plot(res) | |
abline(h = 0, lty = 2) | |
acf(res) | |
eta <- mod1$linear.predictors | |
plot(eta, res) | |
abline(h = 0, lty = 2) | |
plot(chdage$age, res) | |
abline(h = 0, lty = 2) | |
z <- resid(mod1, type = "working") + eta | |
plot(z, eta) | |
abline(0, 1) | |
envelope(mod1, standardized = TRUE) | |
p <- ncol(model.matrix(mod1)) | |
n <- nrow(model.matrix(mod1)) | |
cutoff <- qf(0.5, p, n - p); cutoff | |
cooks <- cooks.distance(mod1) # distancia de Cook | |
plot(cooks, type = "h", ylim = c(0, max(cutoff, max(cooks)))) | |
abline(h = cutoff, lty = 2) | |
# ------------------------------------------------------------------------- | |
# Slides 2, pg. 43 | |
#- Dados | |
library(dobson) | |
data("doctors") | |
head(doctors) | |
doctors2 <- cbind(doctors, agecat = rep(1:5, 2)) | |
doctors2 | |
dim(doctors2) # 10 x 5 | |
plot(doctors2$agecat, 1E5*doctors2$deaths/doctors2$`person-years`, | |
col = rep(c(2, 1), each = 5), pch = 20, xlab = "Idade (categoria)", | |
ylab = "Mortes por 100000") | |
legend("topleft", c("não fumante", "fumante"), pch = 20, col = 1:2, bty = "n") | |
#- Ajuste | |
mod1 <- glm(deaths ~ agecat + smoking, offset = log(`person-years`/1E5), | |
family = poisson(link = "log"), data = doctors2) | |
summary(mod1) | |
#- Diagnostico | |
res <- residuals2(mod1, standardized = TRUE) | |
res <- residuals2(mod1, standardized = TRUE, identify = 4, labels = NULL) | |
plot(res) | |
abline(h = 0, lty = 2) | |
acf(res) | |
eta <- mod1$linear.predictors | |
plot(eta, res) | |
abline(h = 0, lty = 2) | |
z <- resid(mod1, type = "working") + eta | |
plot(z, eta) | |
abline(0, 1) | |
envelope(mod1, standardized = TRUE) | |
p <- ncol(model.matrix(mod1)) | |
n <- nrow(model.matrix(mod1)) | |
cutoff <- qf(0.5, p, n - p); cutoff | |
cooks <- cooks.distance(mod1) # distancia de Cook | |
plot(cooks, type = "h", ylim = c(0, max(cutoff, max(cooks)))) | |
abline(h = cutoff, lty = 2) | |
identify(cooks, n = 1) | |
mod1.omit5 <- update(mod1, subset = c(-5)) | |
coef(mod1) | |
coef(mod1.omit5) | |
# ------------------------------------------------------------------------- | |
# Slides 2, pg. 58 | |
#- Dados | |
store <- read.csv("~/caminho/para/o/arquivo/store.txt", sep = "") | |
head(store) | |
dim(store) # 110 x 6 | |
#- Ajuste | |
mod1 <- glm(nclientes ~ I(domic/1E3) + I(renda/1E3) + idade + distconc | |
+ distloja, family = poisson(link = "log"), data = store) | |
summary(mod1) | |
#- Diagnostico | |
res <- residuals2(mod1, standardized = TRUE) | |
plot(res) | |
abline(h = 0, lty = 2) | |
acf(res) | |
eta <- mod1$linear.predictors | |
plot(eta, res) | |
abline(h = 0, lty = 2) | |
plot(store$domic/1E3, res) | |
abline(h = 0, lty = 2) | |
plot(store$renda/1E3, res) | |
abline(h = 0, lty = 2) | |
plot(store$idade, res) | |
abline(h = 0, lty = 2) | |
z <- resid(mod1, type = "working") + eta | |
plot(z, eta) | |
abline(0, 1) | |
envelope(mod1, standardized = TRUE) | |
p <- ncol(model.matrix(mod1)) | |
n <- nrow(model.matrix(mod1)) | |
cutoff <- qf(0.5, p, n - p); cutoff | |
cooks <- cooks.distance(mod1) # distancia de Cook | |
plot(cooks, type = "h", ylim = c(0, max(cutoff, max(cooks)))) | |
abline(h = cutoff, lty = 2) | |
# ------------------------------------------------------------------------- | |
# Slides 3, pg. 59 | |
#- Dados | |
pesca <- read.csv("~/caminho/para/o/arquivo/pesca.txt", sep = "") | |
head(pesca) | |
dim(pesca) | |
plot(density(pesca$cpue), xlab = "CPUE (kg/dias pesca)", ylab = "Densidade", main = "") | |
#- Ajuste | |
pesca$ano <- factor(pesca$ano) | |
pesca$trimestre <- factor(pesca$trimestre) | |
mod1 <- glm(cpue ~ latitude + longitude + ano + trimestre + frota, | |
family = Gamma(link = "log"), data = pesca) | |
summary(mod1) | |
mod2 <- glm(cpue ~ latitude + ano + trimestre + frota, | |
family = Gamma(link = "log"), data = pesca) | |
summary(mod2) | |
mod3 <- glm(cpue ~ latitude + frota, | |
family = Gamma(link = "log"), data = pesca) | |
summary(mod3) | |
#- Diagnostico | |
res <- residuals2(mod3, standardized = TRUE) | |
plot(res) | |
abline(h = 0, lty = 2) | |
acf(res) | |
eta <- mod3$linear.predictors | |
plot(eta, res) | |
abline(h = 0, lty = 2) | |
plot(pesca$latitude, res) | |
abline(h = 0, lty = 2) | |
z <- resid(mod3, type = "working") + eta | |
plot(z, eta) | |
abline(0, 1) | |
envelope(mod3, standardized = TRUE) | |
p <- ncol(model.matrix(mod3)) | |
n <- nrow(model.matrix(mod3)) | |
cutoff <- qf(0.5, p, n - p); cutoff | |
cooks <- cooks.distance(mod3) # distancia de Cook | |
plot(cooks, type = "h", ylim = c(0, max(cutoff, max(cooks)))) | |
abline(h = cutoff, lty = 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nclientes domic renda idade distconc distloja | |
9 606 41393 3 3.04 6.32 | |
6 641 23635 18 1.95 8.89 | |
28 505 55475 27 6.54 2.05 | |
11 866 64646 31 1.67 5.81 | |
4 599 31972 7 0.72 8.11 | |
4 520 41755 23 2.24 6.81 | |
0 354 46014 26 0.77 9.27 | |
14 483 34626 1 3.51 7.92 | |
16 1034 85207 13 4.23 4.40 | |
13 456 33021 32 3.07 6.03 | |
9 19 39198 22 2.96 6.09 | |
14 530 38794 5 2.77 6.08 | |
5 337 30855 1 1.33 9.86 | |
9 586 28852 7 2.98 8.64 | |
9 1113 120065 9 3.58 5.26 | |
7 525 32229 3 1.27 7.56 | |
4 377 36828 15 1.92 8.91 | |
26 1127 90302 26 5.83 1.74 | |
32 877 51707 27 5.19 3.66 | |
26 1007 89860 55 5.03 2.03 | |
11 657 60513 32 4.38 8.30 | |
12 302 42191 54 3.41 5.21 | |
3 603 28736 41 0.34 8.29 | |
15 556 49129 33 4.78 3.89 | |
12 635 29308 42 2.53 6.17 | |
9 386 26734 14 4.99 9.70 | |
14 1011 57862 54 4.60 3.94 | |
10 925 70030 36 4.58 8.66 | |
22 898 46027 44 3.03 5.60 | |
8 731 32202 43 5.15 9.67 | |
3 584 32871 13 1.47 8.02 | |
11 439 29564 18 3.67 5.10 | |
2 153 46806 21 0.84 9.18 | |
6 1069 59805 22 2.50 9.43 | |
11 443 42555 53 2.62 5.75 | |
10 392 36998 7 1.03 7.74 | |
0 828 85664 4 1.30 9.66 | |
15 159 21238 4 2.98 8.66 | |
9 830 47972 40 2.28 9.26 | |
16 234 33246 26 3.95 4.61 | |
29 1004 45927 24 4.90 2.69 | |
6 643 58315 8 0.78 6.26 | |
26 741 69177 9 6.61 0.87 | |
13 306 40886 27 4.53 2.68 | |
0 180 44588 14 0.88 9.38 | |
8 644 47347 35 2.94 7.69 | |
8 109 31791 9 4.37 9.31 | |
21 809 42740 17 4.10 4.75 | |
12 722 59175 35 2.38 5.09 | |
26 1006 48862 48 5.04 2.21 | |
3 786 54678 20 3.59 8.52 | |
7 1041 59835 40 1.68 7.59 | |
5 524 51756 39 0.57 9.10 | |
9 725 34817 18 1.88 7.96 | |
13 482 29942 14 3.17 6.91 | |
28 666 68684 25 5.78 2.55 | |
10 450 64790 3 4.35 6.03 | |
12 667 58535 25 2.78 5.59 | |
6 921 42919 13 2.48 7.69 | |
11 412 40722 32 2.47 9.43 | |
12 526 42120 30 4.29 6.15 | |
11 523 28647 43 2.69 7.54 | |
9 1066 61464 40 1.15 8.25 | |
8 1001 70136 29 2.58 9.67 | |
9 669 34595 38 4.06 8.78 | |
8 582 30878 58 1.91 6.86 | |
6 872 39366 52 0.73 8.67 | |
6 758 61563 31 3.08 8.33 | |
15 782 38412 26 2.72 6.71 | |
15 551 41045 2 3.62 7.45 | |
12 201 23864 43 4.80 8.74 | |
10 730 38647 9 0.67 7.92 | |
8 738 58387 13 2.01 6.60 | |
3 469 37242 40 1.42 8.37 | |
10 898 38337 32 2.63 9.56 | |
10 780 68201 5 4.12 6.69 | |
15 622 41066 46 4.48 4.10 | |
6 391 40873 19 1.67 6.90 | |
9 531 54655 40 2.32 5.69 | |
21 566 49826 1 3.06 4.03 | |
13 410 29013 50 2.68 7.58 | |
8 719 78082 31 2.70 4.89 | |
6 684 57506 51 2.13 8.31 | |
8 865 47118 46 2.17 9.06 | |
21 1031 72373 48 6.27 1.75 | |
7 862 67787 1 2.10 8.63 | |
19 758 40305 15 3.95 5.58 | |
13 1141 50026 45 2.79 6.18 | |
24 1289 98701 8 5.87 2.73 | |
7 674 58195 54 4.30 6.40 | |
3 683 47991 57 1.54 9.52 | |
8 650 63123 15 3.17 9.46 | |
9 406 39051 29 3.11 9.62 | |
18 966 114633 38 6.33 2.22 | |
12 1103 55773 44 4.58 8.68 | |
8 312 43393 41 2.25 6.43 | |
16 787 61765 53 5.39 3.37 | |
5 416 33348 48 1.48 7.66 | |
8 528 44541 31 4.91 9.67 | |
11 919 40795 8 2.97 7.79 | |
12 482 55972 9 2.91 5.85 | |
14 781 33140 30 1.42 5.71 | |
17 120 19673 21 2.65 6.25 | |
17 693 36190 6 4.70 9.54 | |
6 348 25768 42 1.43 7.11 | |
15 780 53974 47 4.21 6.41 | |
10 752 71814 1 3.13 5.47 | |
6 817 54429 47 1.90 9.90 | |
4 268 34022 54 1.20 9.51 | |
6 519 52850 43 2.92 8.62 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tipo tempo | |
1 3.03 | |
1 5.53 | |
1 5.60 | |
1 9.30 | |
1 9.92 | |
1 12.51 | |
1 12.95 | |
1 15.21 | |
1 16.04 | |
1 16.84 | |
2 3.19 | |
2 4.26 | |
2 4.47 | |
2 4.53 | |
2 4.67 | |
2 4.69 | |
2 5.78 | |
2 6.79 | |
2 9.37 | |
2 12.75 | |
3 3.46 | |
3 5.22 | |
3 5.69 | |
3 6.54 | |
3 9.16 | |
3 9.40 | |
3 10.19 | |
3 10.71 | |
3 12.58 | |
3 13.41 | |
4 5.88 | |
4 6.74 | |
4 6.90 | |
4 6.98 | |
4 7.21 | |
4 8.14 | |
4 8.59 | |
4 9.80 | |
4 12.28 | |
4 25.46 | |
5 6.43 | |
5 9.97 | |
5 10.39 | |
5 13.55 | |
5 14.45 | |
5 14.72 | |
5 16.81 | |
5 18.39 | |
5 20.84 | |
5 21.51 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment