## simulation from negative binomail distribution

Solved
Occasional Contributor
Posts: 12

# simulation from negative binomail distribution

[ Edited ]

I am using base sas 9.4. Now I am working on Simulating data.

After I fitted data to negative binomial distribution using the following code ;

proc countreg data=claim;
model y = / dist=negbin;
output out=exp_nb prob=prob_nb;
freq freq;
run;

proc format;
value yfmt
7-high = ">=7";
run;

proc means sum nway data=exp_nb;
class y;
var prob_nb;
format y yfmt.;
output out=exp_nb sum=_testp_;
run;

data exp_nb;
set exp_nb;
sumexp + _testp_;
sumtolast = lag(sumexp);
if y=7 then _testp_ = 1 - sumtolast;
run;

proc freq data=claims;
table y / chisq(testp=exp_nb df=-2 lrchisq);
format y yfmt.;
weight freq;
run;

Then I got only p-value from fitting this data.

Now I want to simulate data from negative binomial distribution.

How can I do that ? please help

(version of sas program do not have Proc iml procedure)

Accepted Solutions
Solution
‎07-10-2017 10:51 PM
PROC Star
Posts: 1,283

## Re: simulation from negative binomail distribution

You can simulate data from a negative binomial distribution using the RAND function like this

``````data negbin;
do x = 1 to 1000;
y = rand('negbinomial', 0.5, 2);
output;
end;
run;

title 'Plot the Simulated Data';
proc sgplot data = negbin;
histogram y;
run;
title;``````

Adjust p and k to your liking. The documentation is here

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001466748.htm

All Replies
Solution
‎07-10-2017 10:51 PM
PROC Star
Posts: 1,283

## Re: simulation from negative binomail distribution

You can simulate data from a negative binomial distribution using the RAND function like this

``````data negbin;
do x = 1 to 1000;
y = rand('negbinomial', 0.5, 2);
output;
end;
run;

title 'Plot the Simulated Data';
proc sgplot data = negbin;
histogram y;
run;
title;``````

Adjust p and k to your liking. The documentation is here

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001466748.htm

Occasional Contributor
Posts: 12

## Re: simulation from negative binomail distribution

could you please tell me that how can I know p of this data from fitting the distribution.

because when I fitted this distribution I fitted from raw data of frequency of claim of each auto policy. That's why I do not know P of this data.

Super User
Posts: 10,784

## Re: simulation from negative binomail distribution

[ Edited ]

Ha. Here is.

http://blogs.sas.com/content/iml/2012/04/04/fitting-a-poisson-distribution-to-data-in-sas.html

``````proc genmod data=MyData;
model N = / dist=negbin;
output out=Fit p=lambda;
run;``````

http://support.sas.com/kb/24/166.html

Occasional Contributor
Posts: 12

## Re: simulation from negative binomail distribution

I am sorry to bother you. But after following your suggestion I got the attached output.

How can I know this model fits the data or not.

Super User
Posts: 10,784

## Re: simulation from negative binomail distribution

``` It looks good.
Value/DF is near  1 .

```
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 5 replies
• 749 views
• 7 likes
• 3 in conversation