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!
title 'Poisson Regression';
data insure;
input n c car $ age;
ln = log(n);
datalines;
500 42 small 0
1200 37 medium 0
100 1 large 0
400 101 small 1
500 73 medium 1
300 14 large 1
;
1
title 'Poisson Regression';
2
DATA insure;
3
INPUT n c car $ age;
4
ln = log(n);
5
DATALINES;
6
50042 small 0
7
120037 medium 0
8
1001 large 0
9
400101 small 1
10
50073 medium 1
11
30014 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!
proc transreg data=insure design;
model class(car / zero=last);
id n c age ln;
output out=input_insure(drop=_: Int:);
run;
1
PROC TRANSREGDATA=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.
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!
proc genmod data=insure;
ods select PostSummaries PostIntervals;
class car age(descending);
model c = car age / dist=poisson link=log offset=ln;
bayes seed=17 nmc=5000 coeffprior=normal;
run;
1
PROC GENMODDATA=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;
6
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.