[R] plotting functions of chi square

David Winsemius dwinsemius at comcast.net
Tue Aug 17 23:14:52 CEST 2010


On Aug 17, 2010, at 4:57 PM, maiya wrote:

>
> Hi! This is going to be a real newbie question, but I can't figure  
> it out.
>
> I'm trying to plot densities of various functions of chi-square. A  
> simple
> chi-square plot I can do with dchisq(). But e.g. chi.sq/degrees of  
> freedom I
> only know how to do using density(rchisq()/df). For example:
>
> plot(1, type="n",  xlab="", ylab="", xlim=c(0,2), ylim=c(0,7))
>
> for (i in c(10,50,100,200,500)){
> lines(density(rchisq(100000,i)/i))
> }
>
> But even with 100,000 samples the curves still aren't smooth. Surely  
> there
> must be a more elegant way to do this?

If you want smooth curves then dchisq is the way to go. My guess is  
that you have not yet figured out that there is a second parameter  
which controls the df of the dchisq output (as well as having a third  
non-centrality parameter):

set.seed(123)
x <- rchisq(100, 5)
hist(x, prob=TRUE)
curve( dchisq(x, df=5), col='red', add=TRUE)

>
> Thanks!
>
> Maja
> -- 
> View this message in context: http://r.789695.n4.nabble.com/plotting-functions-of-chi-square-tp2329020p2329020.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list