It appears there's a bit of confusion between
The first finds the density value for a distribution given some
x values, these are the "heights", corresponding to each
x = seq(-5, 5, length.out = 100)
y = dnorm(x = x, mean = 0, sd = 1)
So if we plot:
plot(x,y) # notice I used both x and y
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
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-Wilk normality test
# data: y_true
# W = 0.98818, p-value = 0.5219
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
sd, and it will simulate
n "casual" numbers that will follow that distribution.
For additional pieces of information read here.