Merge remote-tracking branch 'origin/master' into sheet
[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 Variable,Description,Position
137 a,Format: F8.2,1
138 b,Format: F8.2,2
139 Ra,"Label: RANK of a
140 Format: F9.3",3
141 RFR001,"Label: RFRACTION of a
142 Format: F6.4",4
143 count,"Label: N of a
144 Format: F6.0",5
145 Rb,"Label: RANK of b
146 Format: F9.3",6
147 RFR002,"Label: RFRACTION of b
148 Format: F6.4",7
149 Nb,"Label: N of b
150 Format: F6.0",8
151
152 Table: Data List
153 a,b,Ra,RFR001,count,Rb,RFR002,Nb
154 .00,24.00,10.000,1.0000,10,8.000,.8889,9
155 1.00,32.00,9.000,.9000,10,4.000,.4444,9
156 2.00,31.00,8.000,.8000,10,5.000,.5556,9
157 2.00,32.00,8.000,.8000,10,4.000,.4444,9
158 4.00,30.00,6.000,.6000,10,6.000,.6667,9
159 5.00,29.00,5.000,.5000,10,7.000,.7778,9
160 6.00,1.00,4.000,.4000,10,9.000,1.0000,9
161 7.00,43.00,3.000,.3000,10,2.000,.2222,9
162 8.00,.  ,2.000,.2000,10,.   ,.    ,.
163 9.00,45.00,1.000,.1000,10,1.000,.1111,9
164 ])
165 AT_CLEANUP
166
167 AT_SETUP([RANK with SAVAGE, PERCENT, PROPORTION, NTILES])
168 AT_DATA([rank.sps], [dnl
169 DATA LIST LIST NOTABLE /a * b *.
170 BEGIN DATA.
171 0 24
172 1 32
173 2 31
174 2 32
175 4 30
176 5 29
177 6 1
178 7 43
179 8 8 
180 9 45
181 END DATA.
182
183 RANK a
184   /PRINT=YES
185   /TIES=CONDENSE
186   /SAVAGE
187   /PERCENT
188   /PROPORTION
189   /NTILES(4)
190   /NORMAL
191 .
192
193 LIST.
194 ])
195 AT_CHECK([pspp -o pspp.csv rank.sps])
196 AT_CHECK([cat pspp.csv], [0], [dnl
197 Variables Created By RANK
198
199
200
201 a into Sa(SAVAGE of a)
202
203 a into Pa(PERCENT of a)
204
205 a into PRO001(PROPORTION of a using BLOM)
206
207 a into Na(NTILES of a)
208
209 a into NOR001(NORMAL of a using BLOM)
210
211 Table: Data List
212 a,b,Sa,Pa,PRO001,Na,NOR001
213 .00,24.00,-.9000,10.00,.0610,1,-1.547
214 1.00,32.00,-.7889,20.00,.1585,1,-1.000
215 2.00,31.00,-.5925,30.00,.2561,2,-.6554
216 2.00,32.00,-.5925,30.00,.2561,2,-.6554
217 4.00,30.00,-.3544,40.00,.3537,2,-.3755
218 5.00,29.00,-.1544,50.00,.4512,2,-.1226
219 6.00,1.00,.0956,60.00,.5488,3,.1226
220 7.00,43.00,.4290,70.00,.6463,3,.3755
221 8.00,8.00,.9290,80.00,.7439,3,.6554
222 9.00,45.00,1.9290,90.00,.8415,4,1.0005
223 ])
224 AT_CLEANUP
225
226 AT_SETUP([RANK with SPLIT FILE])
227 AT_DATA([rank.sps], [dnl
228 DATA LIST LIST NOTABLE /a * g1 g2 *.
229 BEGIN DATA.
230 2 1 2
231 2 1 2
232 3 1 2
233 4 1 2
234 5 1 2
235 1 0 2
236 2 0 2
237 3 0 2
238 4 0 2
239 5 0 2
240 6 0 2
241 7 0 2
242 8 0 2
243 6 1 2
244 7 1 2
245 7 1 2
246 8 1 2
247 9 1 1
248 END DATA.
249
250 RANK a (D) BY g2 g1
251   /PRINT=YES
252   /TIES=LOW
253   /MISSING=INCLUDE
254   /FRACTION=RANKIT
255   /RANK
256   /NORMAL
257   .
258
259 SPLIT FILE BY g1.
260
261 RANK a (D) BY g2
262   /PRINT=YES
263   /TIES=LOW
264   /MISSING=INCLUDE
265   /FRACTION=RANKIT
266   /RANK
267   /NORMAL
268   .
269
270 SPLIT FILE OFF.
271
272 LIST.
273 ])
274 AT_CHECK([pspp -o pspp.csv rank.sps])
275 AT_CHECK([cat pspp.csv], [0], [dnl
276 Variables Created By RANK
277
278
279
280 a into Ra(RANK of a BY g2 g1)
281
282 a into Na(NORMAL of a using RANKIT BY g2 g1)
283
284 Variables Created By RANK
285
286
287
288 a into RAN001(RANK of a BY g2)
289
290 a into NOR001(NORMAL of a using RANKIT BY g2)
291
292 Table: Data List
293 a,g1,g2,Ra,Na,RAN001,NOR001
294 2.00,1.00,2.00,8.000,.9674,4.000,.5244
295 2.00,1.00,2.00,8.000,.9674,4.000,.5244
296 3.00,1.00,2.00,7.000,.5895,3.000,.0000
297 4.00,1.00,2.00,6.000,.2822,2.000,-.5244
298 5.00,1.00,2.00,5.000,.0000,1.000,-1.282
299 1.00,.00,2.00,8.000,1.5341,8.000,1.5341
300 2.00,.00,2.00,7.000,.8871,7.000,.8871
301 3.00,.00,2.00,6.000,.4888,6.000,.4888
302 4.00,.00,2.00,5.000,.1573,5.000,.1573
303 5.00,.00,2.00,4.000,-.1573,4.000,-.1573
304 6.00,.00,2.00,3.000,-.4888,3.000,-.4888
305 7.00,.00,2.00,2.000,-.8871,2.000,-.8871
306 8.00,.00,2.00,1.000,-1.534,1.000,-1.534
307 6.00,1.00,2.00,4.000,-.2822,4.000,1.1503
308 7.00,1.00,2.00,2.000,-.9674,2.000,-.3186
309 7.00,1.00,2.00,2.000,-.9674,2.000,-.3186
310 8.00,1.00,2.00,1.000,-1.593,1.000,-1.150
311 9.00,1.00,1.00,1.000,.0000,1.000,.0000
312 ])
313 AT_CLEANUP
314
315 # Also tests small ranks for special case of SAVAGE ranks.
316 AT_SETUP([RANK with fractional ranks]) 
317 AT_DATA([rank.sps], [dnl
318 DATA LIST LIST NOTABLE  /a *  w * .
319 BEGIN DATA.
320 1 1.5
321 2 0.2
322 3 0.1
323 4 1
324 5 1
325 6 1
326 7 1
327 8 1
328 END DATA.
329
330 WEIGHT BY w.
331
332 RANK a 
333   /FRACTION=TUKEY
334   /PROPORTION
335   /SAVAGE
336 .
337
338 LIST.
339 ])
340 AT_CHECK([pspp -o pspp.csv rank.sps])
341 AT_CHECK([cat pspp.csv], [0], [dnl
342 Variables Created By RANK
343
344
345
346 a into Pa(PROPORTION of a using TUKEY)
347
348 a into Sa(SAVAGE of a)
349
350 Table: Data List
351 a,w,Pa,Sa
352 1.00,1.50,.1285,-.8016
353 2.00,.20,.1776,-.6905
354 3.00,.10,.1986,-.6905
355 4.00,1.00,.3458,-.5305
356 5.00,1.00,.4860,-.2905
357 6.00,1.00,.6262,.0262
358 7.00,1.00,.7664,.4929
359 8.00,1.00,.9065,1.3929
360 ])
361 AT_CLEANUP
362
363 AT_SETUP([RANK all-ties due to tiny weights])
364 AT_DATA([rank.sps], [dnl
365 DATA LIST LIST NOTABLE /x * w *.
366 BEGIN DATA.
367 1 0.1
368 2 0.1
369 3 0.1
370 4 0.2
371 5 0.1
372 6 0.1
373 7 0.1
374 8 0.1
375 END DATA.
376
377 WEIGHT BY w.
378
379 RANK x
380  /TIES=low
381  /RANK into xl.
382
383
384 RANK x
385  /TIES=high
386  /RANK into xh.
387
388 RANK x
389  /TIES=condense
390  /RANK into xc.
391
392
393 * Test VW fraction
394
395 RANK x
396  /FRACTION=VW
397  /NORMAL.
398
399 LIST.
400 ])
401 AT_CHECK([pspp -o pspp.csv rank.sps])
402 AT_CHECK([cat pspp.csv], [0], [dnl
403 Variables Created By RANK
404
405
406
407 x into xl(RANK of x)
408
409 Variables Created By RANK
410
411
412
413 x into xh(RANK of x)
414
415 Variables Created By RANK
416
417
418
419 x into xc(RANK of x)
420
421 Variables Created By RANK
422
423
424
425 x into Nx(NORMAL of x using VW)
426
427 Table: Data List
428 x,w,xl,xh,xc,Nx
429 1.00,.10,.000,.100,1.000,-1.938
430 2.00,.10,.100,.200,2.000,-1.412
431 3.00,.10,.200,.300,3.000,-1.119
432 4.00,.20,.300,.500,4.000,-.8046
433 5.00,.10,.500,.600,5.000,-.5549
434 6.00,.10,.600,.700,6.000,-.4067
435 7.00,.10,.700,.800,7.000,-.2670
436 8.00,.10,.800,.900,8.000,-.1323
437 ])
438 AT_CLEANUP
439
440 AT_SETUP([RANK and TEMPORARY])
441 AT_DATA([rank.sps], [dnl
442 DATA LIST LIST NOTABLE /age (f2) gender (a1).
443 BEGIN DATA.
444 44 m
445 32 f
446 43 m
447 49 m
448 33 f
449 35 f
450 29 f
451 50 m
452 42 m
453 33 f
454 48 m
455 END DATA.
456
457 TEMPORARY.
458 SELECT IF gender = 'm'.
459 RANK age /RANK INTO Rm.
460
461 TEMPORARY.
462 SELECT IF gender = 'f'.
463 RANK age /RANK INTO Rf.
464
465 LIST.
466 ])
467 AT_CHECK([pspp -O format=csv rank.sps], [0], [dnl
468 Variables Created By RANK
469
470
471
472 age into Rm(RANK of age)
473
474 Variables Created By RANK
475
476
477
478 age into Rf(RANK of age)
479
480 Table: Data List
481 age,gender,Rm,Rf
482 44,m,3.000,.   @&t@
483 32,f,.   ,2.000
484 43,m,2.000,.   @&t@
485 49,m,5.000,.   @&t@
486 33,f,.   ,3.500
487 35,f,.   ,5.000
488 29,f,.   ,1.000
489 50,m,6.000,.   @&t@
490 42,m,1.000,.   @&t@
491 33,f,.   ,3.500
492 48,m,4.000,.   @&t@
493 ])
494 AT_CLEANUP
495
496 AT_SETUP([RANK variable name fallback])
497 AT_DATA([rank.sps], [dnl
498 DATA LIST LIST NOTABLE /foo * rfoo * ran003 *.
499 BEGIN DATA.
500 0 3 2
501 1 3 2
502 2 3 2
503 2 3 2
504 4 3 2
505 5 3 2
506 6 3 2
507 7 3 2
508 8 3 2
509 9 3 2
510 END DATA.
511
512 RANK foo.
513
514
515 DISPLAY DICTIONARY.
516 ])
517 AT_CHECK([pspp -o pspp.csv rank.sps])
518 AT_CHECK([cat pspp.csv], [0], [dnl
519 Variables Created By RANK
520
521
522
523 foo into RAN001(RANK of foo)
524
525 Variable,Description,Position
526 foo,Format: F8.2,1
527 rfoo,Format: F8.2,2
528 ran003,Format: F8.2,3
529 RAN001,"Label: RANK of foo
530 Format: F9.3",4
531 ])
532 AT_CLEANUP
533
534 AT_SETUP([RANK robust variable name creation])
535 AT_DATA([rank.sps], [dnl
536 DATA LIST LIST notable /x * rx * ran001 TO ran999.
537 BEGIN DATA.
538 1
539 2
540 3
541 4
542 5
543 6
544 7
545 END DATA.
546
547 RANK x.
548
549 DELETE VAR ran001 TO ran999.
550
551 LIST.
552 ])
553 AT_CHECK([pspp -O format=csv rank.sps], [0], [dnl
554 "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."
555
556 "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."
557
558 "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."
559
560 "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."
561
562 "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."
563
564 "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."
565
566 "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."
567
568 Variables Created By RANK
569
570
571
572 x into RNKRA01(RANK of x)
573
574 Table: Data List
575 x,rx,RNKRA01
576 1.00,.  ,1.000
577 2.00,.  ,2.000
578 3.00,.  ,3.000
579 4.00,.  ,4.000
580 5.00,.  ,5.000
581 6.00,.  ,6.000
582 7.00,.  ,7.000
583 ])
584 AT_CLEANUP
585
586 dnl Test for proper behaviour in the face of invalid input.
587 AT_SETUP([RANK handling of invalid input])
588 AT_DATA([rank.sps], [dnl
589 DATA LIST LIST NOTABLE /x * a (a2).
590 BEGIN DATA.
591 -1 s
592 0  s
593 1  s
594 2  s
595 2  s
596 4  s
597 5  s
598 END DATA.
599
600 DEBUG XFORM FAIL.
601
602 RANK x.
603 ])
604 AT_CHECK([pspp -O format=csv --testing-mode rank.sps], [1], [dnl
605 Variables Created By RANK
606
607
608
609 x into Rx(RANK of x)
610
611 rank.sps:14: error: RANK: DEBUG XFORM FAIL transformation executed
612 ])
613 AT_CLEANUP
614
615 AT_SETUP([RANK handling of invalid syntax])
616 AT_DATA([rank.sps], [dnl
617 DATA LIST LIST NOTABLE /x * a (a2).
618 BEGIN DATA.
619 -1 s
620 0  s
621 1  s
622 2  s
623 2  s
624 4  s
625 5  s
626 END DATA.
627
628 * invalid NTILES (no parameter)
629 RANK x 
630   /NTILES
631 .
632
633 * invalid NTILES (not an integer)
634 RANK x 
635   /NTILES(d)
636 .
637
638
639 * destination variable already exists
640 RANK x 
641  /RANK INTO x.
642
643
644 * Too many variables in INTO
645 RANK x 
646  /RANK INTO foo  bar wiz.
647 ])
648 AT_CHECK([pspp -O format=csv rank.sps], [1], [dnl
649 rank.sps:15.1: error: RANK: Syntax error at end of command: expecting `@{:@'.
650
651 rank.sps:19.11: error: RANK: Syntax error at `d': expecting integer.
652
653 rank.sps:25: error: RANK: Variable x already exists.
654
655 rank.sps:30: error: RANK: Too many variables in INTO clause.
656 ])
657 AT_CLEANUP