Skip to content

Chi-Square Distribution Plots

6 messages · Bayesianbay@aol.com, John Fox, PIKAL Petr +3 more

#
Dear list

I have a vector of values that allegedly have a chi-squared distribution. I 
want to create a plot that shows the values I have obtained,  and the 
chi-squared distribution curve for the specified number of degrees of freedom 
to show what should have been obtained.

At the moment I am plotting the values I have obtained as a histogram and 
somehow want to put on to this plot the expected frequency curve. Is there a 
way of doing this in R?

Many thanks in davance
Laura
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Dear Laura,

You can use the truehist function in the MASS package to draw a histogram 
with density scaling. Then calculate the chi-square densities, and add the 
density curve to the plot; something like:

     x <- seq(min(data), max(data), length=100)
     d <- dchisq(x, df)
     lines(x, d)

Of course, this requires that you know the degrees of freedom (replacing 
df). You may want to modify the range of plotted density values or change 
the range of the axes.

It is probably preferable to use a quantile-comparison plot rather than a 
histogram to visualize the fit of the data to the chi-square distribution. 
It's not hard to construct one yourself, but the qq.plot function in the 
car package makes this simple: qq.plot(data, 'chisq', df=df); the resulting 
plot includes a point-wise confidence envelope.

I hope that this helps,
  John
At 04:57 AM 9/30/2002 -0400, Bayesianbay at aol.com wrote:
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Hi
On 30 Sep 2002 at 4:57, Bayesianbay at aol.com wrote:

            
after making histogram

something like

lines(density(rchisq(number,deg.f)))

could help, where number is number of generated points and deg.f is selected 
degrees of freedom
RegardsPetr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
How about something like (untested)

curve(dchisq(x,df=df),add=TRUE)

 [you'll need to make sure that you've plotted your histogram with 
freq=FALSE]
On Mon, 30 Sep 2002 Bayesianbay at aol.com wrote:

            

  
    
#
Suppose your chi-squared values witrh, let us say, 5 degrees of freedom
are in x. You need to make the histogram with the argument
probability=TRUE first. Then:
Kjetil Halvorsen
Bayesianbay at aol.com wrote:
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 2002-09-30 at 03:57, Bayesianbay at aol.com wrote:
Of course. I assume a qqplot is what you are looking for. Use something
like this line:

qqplot(qchisq(ppoints(375), df=9), actual.values)

This puts the expected distribution on the x-axis, and the empirical
distribution on the y-axix. Change the number of data points (375) and
the degrees of freedom (9) to values appropriate to your data.