Kate
Kate

Reputation: 25

How to draw a Q-Q plot in R?

The task: To generate three sets of 100 observations from your fitted normal distribution and draw the Q-Q plot for each data set without using qqplot or qqnorm.

So far I have:

y = c(194, 209, 205, 180, 196, 178, 214, 199, 224, 230)
mu = (1/10)*sum(y)
v = sd(y)
x=rnorm(10,mu,v)
hist(x,breaks=20)
curve(dnorm(x,mu,v),add=TRUE,col="red")

Upvotes: 2

Views: 2195

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50678

So I assume your question is about how to draw a quantile-quantile (QQ) plot without relying on qqplot or qqnorm. I further assume you want to compare your observed quantiles to theoretical quantiles from a normal distribution.

You can do the following:

# Set fixed seed for reproducibility
set.seed(2017);

# Generate random numbers
y <- c(194, 209, 205, 180, 196, 178, 214, 199, 224, 230);
mu <- (1 / 10) * sum(y);
v <- sd(y);
x <- rnorm(10, mu, v);

# Sort x values
x <- sort(x);

# Theoretical distribution
x0 <- qnorm(ppoints(10), mu, v);

# Plot quantile-quantile plot
plot(x = x0, y = x, xlab = "Theoretical quantiles", ylab = "Observed quantiles")

enter image description here

Upvotes: 2

Related Questions