2
$\begingroup$

Is there a command in R, for which I can compare my specific sample to some specific (given) normal distribution?

Like a qqnorm testing for the specific normal distribution $\mathcal{N}(\mu,\sigma^{2})$.

$\endgroup$
2
  • $\begingroup$ what's wrong with qqplot(scale(x)) ... ? $\endgroup$
    – Ben Bolker
    May 23, 2016 at 20:26
  • $\begingroup$ Sorry i meant qqnorm instead of qqpöot $\endgroup$
    – ziT
    May 23, 2016 at 20:32

3 Answers 3

3
$\begingroup$

Quicker to write the code than search for a function. You only need to

  1. Calculate plotting positions for your sample size. $\frac{k}{n+1}$ for $k =1, \ldots, n$ is a common method—or pick from the dozens of methods in ppoints.

  2. Calculate the corresponding theoretical quantiles from your distribution, whatever it is (normal, Weibull, &c), & whether its parameters are pre-specified or estimated from the data.

  3. Plot the sorted sample data against the sorted theoretical quantiles

Here's some R code:

# make up some data
rnorm(40, mean=2, sd=2) -> x
length(x) -> n
# calculate plotting positions
plot.pos <- (1:n)/(n+1)
# calculate theoretical quantiles, say from N(2,3)
qnorm(plot.pos, mean=2, sd=3) -> theor.quantiles
# sort & plot
plot(sort(theor.quantiles), sort(x))
abline(a=0,b=1)

With regard to formal tests, Stephens (1974) says that for tests of normality based on the empirical distribution function you get higher power (against some alternatives he selects as being typically of interest) when estimating the mean & variance from the data than when using the known mean & variance (comparing the observed value of the test statistic with the appropriate null distribution for each case, of course). So, perhaps surprisingly, you might want to carry out a different test when you're only interested in departures from a hypothesized shape given an assumed location & scale from when you're interested in any kind of departure from a hypothesized, fully specified, distribution.

For any goodness-of-fit test statistic that takes your fancy you can simulate its distribution under a fully specified null distribution easily enough; furthermore, EDF tests are distribution-free in this case. (It's perhaps largely a matter of historical accident that when people say "the Kolmogorov–Smirnov test" they usually mean the test for a fully specified null distribution, but when they say "the Anderson–Darling" test they usually mean the test for a normal null distribution with parameters estimated from the data.) That means you only need to run one simulation, of a uniform random variate for speed, for any given sample size & can store it for future use.

Stephens (1974), "EDF Statistics for goodness of fit and some comparisons", JASA, 69, 347

$\endgroup$
2
  • $\begingroup$ and instead of the qqline command, one can use lines(sort(theor.quantiles),sort(theor.quantiles)) right? $\endgroup$
    – ziT
    May 24, 2016 at 8:32
  • $\begingroup$ abline(a=0,b=1) - to plot a line with unit slope through the origin - seems more direct. (I'll add it to my answer). But see What is the use of the line produced by qqline() in R? for exactly what qqline does - the argument there doesn't apply of course when the theoretical distribution is fully specified. $\endgroup$ May 24, 2016 at 18:27
2
$\begingroup$

If you really have a prespecified mean and variance (and don't estimate them from the data), you can use the Kolmogorov-Smirnov test, ks.test().

Alternatively, the nortest package offers a few additional normality tests.

$\endgroup$
2
  • $\begingroup$ Thanks for the comment. Is the ks.test still viable for large samples? $\endgroup$
    – ziT
    May 23, 2016 at 20:36
  • 2
    $\begingroup$ "Viable" is a big word. The KS test - and any other test - will come up "significant" for tiny deviations from normality if you have really large samples. In such case, you need to think about whether the deviation from normality you are looking for is statistically significant or clinically significant (substitute whatever field you are working in): $\endgroup$ May 23, 2016 at 20:40
1
$\begingroup$

In R, you can use the function qqnorm to visualize how 'normal' the data may look. The library car adds confidence intervals to qqnorm.

Additionally, you can use the Shapiro-Wilk test. SPSS has good documentation for this: https://statistics.laerd.com/spss-tutorials/testing-for-normality-using-spss-statistics.php

In R, the function is shapiro.test.

EDIT: In order to test your sampled data against any $N(\mu, \sigma^2)$ (or any distribution), you can generate samples from the theretical distribution and use the function qqplot to plot out the QQ plot. Additionally, you can use the Kolmogorov–Smirnov test to test if the two data are significantly different from one another.

Example:

truedistribution = rnorm(1000, mean = 10, sd = 3) ## theoretical data

mydata = rnorm(1000, mean = 10, sd = 3) + rnorm(1000) ## real world data

qqplot(mydata, truedistribution)

ks.test(mydata, truedistribution)

Best of luck

$\endgroup$
9
  • $\begingroup$ Thanks for the post. Sorry i meant qqnorm. But i want to know how normal my data is according to a specific normal distribution with given mean and variance. Density plots could be an example. Shapiro wilk is not an option for my large samples. $\endgroup$
    – ziT
    May 23, 2016 at 20:34
  • $\begingroup$ Try the following: truedistribution = rnorm(1000, mean = 10, sd = 3) ## theoretical data mydata = rnorm(1000, mean = 10, sd = 3) + rnorm(1000) ## real world data qqplot(y = mydata, truedistribution) $\endgroup$
    – Jon
    May 23, 2016 at 20:40
  • 1
    $\begingroup$ You shouldn't generate random samples from the theoretical distribution when you can calculate the quantiles precisely. That'll be an especially poor approach in small samples. $\endgroup$ May 23, 2016 at 21:13
  • $\begingroup$ Just an example. I think the situation is with larger data sets though since they mentioned Shapiro Wilk would not work for their sample size. But as a correction, here's another example: qqplot(mydata, qnorm(p = ppoints(n = 1000))) $\endgroup$
    – Jon
    May 23, 2016 at 21:35
  • $\begingroup$ The same applies to the Kolmogorov-Smirnov test - you should specify the theoretical cdf in the ks.test function rather than provide a fake sample simulated from it. $\endgroup$ May 23, 2016 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.