Last active
September 5, 2019 13:29
-
-
Save christopherlovell/f5370022030f48198ffdd85eab8f1fa2 to your computer and use it in GitHub Desktop.
Fitting a lognormal in R to a large data set and plotting the Q-Q distribution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(MASS) | |
# generate a million lognormal samples | |
n <- 1000000 | |
dat <- rlnorm(n, meanlog = 0, sdlog = 1) | |
# add some noise (optional) | |
dat <- dat + runif(n, 0, 1) | |
# create a vector of histogram breaks | |
x <- seq(0,max(dat),length=700) | |
# histogram the data | |
hst <- hist(dat, breaks=x) | |
# fit a lognormal distribution | |
fit_params <- fitdistr(dat,"lognormal") | |
# generate values given our fit parameters | |
fit <- dlnorm(x, fit_params$estimate['meanlog'], fit_params$estimate['sdlog']) | |
# plot the fit and original distributions | |
plot(x, fit, type="l", ylab="Density", | |
xlab="X", ylim=c(0,max(hst$density)), xlim=c(0,10)) | |
title(main = "Density histogram with lognormal fit") | |
lines(hst$mid, hst$density, type="l", col="red") | |
legend(8,0.15,legend=c("Fit","Data"),lty=c(1,1),col=c("black","red")) | |
## Q-Q plot | |
# create a vector of quantiles | |
quants <-seq(0,1,length=81)[2:80] | |
# find quantiles for the fitted distribution | |
fit_quants <- qlnorm(quants,fit_params$estimate['meanlog'], fit_params$estimate['sdlog']) | |
# find quantiles of the original data | |
data_quants <- quantile(dat,quants) | |
# fit and data quantiles side by side | |
data.frame(fit_quants,data_quants) | |
# create Q-Q plot | |
plot(fit_quants, data_quants, xlab="Theoretical Quantiles", ylab="Sample Quantiles") | |
title(main = "Q-Q plot of lognormal fit against data") | |
abline(0,1) |
this works fine with my RStudio installation. Try running in a terminal R session, or you could try debugging your RStudio install by generating another simple plot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I code my data up to line 27 above and run it, the console portion of Rstudio screen just retypes the entire code...... and does nothing, no errors are reported, but no graph is displayed.....???