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})$.
Quicker to write the code than search for a function. You only need to
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
.
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.
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
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$
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.
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
ks.test
function rather than provide a fake sample simulated from it.
$\endgroup$
qqplot(scale(x))
... ? $\endgroup$