[R] Doubts about chi-square distribution

José Cláudio Faria joseclaudio.faria at terra.com.br
Fri Dec 17 14:19:38 CET 2004


Dear list,

For educational purposes I have been working with the script below.

I have a observation:

   line 31

   #CScal[i] = (amo^2)  # IT IS WRONG, I KNOW, BUT IT MAKE R TO CRASHES!

   I'm thinking this is a possible bug in the R!


And I have a couple of doubts:

1) line 51

    #curve(dchisq(x, n-1), add = T, col = 'red'

    I think that it is correct, but the function no match with the observed 
distribution.

    curve(dchisq(x, n), add = T, col = 'red') matches, so, what is wrong?

2) Plot

    I would like to distance the Ylabel from the left limit of the screen, is it 
possible?

Many thanks,

-- 
José Cláudio Faria
Brasil/Bahia/UESC/DCET
Estatística Experimental/Prof. Adjunto
mails:
  joseclaudio.faria at terra.com.br
  jc_faria at uesc.br
  jc_faria at uol.com.br


# Title   : Origin chi-square distribution
# Author  : José Cláudio Faria
# Date    : 16/12/2004
# Version : v1
#-------------------------------------------------------------------------------

   #---------------------- Begin informations -----------------------------------

   # Populational parameters
   Mpop   = 0        # Mean
   Vpop   = 1        # Variance
   N      = 10000    # Size

   # Sampling
   n      = 10       # size of the sample
   sr     = 10000    # sampling repetition

   # Plot parameter
   nchist = 150

   #---------------------- End informations -------------------------------------

   CScal = numeric();  # CScal = Chi-square calculated

   pop = rnorm(N, Mpop, sqrt(Vpop))  # pop~N(Mpop,Vpop)

   for (i in 1:sr)
   {
     amo = sample(pop, n, replace = TRUE)
     #====================================================================
     #CScal[i] = (amo^2)  # IT IS WRONG, I KNOW, BUT IT MAKE R TO CRASHES!
     #====================================================================
     CScal[i] = sum(amo^2)
   }

   win.graph(w = 6, h = 7)
   split.screen(c(2,1))

   screen(1)
   hist(CScal, breaks = nchist, col = 'gray', main = 'Histogram',
        xlab = NULL, ylab = 'Absolute frequence', font.lab = 2, font = 2)
   mtext(expression(chi^2==sum(amo^2)), side = 3, col = 'red', font = 2)

   screen(2)
   hist(CScal, probability = T, breaks = nchist, col = 'gray', main = 'Density',
        xlab = expression(chi^2), ylab = expression(f(chi^2)), font.lab = 2, 
font = 2)
   mtext(expression(chi^2==sum(amo^2)), side = 3, col = "red", font = 2)

   x = CScal
   #===========================================
   # I THINK THIS WAY IS CORRECT (n-1)
   #curve(dchisq(x, n-1), add = T, col = 'red')
   #===========================================
   curve(dchisq(x, n), add = T, col = 'red')


   cat('\nPopulation:'); cat('\n')
   cat('\tMean =', Mpop); cat('\n')
   cat('\tVariance =', Vpop); cat('\n')
   cat('\tSize (N) =', N); cat('\n')
   cat('\nSample:'); cat('\n')
   cat('\tSize (n) =', n, '->', (n - 1), 'df'); cat('\n')
   cat('\nSampling:'); cat('\n')
   cat('\tRepetitions =', sr); cat('\n\n')




More information about the R-help mailing list