40c69d0f97946794572c21dd13f9211fe1b0e3ae
[pspp] / tests / language / stats / rank.at
1 dnl PSPP - a program for statistical analysis.
2 dnl Copyright (C) 2017 Free Software Foundation, Inc.
3 dnl 
4 dnl This program is free software: you can redistribute it and/or modify
5 dnl it under the terms of the GNU General Public License as published by
6 dnl the Free Software Foundation, either version 3 of the License, or
7 dnl (at your option) any later version.
8 dnl 
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 dnl GNU General Public License for more details.
13 dnl 
14 dnl You should have received a copy of the GNU General Public License
15 dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 dnl
17 AT_BANNER([RANK])
18
19 AT_SETUP([RANK simple case with defaults])
20 AT_DATA([rank.sps], [dnl
21 DATA LIST LIST NOTABLE /x (f8).
22 BEGIN DATA.
23 -1
24
25
26
27
28
29
30 END DATA.
31
32 RANK x.
33
34 LIST.
35 ])
36 AT_CHECK([pspp -o pspp.csv rank.sps])
37 AT_CHECK([cat pspp.csv], [0], [dnl
38 Variables Created By RANK
39
40
41
42 x into Rx(RANK of x)
43
44 Table: Data List
45 x,Rx
46 -1,1.000
47 0,2.000
48 1,3.000
49 2,4.500
50 2,4.500
51 4,6.000
52 5,7.000
53 ])
54 AT_CLEANUP
55
56 # This checks for regression against a crash reported as bug #38482
57 # that occurred when multiple variables were specified without any
58 # rank specifications.
59 AT_SETUP([RANK multiple variables with defaults])
60 AT_DATA([rank.sps], [dnl
61 DATA LIST LIST NOTABLE /x * y * z *.
62 BEGIN DATA.
63     1.00     2.00     3.00
64     4.00     5.00     6.00
65 END DATA.
66
67 RANK ALL.
68
69 LIST.
70 ])
71 AT_CHECK([pspp -o pspp.csv rank.sps])
72 AT_CHECK([cat pspp.csv], [0], [dnl
73 Variables Created By RANK
74
75
76
77 x into Rx(RANK of x)
78
79 y into Ry(RANK of y)
80
81 z into Rz(RANK of z)
82
83 Table: Data List
84 x,y,z,Rx,Ry,Rz
85 1.00,2.00,3.00,1.000,1.000,1.000
86 4.00,5.00,6.00,2.000,2.000,2.000
87 ])
88 AT_CLEANUP
89
90 AT_SETUP([RANK with RANK, RFRACTION, N])
91 AT_DATA([rank.sps], [dnl
92 DATA LIST LIST NOTABLE /a * b *.
93 BEGIN DATA.
94 0 24
95 1 32
96 2 31
97 2 32
98 4 30
99 5 29
100 6 1
101 7 43
102 8 .
103 9 45
104 END DATA.
105
106 RANK a b (D)
107    /PRINT=YES 
108    /RANK
109    /TIES=HIGH
110    /RFRACTION
111    /N INTO count
112    .
113
114 DISPLAY DICTIONARY.
115
116 LIST.
117 ])
118 AT_CHECK([pspp -o pspp.csv rank.sps])
119 AT_CHECK([cat pspp.csv], [0], [dnl
120 Variables Created By RANK
121
122
123
124 a into Ra(RANK of a)
125
126 b into Rb(RANK of b)
127
128 a into RFR001(RFRACTION of a)
129
130 b into RFR002(RFRACTION of b)
131
132 a into count(N of a)
133
134 b into Nb(N of b)
135
136 Table: Variables
137 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
138 a,1,,Scale,Input,8,Right,F8.2,F8.2,
139 b,2,,Scale,Input,8,Right,F8.2,F8.2,
140 Ra,3,RANK of a,Scale,Input,8,Right,F9.3,F9.3,
141 RFR001,4,RFRACTION of a,Scale,Input,8,Right,F6.4,F6.4,
142 count,5,N of a,Scale,Input,8,Right,F6.0,F6.0,
143 Rb,6,RANK of b,Scale,Input,8,Right,F9.3,F9.3,
144 RFR002,7,RFRACTION of b,Scale,Input,8,Right,F6.4,F6.4,
145 Nb,8,N of b,Scale,Input,8,Right,F6.0,F6.0,
146
147 Table: Data List
148 a,b,Ra,RFR001,count,Rb,RFR002,Nb
149 .00,24.00,10.000,1.0000,10,8.000,.8889,9
150 1.00,32.00,9.000,.9000,10,4.000,.4444,9
151 2.00,31.00,8.000,.8000,10,5.000,.5556,9
152 2.00,32.00,8.000,.8000,10,4.000,.4444,9
153 4.00,30.00,6.000,.6000,10,6.000,.6667,9
154 5.00,29.00,5.000,.5000,10,7.000,.7778,9
155 6.00,1.00,4.000,.4000,10,9.000,1.0000,9
156 7.00,43.00,3.000,.3000,10,2.000,.2222,9
157 8.00,.  ,2.000,.2000,10,.   ,.    ,.
158 9.00,45.00,1.000,.1000,10,1.000,.1111,9
159 ])
160 AT_CLEANUP
161
162 AT_SETUP([RANK with SAVAGE, PERCENT, PROPORTION, NTILES])
163 AT_DATA([rank.sps], [dnl
164 DATA LIST LIST NOTABLE /a * b *.
165 BEGIN DATA.
166 0 24
167 1 32
168 2 31
169 2 32
170 4 30
171 5 29
172 6 1
173 7 43
174 8 8 
175 9 45
176 END DATA.
177
178 RANK a
179   /PRINT=YES
180   /TIES=CONDENSE
181   /SAVAGE
182   /PERCENT
183   /PROPORTION
184   /NTILES(4)
185   /NORMAL
186 .
187
188 LIST.
189 ])
190 AT_CHECK([pspp -o pspp.csv rank.sps])
191 AT_CHECK([cat pspp.csv], [0], [dnl
192 Variables Created By RANK
193
194
195
196 a into Sa(SAVAGE of a)
197
198 a into Pa(PERCENT of a)
199
200 a into PRO001(PROPORTION of a using BLOM)
201
202 a into Na(NTILES of a)
203
204 a into NOR001(NORMAL of a using BLOM)
205
206 Table: Data List
207 a,b,Sa,Pa,PRO001,Na,NOR001
208 .00,24.00,-.9000,10.00,.0610,1,-1.547
209 1.00,32.00,-.7889,20.00,.1585,1,-1.000
210 2.00,31.00,-.5925,30.00,.2561,2,-.6554
211 2.00,32.00,-.5925,30.00,.2561,2,-.6554
212 4.00,30.00,-.3544,40.00,.3537,2,-.3755
213 5.00,29.00,-.1544,50.00,.4512,2,-.1226
214 6.00,1.00,.0956,60.00,.5488,3,.1226
215 7.00,43.00,.4290,70.00,.6463,3,.3755
216 8.00,8.00,.9290,80.00,.7439,3,.6554
217 9.00,45.00,1.9290,90.00,.8415,4,1.0005
218 ])
219 AT_CLEANUP
220
221 AT_SETUP([RANK with SPLIT FILE])
222 AT_DATA([rank.sps], [dnl
223 DATA LIST LIST NOTABLE /a * g1 g2 *.
224 BEGIN DATA.
225 2 1 2
226 2 1 2
227 3 1 2
228 4 1 2
229 5 1 2
230 1 0 2
231 2 0 2
232 3 0 2
233 4 0 2
234 5 0 2
235 6 0 2
236 7 0 2
237 8 0 2
238 6 1 2
239 7 1 2
240 7 1 2
241 8 1 2
242 9 1 1
243 END DATA.
244
245 RANK a (D) BY g2 g1
246   /PRINT=YES
247   /TIES=LOW
248   /MISSING=INCLUDE
249   /FRACTION=RANKIT
250   /RANK
251   /NORMAL
252   .
253
254 SPLIT FILE BY g1.
255
256 RANK a (D) BY g2
257   /PRINT=YES
258   /TIES=LOW
259   /MISSING=INCLUDE
260   /FRACTION=RANKIT
261   /RANK
262   /NORMAL
263   .
264
265 SPLIT FILE OFF.
266
267 LIST.
268 ])
269 AT_CHECK([pspp -o pspp.csv rank.sps])
270 AT_CHECK([cat pspp.csv], [0], [dnl
271 Variables Created By RANK
272
273
274
275 a into Ra(RANK of a BY g2 g1)
276
277 a into Na(NORMAL of a using RANKIT BY g2 g1)
278
279 Variables Created By RANK
280
281
282
283 a into RAN001(RANK of a BY g2)
284
285 a into NOR001(NORMAL of a using RANKIT BY g2)
286
287 Table: Data List
288 a,g1,g2,Ra,Na,RAN001,NOR001
289 2.00,1.00,2.00,8.000,.9674,4.000,.5244
290 2.00,1.00,2.00,8.000,.9674,4.000,.5244
291 3.00,1.00,2.00,7.000,.5895,3.000,.0000
292 4.00,1.00,2.00,6.000,.2822,2.000,-.5244
293 5.00,1.00,2.00,5.000,.0000,1.000,-1.282
294 1.00,.00,2.00,8.000,1.5341,8.000,1.5341
295 2.00,.00,2.00,7.000,.8871,7.000,.8871
296 3.00,.00,2.00,6.000,.4888,6.000,.4888
297 4.00,.00,2.00,5.000,.1573,5.000,.1573
298 5.00,.00,2.00,4.000,-.1573,4.000,-.1573
299 6.00,.00,2.00,3.000,-.4888,3.000,-.4888
300 7.00,.00,2.00,2.000,-.8871,2.000,-.8871
301 8.00,.00,2.00,1.000,-1.534,1.000,-1.534
302 6.00,1.00,2.00,4.000,-.2822,4.000,1.1503
303 7.00,1.00,2.00,2.000,-.9674,2.000,-.3186
304 7.00,1.00,2.00,2.000,-.9674,2.000,-.3186
305 8.00,1.00,2.00,1.000,-1.593,1.000,-1.150
306 9.00,1.00,1.00,1.000,.0000,1.000,.0000
307 ])
308 AT_CLEANUP
309
310 # Also tests small ranks for special case of SAVAGE ranks.
311 AT_SETUP([RANK with fractional ranks]) 
312 AT_DATA([rank.sps], [dnl
313 DATA LIST LIST NOTABLE  /a *  w * .
314 BEGIN DATA.
315 1 1.5
316 2 0.2
317 3 0.1
318 4 1
319 5 1
320 6 1
321 7 1
322 8 1
323 END DATA.
324
325 WEIGHT BY w.
326
327 RANK a 
328   /FRACTION=TUKEY
329   /PROPORTION
330   /SAVAGE
331 .
332
333 LIST.
334 ])
335 AT_CHECK([pspp -o pspp.csv rank.sps])
336 AT_CHECK([cat pspp.csv], [0], [dnl
337 Variables Created By RANK
338
339
340
341 a into Pa(PROPORTION of a using TUKEY)
342
343 a into Sa(SAVAGE of a)
344
345 Table: Data List
346 a,w,Pa,Sa
347 1.00,1.50,.1285,-.8016
348 2.00,.20,.1776,-.6905
349 3.00,.10,.1986,-.6905
350 4.00,1.00,.3458,-.5305
351 5.00,1.00,.4860,-.2905
352 6.00,1.00,.6262,.0262
353 7.00,1.00,.7664,.4929
354 8.00,1.00,.9065,1.3929
355 ])
356 AT_CLEANUP
357
358 AT_SETUP([RANK all-ties due to tiny weights])
359 AT_DATA([rank.sps], [dnl
360 DATA LIST LIST NOTABLE /x * w *.
361 BEGIN DATA.
362 1 0.1
363 2 0.1
364 3 0.1
365 4 0.2
366 5 0.1
367 6 0.1
368 7 0.1
369 8 0.1
370 END DATA.
371
372 WEIGHT BY w.
373
374 RANK x
375  /TIES=low
376  /RANK into xl.
377
378
379 RANK x
380  /TIES=high
381  /RANK into xh.
382
383 RANK x
384  /TIES=condense
385  /RANK into xc.
386
387
388 * Test VW fraction
389
390 RANK x
391  /FRACTION=VW
392  /NORMAL.
393
394 LIST.
395 ])
396 AT_CHECK([pspp -o pspp.csv rank.sps])
397 AT_CHECK([cat pspp.csv], [0], [dnl
398 Variables Created By RANK
399
400
401
402 x into xl(RANK of x)
403
404 Variables Created By RANK
405
406
407
408 x into xh(RANK of x)
409
410 Variables Created By RANK
411
412
413
414 x into xc(RANK of x)
415
416 Variables Created By RANK
417
418
419
420 x into Nx(NORMAL of x using VW)
421
422 Table: Data List
423 x,w,xl,xh,xc,Nx
424 1.00,.10,.000,.100,1.000,-1.938
425 2.00,.10,.100,.200,2.000,-1.412
426 3.00,.10,.200,.300,3.000,-1.119
427 4.00,.20,.300,.500,4.000,-.8046
428 5.00,.10,.500,.600,5.000,-.5549
429 6.00,.10,.600,.700,6.000,-.4067
430 7.00,.10,.700,.800,7.000,-.2670
431 8.00,.10,.800,.900,8.000,-.1323
432 ])
433 AT_CLEANUP
434
435 AT_SETUP([RANK and TEMPORARY])
436 AT_DATA([rank.sps], [dnl
437 DATA LIST LIST NOTABLE /age (f2) gender (a1).
438 BEGIN DATA.
439 44 m
440 32 f
441 43 m
442 49 m
443 33 f
444 35 f
445 29 f
446 50 m
447 42 m
448 33 f
449 48 m
450 END DATA.
451
452 TEMPORARY.
453 SELECT IF gender = 'm'.
454 RANK age /RANK INTO Rm.
455
456 TEMPORARY.
457 SELECT IF gender = 'f'.
458 RANK age /RANK INTO Rf.
459
460 LIST.
461 ])
462 AT_CHECK([pspp -O format=csv rank.sps], [0], [dnl
463 Variables Created By RANK
464
465
466
467 age into Rm(RANK of age)
468
469 Variables Created By RANK
470
471
472
473 age into Rf(RANK of age)
474
475 Table: Data List
476 age,gender,Rm,Rf
477 44,m,3.000,.   @&t@
478 32,f,.   ,2.000
479 43,m,2.000,.   @&t@
480 49,m,5.000,.   @&t@
481 33,f,.   ,3.500
482 35,f,.   ,5.000
483 29,f,.   ,1.000
484 50,m,6.000,.   @&t@
485 42,m,1.000,.   @&t@
486 33,f,.   ,3.500
487 48,m,4.000,.   @&t@
488 ])
489 AT_CLEANUP
490
491 AT_SETUP([RANK variable name fallback])
492 AT_DATA([rank.sps], [dnl
493 DATA LIST LIST NOTABLE /foo * rfoo * ran003 *.
494 BEGIN DATA.
495 0 3 2
496 1 3 2
497 2 3 2
498 2 3 2
499 4 3 2
500 5 3 2
501 6 3 2
502 7 3 2
503 8 3 2
504 9 3 2
505 END DATA.
506
507 RANK foo.
508
509
510 DISPLAY DICTIONARY.
511 ])
512 AT_CHECK([pspp -o pspp.csv rank.sps])
513 AT_CHECK([cat pspp.csv], [0], [dnl
514 Variables Created By RANK
515
516
517
518 foo into RAN001(RANK of foo)
519
520 Table: Variables
521 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
522 foo,1,,Scale,Input,8,Right,F8.2,F8.2,
523 rfoo,2,,Scale,Input,8,Right,F8.2,F8.2,
524 ran003,3,,Scale,Input,8,Right,F8.2,F8.2,
525 RAN001,4,RANK of foo,Scale,Input,8,Right,F9.3,F9.3,
526 ])
527 AT_CLEANUP
528
529 AT_SETUP([RANK robust variable name creation])
530 AT_DATA([rank.sps], [dnl
531 DATA LIST LIST notable /x * rx * ran001 TO ran999.
532 BEGIN DATA.
533 1
534 2
535 3
536 4
537 5
538 6
539 7
540 END DATA.
541
542 RANK x.
543
544 DELETE VAR ran001 TO ran999.
545
546 LIST.
547 ])
548 AT_CHECK([pspp -O format=csv rank.sps], [0], [dnl
549 "rank.sps:3: warning: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate."
550
551 "rank.sps:4: warning: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate."
552
553 "rank.sps:5: warning: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate."
554
555 "rank.sps:6: warning: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate."
556
557 "rank.sps:7: warning: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate."
558
559 "rank.sps:8: warning: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate."
560
561 "rank.sps:9: warning: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate."
562
563 Variables Created By RANK
564
565
566
567 x into RNKRA01(RANK of x)
568
569 Table: Data List
570 x,rx,RNKRA01
571 1.00,.  ,1.000
572 2.00,.  ,2.000
573 3.00,.  ,3.000
574 4.00,.  ,4.000
575 5.00,.  ,5.000
576 6.00,.  ,6.000
577 7.00,.  ,7.000
578 ])
579 AT_CLEANUP
580
581 dnl Test for proper behaviour in the face of invalid input.
582 AT_SETUP([RANK handling of invalid input])
583 AT_DATA([rank.sps], [dnl
584 DATA LIST LIST NOTABLE /x * a (a2).
585 BEGIN DATA.
586 -1 s
587 0  s
588 1  s
589 2  s
590 2  s
591 4  s
592 5  s
593 END DATA.
594
595 DEBUG XFORM FAIL.
596
597 RANK x.
598 ])
599 AT_CHECK([pspp -O format=csv --testing-mode rank.sps], [1], [dnl
600 Variables Created By RANK
601
602
603
604 x into Rx(RANK of x)
605
606 rank.sps:14: error: RANK: DEBUG XFORM FAIL transformation executed
607 ])
608 AT_CLEANUP
609
610 AT_SETUP([RANK handling of invalid syntax])
611 AT_DATA([rank.sps], [dnl
612 DATA LIST LIST NOTABLE /x * a (a2).
613 BEGIN DATA.
614 -1 s
615 0  s
616 1  s
617 2  s
618 2  s
619 4  s
620 5  s
621 END DATA.
622
623 * invalid NTILES (no parameter)
624 RANK x 
625   /NTILES
626 .
627
628 * invalid NTILES (not an integer)
629 RANK x 
630   /NTILES(d)
631 .
632
633
634 * destination variable already exists
635 RANK x 
636  /RANK INTO x.
637
638
639 * Too many variables in INTO
640 RANK x 
641  /RANK INTO foo  bar wiz.
642 ])
643 AT_CHECK([pspp -O format=csv rank.sps], [1], [dnl
644 rank.sps:15.1: error: RANK: Syntax error at end of command: expecting `@{:@'.
645
646 rank.sps:19.11: error: RANK: Syntax error at `d': expecting integer.
647
648 rank.sps:25: error: RANK: Variable x already exists.
649
650 rank.sps:30: error: RANK: Too many variables in INTO clause.
651 ])
652 AT_CLEANUP