-
Notifications
You must be signed in to change notification settings - Fork 2
/
regression_permutation_tests.R
143 lines (111 loc) · 5.89 KB
/
regression_permutation_tests.R
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
# Code to perform hypothesis testing using permutation tests to analise the slopes of PDI vs duration
# Author: Alfredo Hernández <[email protected]>
# Source base code -----------------------------------------
source("regression_base.R")
source("resampling_base.R")
# load("regression_analysis.RData")
# Get RAW data ---------------------------------------------
pdi.all <- as_tibble(data.table::fread('data/hurdat2-hadisst-1966-2016_pdis.csv')) %>%
mutate(storm.duration = measurements::conv_unit(storm.duration, "sec", "hr"))
pdi.natl <- pdi.all %>%
dplyr::filter(basin == "NATL")
pdi.epac <- pdi.all %>%
dplyr::filter(basin == "EPAC")
compute.flag <- F
read.flag <- T
save.flag <- F
bs.flag <- T
# Load objects from disk -----------------------------------
if (read.flag) {
# Standard
p.values.list <- readRDS("objects/regression_p_values_all.rds")
p.values.list.pdi <- readRDS("objects/regression_p_values_pdi.rds")
p.values.list.max.wind <- readRDS("objects/regression_p_values_maxwind.rds")
p.values.list.mean.wind <- readRDS("objects/regression_p_values_meanwind.rds")
p.values.list.mean.sq.wind <- readRDS("objects/regression_p_values_meansqwind.rds")
# With bootstrap
boot.p.values.list <- readRDS("objects/regression_p_values_boot_all.rds")
boot.p.values.list.pdi <- readRDS("objects/regression_p_values_boot_pdi.rds")
boot.p.values.list.max.wind <- readRDS("objects/regression_p_values_boot_maxwind.rds")
boot.p.values.list.mean.wind <- readRDS("objects/regression_p_values_boot_meanwind.rds")
boot.p.values.list.mean.sq.wind <- readRDS("objects/regression_p_values_boot_meansqwind.rds")
# Latest run for PDI
final.p.values.list.pdi <- readRDS("objects/regression_p_values_pdi_final.rds")
}
# Permutation tests ----------------------------------------
n.sim.test <- 1000
# Permutation test for all data
if (compute.flag) {
# NATL
p.vals.natl.pdi <- summarise_p_values("NATL", "storm.duration", "storm.pdi", 0, bs.flag, n.sim.test)
p.vals.natl.max.wind <- summarise_p_values("NATL", "storm.duration", "max.wind", 0, bs.flag, n.sim.test)
p.vals.natl.mean.wind <- summarise_p_values("NATL", "storm.duration", "mean.wind", 0, bs.flag, n.sim.test)
p.vals.natl.mean.sq.wind <- summarise_p_values("NATL", "storm.duration", "mean.sq.wind", 0, bs.flag, n.sim.test)
# EPAC
p.vals.epac.pdi <- summarise_p_values("EPAC", "storm.duration", "storm.pdi", 0, bs.flag, n.sim.test)
p.vals.epac.max.wind <- summarise_p_values("EPAC", "storm.duration", "max.wind", 0, bs.flag, n.sim.test)
p.vals.epac.mean.wind <- summarise_p_values("EPAC", "storm.duration", "mean.wind", 0, bs.flag, n.sim.test)
p.vals.epac.mean.sq.wind <- summarise_p_values("EPAC", "storm.duration", "mean.sq.wind", 0, bs.flag, n.sim.test)
}
# Permutation test for developing systems
if (compute.flag) {
# NATL
p.vals.natl.pdi.ds <- summarise_p_values("NATL", "storm.duration", "storm.pdi", 33, bs.flag, n.sim.test)
p.vals.natl.max.wind.ds <- summarise_p_values("NATL", "storm.duration", "max.wind", 33, bs.flag, n.sim.test)
p.vals.natl.mean.wind.ds <- summarise_p_values("NATL", "storm.duration", "mean.wind", 33, bs.flag, n.sim.test)
p.vals.natl.mean.sq.wind.ds <- summarise_p_values("NATL", "storm.duration", "mean.sq.wind", 33, bs.flag, n.sim.test)
# EPAC
p.vals.epac.pdi.ds <- summarise_p_values("EPAC", "storm.duration", "storm.pdi", 33, bs.flag, n.sim.test)
p.vals.epac.max.wind.ds <- summarise_p_values("EPAC", "storm.duration", "max.wind", 33, bs.flag, n.sim.test)
p.vals.epac.mean.wind.ds <- summarise_p_values("EPAC", "storm.duration", "mean.wind", 33, bs.flag, n.sim.test)
p.vals.epac.mean.sq.wind.ds <- summarise_p_values("EPAC", "storm.duration", "mean.sq.wind", 33, bs.flag, n.sim.test)
}
# Tidy p-values in a list ----------------------------------
if (compute.flag) {
# Group data frames into a list
p.values.list <- lapply(ls(patt='^p.vals.'), get)
p.values.list.pdi <- lapply(ls(patt='^p.vals.*pdi*'), get)
p.values.list.max.wind <- lapply(ls(patt='^p.vals.*max.wind*'), get)
p.values.list.mean.wind <- lapply(ls(patt='^p.vals.*mean.wind*'), get)
p.values.list.mean.sq.wind <- lapply(ls(patt='^p.vals.*mean.sq.wind*'), get)
# rm(list=ls(pattern="^p.vals."))
}
# Save into RDS files
if (save.flag && !bs.flag) {
saveRDS(p.values.list, "objects/regression_p_values_all.rds")
saveRDS(p.values.list.pdi, "objects/regression_p_values_pdi.rds")
saveRDS(p.values.list.max.wind, "objects/regression_p_values_maxwind.rds")
saveRDS(p.values.list.mean.wind, "objects/regression_p_values_meanwind.rds")
saveRDS(p.values.list.mean.sq.wind, "objects/regression_p_values_meansqwind.rds")
}
if (save.flag && bs.flag) {
saveRDS(p.values.list, "objects/regression_p_values_boot_all.rds")
saveRDS(p.values.list.pdi, "objects/regression_p_values_boot_pdi.rds")
saveRDS(p.values.list.max.wind, "objects/regression_p_values_boot_maxwind.rds")
saveRDS(p.values.list.mean.wind, "objects/regression_p_values_boot_meanwind.rds")
saveRDS(p.values.list.mean.sq.wind, "objects/regression_p_values_boot_meansqwind.rds")
}
# Analyse p-values -----------------------------------------
# Print regressions with p-value <= alpha
explore_p_values(p.values.list, 0.05)
explore_p_values(boot.p.values.list, 0.05)
# NATL
p.vals.natl.pdi.ds.ols <- final.p.values.list.pdi[[3]]
p.vals.natl.pdi.ds.boot <- final.p.values.list.pdi[[4]]
p.vals.natl.pdi.ds.ols[,c(3,1,11,7,5,9)]
p.vals.natl.pdi.ds.boot[,c(3,1,11,7,5,9)]
# EPAC
p.vals.epac.pdi.ds.ols <- final.p.values.list.pdi[[1]]
p.vals.epac.pdi.ds.boot <- final.p.values.list.pdi[[2]]
p.vals.epac.pdi.ds.ols[,c(3,1,11,7,5,9)]
p.vals.epac.pdi.ds.boot[,c(3,1,11,7,5,9)]
# Compare statistics and methods ---------------------------
# Compare slope/intercept with alt calculation
(stats.std <- compare_perm_statistics(p.values.list))
(stats.boot <- compare_perm_statistics(boot.p.values.list))
stats.std[[1]]
stats.std[[2]]
stats.boot[[1]]
stats.boot[[2]]
# Compare calculation methods
compare_perm_methods(p.values.list, boot.p.values.list, 1.75, 0.6)