-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfitting.R
228 lines (191 loc) · 6.69 KB
/
dfitting.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
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
#created by Nikolas Mavrogeneiadis - 161014
# Load libraries ----
library(xts)
library(lmom)
library(lubridate)
library(moments)
library(lmom)
library(fitdistrplus)
# Load functions ----
# we assume that functions are in the same folder with the current file
source("get_month.R")
source("EmpCdf.R")
# create folder that we will store plots
foldername = "gammadistr"
dir.create(foldername)
wfoldername = "weibulldistr"
dir.create(wfoldername)
errorfolder = 'errors'
dir.create(errorfolder)
# Load time series and create the appropriate xts object----
fileName = "NileData_BCM.txt"
filedata = read.csv(file = fileName)
alldata = xts(x = filedata$data, order.by = as.Date(filedata$Date))
colnames(alldata) = "data"
#choose start and end day
startday = "1871-01-01"; endDay = "1944-12-01"
maindata = alldata[paste(startday, endDay, sep = "/")]
#hist(maindata)
#plot(maindata)
# Calculate mean, standard deviation and skewness for each month ----
means = rep(NA, 12)
stds = rep(NA, 12)
skwns = rep(NA, 12)
for(i in 1:12){
sample = get_month(maindata, i, 1)
means[i] = mean(sample)
stds[i] = sd(sample)
skwns[i] = moments::skewness(sample)
}
# Calculate empirical moments ----
lmoments = list()
ratiomom = list()
for(i in 1:12){
sample = get_month(maindata, i, 1)
lmoments[[i]] = lmom::samlmu(x = sample, nmom = 3, ratios = F) #calculate L1, L2, L3
ratiomom[[i]] = c(lmoments[[i]][2]/lmoments[[i]][1], lmoments[[i]][3]/lmoments[[i]][2]) #calculate the L-variation and L-skewness via L2/L1 and L3/L2
}
# Fit the gamma distribution to the data ----
layout_matrix <- matrix(1:6, ncol = 3)
layout(layout_matrix)
gamdis = list()
thgam = list()
for(i in 1:12){
sample = get_month(maindata, i, 1)
a = EmpCdf(sample, i, "Gamma", 0)
temp = seq(from = 0, to = max(a[, 2])+1, by = 0.0001)
gamdis[[i]] = lmom::pelgam(lmom = lmoments[[i]]) #we pass the first 3 moments, but L3 is not used
thgam[[i]] = lmom::cdfgam(x=temp,para = gamdis[[i]])
}
dtfm = list() #list with dataframes for each month for theoritical distr of gamma function
# Plot empirical and theoretical CDF by each month ----
for(i in 1:6){
sample = get_month(maindata, i, 1)
#plot to file
plotfile = gsub(" ", "", paste(foldername, "/Month", i, ".png", sep = ""))
png(file=plotfile, width=600, height=350)
a = EmpCdf(sample, i, "Gamma")
temp = seq(from = 0, to = max(a[, 2])+1, by = 0.0001)
lines(temp, thgam[[i]], col='magenta', lwd = 3)
dev.off()
#plot to screen
a = EmpCdf(sample, i, "Gamma")
lines(temp, thgam[[i]], col='magenta', lwd = 3)
#save to dataframe
dtfm[[i]] = data.frame(temp, thgam[[i]])
colnames(dtfm[[i]]) = c('x', 'prob')
}
layout_matrix2 <- matrix(1:6, ncol = 3)
layout(layout_matrix2)
for(i in 7:12){
sample = get_month(maindata, i, 1)
#plot to file
plotfile = gsub(" ", "", paste(foldername, "/Month", i, ".png", sep = ""))
png(file=plotfile, width=600, height=350)
a = EmpCdf(sample, i, "Gamma")
temp = seq(from = 0, to = max(a[, 2])+1, by = 0.0001)
lines(temp, thgam[[i]], col='magenta', lwd = 3)
dev.off()
#plot to screen
a = EmpCdf(sample, i, "Gamma")
lines(temp, thgam[[i]], col='magenta', lwd = 3)
#save to dataframe
dtfm[[i]] = data.frame(temp, thgam[[i]])
colnames(dtfm[[i]]) = c('x', 'prob')
}
#create a time series expressing the mean Nile River supplies for each year ----
ymaindata = xts::apply.yearly(x = maindata, mean)
# Fit the Weibull distribution to the data ----
gdtfm = list() #list with dataframes for each month for theoritical distr of gamma function
weidis = list()
thwei = list()
temp2 = NA
for(i in 1:12){
sample = get_month(maindata, i, 1)
a = EmpCdf(sample, i, "Weibull", 0)
temp2 = seq(from = 0, to = max(a[, 2])+1, by = 0.0001)
weidis[[i]] = lmom::pelwei(lmom = lmoments[[i]]) #we pass the first 3 moments, but L3 is not used
thwei[[i]] = lmom::cdfwei(x = temp2, para = weidis[[i]])
}
#save plots
# Plot empirical and theoretical CDF by each month ----
for(i in 1:12){
sample = get_month(maindata, i, 1)
#plot to file
plotfile = gsub(" ", "", paste(wfoldername, "/Month", i, ".png", sep = ""))
png(file=plotfile, width=600, height=350)
a = EmpCdf(sample, i, "Weibull")
temp = seq(from = 0, to = max(a[, 2])+1, by = 0.0001)
lines(temp, thwei[[i]], col='blue', lwd = 3)
dev.off()
#save to dataframe
gdtfm[[i]] = data.frame(temp, thwei[[i]])
colnames(gdtfm[[i]]) = c('x', 'prob')
}
#the following code would be better to be a function...
#counting errors for gamma distribution
gammaerrors = rep(NA,12)
for(j in 1:12){
sample = get_month(maindata, j, 1)
empcdf = EmpCdf(sample, j, "Gamma", 0)
sum = 0
for(i in 1:length(sample)){ #74
empvalue = empcdf$u[empcdf$x == sample[i]]
if(length(empvalue) > 1){
empvalue = empvalue[length(empvalue)]
}
thvalue = dtfm[[j]]$prob[dtfm[[j]]$x == signif(sample[i], digits = 3)]
#check if thvalue is empty. Problem with compare floating point numbers.
#How can we solve that?
sample[i] = signif(sample[i], digits = 5)
try = 0
while(length(thvalue) == 0){
thvalue = dtfm[[j]]$prob[dtfm[[j]]$x == sample[i]+try] #try the next one
try = try + 0.001
}
thvalue = signif(thvalue, digits = 4)
empvalue = signif(empvalue, digits = 4)
sum = sum + signif((thvalue - empvalue)^2, digits = 6)
}
gammaerror = sqrt(sum)
gammaerror = signif(gammaerror, digits = 6)
gammaerrors[j] = gammaerror
#message('Error for month: ', j, 'is: ', gammaerror)
}
#counting errors for weibull distribution
weibullerrors = rep(NA,12)
for(j in 1:12){
sample = get_month(maindata, j, 1)
empcdf = EmpCdf(sample, j, "Weibull", 0)
sum = 0
for(i in 1:length(sample)){ #74
empvalue = empcdf$u[empcdf$x == sample[i]]
if(length(empvalue) > 1){
empvalue = empvalue[length(empvalue)]
}
thvalue = gdtfm[[j]]$prob[gdtfm[[j]]$x == signif(sample[i], digits = 3)]
#check if thvalue is empty. Problem with compare floating point numbers.
#How can we solve that?
sample[i] = signif(sample[i], digits = 5)
try = 0
while(length(thvalue) == 0){
thvalue = gdtfm[[j]]$prob[gdtfm[[j]]$x == sample[i]+try] #try the next one
try = try + 0.001
}
thvalue = signif(thvalue, digits = 4)
empvalue = signif(empvalue, digits = 4)
sum = sum + signif((thvalue - empvalue)^2, digits = 6)
}
weibullerror = sqrt(sum)
weibullerror = signif(weibullerror, digits = 6)
weibullerrors[j] = weibullerror
message('Error for month: ', j, 'is: ', weibullerror)
}
layout_matrix3 <- matrix(1:1)
layout(layout_matrix3)
plotfile = gsub(" ", "", paste(errorfolder, "/errors.png"))
png(file=plotfile, width=600, height=350)
months = 1:12
plot(months,gammaerrors, type = 'l' ,col="red", xlab = 'months', ylab = 'errors')
lines(months,weibullerrors,col="green")
dev.off()