-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathcrabsatellites-logit.sas
298 lines (237 loc) · 8.93 KB
/
crabsatellites-logit.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* Factors infuencing satelitte males at female crabs
These concepts will be illustrated using a dataset on nesting horseshoe crabs that is analyzed in
Agresti's book
The design of the study is given in Brockmann H.J. (1996). Satellite male groups in horseshoe crabs,
Limulus polyphemus. Ethology, 102, 1-21.
Each female horseshoe crab had a male resident in her nest. The study investigated other factors
affecting whether the female had any other males, called satellites residing nearby. These other factors
includes:
- crab color where 2=light medium, 3=medium, 4=dark medium, 5=dark.
- spine condition where 1=both good, 2=one worn or broken, or 3=both worn or broken.
- weight
- carapace width
The number of satellites was measured; for this example we will convert the number of satellite males
into a presence (number at least 1) or absence (no satellites). */
/* Change log
2015-07-13 CJS First edition */
/* Lines starting with *---part001b; or *---part001e; bracket the source
line for inclusion by LaTex and usually are not coded. */
dm 'output' clear;
dm 'log' clear;
proc datasets kill; run;
title; footnote; run;
ods graphics on;
title 'What factor influence presence/absence of satellite males?';
ods pdf file='crabsatellites-logit-SAS.pdf' style=styles.printer;
*---part001b;
data crabs;
infile 'crabsatellites.csv' dlm=',' dsd missover firstobs=2;
length males $8 weightclass $10;
input color spine width NMales Weight Males $ WeightClass $;
run;
*---part001e;
proc print data=crabs(obs=10);
title2 'part of the raw data';
run;
ods document name=scatter(write);
/* Preliminary scatter plot */
proc sgscatter data=crabs;
title2 'Preliminary Scatter plot';
matrix color spine width weight NMales;
run;
ods document close;
ods document name=weightplot(write);
/* plot of weight vs width */
proc sgplot data=crabs;
title2 'Weight vs Width';
scatter x=Width y=Weight;
run;
ods document close;
/* remove the outliers */
data crabs outliers;
set crabs;
outlier=0;
if weight < 1500 and width > 25 then outlier = 1;
if width < 21.5 then outlier=1;
if width > 33 then outlier=1;
if outlier=1 then output outliers;
else output crabs;
run;
proc print data=outliers;
title2 'outliers removed from data';
run;
*---partprelimcompb;
/* compute the proportion of female crabs with satellite males in each weight class */
data crabs2;
set crabs;
pmale=0;
if males = 'yes' then pmale=1;
run;
proc sort data=crabs2; by color weightclass; run;
proc means data=crabs2 noprint;
by color weightclass;
var pmale;
output out=pmale n=ncrabs mean=pmale;
run;
*---partprelimcompe;
proc print data=pmale;
title2 'Empirical proportion of satellite males';
var color weightclass ncrabs pmale;
format pmale 7.3;
run;
*---partprofileb;
/* Create profile plots of the summarized */
/* in order to offset the standard error bars, we need to create a special code
for the weightclass variable with special formats for plotting */
data plotdata;
set pmale;
pmale_lcl = max(0,pmale - 1.96*sqrt(pmale*(1-pmale)/ncrabs));
pmale_ucl = min(1,pmale + 1.96*sqrt(pmale*(1-pmale)/ncrabs));
if weightclass = '0000-2000' then weightclass2 = 1;
if weightclass = '2000-2500' then weightclass2 = 2;
if weightclass = '2500-3000' then weightclass2 = 3;
if weightclass = '3000+' then weightclass2 = 4;
if color = 2 then weightclass2 = weightclass2 - .1;
if color = 3 then weightclass2 = weightclass2 - .05;
if color = 4 then weightclass2 = weightclass2 + .0;
if color = 5 then weightclass2 = weightclass2 + .05;
run;
*proc print data=plotdata;
*run;
proc format;
value weightclassfmt 1='0000-2000' 2='2000-2500' 3='2500-3000' 4='3000+';
run;
ods document name=profile1(write);
proc sgplot data=plotdata;
title2 'Preliminary profile plot';
series x=weightclass2 y=pmale / group=color;
highlow x=weightclass2 low=pmale_lcl high=pmale_ucl / group=color;
format weightclass2 weightclassfmt.;
xaxis label='Weight Class' values=(1,2,3,4) integer offsetmin=.07 offsetmax=0.07;
yaxis label='p(satellite male) with 95% ci';
run;
ods document close;
*---partprofilee;
*---partgenmodfitb;
ods document name=genmod1(write);
/********* using GENMOD - full model ********/
proc genmod data=crabs plots=all descending ;
class color;
model males = weight | color / dist=binomial link=logit type3;
ods output parameterestimates=genmodest;
ods output type3 =genmodtest;
run;
*---partgenmodfite;
ods document close;
ods document name=genmod2(write);
*---partgenmodredfitb;
/********* using GENMOD - reduced model ********/
proc genmod data=crabs plots=all ;
class color;
model males = color weight / dist=binomial link=logit type3;
output out=genmodredpred xbeta=xbeta pred=pred_pmale lower=lcl_pmale upper=ucl_pmale;
lsmeans color / diff cl adjust=tukey ilink oddsratio;
estimate 'color 5 vs avg rest' color -1 -1 -1 3 / divisor=3;
ods output parameterestimates=genmodredest;
ods output type3 =genmodredtest;
ods output lsmeans =genmodredlsmeans;
ods output diffs =genmodredlsmeansdiffs;
ods output estimates =genmodredestimates;
run;
*---partgenmodredfite;
ods document close;
ods document name=finalprofile(write);
proc sort data=genmodredpred; by color weight; run;
proc sgplot data=genmodredpred;
title2 'Predicted values from the reduced model';
series x=weight y=pred_pmale / group=color;
*band x=weight lower=lcl_pmale upper=ucl_pmale / group=color;
xaxis label='Weight';
yaxis label='Predicted P(satelitte alive)';
run;
ods document close;
ods pdf close;
/* now create the LaTeX files for inclusion in the course notes */
%include "../../MyLatexTagset.sas"; run;
ods listing close;
title;
footnote;
ods listing;
proc document name=genmod1;
list /levels=all;
run;
ods tagsets.mylatex file='crabsatellites-logit-SAS-data.tex' (notop nobot);
proc print data=crabs(obs=10);
run;
ods tagsets.mylatex close;
/* the scatterplot matrix */
ods listing;
goptions device=png colors=(black) rotate=landscape;
ods graphics on / imagefmt=png imagename='crabsatellites-logit-SAS-pairsplot' reset=index;
proc document name=scatter;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;
/* the weight vs width plot matrix */
ods listing;
goptions device=png colors=(black) rotate=landscape;
ods graphics on / imagefmt=png imagename='crabsatellites-logit-SAS-weightplot' reset=index;
proc document name=weightplot;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;
/* proportion of males by color and weight class */
ods tagsets.mylatex file='crabsatellites-logit-SAS-prelimcomp.tex' (notop nobot);
proc print data=pmale;
var color weightclass ncrabs pmale;
format pmale 7.3;
run;
ods tagsets.mylatex close;
/* preliminary profile plot */
ods listing;
goptions device=png colors=(black) rotate=landscape;
ods graphics on / imagefmt=png imagename='crabsatellites-logit-SAS-prelimplot' reset=index;
proc document name=profile1;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;
/* genmod output from the full model */
ods tagsets.mylatex file='crabsatellites-logit-SAS-genmodtest.tex' (notop nobot);
proc print data=genmodtest noobs label split=" ";
run;
ods tagsets.mylatex close;
/* genmod output from the reduced model */
ods tagsets.mylatex file='crabsatellites-logit-SAS-genmodredest.tex' (notop nobot);
proc print data=genmodredest noobs label split=" ";
run;
ods tagsets.mylatex close;
ods tagsets.mylatex file='crabsatellites-logit-SAS-genmodredtest.tex' (notop nobot);
proc print data=genmodredtest noobs label split=" ";
run;
ods tagsets.mylatex close;
ods tagsets.mylatex file='crabsatellites-logit-SAS-genmodredestimates.tex' (notop nobot);
proc print data=genmodredestimates noobs label split=" ";
run;
ods tagsets.mylatex close;
ods tagsets.mylatex file='crabsatellites-logit-SAS-genmodredlsmeandiff1.tex' (notop nobot);
proc print data=genmodredlsmeansdiffs noobs label split=" ";
var effect color _color estimate stderr;
run;
ods tagsets.mylatex close;
ods tagsets.mylatex file='crabsatellites-logit-SAS-genmodredlsmeandiff2.tex' (notop nobot);
proc print data=genmodredlsmeansdiffs noobs label split=" ";
var effect color _color oddsratio lowerOR upperOR;
run;
ods tagsets.mylatex close;
/* final profile plot */
ods listing;
goptions device=png colors=(black) rotate=landscape;
ods graphics on / imagefmt=png imagename='crabsatellites-logit-SAS-finalprofileplot' reset=index;
proc document name=finalprofile;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;