forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathant.sas
270 lines (225 loc) · 7.25 KB
/
ant.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
/* Searching for the time where treatment and control diverge
Change point problem.
This example is based on a project by Nate Derstine of
Biological Sciences at Simon Fraser University.
The data are simulated, but illustrative of the process.
Considered one of the worst invasive pest ants, the electric ant,
or little fire ant ({\it Wasmannia auropunctata} (Roger) (Hymenoptera:
Formicidae)) has negatively impacted both biodiversity and agriculture.
Its distribution is nearlypantropical, and greenhouse infestations have
been reported as far north as Canada and the United
Kingdom. Current {\it W. auropunctata} detection methods commonly employ a food item like peanut
butter. An alternative detection method may be found in pheromone attractants. For W. auropunctata,
a one-way trap containing an alarm pheromone has been
successfully used to detect little fire ant populations in macadamia nut orchards. What is the
longevity of this type of pheromone lure as used in a unique one-way ant
trap.
At the beginning of the experiment, 180 control traps and 180 treatment traps were prepared. On each day,
three traps of each type were randomized to locations in the orchard where the ant species were known to be present.
24 hours later, the traps were retrieved and the number of ants capture counted and the trap is discarded. */
/* Lines starting with *---partxxxe and *---parxxxb are used in my Latex file and
should be ignored */
/* The tagsets.tablesonlylatex again is used by the Latex Program course notes */
dm 'output' clear;
dm 'log' clear;
proc datasets kill; run;
title 'Lifetime of pheromone attraction to ants';
options orientation=landscape;
ods pdf file='ant-SAS.pdf';
goptions device=pdf color=(black) rotate=landscape;
*---part010b;
proc import file='ant.csv' dbms=csv out=ant replace;
run;
*---part010e;
proc print data=ant(obs=8);
title2 'part of the raw data';
run;
ods document name=plot20(write);
*---part020b;
/* plot of the raw data */
proc sgplot data=ant;
title2 'plot of the raw counts';
scatter x=day y=count / group=trt;
xaxis label='Day';
yaxis label='Count';
run;
*---part020e;
ods document close;
*---part023b;
/* Do a t-test for each day */
proc sort data=ant; by day; run;
ods select none;
proc ttest data=ant;
by day;
class trt;
var count;
ods output ttests=ttests;
run;
ods select all; run;
data ttests; /* only select unequal var ttest */
set ttests;
if variances = 'Unequal';
run;
*---part023e;
proc print data=ttests(obs=10);
title2 'part of the output from the ttests';
run;
ods document name=plot23(write);
proc sgplot data=ttests;
title2 'P-values of t-tests by day';
scatter x=day y=probt;
yaxis label='P-value from t-test' max=0 min=1;
xaxis label='Day';
refline .05 / axis=Y;
format probt 7.2;
run;
ods document close;
*---part030b;
/* compute the mean for each day-trt combination */
proc sort data=ant; by day trt; run;
proc means data=ant noprint;
by day trt;
var count;
output out=mean_count mean=mean_count;
run;
*---part030e;
ods document name=plot30(write);
/* plot the mean counts */
proc sgplot data=mean_count;
title2 'Plot of the mean daily counts';
scatter x=day y=mean_count / group=trt;
xaxis label='Day';
yaxis label='Mean Count';
run;
ods document close;
*---part032b;
/* Find the ratio of the means */
proc transpose data=mean_count out=trans_mean_count;
by day;
var mean_count;
id trt;
run;
data trans_mean_count;
set trans_mean_count;
log_ratio = log(treatment/control);
attrib log_ratio format=7.2 label='log(Treatment/Control)';
run;
*---part032e;
proc print data=trans_mean_count(obs=10);
title2 'part of log(ratio) dataset';
run;
ods document name=plot32(write);
proc sgplot data=trans_mean_count;
title2 'Plot of log(Treatment/Control) means';
scatter x=day y=log_ratio;
refline 0 / axis=Y;
xaxis label='Day';
yaxis label='log(Treatment Mean/Control Mean)';
run;
ods document close;
ods document name=nlin40(write);
*---part040b;
/* Fit a changepoint model */
proc nlin data=trans_mean_count;
title2 'Change point model';
parms CP=45 beta0=1 beta1=.1 beta2=.1;
if (day < CP) then
mean = beta0 + beta1*day;
else mean = beta0 + beta1*day +beta2*(day-CP);
model log_ratio = mean;
output out=model_fit predicted=pred;
ods output ParameterEstimates=Nlin_est;
run;
*---part040e;
ods document close;
ods document name=plot40(write);
proc sgplot data=model_fit;
title2 'Fitted model';
scatter x=day y=log_ratio;
series x=day y=pred;
run;
ods document close;
ods document name=nlin50(write);
*---part050b;
/* Fit a change point model where the slope is forced to be zero after the change*/
proc nlin data=trans_mean_count;
title2 'Change point model - slope = 0 after change point';
parms CP=45 beta0=1 beta1=.1 1;
mean = beta0 + beta1*min(cp,day);
model log_ratio = mean;
output out=model_fit0 predicted=pred;
ods output ParameterEstimates=Nlin_est0;
run;
*---part050e;
ods document close;
ods document name=plot50(write);
proc sgplot data=model_fit0;
title2 'Fitted model - slope = 0 after change point';
scatter x=day y=log_ratio;
series x=day y=pred;
run;
ods document close;
ods pdf close;
/* create the files to be included in the LaTex document */
/* Create LaTeX files for inclusion by my notes */
%include "../../MyLatexTagset.sas"; run;
title;
footnote;
ods listing;
proc document name=nlin40;
list /levels=all;
run;
ods tagsets.mycolorlatex file='ant-SAS-010.tex' (notop nobot);
proc print data=ant(obs=10) label split=" ";
run;
ods tagsets.mycolorlatex close;
ods listing;
ods graphics on / imagefmt=png imagename='ant-SAS-020' reset=index;
proc document name=plot20;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;
ods listing;
ods graphics on / imagefmt=png imagename='ant-SAS-023' reset=index;
proc document name=plot23;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;
ods listing;
ods graphics on / imagefmt=png imagename='ant-SAS-030' reset=index;
proc document name=plot30;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;
ods listing;
ods graphics on / imagefmt=png imagename='ant-SAS-032' reset=index;
proc document name=plot32;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;
ods tagsets.mycolorlatex file='ant-SAS-040.tex' (notop nobot);
proc print data=Nlin_est label split=" ";
run;
ods tagsets.mycolorlatex close;
ods listing;
ods graphics on / imagefmt=png imagename='ant-SAS-040' reset=index;
proc document name=plot40;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;
ods tagsets.mycolorlatex file='ant-SAS-050.tex' (notop nobot);
proc print data=Nlin_est0 label split=" ";
run;
ods tagsets.mycolorlatex close;
ods listing;
ods graphics on / imagefmt=png imagename='ant-SAS-050' reset=index;
proc document name=plot50;
replay \Sgplot#1\SGPlot#1 / dest=listing;
run;
ods graphics off;
ods listing close;