Why does the curve() doesn't work for negative binomial distribution?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does the curve() doesn't work for negative binomial distribution?

qqjwl
Dear everyone,
      I want to draw the density lines of the a negative binomial distribution X~Neg(6,0.3). The code lines(1:50, dnbinom(1:50, 6, 0.3)) work, but curve(dnbinom(x, 6, 0.3)) doesn't work, why?
 Thank you very much!
        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: Why does the curve() doesn't work for negative binomial distribution?

Michael Weylandt
> I'm no R expert, but I think I can help with this one.

>
> In short, the problem is that you are trying to evaluate dnbinom(x) for x
> not an integer. Well, not you exactly: but the curve function, which
> automatically makes a mesh of rational numbers over which to graph
> dnbinom(x).  On my system, the call produces a funny graph (flat with spikes
> at the integers if I jiggle the parameters just right) and produces warnings
> of the form:
>
> " In dnbinom(x, 6, 0.3) : non-integer x = 1.040000 "
>
> which should make the underlying problem pretty clear to you.
>
> One simple work-around, depending on what you want is to call instead:
>
> curve(dnbinom(floor(x),6,0.3))  or curve(dnbinom(ceil(x),6,0.3)) or
> curve(dnbinom(round(x),6,0.3))
>
> which will round all the non-integer calls down/up/nearest to an integer;
> this will produce a graph that I believe gets at what you want.
>
> You'll note that in your code that does work, you only call dnbinom at the
> integers 1,2,...,50.
>
> Depending on how much probability theory you know, you should be careful of
> how you interpret the graph that lines(1:50, dnbinom(1:50, 6, 0.3))
> produces...there's a few knotty technical issues relating to the values of
> the PMF between integers and this graph solves them in one way (linear
> interpolation), but you have to engage the question for yourself before
> making too many inferences. Since this is a somewhat thorny issue, my
> recommendation is that you handle the plot call yourself so you know exactly
> what you are getting.
>
> Hope this helps and good luck getting started with R,
>
> Michael Weylandt
>
>
>
>
>
>
> On Wed, Jul 13, 2011 at 4:05 AM, 郝立亚 <[hidden email]> wrote:
>
>> Dear everyone,
>>      I want to draw the density lines of the a negative binomial
>> distribution X~Neg(6,0.3). The code lines(1:50, dnbinom(1:50, 6, 0.3)) work,
>> but curve(dnbinom(x, 6, 0.3)) doesn't work, why?
>>  Thank you very much!
>>        [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R questions
>> should go.
>>
>
>
        [[alternative HTML version deleted]]


_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: Why does the curve() doesn't work for negative binomial distribution?

Arun.stat
In reply to this post by qqjwl
I am quite sure that, the 1st reply (other that this!) you would get is to post this in R-forum.
_____________________________________________________

Arun Kumar Saha, FRM
QUANTITATIVE RISK AND HEDGE CONSULTING SPECIALIST
Visit me at: http://in.linkedin.com/in/ArunFRM
_____________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: Why does the curve() doesn't work for negative binomial distribution?

Matthieu Stigler
the second reply is that to analyse the output of the warnings: they
tell you the function is evluated at non-integers value.

A solution is then to specify yourself the values, making sure the
sequence has only integers, like for example:
curve(dbinom(x, size=6, prob=0.3), from=1, to=50,n=50)

Mat

Le 13/07/2011 15:22, Arun.stat a écrit :

> I am quite sure that, the 1st reply (other that this!) you would get is to
> post this in R-forum.
> _____________________________________________________
>
> Arun Kumar Saha, FRM
> QUANTITATIVE RISK AND HEDGE CONSULTING SPECIALIST
> Visit me at: http://in.linkedin.com/in/ArunFRM
> _____________________________________________________
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Why-does-the-curve-doesn-t-work-for-negative-binomial-distribution-tp3664440p3665003.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.