0
$\begingroup$

I'm trying to use shapiro.test() function in R to test if my data fits in a normal curve. For this, I'm looking to the p-value of the output, if it's bigger than 0.05, for example.

However, when I try a simple test with the dnorm(), it results in a p-value << 0.05, what means that my data doesn't represents a normal model.

This is the simple code that I used:

x = seq(-5, 5, length.out = 100)
y = dnorm(x = x, mean = 0, sd = 1)
shapiro.test(y)

And I've got this result:

Shapiro-Wilk normality test

data:  y
W = 0.73527, p-value = 3.887e-12

What's the problem about my test?

$\endgroup$
  • 1
    $\begingroup$ The problem is that the test should give me p-value > 0.05, because my data represents a normal distribution. $\endgroup$ – lpedrassoli May 23 '19 at 8:57
  • 1
    $\begingroup$ are you meant to be using rnorm? $\endgroup$ – user20650 May 23 '19 at 8:57
  • $\begingroup$ Even rnorm(x,0,1) makes no sense, as most of the values of x are not natural numbers. I wonder what OP actually tried to do. $\endgroup$ – yarnabrina May 23 '19 at 9:28
2
$\begingroup$

It appears there's a bit of confusion between dnorm and rnorm.

The first finds the density value for a distribution given some x values, these are the "heights", corresponding to each x values.

x = seq(-5, 5, length.out = 100)
y = dnorm(x = x, mean = 0, sd = 1)

So if we plot:

par(mfrow=c(2,1))
plot(x,y) # notice I used both x and y
hist(y)

enter image description here

Notice that we get the y values of each x you passed, as you defined them. But the frequency plot (hist) of these "heights" tells us a different story.

This is the problem, your frequencies are not normal.


Now, look the same plots for rnorm(), note that we don't define x, we just need to tell how many points (n) we want :

y_true = rnorm(n=100, 0, 1) # n defines how many points to simulate

par(mfrow=c(2,1))
plot(y_true)
hist(y_true)

enter image description here

Notice that now we have the frequencies following the classical bell-shaped curve (try with 1000 to see it more clear).

And the test works as expected:

shapiro.test(y_true)
# 
# Shapiro-Wilk normality test
# 
# data:  y_true
# W = 0.98818, p-value = 0.5219

Also, dnorm() is deterministic, you give x,mean,sd values and get the corresponding y value (following the Normal distribution function). Instead rnorm() is, in some way the opposite, you just need to define mean and sd, and it will simulate n "casual" numbers that will follow that distribution.

For additional pieces of information read here.

$\endgroup$
  • $\begingroup$ Thank you. I really could see the difference between them. $\endgroup$ – lpedrassoli May 23 '19 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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