-
Notifications
You must be signed in to change notification settings - Fork 1
/
growth_dist_large.R
227 lines (177 loc) · 9.71 KB
/
growth_dist_large.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
small_growth <- groupByGrowth(small_firms)$Growth
medium_growth <- groupByGrowth(medium_firms)$Growth
large_growth <- groupByGrowth(large_firms)$Growth
sample_size <-5000
growth_large <- sample(large_growth, sample_size)
############################################################################################################
fit_norm <- fitdist(growth_large, distr="norm", method="mle")
fit_cauchy <- fitdist(growth_large, distr="cauchy", method="mle")
fit_laplace1 <-fitdist(growth_large, distr = "laplace", method = "mle", start=list(location=-0.1, scale=0.001))
est1 <- c()
est2 <- c()
for (i in 1:1000) {
fcauchy<-fitdist(sample(large_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_large)
length(growth_large)
shifted<- growth_large + abs(min(growth_large)) + .000001 # to fit distributions that require values to be positive
min(shifted)
scaled <- (growth_large - min(growth_large) +0.000001) /(max(growth_large) - min(growth_large)+ 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_large, discrete = FALSE)
#################################################################################################
#Fit distributions using fitdist and mle2
#norm, exp, gamma, beta, weibull, cauchy, pareto, logis, lnorm, laplace
growth<-growth_large; 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_large, 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_large, "pnorm", fit_norm$estimate[1], fit_norm$estimate[2])
ks.test(growth_large, "pcauchy", fit_cauchy$estimate[1], fit_cauchy$estimate[2])
ks.test(growth_large, "plaplace", fit_laplace1$estimate[1], fit_laplace1$estimate[2])
ks.test(growth_large, "plaplace", coef(fit_laplace2)[1], coef(fit_laplace2)[2])
ks.test(growth_large, "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_large)
, 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_large
, "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_laplace1), BIC(fit_laplace1)) # 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_large),max(growth_large),0.2), Inf))
growth_large.cut<-cut(growth_large,breaks=breaks, include.lowest = TRUE) #binning data
#table(growth_large.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_large.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
#################################################################################
ks.test(medium_growth, large_growth)
#################################################################################
#Visualise the obtained distributions
kurtosis(large_growth)
skewness(large_growth)
mean(large_growth)
var(large_growth)
par(mfrow=c(1,1))
grid(5,5)
x<-large_growth #(normal, cauchy, laplace, logis)
hist(x, prob=T, breaks=c(seqlast(min(large_growth),max(large_growth),0.1)), xlab="Growth",xlim = c(-3, 4), col="grey", main="Fit for large firm growth 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=3, 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 large firms")
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 large firms")
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)
qqcomp(fit_laplace1)
ppcomp(fit_laplace1)
#For other objects:
#QQ plot
lengr <- length(growth_large)
y <- rlaplace(lengr, coef(fit_laplace2)[1], coef(fit_laplace2)[2])
qqplot(y, growth_large, 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_large),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")
###########################################################################################################