Published on :
Statistics CREATION_INTERNE

Negative-Binomial Data Generation and Estimation

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins with a DATA STEP to simulate a variable Y following a negative-binomial distribution with defined Mu and Kappa parameters. The simulation uses the RANGAM and RANPOI functions. Subsequently, PROC GLIMMIX is employed to estimate the parameters of the negative-binomial distribution from the generated data. PROC PRINT and PROC MEANS steps are used to display the true and estimated values of Mu, Kappa, the mean, and the variance, allowing for comparison.
Data Analysis

Type : CREATION_INTERNE


Data is generated internally via a DATA STEP simulating a negative-binomial distribution. No external dataset is required.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block generates 2000 observations of a variable 'Y' following a negative-binomial distribution. The parameters Mu (mean=30) and Kappa (dispersion=0.1) are defined, along with a seed for reproducibility. The RANGAM (for Gamma distribution) and RANPOI (for Poisson distribution) functions are used for simulation. The theoretical variance is also calculated.
Copied!
1DATA one;
2 Mu = 30;
3 Kappa = 0.1;
4 alpha = 1 / kappa;
5 beta = kappa * mu;
6 n = 2000;
7 seed = 1917;
8 Variance = mu * ( 1 + mu*kappa);
9 DO id=1 to n;
10 u = beta * rangam( seed, alpha );
11 Y = ranpoi( seed, u );
12 OUTPUT;
13 END;
14 keep Mu Kappa Variance id y;
15 RUN;
2 Code Block
PROC GLIMMIX
Explanation :
This block uses PROC GLIMMIX to estimate the Mu and Kappa parameters of the negative-binomial distribution from the generated data 'one'. The `dist=negbin` option specifies the distribution, and `link=identity` the link. The ODS statement is used to capture parameter estimates into the 'parms' dataset and suppress standard output during procedure execution.
Copied!
1*--- Get estimates of Mu and Kappa using PROC GLIMMIX;
2 ods select none;
3 ods OUTPUT ParameterEstimates=parms;
4 PROC GLIMMIX DATA=one;
5 model y = / dist=negbin link=identity s;
6 RUN;
7 ods select all;
3 Code Block
PROC PRINT
Explanation :
This block configures HTML output and displays the initial (true) values of Mu and Kappa used for data generation, extracted from the first record of the 'one' dataset.
Copied!
1ods html;
2 title "True Mu and Kappa of Y's iid Negative-binomial(Mu, Kappa)";
3 PROC PRINT DATA=one noobs;
4 where id = 1;
5 var Mu Kappa;
6 RUN;
4 Code Block
PROC PRINT
Explanation :
This block displays the estimates of Mu and Kappa obtained by PROC GLIMMIX, stored in the 'parms' dataset.
Copied!
1title "Estimated Mu and Kappa using PROC GLIMMIX";
2 PROC PRINT DATA=parms noobs;
3 var Effect Estimate;
4 RUN;
5 Code Block
DATA STEP
Explanation :
This simple DATA STEP renames the variable 'Mu' to 'Mean' in the 'one' dataset for better clarity in subsequent displays.
Copied!
1DATA one;
2 SET one;
3 rename Mu = Mean;
4 RUN;
6 Code Block
PROC PRINT
Explanation :
This block displays the theoretical mean (Mean) and variance (Variance) of the negative-binomial distribution, calculated and stored during data generation.
Copied!
1title "True Mean and Variance of Y's iid Negative-binomial(Mu, Kappa)";
2 PROC PRINT DATA=one noobs;
3 where id = 1;
4 var Mean Variance;
5 RUN;
7 Code Block
PROC MEANS
Explanation :
This block uses PROC MEANS to calculate and display the observed mean and variance of the variable 'Y' from the generated data. This allows comparing the actual sample statistics with theoretical and estimated values. The `ods html close` statement closes the HTML output file.
Copied!
1title "Estimated Mean and Variance";
2 PROC MEANS DATA=one n mean var maxdec=2;
3 var y;
4 RUN;
5 ods html close;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.