The qqnorm function is used for the qqnorm plots.
The quantile of x is plotted against the normal distribution quantiles. The probabilities, of the quantiles, are slightly different as you can see by printing prob1 and prob2. This is done because the normal is infinite in x, and any sample is finite. This means, for the sample, 0% quantile and 100% quantile are known, the minimum and maximum values. For the theoretical norm, there are no minimum and maximum values (-Inf and +Inf).
The calculated value, as well as that from qqnorm, are plotted. Ofcourse, there is no difference. qqnorm returns a list of x and y coordinates. We can compare them with the calculated ones. The compare library is used, which may be installed with install.packages function. The reason identical function can not be used is because comparing real numbers will lead to small errors, in the 10^-16 range, for double precision numbers.
# ex20.R
library(compare)
N <- 100
nm <- c('normal','beta','uniform')
ld <- 1
if (ld==1) {
y <- rnorm(N)
} else if (ld == 2) {
y <- rbeta(N,2,5)
} else if (ld == 3) {
y <- runif(N,-.1,3)
}
z <- (y-mean(y))/sd(y)
prob1 <- seq(0, 1, length.out = N)
prob2 <- seq(1/(2*N), by = 1/N, along.with = prob1)
zs <- quantile(sort(z),probs = prob1)
par(mfrow = c(1,2))
pz <- qnorm(prob2)
g <- qqnorm(sort(z))
qqline(sort(z))
mtext(nm[ld], line = -1)
plot(pz,sort(z),main = "Calculated")
abline(0,1)
mtext(nm[ld], line = -1)
print(compare(g$y,sort(z)))
print(compare(g$x,pz))
# TRUE
# TRUE
Output:
No comments:
Post a Comment