Published on :
Général CREATION_INTERNE

Sans titre

This code is also available in: Deutsch Español Français
Awaiting validation
This program illustrates the use of the GENMOD procedure to analyze data following a Tweedie distribution. First, it simulates a dataset of 250 observations via a DATA step, constructing the response variable as a Poisson sum of Gamma variables (mathematical representation of the Tweedie distribution). Then, it fits two models: the first estimates parameters with the default Tweedie distribution, and the second fixes the power parameter p to 1.5.
Data Analysis

Type : CREATION_INTERNE


Data is entirely generated in the 'tmp1' DATA step using random functions (ranuni, ranpoi, rangam) based on parameters defined by macro variables.

1 Code Block
DATA STEP Data
Explanation :
Initialization of configuration macro-variables. Creation of the 'tmp1' table containing categorical (c1-c5) and continuous (d1, d2) explanatory variables, as well as a target variable 'yTweedie' simulated according to a compound Poisson-Gamma process.
Copied!
1%let nObs = 250;
2%let nClass = 5;
3%let nLevs = 4;
4%let seed = 100;
5 
6DATA tmp1;
7 array c{&nClass};
8 
9 keep c1-c&nClass yTweedie d1 d2;
10 
11 /* Tweedie parms */
12 phi=0.5;
13 p=1.5;
14 
15 DO i=1 to &nObs;
16 
17 DO j=1 to &nClass;
18 c{j} = int(ranuni(1)*&nLevs);
19 END;
20 
21 d1 = ranuni(&seed);
22 d2 = ranuni(&seed);
23 
24 xBeta = 0.5*((c2<2) - 2*(c1=1) + 0.5*c&nClass + 0.05*d1);
25 mu = exp(xBeta);
26 
27 /* Poisson distributions parms */
28 lambda = mu**(2-p)/(phi*(2-p));
29 /* Gamma distribution parms */
30 alpha = (2-p)/(p-1);
31 gamma = phi*(p-1)*(mu**(p-1));
32 
33 rpoi = ranpoi(&seed,lambda);
34 IF rpoi=0 THEN yTweedie=0;
35 ELSE DO;
36 yTweedie=0;
37 DO j=1 to rpoi;
38 yTweedie = yTweedie + rangam(&seed,alpha);
39 END;
40 yTweedie = yTweedie * gamma;
41 END;
42 OUTPUT;
43 END;
44RUN;
2 Code Block
PROC GENMOD
Explanation :
Execution of the GENMOD procedure to fit a generalized linear model on the simulated data, specifying a Tweedie distribution. The type3 option requests type 3 statistics.
Copied!
1PROC GENMOD DATA=tmp1;
2 class C1-C5;
3 model yTweedie = C1-C5 D1 D2 / dist=Tweedie type3;
4RUN;
3 Code Block
PROC GENMOD
Explanation :
Second execution of PROC GENMOD with a simplified model (fewer explanatory variables) and explicitly setting the Tweedie distribution's power parameter to 1.5.
Copied!
1PROC GENMOD DATA=tmp1;
2 class C1 C2;
3 model yTweedie = C1 C2 D1 / dist=Tweedie(p=1.5) type3;
4RUN;
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.
Copyright Info : SAS SAMPLE LIBRARY