-
Notifications
You must be signed in to change notification settings - Fork 1
/
Functions.R
184 lines (158 loc) · 6.67 KB
/
Functions.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
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
# Slater, L.J., Singer, M.B. and Kirchner, J.W., 2015.
# Hydrologic versus geomorphic drivers of trends in flood hazard.
# Geophysical Research Letters, 42(2), pp.370-376.
library(minpack.lm)
###################################
# #
# IRLS COEFFS #
# #
###################################
IRLS.exp.unbiasedmean <- function(Y, X, type="Cauchy") {
Cauchy <- function(x,MAR) {
if (MAR==0) w <- ifelse(x==0, 1, 0)
else w <- 1/(1+(x/(3.536*MAR))^2)
return(w)
}
if (type!="Cauchy") stop("IRLS stopped: no valid weight type specified. Only 'Cauchy' is implemented here!")
wt <- rep(1,length(Y))
delta_x <- X - mean(X)
mean_y <- weighted.mean(Y, wt)
fit <- nlsLM(Y/mean_y ~ exp(a*delta_x)/weighted.mean(exp(a*delta_x),wt),
start=list(a=0),
#start=list(a =0.0),
weights=wt, na.action="na.omit", control = nls.lm.control (maxiter=1000))
wt_chg <- 999.0
iter <- 0
while ( (max(wt_chg,na.rm=TRUE) > 0.01) & !all(iter>10, summary(fit)$r.squared>0.999)
){
if (iter>1000) stop("IRLS stopped: more than 1000 interations, sorry!")
iter <- iter+1
old_wt <- wt
mean_y <- weighted.mean(Y,wt)
slope <- summary(fit)$coefficients[1,1]
resid <- Y - mean_y*(exp(slope*delta_x)/mean(exp(slope*delta_x)))
abs_resid <- abs(resid)
abs_resid_nonzero <- ifelse(Y==0, NA, abs_resid)
MAR <- median(abs_resid_nonzero, na.rm=TRUE)
if (MAR==0.0) stop("IRLS stopped. Solution has collapsed: median absolute residual is zero!")
wt <- Cauchy(abs_resid,MAR)
fit <- nlsLM(Y/mean_y ~ exp(a*delta_x)/weighted.mean(exp(a*delta_x), wt),
start=list(a=0),
#start=list(a=0.0),
weights=wt, na.action="na.omit", control = nls.lm.control (maxiter=1000))
wt_chg <- abs(wt-old_wt)
}
return(fit)
}
# trends <- tapply(1:length(df$site), df$site, function (idx) {
# fit <- summary(IRLS.exp.unbiasedmean(
# df$Qhat_extracted_RIs[idx], as.numeric(df$Date)[idx],type="Cauchy"))})
###################################
# #
# IRLS FITS #
# #
###################################
IRLS.fits <- function(Y, X, type="Cauchy") {
Cauchy <- function(x,MAR) {
if (MAR==0) w <- ifelse(x==0, 1, 0)
else w <- 1/(1+(x/(3.536*MAR))^2)
return(w)
}
if (type!="Cauchy") stop("IRLS stopped: no valid weight type specified. Only 'Cauchy' is implemented here!")
wt <- rep(1,length(Y))
delta_x <- X - mean(X)
mean_y <- weighted.mean(Y, wt)
fit <- nlsLM(Y/mean_y ~ exp(a*delta_x)/weighted.mean(exp(a*delta_x),wt),
start=list(a=0),
#start=list(a=0.0),
weights=wt, na.action="na.omit", control = nls.lm.control (maxiter=1000))
wt_chg <- 999.0
iter <- 0
while ( (max(wt_chg,na.rm=TRUE) > 0.01) & !all(iter>10, summary(fit)$r.squared>0.999)
){
if (iter>1000) stop("IRLS stopped: more than 1000 interations, sorry!")
iter <- iter+1
old_wt <- wt
mean_y <- weighted.mean(Y,wt)
slope <- summary(fit)$coefficients[1,1]
resid <- Y - mean_y*(exp(slope*delta_x)/mean(exp(slope*delta_x)))
abs_resid <- abs(resid)
abs_resid_nonzero <- ifelse(Y==0, NA, abs_resid)
MAR <- median(abs_resid_nonzero, na.rm=TRUE)
if (MAR==0.0) stop("IRLS stopped. Solution has collapsed: median absolute residual is zero!")
wt <- Cauchy(abs_resid,MAR)
fit <- nlsLM(Y/mean_y ~ exp(a*delta_x)/weighted.mean(exp(a*delta_x), wt),
start=list(a=0),
#start = list(a = 0.0),
weights=wt, na.action="na.omit", control = nls.lm.control (maxiter=1000))
wt_chg <- abs(wt-old_wt)
}
qq <- list("fit"=fit, "wt"=wt)
return(qq)
}
# fits <- tapply(1:length(df$site), df$site, function (idx) {
# fit <- fitted((IRLS.fits(df$Qhat_extracted_RIs[idx],as.numeric(df$Date)[idx],type="Cauchy"))$fit)*
# weighted.mean(df$Qhat_extracted_RIs[idx],IRLS.fits(df$Qhat_extracted_RIs[idx],
# as.numeric(df$Date)[idx],type="Cauchy")$wt) })
#
# fits <- tapply(1:length(site), site, function (idx) {
# fit <- fitted((IRLS.fits(y[idx], x[idx],type="Cauchy"))$fit)*
# weighted.mean(y[idx],IRLS.fits(y[idx], x[idx],type="Cauchy")$wt) })
###################################
# #
# FF TREND #
# #
###################################
# coefficients
FF_ExpoUnb <- function(Y,X) {
delta_x <- X-mean(X)
mean_y <- mean(Y)
fit <- nlsLM(Y/mean_y~exp(a*delta_x)/mean(exp(a*delta_x)),
start=list(a=0),
#start=list(a=0.0),
control=nls.lm.control(maxiter=1000))
return(fit)
}
# Ftrend <- tapply(1:length(mergeFF$site), mergeFF$site,
# function(idx) { fit <- summary(FF_ExpoUnb(mergeFF$FF_POT[idx], mergeFF$Year[idx]))})
# FFeffect <-ldply(lapply(Ftrend, function(x)coef(x)), rbind)
# list of fits for printing
FF_fits <- function(Y,X) {
delta_x <- X-mean(X)
mean_y <- mean(Y)
fit <- nlsLM(Y/mean_y~exp(a*delta_x)/mean(exp(a*delta_x)),
start=list(a=0),
#start=list(a=0.0),
control=nls.lm.control(maxiter=1000))
qq <- list("fit"=fit, "wt"=mean_y)
return(qq)
}
# fits <- tapply(1:length(mergeFF$site), mergeFF$site,
# function (idx) { fit <- fitted((FF_fits(mergeFF$Q_POT[idx], mergeFF$Year[idx]))$fit)*
# FF_fits(mergeFF$Q_POT[idx], mergeFF$Year[idx])$wt })
#Monte Carlo P values
MonteP <- function(Y,X) {
delta_x <- X-mean(X)
mean_y <- mean(Y)
fit <- nlsLM(Y/mean_y~exp(a*delta_x)/mean(exp(a*delta_x)),
start=list(a=0),
#start=list(a=0.0),
control=nls.lm.control(maxiter=1000))
slope.obs <- summary(fit)$coefficients[1]
#now the shuffled bit
teststat <- rep(NA, 1000)
slope <- rep(NA, 1000)
for(i in 1:1000) {
ySHUFFLE <- sample(Y)
SHUFFLE.nls <- nlsLM(ySHUFFLE/mean_y~exp(a*delta_x)/mean(exp(a*delta_x)),
start=list(a=0),
#start=list(a=0.0),
control=nls.lm.control(maxiter=1000))
slope[i] <- summary(SHUFFLE.nls)$coefficients[1]
}
result <- sum(abs(slope)>=abs(slope.obs))/1000 # count the number of abs slopes that are greater than the abs measured one
return(result)
}
# MonteP <- tapply(1:length(mergeFF$site), mergeFF$site,
# function(idx) { result <- MonteP(mergeFF$Q_POT[idx], mergeFF$Year[idx])})
# as.data.frame(MonteP)