-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
425 lines (425 loc) · 20.1 KB
/
.Rhistory
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
liz <- read.csv("data/polis.csv")
head(liz)
fit <- lm(PA ~ PARATIO, data = liz)
summary(fit)
library(ggplot2)
ggplot(liz, aes(x=PARATIO, y=PA)) + geom_point() + geom_smooth(method="lm", se=FALSE)
ggplot(liz, aes(x=PARATIO, y=PA)) + geom_smooth(method="glm", family="binomial", se=FALSE, size = 2) + ylab("Предсказанная вероятность встречи") + geom_point()
liz_model <- glm(PA ~ PARATIO , family="binomial", data = liz)
summary(liz_model)
liz_model <- glm(PA ~ PARATIO , family="binomial", data = liz)
summary(liz_model)
(Dev_resid <- -2*as.numeric(logLik(liz_model))) #Остаточная девианса
(Dev_nul <- -2*as.numeric(logLik(update(liz_model, ~-PARATIO)))) #Нулевая девианса
(G2 <- Dev_nul - Dev_resid)
#Остаточная девианса
Dev_resid <- -2*as.numeric(logLik(liz_model))
#Нулевая девианса
Dev_nul <- -2*as.numeric(logLik(update(liz_model, ~-PARATIO)))
# Значение критерия
(G2 <- Dev_nul - Dev_resid)
(p_value <- 1 - pchisq(G2, df = 1))
anova(liz_model, test="Chi")
coef(liz_model)
exp(coef(liz_model)[2])
ggplot(liz, aes(x=PARATIO, y=PA)) + geom_point() + geom_smooth(method="glm", family="binomial", se=TRUE, size = 2) + ylab("Вероятность встречи ящериц") + annotate("text", x=40, y=0.75, parse=TRUE, label = "pi == frac(e ^ {beta[0]+beta[1] %.% x}, 1 + e ^ {beta[0]+beta[1] %.% x})", size = 10)
confint(liz_model) # для логитов
exp(confint(liz_model)) # для отношения шансов
#-- RUN THE FRAGMENT BETWEEN LINES BEFORE COMPILING MARKDOWN
# to conimages markdown parsing
options(markdown.extensions = c("no_intra_emphasis", "tables", "fenced_code", "autolink", "strikethrough", "lax_spacing", "space_headers", "latex_math"))
#------
# output options
options(width = 70, scipen = 6, digits = 3)
# to render cyrillics in plots use cairo pdf
options(device = function(file, width = 7, height = 7, ...) {
cairo_pdf(tempfile(), width = width, height = height, ...)
})
library(knitr)
# chunk default options
opts_chunk$set(fig.align='center', tidy = TRUE, fig.width = 7, fig.height = 3, message=FALSE, warning=FALSE)
myt <- read.table("data/myt.csv", sep=";", header =T)
head(myt, 12)
myt$Sq_Recruits <- sqrt(myt$Recruits)
myt$fYear <- factor(myt$Year)
mod_formula <- formula(Sq_Recruits ~ Large + fYear + Bank + Large:fYear + Large:Bank )
M1_lm <- lm(mod_formula , data = myt)
anova(M1_lm)
library(ggplot2)
library(gridExtra)
diag_M1_lm <- fortify(M1_lm)
Res_plot1 <- ggplot(diag_M1_lm, aes(x=.fitted, y = .stdresid)) + geom_point() + geom_hline(yintercept = 0) + geom_smooth(se = F)
Res_plot2 <- ggplot(diag_M1_lm, aes(x=Large, y = .stdresid)) + geom_point() + geom_hline(yintercept = 0)+ geom_smooth(se = F)
Res_plot3 <- ggplot(diag_M1_lm, aes(x=fYear, y = .stdresid)) + geom_boxplot() + geom_hline(yintercept = 0) + theme(axis.text.x = element_text(angle = 90))
Res_plot4 <- ggplot(diag_M1_lm, aes(x=Bank, y = .stdresid)) + geom_boxplot() + geom_hline(yintercept = 0)
grid.arrange(Res_plot1, Res_plot2, Res_plot3, Res_plot4, ncol = 2)
grid.arrange(Res_plot2, Res_plot3, ncol = 2)
library(nlme)
M1_gls <- gls(mod_formula, data = myt )
anova(M1_gls)
M2_gls <- gls(mod_formula, data = myt, weights = varFixed( ~ Large))
AIC(M1_gls, M2_gls)
M3_gls <- gls(mod_formula, data = myt, weights = varIdent(form = ~1|fYear))
anova(M1_gls, M3_gls)
M3_gls2 <- gls(mod_formula, data = myt,
weights = varIdent(form = ~1|Bank))
anova(M1_gls, M3_gls2)
summary(M3_gls)
M4_gls <- gls(mod_formula, data = myt, weights = varPower(form = ~ Large))
M4_gls$modelStruct
M5_gls <- gls(mod_formula, data = myt, weights = varPower(form = ~ Large|fYear))
M6_gls <- gls(mod_formula, data = myt, weights = varPower(form = ~ Large|Bank))
M5_gls$modelStruct
M6_gls$modelStruct
M7_gls <- gls(mod_formula, data = myt, weights = varExp(form = ~ Large))
M8_gls <- gls(mod_formula, data = myt, weights = varExp(form = ~ Large|fYear))
M9_gls <- gls(mod_formula, data = myt, weights = varExp(form = ~ Large|Bank))
M7_gls$modelStruct
M8_gls$modelStruct
M9_gls$modelStruct
M10_gls <- gls(mod_formula, data = myt,
weights = varConstPower(form = ~ Large))
#M11_gls <-gls(mod_formula, data = myt,
# weights = varConstPower(form = ~ Large|fYear))
M12_gls <- gls(mod_formula, data = myt,
weights = varConstPower(form = ~ Large|Bank))
M10_gls$modelStruct
M12_gls$modelStruct
M13_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ fYear),
varPower(form = ~ Large)))
M14_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ Bank),
varPower(form = ~ Large)))
M15_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ fYear),
varExp(form = ~ Large)))
M16_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ Bank),
varExp(form = ~ Large)))
AICs <- AIC(M1_gls, M2_gls, M3_gls,
M4_gls, M5_gls, M6_gls,
M7_gls, M8_gls, M9_gls,
M10_gls, M12_gls,M13_gls,
M14_gls, M15_gls, M16_gls)
AICs[AICs$AIC == min(AICs$AIC),]
M5_gls$call
diag_gls <- data.frame(.pears_resid = residuals(M5_gls, type = "pearson"), .fitted = fitted(M5_gls), Large = myt$Large, fYear = myt$fYear, Bank = myt$Bank)
Diag_gls_plot1 <- ggplot(diag_gls, aes(x=.fitted, y = .pears_resid)) + geom_point() + geom_hline(yintercept = 0) + geom_smooth(se = F)
Diag_gls_plot2 <- ggplot(diag_gls, aes(x=Large, y = .pears_resid)) + geom_point() + geom_hline(yintercept = 0)+ geom_smooth(se = F)
Diag_gls_plot3 <- ggplot(diag_gls, aes(x=fYear, y = .pears_resid)) + geom_boxplot() + geom_hline(yintercept = 0)
Diag_gls_plot4 <- ggplot(diag_gls, aes(x=Bank, y = .pears_resid)) + geom_boxplot() + geom_hline(yintercept = 0)
Res_plot1 <- Res_plot1 + ggtitle("Было \nв начальной модели")
Diag_gls_plot1 <- Diag_gls_plot1 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot1, Diag_gls_plot1, ncol = 2)
Res_plot2 <- Res_plot2 + ggtitle("Было \nв начальной модели")
Diag_gls_plot2 <- Diag_gls_plot2 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot2, Diag_gls_plot2, ncol = 2)
Res_plot3 <- Res_plot3 + ggtitle("Было \nв начальной модели")
Diag_gls_plot3 <- Diag_gls_plot3 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot3, Diag_gls_plot3, ncol = 2)
Res_plot4 <- Res_plot4 + ggtitle("Было \nв начальной модели")
Diag_gls_plot4 <- Diag_gls_plot4 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot4, Diag_gls_plot4, ncol = 2)
M5_gls_ML <- update(M5_gls, method = "ML")
drop1(M5_gls_ML, test = "Chi")
qplot(x=c(1997, 1999:2011), y=as.vector(unlist(M5_gls$modelStruct))) + xlab("Годы") + ylab("Delta")
data("BodyWeight")
bw <- as.data.frame(BodyWeight)
head(bw, 14)
M1 <- gls(weight ~ Time*Diet, data = bw)
anova(M1)
M2 <- lme(weight ~ Time*Diet, data = bw, random = ~1|Rat)
M3 <- lme(weight ~ Time*Diet, data = bw, random = ~1 + Time|Rat)
AIC(M1, M2, M3)
anova(M3)
diagnostic <- data.frame(.fitted = fitted(M3), .residuals = residuals(M3, type = "pearson"), Diet = bw$Diet, Time = bw$Time)
Pl1 <- ggplot(diagnostic, aes(x=.fitted, y=.residuals) ) + geom_point()
Pl2 <- ggplot(diagnostic, aes(x=Time, y=.residuals) ) + geom_point()
Pl3 <- ggplot(diagnostic, aes(x=Diet, y=.residuals) ) + geom_boxplot()
grid.arrange(Pl1, Pl2, Pl3, ncol=2)
M3_1 <- update(M3, weights = varIdent(form = ~ 1|Diet))
M3_2 <- update(M3, weights = varPower(form = ~Time))
M3_3 <- update(M3, weights = varPower(form = ~Time|Diet))
M3_4 <- update(M3, weights = varExp(form = ~Time))
M3_5 <- update(M3, weights = varExp(form = ~Time|Diet))
M3_6 <- update(M3, weights = varComb(varExp(form = ~Time), varIdent(form = ~1|Diet)))
AIC(M3, M3_1, M3_2, M3_3, M3_4, M3_5, M3_6)
anova(M3_5)
MyData <- expand.grid(Time = 1:64, Diet = factor(1:3))
MyData$Predicted <- predict(M3_5, newdata = MyData, level = 0)
ggplot(MyData, aes(x = Time, y = Predicted, color = Diet)) + geom_line( size = 1.5) + geom_point(data = bw, aes(x= Time, y = weight), position = position_jitter())
M5_gls_ML <- update(M5_gls, method = "ML")
drop1(M5_gls_ML, test = "Chi")
drop1.gls(M5_gls_ML, test = "Chi")
class(M5_gls_ML)
M5_gls_ML <- update(M5_gls, method = "ML")
drop1(M5_gls_ML, test = "Chi")
MethodsList(drop1)
MethodsList(M5_gls_ML)
nlme::drop1(M5_gls_ML, test = "Chi")
MASS::drop1(M5_gls_ML, test = "Chi")
install.packages(c("checkmate", "devtools", "DiagrammeR", "forestplot", "geomorph", "ggmap", "ggrepel", "Gmisc", "kernlab", "knitr", "ks", "labdsv", "latticeExtra", "lsmeans", "maptools", "MCMCglmm", "memoise", "mgcv", "multcomp", "mvtnorm", "nlme", "pbkrtest", "permute", "pgirmess", "phangorn", "RcppArmadillo", "RcppEigen", "R.matlab", "RMySQL", "rstudioapi", "sfsmisc", "sp", "spaMM", "statmod", "TH.data", "tidyr", "tikzDevice", "visNetwork", "visreg", "xtable"))
# Chunk 1: setup
#-- RUN THE FRAGMENT BETWEEN LINES BEFORE COMPILING MARKDOWN
# to conimages markdown parsing
options(markdown.extensions = c("no_intra_emphasis", "tables", "fenced_code", "autolink", "strikethrough", "lax_spacing", "space_headers", "latex_math"))
#------
# output options
options(width = 70, scipen = 6, digits = 3)
# to render cyrillics in plots use cairo pdf
options(device = function(file, width = 7, height = 7, ...) {
cairo_pdf(tempfile(), width = width, height = height, ...)
})
library(knitr)
# chunk default options
opts_chunk$set(fig.align='center', tidy = TRUE, fig.width = 7, fig.height = 3, message=FALSE, warning=FALSE)
# Chunk 2: muss-data
myt <- read.table("data/myt.csv", sep=";", header =T)
head(myt, 12)
# Chunk 3
myt$Sq_Recruits <- sqrt(myt$Recruits)
myt$fYear <- factor(myt$Year)
# Chunk 4
mod_formula <- formula(Sq_Recruits ~ Large + fYear + Bank + Large:fYear + Large:Bank )
M1_lm <- lm(mod_formula , data = myt)
anova(M1_lm)
# Chunk 5
library(ggplot2)
library(gridExtra)
diag_M1_lm <- fortify(M1_lm)
Res_plot1 <- ggplot(diag_M1_lm, aes(x=.fitted, y = .stdresid)) + geom_point() + geom_hline(yintercept = 0) + geom_smooth(se = F)
Res_plot2 <- ggplot(diag_M1_lm, aes(x=Large, y = .stdresid)) + geom_point() + geom_hline(yintercept = 0)+ geom_smooth(se = F)
Res_plot3 <- ggplot(diag_M1_lm, aes(x=fYear, y = .stdresid)) + geom_boxplot() + geom_hline(yintercept = 0) + theme(axis.text.x = element_text(angle = 90))
Res_plot4 <- ggplot(diag_M1_lm, aes(x=Bank, y = .stdresid)) + geom_boxplot() + geom_hline(yintercept = 0)
# Chunk 6
grid.arrange(Res_plot1, Res_plot2, Res_plot3, Res_plot4, ncol = 2)
# Chunk 7
grid.arrange(Res_plot2, Res_plot3, ncol = 2)
# Chunk 8
library(nlme)
M1_gls <- gls(mod_formula, data = myt )
# Chunk 9
anova(M1_gls)
# Chunk 10
M2_gls <- gls(mod_formula, data = myt, weights = varFixed( ~ Large))
# Chunk 11
AIC(M1_gls, M2_gls)
# Chunk 12
M3_gls <- gls(mod_formula, data = myt, weights = varIdent(form = ~1|fYear))
# Chunk 13
anova(M1_gls, M3_gls)
# Chunk 14
M3_gls2 <- gls(mod_formula, data = myt,
weights = varIdent(form = ~1|Bank))
anova(M1_gls, M3_gls2)
# Chunk 16
M4_gls <- gls(mod_formula, data = myt, weights = varPower(form = ~ Large))
# Chunk 17
M4_gls$modelStruct
# Chunk 18
M5_gls <- gls(mod_formula, data = myt, weights = varPower(form = ~ Large|fYear))
M6_gls <- gls(mod_formula, data = myt, weights = varPower(form = ~ Large|Bank))
# Chunk 19
M5_gls$modelStruct
M6_gls$modelStruct
# Chunk 20
M7_gls <- gls(mod_formula, data = myt, weights = varExp(form = ~ Large))
M8_gls <- gls(mod_formula, data = myt, weights = varExp(form = ~ Large|fYear))
M9_gls <- gls(mod_formula, data = myt, weights = varExp(form = ~ Large|Bank))
# Chunk 21
M7_gls$modelStruct
M8_gls$modelStruct
M9_gls$modelStruct
# Chunk 22
M10_gls <- gls(mod_formula, data = myt,
weights = varConstPower(form = ~ Large))
#M11_gls <-gls(mod_formula, data = myt,
# weights = varConstPower(form = ~ Large|fYear))
M12_gls <- gls(mod_formula, data = myt,
weights = varConstPower(form = ~ Large|Bank))
# Chunk 23
M10_gls$modelStruct
M12_gls$modelStruct
# Chunk 24
M13_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ fYear),
varPower(form = ~ Large)))
M14_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ Bank),
varPower(form = ~ Large)))
M15_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ fYear),
varExp(form = ~ Large)))
M16_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ Bank),
varExp(form = ~ Large)))
# Chunk 25
AICs <- AIC(M1_gls, M2_gls, M3_gls,
M4_gls, M5_gls, M6_gls,
M7_gls, M8_gls, M9_gls,
M10_gls, M12_gls,M13_gls,
M14_gls, M15_gls, M16_gls)
# Chunk 26
AICs[AICs$AIC == min(AICs$AIC),]
# Chunk 27
M5_gls$call
# Chunk 28
diag_gls <- data.frame(.pears_resid = residuals(M5_gls, type = "pearson"), .fitted = fitted(M5_gls), Large = myt$Large, fYear = myt$fYear, Bank = myt$Bank)
Diag_gls_plot1 <- ggplot(diag_gls, aes(x=.fitted, y = .pears_resid)) + geom_point() + geom_hline(yintercept = 0) + geom_smooth(se = F)
Diag_gls_plot2 <- ggplot(diag_gls, aes(x=Large, y = .pears_resid)) + geom_point() + geom_hline(yintercept = 0)+ geom_smooth(se = F)
Diag_gls_plot3 <- ggplot(diag_gls, aes(x=fYear, y = .pears_resid)) + geom_boxplot() + geom_hline(yintercept = 0)
Diag_gls_plot4 <- ggplot(diag_gls, aes(x=Bank, y = .pears_resid)) + geom_boxplot() + geom_hline(yintercept = 0)
Res_plot1 <- Res_plot1 + ggtitle("Было \nв начальной модели")
Diag_gls_plot1 <- Diag_gls_plot1 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot1, Diag_gls_plot1, ncol = 2)
# Chunk 29
Res_plot2 <- Res_plot2 + ggtitle("Было \nв начальной модели")
Diag_gls_plot2 <- Diag_gls_plot2 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot2, Diag_gls_plot2, ncol = 2)
# Chunk 30
Res_plot3 <- Res_plot3 + ggtitle("Было \nв начальной модели")
Diag_gls_plot3 <- Diag_gls_plot3 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot3, Diag_gls_plot3, ncol = 2)
# Chunk 31
Res_plot4 <- Res_plot4 + ggtitle("Было \nв начальной модели")
Diag_gls_plot4 <- Diag_gls_plot4 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot4, Diag_gls_plot4, ncol = 2)
M5_gls_ML <- update(M5_gls, method = "ML")
drop1(M5_gls_ML, test = "Chi")
M5_gls_ML
# Chunk 1: setup
#-- RUN THE FRAGMENT BETWEEN LINES BEFORE COMPILING MARKDOWN
# to conimages markdown parsing
options(markdown.extensions = c("no_intra_emphasis", "tables", "fenced_code", "autolink", "strikethrough", "lax_spacing", "space_headers", "latex_math"))
#------
# output options
options(width = 70, scipen = 6, digits = 3)
# to render cyrillics in plots use cairo pdf
options(device = function(file, width = 7, height = 7, ...) {
cairo_pdf(tempfile(), width = width, height = height, ...)
})
library(knitr)
# chunk default options
opts_chunk$set(fig.align='center', tidy = TRUE, fig.width = 7, fig.height = 3, message=FALSE, warning=FALSE)
# Chunk 2: muss-data
myt <- read.table("data/myt.csv", sep=";", header =T)
head(myt, 12)
# Chunk 3
myt$Sq_Recruits <- sqrt(myt$Recruits)
myt$fYear <- factor(myt$Year)
# Chunk 4
mod_formula <- formula(Sq_Recruits ~ Large + fYear + Bank + Large:fYear + Large:Bank )
M1_lm <- lm(mod_formula , data = myt)
anova(M1_lm)
# Chunk 5
library(ggplot2)
library(gridExtra)
diag_M1_lm <- fortify(M1_lm)
Res_plot1 <- ggplot(diag_M1_lm, aes(x=.fitted, y = .stdresid)) + geom_point() + geom_hline(yintercept = 0) + geom_smooth(se = F)
Res_plot2 <- ggplot(diag_M1_lm, aes(x=Large, y = .stdresid)) + geom_point() + geom_hline(yintercept = 0)+ geom_smooth(se = F)
Res_plot3 <- ggplot(diag_M1_lm, aes(x=fYear, y = .stdresid)) + geom_boxplot() + geom_hline(yintercept = 0) + theme(axis.text.x = element_text(angle = 90))
Res_plot4 <- ggplot(diag_M1_lm, aes(x=Bank, y = .stdresid)) + geom_boxplot() + geom_hline(yintercept = 0)
# Chunk 6
grid.arrange(Res_plot1, Res_plot2, Res_plot3, Res_plot4, ncol = 2)
# Chunk 7
grid.arrange(Res_plot2, Res_plot3, ncol = 2)
# Chunk 8
library(nlme)
M1_gls <- gls(mod_formula, data = myt )
# Chunk 9
anova(M1_gls)
# Chunk 10
M2_gls <- gls(mod_formula, data = myt, weights = varFixed( ~ Large))
# Chunk 11
AIC(M1_gls, M2_gls)
# Chunk 12
M3_gls <- gls(mod_formula, data = myt, weights = varIdent(form = ~1|fYear))
# Chunk 13
anova(M1_gls, M3_gls)
# Chunk 14
M3_gls2 <- gls(mod_formula, data = myt,
weights = varIdent(form = ~1|Bank))
anova(M1_gls, M3_gls2)
# Chunk 16
M4_gls <- gls(mod_formula, data = myt, weights = varPower(form = ~ Large))
# Chunk 17
M4_gls$modelStruct
# Chunk 18
M5_gls <- gls(mod_formula, data = myt, weights = varPower(form = ~ Large|fYear))
M6_gls <- gls(mod_formula, data = myt, weights = varPower(form = ~ Large|Bank))
# Chunk 19
M5_gls$modelStruct
M6_gls$modelStruct
# Chunk 20
M7_gls <- gls(mod_formula, data = myt, weights = varExp(form = ~ Large))
M8_gls <- gls(mod_formula, data = myt, weights = varExp(form = ~ Large|fYear))
M9_gls <- gls(mod_formula, data = myt, weights = varExp(form = ~ Large|Bank))
# Chunk 21
M7_gls$modelStruct
M8_gls$modelStruct
M9_gls$modelStruct
# Chunk 22
M10_gls <- gls(mod_formula, data = myt,
weights = varConstPower(form = ~ Large))
#M11_gls <-gls(mod_formula, data = myt,
# weights = varConstPower(form = ~ Large|fYear))
M12_gls <- gls(mod_formula, data = myt,
weights = varConstPower(form = ~ Large|Bank))
# Chunk 23
M10_gls$modelStruct
M12_gls$modelStruct
# Chunk 24
M13_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ fYear),
varPower(form = ~ Large)))
M14_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ Bank),
varPower(form = ~ Large)))
M15_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ fYear),
varExp(form = ~ Large)))
M16_gls <- gls(mod_formula, data = myt,
weights = varComb(varIdent(form = ~ Bank),
varExp(form = ~ Large)))
# Chunk 25
AICs <- AIC(M1_gls, M2_gls, M3_gls,
M4_gls, M5_gls, M6_gls,
M7_gls, M8_gls, M9_gls,
M10_gls, M12_gls,M13_gls,
M14_gls, M15_gls, M16_gls)
# Chunk 26
AICs[AICs$AIC == min(AICs$AIC),]
# Chunk 27
M5_gls$call
# Chunk 28
diag_gls <- data.frame(.pears_resid = residuals(M5_gls, type = "pearson"), .fitted = fitted(M5_gls), Large = myt$Large, fYear = myt$fYear, Bank = myt$Bank)
Diag_gls_plot1 <- ggplot(diag_gls, aes(x=.fitted, y = .pears_resid)) + geom_point() + geom_hline(yintercept = 0) + geom_smooth(se = F)
Diag_gls_plot2 <- ggplot(diag_gls, aes(x=Large, y = .pears_resid)) + geom_point() + geom_hline(yintercept = 0)+ geom_smooth(se = F)
Diag_gls_plot3 <- ggplot(diag_gls, aes(x=fYear, y = .pears_resid)) + geom_boxplot() + geom_hline(yintercept = 0)
Diag_gls_plot4 <- ggplot(diag_gls, aes(x=Bank, y = .pears_resid)) + geom_boxplot() + geom_hline(yintercept = 0)
Res_plot1 <- Res_plot1 + ggtitle("Было \nв начальной модели")
Diag_gls_plot1 <- Diag_gls_plot1 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot1, Diag_gls_plot1, ncol = 2)
# Chunk 29
Res_plot2 <- Res_plot2 + ggtitle("Было \nв начальной модели")
Diag_gls_plot2 <- Diag_gls_plot2 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot2, Diag_gls_plot2, ncol = 2)
# Chunk 30
Res_plot3 <- Res_plot3 + ggtitle("Было \nв начальной модели")
Diag_gls_plot3 <- Diag_gls_plot3 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot3, Diag_gls_plot3, ncol = 2)
# Chunk 31
Res_plot4 <- Res_plot4 + ggtitle("Было \nв начальной модели")
Diag_gls_plot4 <- Diag_gls_plot4 + ggtitle("Стало после моделирования \nструктуры дисперсии")
grid.arrange(Res_plot4, Diag_gls_plot4, ncol = 2)
drop1(M5_gls_ML, test = "Chi")
M5_gls_ML <- update(M5_gls, method = "ML")
# С какого-то момента перестал работать drop1()
drop1(M5_gls_ML, test = "Chi")
formula(M5_gls_ML)
anova(M5_gls_ML, M5_gls_ML_a)
M5_gls_ML_a <- update(M5_gls_ML, .~.-Large:fYear)
M5_gls_ML_b <- update(M5_gls_ML, .~.-Large:Bank)
anova(M5_gls_ML, M5_gls_ML_a)
anova(M5_gls_ML, M5_gls_ML_b)