# plotting functions of chi square

 Classic List Threaded
8 messages
Reply | Threaded
Open this post in threaded view
|

## plotting functions of chi square

 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? Thanks! Maja
Reply | Threaded
Open this post in threaded view
|

## Re: plotting functions of chi square

 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. > > ______________________________________________ > [hidden email] 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: plotting functions of chi square

 Thanks, but that wasn't what I was going for. Like I said, I know how to do a simple chi-square density plot with dchisq(). What I'm trying to do is chi-square / degrees of freedom. Hence rchisq(100000,i)/i). How do I do that with dchisq?
Reply | Threaded
Open this post in threaded view
|

## Re: plotting functions of chi square

 Hi: On Tue, Aug 17, 2010 at 2:28 PM, maiya <[hidden email]> wrote: > > Thanks, but that wasn't what I was going for. Like I said, I know how to do > a > simple chi-square density plot with dchisq(). > > What I'm trying to do is chi-square / degrees of freedom. Hence > rchisq(100000,i)/i). > > How do I do that with dchisq? > You write a function. Compare the following: # chi-square densities: curve( dchisq(x, df=5), 0, 50, col='red') curve( dchisq(x, df=10), 0, 50, col='blue', add = TRUE) curve( dchisq(x, df=20), 0, 50, col='green', add = TRUE) # chi-square density/df: g <- function(x, df)  dchisq(x, df)/df curve(g(x, df = 5), 0, 50) curve(g(x, df = 10), 0, 50, col = 'blue', add = TRUE) curve(g(x, df = 20), 0, 50, col = 'green', add = TRUE) Is that what you're after? HTH, Dennis > -- > View this message in context: > http://r.789695.n4.nabble.com/plotting-functions-of-chi-square-tp2329020p2329057.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: plotting functions of chi square

 In reply to this post by maiya x <- seq(from = 0, to = 10, by = .01) plot(x, dchisq(x = x, df = 1), main = "title", sub = "subtitle", type = "l", xlab = "x-title", ylab = "y-title", ylim = c(0, .4), col = "red") lines(x, dchisq(x = x, df = 2), type = "l", col = "green") lines(x, dchisq(x = x, df = 3), type = "l", col = "blue") lines(x, dchisq(x = x, df = 4), type = "l", col = "pink") lines(x, dchisq(x = x, df = 5), type = "l", col = "darkgreen") On Aug 17, 2010, at 1: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? Thanks! Maja -- View this message in context: http://r.789695.n4.nabble.com/plotting-functions-of-chi-square-tp2329020p2329020.htmlSent from the R help mailing list archive at Nabble.com. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: plotting functions of chi square

 In reply to this post by maiya maiya gmail.com> writes: > Thanks, but that wasn't what I was going for. Like I said, I know how to do a > simple chi-square density plot with dchisq(). > > What I'm trying to do is chi-square / degrees of freedom. Hence > rchisq(100000,i)/i). > > How do I do that with dchisq?   By explicitly using the formula to change variables: plot(density(rchisq(10000,5)/5)) dscchisq <- function(x,df) {   dchisq(x*df,df)*df } curve(dscchisq(x,5),add=TRUE,col=2)   note that density() generates a smoothing artifact near zero ... ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: plotting functions of chi square

 In reply to this post by maiya On Aug 17, 2010, at 5:28 PM, maiya wrote: > > Thanks, but that wasn't what I was going for. Like I said, I know   > how to do a > simple chi-square density plot with dchisq(). > > What I'm trying to do is chi-square / degrees of freedom. Hence > rchisq(100000,i)/i). > > How do I do that with dchisq? Maybe you should explain what you are trying to achieve or illustrate?   X^2/df is not a verb, hence one cannot "do" it. If you want to plot a   chi-square density on a scaled x-axis then you should say so. Let's   assume for a moment that my guess is correct, ... then this would be   the first cut: plot(1, type="n",  xlab="", ylab="", xlim=c(0,2), ylim=c(0,1e-2)) for (i in c(10,50,100,200,500)){ lines(seq(0,2, by=0.01), dchisq(seq(0,2*i, 0.01*i),i)/i)                                 } Notice that the peak heights are gradually spreading out. You will   need a vertical factor that increases as some function of "i" in front   of that dchisq(). Playing around with the ylims and scaling factors produces: plot(1, type="n",  xlab="", ylab="", xlim=c(0,2), ylim=c(0,.1)) for (i in c(10,50,100,200,500)){ lines(seq(0,2, by=0.01), i*dchisq(seq(0,2*i, 0.01*i),i)/i) } Using i^2 as a vertical scaling factor : plot(1, type="n",  xlab="", ylab="", xlim=c(0,2), ylim=c(0,10)) for (i in c(10,50,100,200,500)){ lines(seq(0,2, by=0.01), i^2*dchisq(seq(0,2*i, 0.01*i),i)/i)                                } Now, just what is it that you are trying to do? Or have I just done   someone's homework again? > -- > View this message in context: http://r.789695.n4.nabble.com/plotting-functions-of-chi-square-tp2329020p2329057.html> Sent from the R help mailing list archive at Nabble.com. > -- David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: plotting functions of chi square

 OK, for the record, this is not my homework, thanks for asking! Also, I am sure I could have phrased my question more eloquently, but (hence the newbie qualifier) I didn't.  The code I posted was for the plot I want, only smoothed i.e not based on random sampling from the distribution. Dennis: I tried that :) but your code divides the densities by df. I want the density of X^2/df Rookie: Same thing as David before - I know how to plot chi squared densities with different dfs! David: looks great! It's only the "playing around" that is off-putting... (sorry again for not explaining well, but illustrate I definitely did!) Ben & William: Thank you! Jointly you managed to plot exactly what I wanted and show me why and how so I can do it to more complicated functions! And just to prove you guys right, here's what I really wanted to plot - but refrained from mentioning in my original post: how by the central limit theorem for large df chi^2 approaches normality with a mean of df and variance of 2*df. d2chisq <- function(x,df) {   dchisq(x*sqrt(2*df)+df,df)*sqrt(2*df) } plot(1, type="n",  xlab="", ylab="", xlim=c(-3,3), ylim=c(0,0.5)) for (i in c(5,10,50,100,200,500)){ curve(d2chisq(x,i),add=TRUE) } lines(seq(-3,3,.1),dnorm(seq(-3,3,.1),0,1 ), col="red") Not bad considering I had to look up the chain rule on Wikipedia ;) Thanks again guys! maja.