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