-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
209 lines (158 loc) · 5.46 KB
/
index.Rmd
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
---
lang: "PT-br"
title: "Introdução ao GAMLSS"
author: "Gustavo Thomas e Cristian Villegas"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [book.bib, packages.bib]
# url: your book url like https://bookdown.org/yihui/bookdown
# cover-image: path to the social sharing image like images/cover.jpg
description: |
This is a minimal example of using the bookdown package to write a book.
The HTML output format for this example is bookdown::gitbook,
set in the _output.yml file.
link-citations: yes
github-repo: rstudio/bookdown-demo
---
```{r, echo=FALSE}
knitr::opts_chunk$set(message = FALSE,
warning = TRUE,
cache = TRUE)
```
# Introdução
```{r}
library(knitr)
library(gamlss)
library(xtable)
PPP <- par(mfrow=c(2,2))
plot(R~Fl, data=rent, col=gray(0.7), pch=15, cex=0.5)
plot(R~A, data=rent, col=gray(0.7), pch=15, cex=0.5)
plot(R~H, data=rent, col=gray(0.7), pch=15, cex=0.5)
plot(R~loc, data=rent, col=gray(0.7), pch=15, cex=0.5)
```
```{r}
par(PPP)
# ----eval=FALSE----------------------------------------------------------
## R ~ Fl+A+H+loc
## ----cache=TRUE----------------------------------------------------------
r1 <- gamlss(R ~ Fl+A+H+loc, family=NO, data=rent, trace=FALSE)
l1 <- lm(R ~ Fl+A+H+loc,data=rent)
coef(r1)
coef(l1)
```
```{r, tidy=TRUE, tidy.opts=list(width=60)}
fitted(r1, "sigma")[1]
summary(r1)
Rsq(r1)
## ----LM_residual_plot,echo=TRUE,fig.show='hide', fig.asp=1---------------
plot(r1)
## ----cache=TRUE----------------------------------------------------------
### using gamlss
r2 <- gamlss(R ~ Fl+A+H+loc, family=GA, data=rent)
coef(r2)
coef(r2, "sigma") ### extract log(sigma)
deviance(r2)
### using glm
l2 <- glm(R ~ Fl+A+H+loc, family=Gamma(link="log"), data=rent)
coef(l2)
summary(l2)$dispersion ### extract phi
deviance(l2)
## ------------------------------------------------------------------------
summary(r2)
## ------------------------------------------------------------------------
r22 <- gamlss(R ~ Fl+A+H+loc, family=IG, data=rent, trace=FALSE)
GAIC(r1, r2, r22, k=0) # GD
## ----GLM_residual_plot,echo=TRUE,fig.show='hide', fig.asp=1--------------
plot(r2)
## ----cache=TRUE----------------------------------------------------------
r3 <- gamlss(R ~ pb(Fl)+pb(A)+H+loc, family=GA, data=rent,
trace=FALSE)
AIC(r2,r3)
## ------------------------------------------------------------------------
summary(r3)
## ----cache=TRUE----------------------------------------------------------
drop1(r3)
## ----GAM_termplot, echo=TRUE,fig.show='hide', fig.asp=1,cache=TRUE-------
term.plot(r3, pages=1, ask=FALSE)
## ----GAM_wormplot, echo=TRUE,fig.show='hide', fig.asp=1,cache=TRUE-------
wp(r3, ylim.all=.6)
## ----cache=TRUE----------------------------------------------------------
r4 <- gamlss(R ~ pb(Fl)+pb(A)+H+loc, sigma.fo=~pb(Fl)+pb(A)+H+loc,
family=GA, data=rent, trace=FALSE)
r5 <- gamlss(R ~ pb(Fl)+pb(A)+H+loc, sigma.fo=~pb(Fl)+pb(A)+H+loc,
family=IG, data=rent, trace=FALSE)
AIC(r3, r4, r5)
## ----MADAM_termplot, echo=TRUE,fig.show='hide', fig.asp=1,cache=TRUE-----
term.plot(r4, pages=1, what="sigma", ask=FALSE)
## ----cache=TRUE----------------------------------------------------------
drop1(r4, what="sigma")
## ----MADAM_wormplot, echo=TRUE,fig.show='hide', fig.asp=1,cache=TRUE-----
wp(r4, ylim.all=.6)
## ----cache=TRUE----------------------------------------------------------
r6 <- gamlss(R ~ pb(Fl)+pb(A)+H+loc, sigma.fo=~pb(Fl)+pb(A)+H+loc,
nu.fo=~1, family=BCCGo, data=rent, trace=FALSE)
r7 <- gamlss(R ~ pb(Fl)+pb(A)+H+loc,sigma.fo=~pb(Fl)+pb(A)+H+loc,
nu.fo=~pb(Fl)+pb(A)+H+loc, family=BCCGo, data=rent,
trace=FALSE)
AIC(r4, r6, r7)
## ----WP_BCCG1_CH1, echo=TRUE, fig.show='hide', fig.asp=1-----------------
wp(r6, ylim.all=.6) ; title("r6: BCCG(mu, sigma)")
wp(r7, ylim.all=.6) ; title("r7: BCCG(mu, sigma, nu)")
```
## exercises Chapter 1
```{r, eval=FALSE}
## data(airquality)
## plot(airquality[,-c(5,6)])
## # Fit the standard linear model
## air.lm <- lm(Ozone~Temp+Wind+Solar.R,data=airquality)
## summary(air.lm)
## op<-par(mfrow=c(1,3))
## termplot(air.lm,partial.resid=TRUE,se=T)
## par(op)
## op<-par(mfrow=c(1,2))
## plot(air.lm,which=1:2)
## par(op)
## library(gamlss)
## da <- na.omit(airquality) # clear the data of NA's
## mno<-gamlss(Ozone~Temp+Wind+Solar.R, data=da) # fit the model
## summary(mno)
## term.plot(mno, pages=1, partial=T) # plot the fitted terms
## plot(mno)
## wp(mno)
## # fit different distributions
## mga <- gamlss(Ozone~Temp+Wind+Solar.R, data=da, family=GA)
## mig <- gamlss(Ozone~Temp+Wind+Solar.R, data=da, family=IG)
## mbccg <- gamlss(Ozone~Temp+Wind+Solar.R, data=da, family=BCCGo)
## GAIC(mno, mga, mig, mbccg)
## # fit smoothers
## mga1=gamlss(Ozone~pb(Temp)+pb(Wind)+pb(Solar.R),data=da,
## family=GA)
## term.plot(mga1, pages=1)
## plot(mga1)
## wp(mga1)
```
## gamlss.demo()
```{r, eval=FALSE}
#install.packages("gamlss.demo", dep=TRUE)
#Based on rpanel
library(gamlss.demo)
#Examples
#t family distribution
demo.TF()
#Skew Normal Type 1 distribution
demo.SN1()
#Box-Cox Power Exponential distribution
demo.BCPE()
#Demos for smoothing techniques
demo.BSplines()
demo.PSplines()
demo.interpolateSmo()
demo.histSmo()
#Interface for demonstrating the gamlss.family distributions
demoDist()
#Demo for local polynomial smoothing
demoLpolyS()
#The demo for gamlss distributions and smoothing
gamlss.demo()
```