You are on page 1of 2

5.

c) The R code used for this problem was the following.


library(MASS)
library(statmod)
dIG <- function(x, mu, lambda)
(lambda/(2*pi*x^3))^(1/2)*exp(-lambda*(x-mu)^2/(2*mu^2*x))
s1 = c(10.6, 91.3, 51.7, 2.2, 3.8, 6.0, 17.8, 131.8, 31.0, 4.2,
13.7, 10.2, 9.9, 4.3, 5.6, 12.9, 42.0, 14.1, 3.8, 9.3,
2.6, 27.6, 1.7, 7.0, 2.1, 1.5, 7.5, 2.5, 2.4, 51.9)
n = length(s1)
mu_hat = mean(s1)
lambda_hat = n/sum(1/s1-1/mean(s1))
estimates = c(mu_hat,lambda_hat)
estimates
dist = fitdistr(s1,densfun = dIG,start = list(mu = 50, lambda= 10))
#checking our MLE estimates with R's optimisation tools
dist
confidence_interval = c(qchisq(0.025,n-1)*lambda_hat/n,qchisq(0.975,n-
1)*lambda_hat/n)
confidence_interval
p = (1:n)/(n + 1)
y = sort(s1)
r = qinvgauss(p,mean = mu_hat, shape = lambda_hat) #computing the
theoretical quantiles for the distribution
plot(r,y, xlab = "Theoretical quantiles", ylab = "Sample quantiles")
fit <- lm(y ~ r) # this computes the "line of best fit"
abline(fit) # this plots the "line of best fit"

i.
After running the code up till the seventh-last line, we obtain the following
output:
> estimates
[1] 19.433333 6.993728
> dist
mu lambda
19.453130 6.993238
( 5.932648) ( 1.805647)
> confidence_interval
[1] 3.740962 10.658974

Note that the parameter estimates based on the maximum likelihood


estimators (represented by the output variable estimates) that we derived in
the first part of the question are concordant with those obtained from R’s
optimisation tools (represented by the output variable dist). Nonetheless, there
is a very slight discrepancy between the two sets of values, and we will use
the exact values (given on the second line of output) when constructing the
QQ plot.

Hence, our maximum likelihood estimate for λ , ^λ is 6.993728, and a 95%


confidence interval for λ is (3.740962, 10.658974).
ii.
After running the last six lines of the R code, we obtain the following QQ
plot. There is a close correspondence between the sample and theoretical
quantiles, as seen in the figure, suggesting that the model fits the data very
well.

You might also like