[R] "some problems"

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Apr 21 00:28:11 CEST 2010


On 20-Apr-10 21:25:28, tamas barjak wrote:
> Dear R community!
> 
> I am a mathematician listener, and I have to rewrite some
> source codes from Matlab to R. I would like to ask a solution
> of people who are skilled at him better hereby, because it is
> very new for me R and I do not receive it final result than
> in Matlab.
> 
> So the problem:
> 
> ---
> Let us generate 2005 standard normal random numbers, we depict
> it these the histogram of density together with the standard normal
> density function! We do this so that on [0,1] we write even random
> numbers the normal distribution function into inverse one's!
> ---
> 
> I have a problem with it mainly when who I want to have it drawn.
> The drawing the arrangement of the histogram and the density
> function slips compared to each other. But the same situation then,
> if barplot() I use it.
> 
> I apologise for my ridiculous problem, and my bad composition
> (translator I use a program).
> 
> Thank you very much: Barjak Tamas

Here is an example of making a histogram of 2005 standard Normal
random numbers, drawing a histogram, and plotting the curve of
the standard Normal density on top of it. Explanations are added
as comments ("#").

# set the RNG seed (for reproducibility of this example)
set.seed(54321)
# Generate 2005 standard Normal numbers
X <- rnorm(2005)
# Draw a histogram (arbitrary break-points)
#   breAkpoints at -4.0, -3.6, -3.2, ... -0.4, 0, 0.4, ... 3.6, 4.0
hist(X, breaks = 0.4*(-10:10))

# Draw the curve of the Normal distributiom
#   using 10 points per interval of the histogram
#   and multiplying by the width 0.4 of the interval
#   in order to match the probabilities of the intervals
#   and also by N=2005 to scale the curve up to give counts
x0 <- 0.04*(-100:100)
y0 <- 2005*0.4*dnorm(x0)
lines(x0,y0)

There is no perceptible "slip" between the histigram and the
Normal curve Ithe slight differences are due to random variation
in the positions of the X values).

To see it better, use a much larger random sample:

set.seed(54321)
# Generate 200000 standard Normal numbers
X <- rnorm(200000)
hist(X, breaks = 0.4*(-12:12))

x0 <- 0.04*(-120:120)
y0 <- 200000*0.4*dnorm(x0)
lines(x0,y0)

(For this, the range of the histogram has been extended,
to include all the points)

I hope this helps to make it clearer how to do this in R.

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 20-Apr-10                                       Time: 23:28:07
------------------------------ XFMail ------------------------------



More information about the R-help mailing list