Published on :
Statistics CREATION_INTERNE

Poisson Regression with PROC MCMC

This code is also available in: Deutsch Español Français
Awaiting validation
This script illustrates how to perform Poisson regression in a Bayesian framework. It starts by creating an 'insure' dataset containing insurance claims. It then uses PROC TRANSREG to create a design matrix for categorical variables. The main analysis is performed with PROC MCMC to estimate model parameters. Finally, a comparative validation is performed using PROC GENMOD with the BAYES statement.
Data Analysis

Type : CREATION_INTERNE


Data is generated directly within the script via a DATA step using 'datalines'.

1 Code Block
DATA STEP Data
Explanation :
Creation of the 'insure' dataset containing information on the number of policies (n), the number of claims (c), the car type (car), and age. A logarithmic offset variable 'ln' is calculated.
Copied!
1title 'Poisson Regression';
2DATA insure;
3 INPUT n c car $ age;
4 ln = log(n);
5 DATALINES;
6 500 42 small 0
7 1200 37 medium 0
8 100 1 large 0
9 400 101 small 1
10 500 73 medium 1
11 300 14 large 1
12;
2 Code Block
PROC TRANSREG Data
Explanation :
Use of PROC TRANSREG to generate dummy variables for the categorical variable 'car', thus preparing the data for PROC MCMC.
Copied!
1PROC TRANSREG DATA=insure design;
2 model class(car / zero=last);
3 id n c age ln;
4 OUTPUT out=input_insure(drop=_: Int:);
5 RUN;
3 Code Block
PROC MCMC
Explanation :
Execution of the Bayesian analysis with PROC MCMC. The model specifies a Poisson distribution for the response variable 'c', with a log link function and an 'ln' offset. Priors are defined as normal.
Copied!
1PROC MCMC DATA=input_insure outpost=insureout nmc=5000 propcov=quanew
2 maxtune=0 seed=7;
3 ods select PostSumInt;
4 array DATA[4] 1 &_trgind age;
5 array beta[4] alpha beta_car1 beta_car2 beta_age;
6 parms alpha beta:;
7 prior alpha beta: ~ normal(0, prec = 1e-6);
8 call mult(DATA, beta, mu);
9 model c ~ poisson(exp(mu+ln));
10RUN;
4 Code Block
PROC GENMOD
Explanation :
Use of PROC GENMOD with the BAYES statement to fit the same Poisson regression model. This serves as a comparison point to validate the results obtained with PROC MCMC.
Copied!
1PROC GENMOD DATA=insure;
2 ods select PostSummaries PostIntervals;
3 class car age(descending);
4 model c = car age / dist=poisson link=log offset=ln;
5 bayes seed=17 nmc=5000 coeffprior=normal;
6RUN;
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.