-
Notifications
You must be signed in to change notification settings - Fork 1
/
growth_dist_region.R
226 lines (178 loc) · 10.1 KB
/
growth_dist_region.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
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
source(paste(wdir, "growth_rate_dist.R", sep="")) ### this also loads every needed package
sample_size <-5000
if(!"Growth" %in% names(manufacturing))
manufacturing<-getGrowth(manufacturing)
manufacturing_north<-subset(manufacturing, manufacturing$GeoArea == "Nord")
manufacturing_center<-subset(manufacturing, manufacturing$GeoArea == "Centro")
manufacturing_south<-subset(manufacturing, manufacturing$GeoArea == "Sud")
north_growth <- manufacturing_north$Growth[!is.na(manufacturing_north$Growth)]
growth_north <- sample(north_growth, sample_size)
#growth_north <- growth_north[growth_north != 0]
center_growth <- manufacturing_center$Growth[!is.na(manufacturing_center$Growth)]
growth_center <- sample(center_growth, sample_size)
south_growth <- manufacturing_south$Growth[!is.na(manufacturing_south$Growth)]
growth_south <- sample(south_growth, sample_size)
############################################################################################################
fit_norm <- fitdist(growth_north, distr="norm", method="mle")
fit_cauchy <- fitdist(growth_north, distr="cauchy", method="mle")
fit_laplace1 <-fitdist(growth_north, distr = "laplace", method = "mle", start=list(location=-0.1, scale=0.001))
est1 <- c()
est2 <- c()
for (i in 1:1000) {
fcauchy<-fitdist(sample(north_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")
############################################################################################################
ks.test(south_growth, center_growth)
############################################################################################################
mean(growth_north)
length(growth_north)
shifted<- growth_north + abs(min(growth_north)) + .000001 # to fit distributions that require values to be positive
min(shifted)
scaled <- (growth_north - min(growth_north) +0.000001) /(max(growth_north) - min(growth_north)+ 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_north, discrete = FALSE)
#################################################################################################
#Fit distributions using fitdist and mle2
#norm, exp, gamma, beta, weibull, cauchy, pareto, logis, lnorm, laplace
growth<-growth_north; 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_north, 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_north, "pnorm", fit_norm$estimate[1], fit_norm$estimate[2])
ks.test(growth_north, "pcauchy", fit_cauchy$estimate[1], fit_cauchy$estimate[2])
ks.test(growth_north, "plaplace", fit_laplace1$estimate[1], fit_laplace1$estimate[2])
ks.test(growth_north, "plaplace", coef(fit_laplace2)[1], coef(fit_laplace2)[2])
ks.test(growth_north, "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_north)
, 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_north
, "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_north),max(growth_north),0.2), Inf))
growth_north.cut<-cut(growth_north,breaks=breaks, include.lowest = TRUE) #binning data
#table(growth_north.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_north.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_north #(normal, cauchy, laplace, logis)
hist(x, prob=T, breaks="fd", xlab="growth_north rate",xlim = c(-3, 4), col="grey", main="Empirical small 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 for North")
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 for North")
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_north)
y <- rlaplace(lengr, coef(fit_laplace2)[1], coef(fit_laplace2)[2])
qqplot(y, growth_north, 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_north),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")
###########################################################################################################