-
Notifications
You must be signed in to change notification settings - Fork 1
/
growth_dist_sectors.R
218 lines (170 loc) · 9.66 KB
/
growth_dist_sectors.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
#wdir <- ""
wdir <- ""
dataDir <- "data/"
packagesFile <- "packages.txt"
source(paste(wdir, "functions.R", sep="")) ### this also loads every needed package
#loadDatasets(paste(wdir,dataDir,sep="")) ###USE THIS IF YOU CURRENTLY HAVEN'T DATASETS IN WORKSPACE
sample_size <-5000
restaurants_growth <- restaurants$Growth[!is.na(restaurants$Growth)]
growth_restaurants <- sample(restaurants_growth, sample_size)
media_growth <- media$Growth[!is.na(media$Growth)]
growth_media <- sample(media_growth, sample_size)
#growth_media <- growth_media[growth_media >= 0]
#############################################################################################################
############################################################################################################
fit_norm <- fitdist(growth_restaurants, distr="norm", method="mle")
fit_cauchy <- fitdist(growth_restaurants, distr="cauchy", method="mle")
fit_laplace1 <-fitdist(growth_restaurants, distr = "laplace", method = "mle", start=list(location=-0.1, scale=0.001))
est1 <- c()
est2 <- c()
for (i in 1:1000) {
fcauchy<-fitdist(sample(restaurants_growth, sample_size), method="mle", distr = "cauchy")
est1[i] <- fcauchy$estimate[1]
est2[i] <- fcauchy$estimate[2]
}
plotConfidInterv(est2, fit_cauchy$estimate[2], xtitle ="Scale parameter distribution")
############################################################################################################
mean(growth_restaurants)
length(growth_restaurants)
shifted<- growth_restaurants + abs(min(growth_restaurants)) + .01 # to fit distributions that require values to be positive
min(shifted)
scaled <- (growth_restaurants - min(growth_restaurants) +0.000001) /(max(growth_restaurants) - min(growth_restaurants)+ 0.000002)
#fit distributions that require values to be within ]0, 1[
#Get an idea of the possible distribution for the empirical data
descdist(growth_restaurants, discrete = FALSE)
#################################################################################################
#Fit distributions using fitdist and mle2
#norm, exp, gamma, beta, weibull, cauchy, pareto, logis, lnorm, laplace
growth<-growth_restaurants; fit_laplace2 <- mle2(LL2, start=list(m=-0.1, s=0.001), method = "L-BFGS-B", lower = c(m=-0.1, s=0.001))
fit_logis <-fitdist(growth_restaurants, distr="logis", method="mle")
fit_lnorm <- fitdist(shifted, distr="lnorm", method="mle")
fit_pareto <- mle2(LL3, start = list(m = 1, s = min(shifted)), method = "L-BFGS-B", lower = c(m=0.001), fixed = list(s=(min(shifted))))
fit_exp <- fitdist(shifted, distr="exp", method="mle")
fit_weib <- fitdist(shifted, distr="weibull", method="mle")
fit_gamma <- fitdist(shifted, distr="gamma", method="mle")
fit_beta <- fitdist(scaled, distr="beta", method="mle")
##################################################################################################
#Compute confidence intervals for the estimated parameters using boostrap
# For mle2 objects:
confint(fit_laplace2) # gives the confidence intervals of the estimated parameters (default is 95% c.l.)
plot(profile(fit_laplace2)) # plots profile (confidence intervals) of the parameters
# For fitdistr objects:
boot <- bootdist(fit_cauchy, bootmethod = "param", niter = 1000) #uses parametric bootsrap to generate
# 1000 samples and compute their parameters according to the given distribution
boot$CI[,-1] # returns the 95% bootstrap CIs for all parameters
##################################################################################################
summary(fit_norm)
summary(fit_cauchy)
summary(fit_laplace1)
summary(fit_laplace2)
summary(fit_logis)
summary(fit_lnorm)
summary(fit_pareto)
summary(fit_exp)
summary(fit_weib)
summary(fit_gamma)
summary(fit_beta)
#KS test
ks.test(growth_restaurants, "pnorm", fit_norm$estimate[1], fit_norm$estimate[2])
ks.test(growth_restaurants, "pcauchy", fit_cauchy$estimate[1], fit_cauchy$estimate[2])
ks.test(growth_restaurants, "plaplace", fit_laplace1$estimate[1], fit_laplace1$estimate[2])
ks.test(growth_restaurants, "plaplace", coef(fit_laplace2)[1], coef(fit_laplace2)[2])
ks.test(growth_restaurants, "plogis",fit_logis$estimate[1], fit_logis$estimate[2])
ks.test(shifted, "plnorm", fit_lnorm$estimate[1] , fit_lnorm$estimate[2])
ks.test(shifted, "ppareto", coef(fit_pareto)[1], coef(fit_pareto)[2])
ks.test(shifted, "pexp", fit_exp$estimate[1])
ks.test(shifted, "pweibull", fit_weib$estimate[1], fit_weib$estimate[2])
ks.test(shifted, "pgamma", fit_gamma$estimate[1], fit_gamma$estimate[2])
ks.test(scaled, "pbeta",fit_beta$estimate[1], fit_beta$estimate[2])
#############################################################################
# Generates 1000 ks test statistics using parametric bootstrap and returns
# a p-value based on a logspline distribution fit
n.sims <- 1000
stats <- replicate(n.sims, {
r <- rcauchy(length(growth_restaurants)
, fit_cauchy$estimate[1]
, fit_cauchy$estimate[2])
as.numeric(ks.test(r
, "pcauchy"
, fit_cauchy$estimate[1]
, fit_cauchy$estimate[2])$statistic)})
fit_log <- logspline(stats)
1-plogspline(ks.test(growth_restaurants
, "pcauchy"
, fit_cauchy$estimate[1]
, fit_cauchy$estimate[2])$statistic
, fit_log
)
##############################################################################
# Take a look at the AICs and BICs among other gof statistics
gof_original <- gofstat(list(fit_norm, fit_cauchy,fit_logis, fit_laplace1))
gof_shifted <- gofstat(list(fit_lnorm, fit_exp, fit_weib, fit_gamma))
gof_scaled <-gofstat(fit_beta)
gof_laplace<-c(AIC(fit_laplace2), BIC(fit_laplace2)) # BIC is NA. It can be retrieved
gof_pareto<-c(AIC(fit_pareto), BIC(fit_pareto)) # using mle of {stats4} instead of mle2
gof_original
gof_original$chisqpvalue # returns the Chi-square p-values for the given fits. (They are surprisingly low)
gof_laplace
gof_pareto
gof_shifted
gof_scaled
#####################################################################################
breaks <- -Inf
breaks <- append (breaks, c(seqlast(min(growth_restaurants),max(growth_restaurants),0.2), Inf))
growth_restaurants.cut<-cut(growth_restaurants,breaks=breaks, include.lowest = TRUE) #binning data
#table(growth_restaurants.cut) # frequencies of empirical data in the respective bins
p<-c()
for (i in 1:(length(breaks)-1)) {
p[i] <- pcauchy(breaks[i+1],fit_cauchy$estimate[1], fit_cauchy$estimate[2])-pcauchy(breaks[i],fit_cauchy$estimate[1], fit_cauchy$estimate[2])
}
f.os<-c()
for (j in 1:(length(breaks)-1)) {
f.os[j]<- table(growth_restaurants.cut)[[j]]
}
chisq.test(x=f.os, p=p)# p-value is very low, probably due to binning choice
chisq.test(x=f.os, simulate.p.value = T) # test with Monte Carlo simulated p-values
#################################################################################
#################################################################################
#Visualise the obtained distributions
par(mfrow=c(1,1))
x<-growth_restaurants #(normal, cauchy, laplace, logis)
hist(x, prob=T, breaks="scott", xlab="growth_restaurants rate", col="grey", main="Empirical growth Rate Distribution")
curve(dnorm(x, fit_norm$estimate[1], fit_norm$estimate[2]), add=T,col = 1, lwd=2)
curve(dcauchy(x, fit_cauchy$estimate[1], fit_cauchy$estimate[2]), add=T,col = 2, lwd=2)
curve(dlaplace(x, fit_laplace1$estimate[1] , fit_laplace1$estimate[2]), add=T, col=4, lwd=2)
#curve(dlaplace(x, coef(fit_laplace2)[1] , coef(fit_laplace2)[2]), add=T, col=5, lwd=2)
curve(dlogis(x, fit_logis$estimate[1], fit_logis$estimate[2]), add=T,col = 7, lwd=2)
legend("topright", c("Normal", "Cauchy", "Laplace", "Logistic"), col=c(1,2,4,7), lwd=3)
x<-shifted #(lnorm, pareto, exp, weib, gamma)
hist(x, add = F, prob=T, breaks=c(seqlast(min(shifted),max(shifted),0.2)),xlim=c(0, 20), xlab="Growth rate", col="light grey", main="Empirical growth rate distribution in HORECA")
curve(dlnorm(x, fit_lnorm$estimate[1], fit_lnorm$estimate[2]), add=T,col =6, lwd=2)
curve(dpareto(x, coef(fit_pareto)[1] , coef(fit_pareto)[2]), add=T,col =7 , lwd=2)
curve(dexp(x, fit_exp$estimate[1]), add=T,col = 8, lwd=2)
curve(dweibull(x, fit_weib$estimate[1], fit_weib$estimate[2]), add=T,col = 9, lwd=2)
curve(dgamma(x, fit_gamma$estimate[1], fit_gamma$estimate[2]), add=T,col = 10, lwd=2)
x<-scaled #(beta)
hist(x, add = F, prob=T, breaks=c(seqlast(min(scaled),max(scaled),0.02)),xlim=c(0, 1), xlab="Growth rate", col="light grey", main="Empirical growth rate distribution in HORECA")
curve(dbeta(x, fit_beta$estimate[1], fit_beta$estimate[2]), add=T,col = 11, lwd=2)
# Visualise some Q-Q and P-P plots
# For fitdistr objects:
# QQ plot
qqcomp(fit_norm)
ppcomp(fit_norm)
# PP plot
qqcomp(fit_cauchy)
ppcomp(fit_cauchy)
#For other objects:
#QQ plot
lengr <- length(growth_restaurants)
y <- rlaplace(lengr, coef(fit_laplace2)[1], coef(fit_laplace2)[2])
qqplot(y, growth_restaurants, xlab="Theoretical Quantiles", ylab = "Empirical Quantiles")
qqline(rlaplace(lengr), col = 2,lwd=2,lty=2, distribution = qlaplace)
#qnorm(0.5) # for a given surface under the std normal dist (50% in this case) give me the corresponding z score
#CDF plot:
#lines(ecdf(rcauchy(lengr, fit_cauchy$estimate[1], fit_cauchy$estimate[2])), col="blue", cex=0.5, xlim=c(-1,1))
#plot(ecdf(rlaplace(lengr, 0.0315464, 0.0571342)), col="blue", cex=0.5)
plot(ecdf(growth_restaurants),cex=0.5, col="red")
curve(pcauchy(x,fit_cauchy$estimate[1], fit_cauchy$estimate[2]), add = T , cex=0.5, col="blue")
curve(plaplace(x,fit_laplace1$estimate[1], fit_laplace1$estimate[2]), add = T , cex=0.5, col="green")
curve(plaplace(x,coef(fit_laplace2)[1], coef(fit_laplace2)[2]), add = T , cex=0.5,col="purple")
###########################################################################################################