Legend without a range of color - r

The following script gives me a graph with a legend in a range of colors between Vc=30 and Vc=40. All I need is to have a legend with only two colors, one for Vc=30 and other for Vc=40. How can I do that?
dados
Vc Tool Lu Fres Edge
1 30 1 10 466 1
2 30 1 10 416 2
3 30 1 10 465 1
4 30 1 10 416 2
5 30 1 10 464 1
6 30 1 10 416 2
7 30 1 10 476 1
8 30 1 10 412 2
9 30 1 10 468 1
10 30 1 10 410 2
11 30 1 10 470 1
12 30 1 10 407 2
13 30 1 10 468 1
14 30 1 10 412 2
15 30 1 10 469 1
16 30 1 10 414 2
17 30 1 10 469 1
18 30 1 10 412 2
19 30 1 10 467 1
20 30 1 10 409 2
21 30 1 10 469 1
22 30 1 10 415 2
23 30 1 10 471 1
24 30 1 10 420 2
25 30 1 10 469 1
26 30 1 10 416 2
27 30 1 10 464 1
28 30 1 10 409 2
29 30 1 10 465 1
30 30 1 10 412 2
31 30 1 10 464 1
32 30 1 10 409 2
33 30 1 10 466 1
34 30 1 10 417 2
35 30 1 10 466 1
36 30 1 10 417 2
37 30 1 10 464 1
38 30 1 10 414 2
39 30 1 10 466 1
40 30 1 10 415 2
41 40 1 38 457 1
42 40 1 38 416 2
43 40 1 38 460 1
44 40 1 38 438 2
45 40 1 38 465 1
46 40 1 38 441 2
47 40 1 38 467 1
48 40 1 38 442 2
49 40 1 38 473 1
50 40 1 38 452 2
51 40 1 38 469 1
52 40 1 38 446 2
53 40 1 38 478 1
54 40 1 38 450 2
55 40 1 38 476 1
56 40 1 38 454 2
57 40 1 38 479 1
58 40 1 38 452 2
59 40 1 38 480 1
60 40 1 38 450 2
61 40 1 38 481 1
62 40 1 38 443 2
63 40 1 38 476 1
64 40 1 38 447 2
65 40 1 38 472 1
66 40 1 38 450 2
67 40 1 38 479 1
68 40 1 38 449 2
69 40 1 38 478 1
70 40 1 38 455 2
71 40 1 38 478 1
72 40 1 38 457 2
73 40 1 38 481 1
74 40 1 38 447 2
75 40 1 38 504 1
76 40 1 38 452 2
77 40 1 38 472 1
78 40 1 38 447 2
79 40 1 38 472 1
80 40 1 38 451 2
81 40 1 66 622 1
82 40 1 66 377 2
83 40 1 66 619 1
84 40 1 66 378 2
85 40 1 66 622 1
86 40 1 66 369 2
87 40 1 66 616 1
88 40 1 66 374 2
89 40 1 66 619 1
90 40 1 66 374 2
91 40 1 66 616 1
92 40 1 66 374 2
93 40 1 66 621 1
94 40 1 66 375 2
95 40 1 66 618 1
96 40 1 66 397 2
97 40 1 66 633 1
98 40 1 66 406 2
99 40 1 66 652 1
100 40 1 66 412 2
101 40 1 66 652 1
102 40 1 66 419 2
103 40 1 66 658 1
104 40 1 66 423 2
105 40 1 66 659 1
106 40 1 66 409 2
107 40 1 66 650 1
108 40 1 66 405 2
109 40 1 66 653 1
110 40 1 66 405 2
111 40 1 66 652 1
112 40 1 66 403 2
113 40 1 66 656 1
114 40 1 66 408 2
115 40 1 66 644 1
116 40 1 66 406 2
117 40 1 66 649 1
118 40 1 66 412 2
119 40 1 66 650 1
120 40 1 66 406 2
121 30 1 94 585 1
122 30 1 94 234 2
123 30 1 94 589 1
124 30 1 94 231 2
125 30 1 94 585 1
126 30 1 94 223 2
127 30 1 94 586 1
128 30 1 94 223 2
129 30 1 94 572 1
130 30 1 94 233 2
131 30 1 94 585 1
132 30 1 94 233 2
133 30 1 94 589 1
134 30 1 94 234 2
135 30 1 94 598 1
136 30 1 94 237 2
137 30 1 94 605 1
138 30 1 94 237 2
139 30 1 94 586 1
140 30 1 94 233 2
141 30 1 94 588 1
142 30 1 94 227 2
143 30 1 94 585 1
144 30 1 94 230 2
145 30 1 94 586 1
146 30 1 94 230 2
147 30 1 94 591 1
148 30 1 94 237 2
149 30 1 94 586 1
150 30 1 94 234 2
151 30 1 94 592 1
152 30 1 94 237 2
153 30 1 94 595 1
154 30 1 94 236 2
155 30 1 94 600 1
156 30 1 94 227 2
157 30 1 94 592 1
158 30 1 94 237 2
159 30 1 94 592 1
160 30 1 94 240 2
161 40 1 122 853 1
162 40 1 122 330 2
163 40 1 122 859 1
164 40 1 122 323 2
165 40 1 122 842 1
166 40 1 122 308 2
167 40 1 122 842 1
168 40 1 122 324 2
169 40 1 122 831 1
170 40 1 122 334 2
171 40 1 122 838 1
172 40 1 122 341 2
173 40 1 122 836 1
174 40 1 122 328 2
175 40 1 122 840 1
176 40 1 122 324 2
177 40 1 122 836 1
178 40 1 122 321 2
179 40 1 122 831 1
180 40 1 122 328 2
181 40 1 122 833 1
182 40 1 122 328 2
183 40 1 122 840 1
184 40 1 122 330 2
185 40 1 122 831 1
186 40 1 122 321 2
187 40 1 122 833 1
188 40 1 122 328 2
189 40 1 122 833 1
190 40 1 122 321 2
191 40 1 122 840 1
192 40 1 122 319 2
193 40 1 122 838 1
194 40 1 122 317 2
195 40 1 122 831 1
196 40 1 122 319 2
197 40 1 122 827 1
198 40 1 122 323 2
199 40 1 122 836 1
200 40 1 122 328 2
201 30 2 10 468 1
202 30 2 10 408 2
203 30 2 10 471 1
204 30 2 10 405 2
205 30 2 10 475 1
206 30 2 10 403 2
207 30 2 10 470 1
208 30 2 10 409 2
209 30 2 10 478 1
210 30 2 10 405 2
211 30 2 10 474 1
212 30 2 10 403 2
213 30 2 10 472 1
214 30 2 10 402 2
215 30 2 10 478 1
216 30 2 10 408 2
217 30 2 10 477 1
218 30 2 10 406 2
219 30 2 10 473 1
220 30 2 10 406 2
221 30 2 10 474 1
222 30 2 10 406 2
223 30 2 10 477 1
224 30 2 10 411 2
225 30 2 10 480 1
226 30 2 10 413 2
227 30 2 10 479 1
228 30 2 10 408 2
229 30 2 10 476 1
230 30 2 10 406 2
231 30 2 10 476 1
232 30 2 10 404 2
233 30 2 10 472 1
234 30 2 10 407 2
235 30 2 10 474 1
236 30 2 10 411 2
237 30 2 10 473 1
238 30 2 10 415 2
239 30 2 10 479 1
240 30 2 10 409 2
241 40 2 38 442 1
242 40 2 38 407 2
243 40 2 38 437 1
244 40 2 38 410 2
245 40 2 38 444 1
246 40 2 38 412 2
247 40 2 38 440 1
248 40 2 38 414 2
249 40 2 38 439 1
250 40 2 38 413 2
251 40 2 38 436 1
252 40 2 38 416 2
253 40 2 38 446 1
254 40 2 38 412 2
255 40 2 38 438 1
256 40 2 38 414 2
257 40 2 38 443 1
258 40 2 38 408 2
259 40 2 38 446 1
260 40 2 38 407 2
261 40 2 38 445 1
262 40 2 38 413 2
263 40 2 38 453 1
264 40 2 38 414 2
265 40 2 38 449 1
266 40 2 38 417 2
267 40 2 38 447 1
268 40 2 38 411 2
269 40 2 38 443 1
270 40 2 38 417 2
271 40 2 38 447 1
272 40 2 38 410 2
273 40 2 38 449 1
274 40 2 38 409 2
275 40 2 38 442 1
276 40 2 38 413 2
277 40 2 38 451 1
278 40 2 38 412 2
279 40 2 38 447 1
280 40 2 38 420 2
281 40 2 66 526 1
282 40 2 66 467 2
283 40 2 66 532 1
284 40 2 66 470 2
285 40 2 66 528 1
286 40 2 66 474 2
287 40 2 66 529 1
288 40 2 66 472 2
289 40 2 66 533 1
290 40 2 66 480 2
291 40 2 66 542 1
292 40 2 66 487 2
293 40 2 66 545 1
294 40 2 66 504 2
295 40 2 66 549 1
296 40 2 66 507 2
297 40 2 66 546 1
298 40 2 66 517 2
299 40 2 66 541 1
300 40 2 66 518 2
301 40 2 66 554 1
302 40 2 66 514 2
303 40 2 66 564 1
304 40 2 66 514 2
305 40 2 66 571 1
306 40 2 66 522 2
307 40 2 66 575 1
308 40 2 66 525 2
309 40 2 66 582 1
310 40 2 66 533 2
311 40 2 66 588 1
312 40 2 66 536 2
313 40 2 66 591 1
314 40 2 66 553 2
315 40 2 66 592 1
316 40 2 66 557 2
317 40 2 66 592 1
318 40 2 66 563 2
319 40 2 66 583 1
320 40 2 66 568 2
321 30 2 94 578 1
322 30 2 94 370 2
323 30 2 94 570 1
324 30 2 94 378 2
325 30 2 94 575 1
326 30 2 94 367 2
327 30 2 94 579 1
328 30 2 94 371 2
329 30 2 94 576 1
330 30 2 94 362 2
331 30 2 94 579 1
332 30 2 94 372 2
333 30 2 94 588 1
334 30 2 94 375 2
335 30 2 94 586 1
336 30 2 94 372 2
337 30 2 94 589 1
338 30 2 94 378 2
339 30 2 94 587 1
340 30 2 94 375 2
341 30 2 94 578 1
342 30 2 94 368 2
343 30 2 94 575 1
344 30 2 94 375 2
345 30 2 94 574 1
346 30 2 94 376 2
347 30 2 94 575 1
348 30 2 94 367 2
349 30 2 94 580 1
350 30 2 94 382 2
351 30 2 94 583 1
352 30 2 94 368 2
353 30 2 94 591 1
354 30 2 94 386 2
355 30 2 94 595 1
356 30 2 94 379 2
357 30 2 94 593 1
358 30 2 94 384 2
359 30 2 94 607 1
360 30 2 94 399 2
361 30 2 122 760 1
362 30 2 122 625 2
363 30 2 122 746 1
364 30 2 122 612 2
365 30 2 122 762 1
366 30 2 122 625 2
367 30 2 122 783 1
368 30 2 122 637 2
369 30 2 122 778 1
370 30 2 122 640 2
371 30 2 122 778 1
372 30 2 122 638 2
373 30 2 122 791 1
374 30 2 122 638 2
375 30 2 122 782 1
376 30 2 122 635 2
377 30 2 122 792 1
378 30 2 122 640 2
379 30 2 122 783 1
380 30 2 122 637 2
381 30 2 122 774 1
382 30 2 122 622 2
383 30 2 122 777 1
384 30 2 122 618 2
385 30 2 122 777 1
386 30 2 122 622 2
387 30 2 122 765 1
388 30 2 122 623 2
389 30 2 122 769 1
390 30 2 122 625 2
391 30 2 122 775 1
392 30 2 122 622 2
393 30 2 122 777 1
394 30 2 122 628 2
395 30 2 122 769 1
396 30 2 122 620 2
397 30 2 122 778 1
398 30 2 122 623 2
399 30 2 122 788 1
400 30 2 122 634 2
> dadosc <- summarySE(dados, measurevar="Fres", groupvars=c("Vc","Lu"))
> dadosc
Vc Lu N Fres sd se ci
1 30 10 80 440.6875 30.91540 3.456447 6.879885
2 30 94 80 445.0250 150.97028 16.878990 33.596789
3 30 122 40 701.7000 75.06688 11.869115 24.007552
4 40 38 80 444.6125 23.31973 2.607225 5.189552
5 40 66 80 526.7125 90.77824 10.149316 20.201707
6 40 122 40 581.1250 259.74092 41.068645 83.069175
ggplot(dadosc, aes(x=Lu, y=Fres,ymax=max(Fres), colour=Vc)) +
geom_errorbar(aes(ymin=Fres-se, ymax=Fres+se), colour="black", width=2, position=pd) +
geom_point(position=pd, size=3, shape=21, fill="white") +
xlab("Machining lenght (mm)") +
ylab("Machining forces (N)") +
ggtitle("The Effect of Cutting Velocity on Machining Forces") +
expand_limits(y=400) + # Expand y range
scale_y_continuous(breaks=0:20*50) + # Set tick every 4
theme_bw() +
theme(legend.justification=c(1,0),
legend.position=c(1,0))
pd <- position_dodge(0.1)

Related

How to test for p-value with groups/filters in dplyr

My data looks like the example below. (sorry if it's too long, not sure what's acceptable/needed).
I have used the following code to calculate the median and IQR of each time difference (tdif) between tests (testno):
data %>% group_by(testno) %>% filter(type ==1) %>%
summarise(Median = median(tdif), IQR= IQR(tdif), n= n(), .groups = 'keep') -> result
I have done this for each category of 'type' (coded as 1 - 10), which brought me to the added table (bottom).
My question is, if it is possible to:
Do this an easier way (without the filters? So I can do this all in 1 run), and
Is it possible run a test for p-value with all the groups/filters?
data <- read.table(header=T, text= '
PID time tdif testno type
3 205 0 1 1
4 77 0 1 1
4 85 8 2 1
4 126 41 3 1
4 165 39 4 1
4 202 37 5 1
4 238 36 6 1
4 272 34 7 1
4 277 5 8 1
4 370 93 9 1
4 397 27 10 1
4 452 55 11 1
4 522 70 12 1
4 529 7 13 1
4 608 79 14 1
4 651 43 15 1
4 655 4 16 1
4 713 58 17 1
4 804 91 18 1
4 900 96 19 1
4 944 44 20 1
4 979 35 21 1
4 1015 36 22 1
4 1051 36 23 1
4 1077 26 24 1
4 1124 47 25 1
4 1162 38 26 1
4 1222 60 27 1
4 1334 112 28 1
4 1383 49 29 1
4 1457 74 30 1
4 1506 49 31 1
4 1590 84 32 1
4 1768 178 33 1
4 1838 70 34 1
4 1880 42 35 1
4 1915 35 36 1
4 1973 58 37 1
4 2017 44 38 1
4 2090 73 39 1
4 2314 224 40 1
4 2381 67 41 1
4 2433 52 42 1
4 2484 51 43 1
4 2694 210 44 1
4 2731 37 45 1
4 2792 61 46 1
4 2958 166 47 1
5 48 0 1 3
5 111 63 2 3
5 699 588 3 3
5 1077 378 4 3
6 -43 0 1 3
8 67 0 1 1
8 168 101 2 1
8 314 146 3 1
8 368 54 4 1
8 586 218 5 1
10 639 0 1 6
13 -454 0 1 3
13 -384 70 2 3
13 -185 199 3 3
13 193 378 4 3
13 375 182 5 3
13 564 189 6 3
13 652 88 7 3
13 669 17 8 3
13 718 49 9 3
14 704 0 1 8
15 -165 0 1 3
15 -138 27 2 3
15 1335 1473 3 3
16 168 0 1 6
18 -1329 0 1 3
18 -1177 152 2 3
18 -1071 106 3 3
18 -945 126 4 3
18 -834 111 5 3
18 -719 115 6 3
18 -631 88 7 3
18 -497 134 8 3
18 -376 121 9 3
18 -193 183 10 3
18 -78 115 11 3
18 -13 65 12 3
18 100 113 13 3
18 196 96 14 3
18 552 356 15 3
18 650 98 16 3
18 737 87 17 3
18 804 67 18 3
18 902 98 19 3
18 983 81 20 3
18 1119 136 21 3
19 802 0 1 1
19 1593 791 2 1
26 314 0 1 8
26 389 75 2 8
26 597 208 3 8
33 639 0 1 6
Added table (values differ from example data, because this isn't the complete set).

GAMs in R: Fewer unique covariate combinations than df

I tried fitting gams to some dataframes I have. All minus one work. It fails with the error:
Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) : A term has fewer unique covariate combinations than specified maximum degrees of freedom
I looked a bit on the internet but couldn't really figure out what's really going wrong. All my 7 over dataframes run without a problem.
I then ran epiR::epi.cp(srtm[-c(1,7,8)]) and it gave me this output:
$cov.pattern
id n curv_plan curv_prof dem slope ca
1 1 1 1.113192e-02 3.991046e-03 3909 43.601479 5.225853
2 2 1 -2.686749e-03 3.474989e-03 3312 35.022511 4.418310
3 3 1 -1.033450e-02 -4.626922e-03 3326 36.678623 4.421465
4 4 1 -5.439283e-03 2.066148e-03 4069 31.501045 3.887526
5 5 1 -2.602015e-03 -1.249511e-04 3021 37.199219 5.010560
6 6 1 1.068216e-03 1.216902e-03 2844 44.694374 4.852220
7 7 1 -1.855443e-02 -5.965539e-03 2841 42.753750 5.088554
8 8 1 2.363193e-03 2.353357e-03 2833 33.160995 4.652209
9 9 1 2.169674e-02 1.049735e-02 2964 32.311535 4.671970
10 10 1 2.850910e-02 9.416230e-03 2956 50.791847 3.496096
11 11 1 -1.932028e-02 4.949751e-04 2794 38.714302 4.217102
12 12 1 -1.372750e-03 -4.437230e-03 3799 48.356312 4.597039
13 13 1 1.154181e-04 -4.114155e-03 3808 54.669777 3.518823
14 14 1 2.743768e-02 7.829833e-03 3580 23.674162 3.268744
15 15 1 7.216539e-03 9.818082e-04 3969 29.421440 4.354250
16 16 1 2.385139e-03 6.333927e-04 3635 10.555381 4.905733
17 17 1 -1.129411e-02 2.719948e-03 2805 29.195084 4.807369
18 18 1 4.584329e-04 -1.497223e-03 3676 32.754879 3.729304
19 19 1 1.883965e-03 4.189690e-03 3165 30.973505 4.833158
20 20 1 -5.350136e-03 -2.615470e-03 2745 32.534698 4.420852
21 21 1 1.484253e-02 -1.245213e-03 3872 26.113234 4.045357
22 22 1 -2.449377e-02 -5.045668e-04 2931 31.060991 5.170872
23 23 1 -2.962795e-02 -9.271557e-03 2917 21.680889 4.547461
24 24 1 -2.487545e-02 -7.834328e-03 2736 41.775677 4.543325
25 25 1 2.890568e-03 -2.040353e-03 2577 47.003765 3.739546
26 26 1 -5.119631e-03 8.869720e-03 3401 38.519680 5.428564
27 27 1 6.171266e-03 -6.515175e-04 2687 36.678623 4.152842
28 28 1 -8.297552e-03 -7.053435e-03 3678 39.532673 4.081311
29 29 1 8.652663e-03 2.394378e-03 3515 33.895370 4.220177
30 30 1 -2.528805e-03 -1.293259e-03 3404 42.548138 4.266330
31 31 1 1.899994e-02 6.367806e-03 3191 41.696201 3.300749
32 32 1 -2.243623e-02 -1.866033e-04 2433 34.162479 5.364681
33 33 1 -6.934012e-03 9.280805e-03 2309 32.667160 5.650699
34 34 1 -1.121149e-02 6.376335e-05 2188 31.119059 4.706416
35 35 1 -1.429000e-02 5.299596e-04 2511 34.543365 4.538456
36 36 1 -7.168889e-03 1.301791e-03 2625 30.826660 4.059711
37 37 1 -4.226461e-03 7.440552e-03 2830 33.398251 4.941027
38 38 1 -2.635832e-03 8.748529e-03 3378 45.972672 4.861779
39 39 1 -2.007920e-02 -8.081778e-03 3281 31.735376 5.173269
40 40 1 -3.453595e-02 -6.867430e-03 2690 47.515182 4.935358
41 41 1 1.698363e-03 -8.296107e-03 2529 42.224693 4.386349
42 42 1 5.257193e-03 1.021242e-02 2571 43.070564 4.194372
43 43 1 6.968817e-03 5.538784e-03 2581 36.055031 4.209373
44 44 1 -7.632907e-04 2.803704e-04 2582 28.257311 4.230427
45 45 1 -3.468894e-03 -9.099842e-04 2409 29.421440 4.190946
46 46 1 1.879089e-02 6.532978e-03 3733 41.535984 4.032614
47 47 1 -1.076225e-03 -1.138945e-03 2712 39.260731 4.580621
48 48 1 -5.306205e-03 2.667941e-03 3446 34.250553 4.925404
49 49 1 -5.380515e-03 -2.595619e-03 3785 50.561493 4.642792
50 50 1 -2.571232e-03 -2.063937e-03 3768 46.160892 4.728879
51 51 1 -7.638110e-03 -2.432463e-03 3413 32.401161 5.058373
52 52 1 -2.950254e-03 -2.034031e-04 3852 32.543564 4.443869
53 53 1 -2.702386e-03 -1.776183e-03 2483 31.002720 3.879390
54 54 1 -3.892425e-02 -2.266178e-03 2225 26.126318 5.750985
55 55 1 -2.644659e-03 3.034660e-03 2192 32.103516 4.949506
56 56 1 -2.862503e-02 3.673996e-04 2361 23.930893 5.181818
57 57 1 6.263880e-03 -7.725377e-04 3780 17.752790 4.890797
58 58 1 1.054093e-03 -1.563014e-03 3089 36.422310 4.520845
59 59 1 9.474340e-04 -3.901043e-03 3155 42.552841 4.265886
60 60 1 5.569567e-03 -1.770366e-04 3516 13.166321 4.772187
61 61 1 -8.342760e-03 -9.908290e-03 3097 36.815479 5.346615
62 62 1 -1.422498e-03 -1.645628e-03 2865 29.802414 4.131463
63 63 1 4.523963e-02 1.067406e-02 2163 36.154739 3.369432
64 64 1 -1.164162e-02 6.808200e-04 2316 19.610609 4.634536
65 65 1 -8.043590e-03 9.395104e-03 2614 44.298817 3.983136
66 66 1 -1.925332e-02 -4.521391e-03 2035 31.205780 4.134195
67 67 1 -1.429050e-02 5.435983e-03 2799 38.876656 4.180761
68 68 1 6.935605e-04 3.015038e-03 2679 37.863647 4.213497
69 69 1 -5.062089e-03 5.961242e-04 2831 32.401161 3.729215
70 70 1 -3.617065e-04 -2.874465e-03 3152 45.871994 4.703659
71 71 1 -4.216370e-02 -4.917050e-03 3726 25.376934 4.614913
72 72 1 -2.184333e-02 -2.840071e-03 3610 43.138550 4.237120
73 73 1 -1.735273e-02 -2.199261e-03 3339 33.984894 4.811754
74 74 1 1.929157e-02 5.358084e-03 3447 32.356407 3.355368
75 75 1 -4.118797e-02 -2.408211e-03 3251 22.373844 5.160147
76 76 1 -1.393304e-02 7.900328e-05 3297 22.090260 4.724728
77 77 1 -3.078095e-02 -5.535597e-03 3143 37.298687 4.625203
78 78 1 1.717030e-02 -1.120720e-03 3617 37.965389 4.627342
79 79 1 -5.965119e-04 -5.377157e-04 3689 28.360373 4.767213
80 80 1 7.843294e-03 -9.579902e-04 3676 48.356312 3.907819
81 81 1 5.994634e-03 2.034169e-03 2759 25.142431 3.980591
82 82 1 -1.323012e-02 2.393529e-03 3972 26.880308 5.107575
83 83 1 6.312347e-03 2.877600e-04 3323 32.167103 3.496723
84 84 1 -1.180464e-02 4.438243e-03 3790 40.369972 4.081389
85 85 1 -8.333334e-03 4.009274e-03 3248 14.931417 4.881107
86 86 1 2.016023e-03 -5.707344e-04 3994 18.305449 4.278613
87 87 1 -5.515654e-03 -8.373593e-04 3368 40.703190 4.229169
88 88 1 8.931696e-03 1.677515e-03 4651 30.133842 4.327270
89 89 1 1.962347e-04 -7.458636e-04 5075 57.352509 3.263017
90 90 1 -2.880805e-02 -5.200595e-04 2645 11.976726 5.634262
91 91 1 -2.101875e-02 -5.110677e-03 3109 34.218582 4.925558
92 92 1 -8.390786e-03 -1.188547e-02 3667 39.895481 4.249029
93 93 1 -1.366958e-02 9.873455e-04 2827 22.636129 5.269634
94 94 1 1.004551e-02 5.205147e-04 3667 44.028976 3.993555
95 95 1 5.892557e-03 -5.482296e-04 2416 5.385977 4.614692
96 96 1 -1.662132e-02 -9.946494e-04 3806 42.599808 3.951163
97 97 1 -7.977792e-03 5.937776e-03 3470 28.888371 3.120762
98 98 1 -2.408042e-02 -2.647421e-03 2975 16.228737 4.227977
99 99 1 -1.191509e-02 -2.014583e-03 2461 30.051607 4.361413
100 100 1 1.110316e-02 2.506189e-04 3362 29.517509 4.591039
101 101 1 2.010373e-03 4.185408e-04 5104 17.387333 3.642855
102 102 1 -3.218945e-03 1.004196e-02 4113 44.448421 3.282414
103 103 1 2.438254e-03 2.551999e-03 3234 31.205780 3.844411
104 104 1 -1.178511e-02 2.775465e-04 1864 1.350224 3.875072
105 105 1 -9.511201e-04 -1.446065e-03 2351 22.406872 4.392300
106 106 1 -4.563018e-03 -5.890041e-03 3141 24.862123 3.998985
107 107 1 -1.471223e-02 5.965497e-03 3765 25.363234 3.661456
108 108 1 -5.857890e-03 -9.363544e-03 2272 22.878105 5.105480
109 109 1 1.369277e-02 1.019289e-02 4016 44.848000 4.092690
110 110 1 -8.784844e-03 3.358194e-03 3293 32.543564 4.115062
111 111 1 -5.148044e-03 5.372697e-03 3038 31.772562 3.626687
112 112 1 -1.556184e+35 5.799786e+34 4961 29.421440 3.020591
113 113 1 3.831991e-03 1.570888e-03 2069 28.821898 3.790284
114 114 1 8.289138e-04 6.439757e-04 2154 21.045721 3.959267
115 115 1 -4.800863e-03 3.194520e-03 5294 45.660866 3.701611
116 116 1 2.974254e-02 1.197812e-02 4380 31.670097 3.877057
117 117 1 1.137725e-02 -1.082659e-02 5172 18.774675 3.572600
118 118 1 -4.678526e-03 7.448288e-03 2257 39.260731 4.227000
119 119 1 -4.655881e-03 -1.119303e-03 3233 30.205467 5.613868
120 120 1 -4.827522e-03 -4.766134e-03 3414 42.974857 3.831894
121 121 1 -8.568994e-04 1.053632e-03 1750 29.421440 4.132886
122 122 1 1.212121e-02 0.000000e+00 5018 20.136303 3.669850
123 123 1 -4.711660e-03 -2.261143e-03 3013 45.007954 3.622240
124 124 1 -1.226328e-02 4.688181e-04 3842 26.880308 3.098333
125 125 1 3.438910e-03 1.441129e-03 3470 11.386165 4.552782
126 126 1 1.192164e-02 -1.295839e-03 3473 22.684824 4.748498
127 127 1 -1.960781e-40 0.000000e+00 4155 90.000000 2.960569
128 128 1 2.124726e-04 1.945100e-03 2496 32.103516 5.242211
129 129 1 5.669804e-03 -4.589476e-03 2577 35.398876 4.271112
130 130 1 -8.838220e-03 -9.496282e-04 4921 14.506372 4.088247
131 131 1 1.009090e-02 -2.243944e-03 3385 38.372120 4.067030
132 132 1 5.630660e-03 -8.632211e-04 4003 33.322365 3.776054
133 133 1 -9.103803e-03 -6.322661e-03 2758 47.934212 3.739807
134 134 1 6.225513e-03 -1.824928e-03 3925 37.085732 3.389725
135 135 1 -1.303080e-03 3.580316e-03 2978 27.432941 4.345174
136 136 1 1.355920e-02 3.468190e-03 5058 57.797195 3.739124
137 137 1 2.092464e-02 -3.244962e-04 2400 3.931096 3.032193
138 138 1 5.691811e-02 -7.933985e-04 3885 15.069956 3.414036
139 139 1 8.052407e-05 -3.197287e-03 3493 33.993008 3.881695
140 140 1 -1.892967e-02 -5.049255e-03 2985 24.904482 4.417928
141 141 1 2.278842e-02 1.188287e-02 3666 31.670097 3.313449
142 142 1 1.496110e-02 2.181270e-03 3702 30.498932 3.171413
[ reached 'max' / getOption("max.print") -- omitted 18 rows ]
$id
[1] 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] 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] 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] 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] 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
I tried to lower the number of knots in the gam-call but didn't suceed as well...
Anyone might have an idea?
I fit the gam using the following line:
mgcv::gam(slide ~ s(curv_plan) + s(curv_prof) + s(dem) + s(slope) + s(ca), data = dataframes_new[[7]], family = binomial)
I have experienced the same issue. The root cause was that some of my categorical variables had fewer levels than k in my formula specification. To give an example:
Suppose one of the terms in my formula specification was:
s(I(pmin(example_variable, 120)), k = 5)
and the data in my example_variable had 3 levels (say, "yes", "no", "maybe"). This would throw the above-mentioned error.
In my case, I solved it by creating additional levels in my data (I was creating test data for a unit test). In other cases it could be solved by ensuring k does not exceed the number of levels in your categorical variables.
If you're using categorical variables, check if the root cause might be the same for you.
I found the solution to my problem by reading these:
https://stat.ethz.ch/pipermail/r-sig-ecology/2011-May/002148.html
https://stat.ethz.ch/pipermail/r-help/2007-October/143569.html
The error means that you tried to create a thin plate spline basis expansion with more basis functions than the variable from which the expansion is to be made has unique values.
As you don't show the model fitting code, we can't say more than that one of the smooths in the model you tried to fit didn't have enough unique values for the value of k you specific or used (if you didn't set k a default value was used).

Reindexing a column in R

I'm dealing with the following dataset
animal protein herd sire dam
6 416 189.29 2 15 236
7 417 183.27 2 6 295
9 419 193.24 3 11 268
10 420 198.84 2 12 295
11 421 205.25 3 3 251
12 422 204.15 2 2 281
13 423 200.20 2 3 248
14 424 197.22 2 11 222
15 425 201.14 1 10 262
17 427 196.20 1 11 290
18 428 208.13 3 9 294
19 429 213.01 3 14 254
21 431 203.38 2 4 273
22 432 190.56 2 8 248
25 435 196.59 3 9 226
26 436 193.31 3 10 249
27 437 207.89 3 7 272
29 439 202.98 2 10 260
30 440 177.28 2 4 291
31 441 182.04 1 6 282
32 442 217.50 2 3 265
33 443 190.43 2 11 248
35 445 197.24 2 4 256
37 447 197.16 3 5 240
42 452 183.07 3 5 293
43 453 197.99 2 6 293
44 454 208.27 2 6 254
45 455 187.61 3 12 271
46 456 173.18 2 6 280
47 457 187.89 2 6 235
48 458 191.96 1 7 286
49 459 196.39 1 4 275
50 460 178.51 2 13 262
52 462 204.17 1 6 253
53 463 203.77 2 11 273
54 464 206.25 1 13 249
55 465 211.63 2 13 222
56 466 211.34 1 6 228
57 467 194.34 2 1 217
58 468 201.53 2 12 247
59 469 198.01 2 3 251
60 470 188.94 2 7 290
61 471 190.49 3 2 220
62 472 197.34 2 3 224
63 473 194.04 1 15 229
64 474 202.74 2 1 287
67 477 189.98 1 6 300
69 479 206.37 3 2 293
70 480 183.81 2 10 274
72 482 190.70 2 12 265
74 484 194.25 3 2 262
75 485 191.15 3 10 297
76 486 193.23 3 15 255
77 487 193.29 2 4 266
78 488 182.20 1 15 260
81 491 195.89 2 12 294
82 492 200.77 1 8 278
83 493 179.12 2 7 281
85 495 172.14 3 13 252
86 496 183.82 1 4 264
88 498 195.32 1 6 249
89 499 197.19 1 13 274
90 500 178.07 1 8 293
92 502 209.65 2 7 241
95 505 199.66 3 5 220
96 506 190.96 2 11 259
98 508 206.58 3 3 230
100 510 196.60 2 5 231
103 513 193.25 2 15 280
104 514 181.34 2 3 227
I'm interested with the animals indexes and corresponding to them the dams' indexes. Using table function I was able to check that some dams are matched to different animals. In fact I got the following output
217 220 222 224 226 227 228 229 230 231 235 236 240 241 247 248 249 251 252 253 254 255 256 259 260 262
1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 3 3 2 1 1 2 1 1 1 2 3
264 265 266 268 271 272 273 274 275 278 280 281 282 286 287 290 291 293 294 295 297 300
1 2 1 1 1 1 2 2 1 1 2 2 1 1 1 2 1 4 2 2 1 1
Using length function I checked that there are only 48 dams in this dataset.
I would like to 'reindex' them with the integers 1, ..., 48 instead of these given in my set. Is there any method of doing such things?
You can use match and unique.
df$index <- match(df$dam, unique(df$dam))
Or convert to factor and then integer
df$index <- as.integer(factor(df$dam))
Another option is group_indices from dplyr.
df$index <- dplyr::group_indices(df, dam)
We can use .GRP in data.table
library(data.table)
setDT(df)[, index := .GRP, dam]

Issue with R Shiny App --> Interactive Survival Plots

I am new to R Shiny and I am trying to build a Shiny Web App that produces a survival plot with two reactive inputs. The first input is the study (total=4). The second input is the groups (total=19) to compare survival curves. Ideally, these two inputs would allow me to see in a particular study, how group X survival curve compares to the survival curve of all other groups.
Here is a sample of my data:
ObsNum UniqueID Time Censored Group Group2 Study
1 523B95015 27 1 1 523 1
2 523B95014 27 1 1 523 1
3 523B85051 27 1 1 523 1
4 523B95009 27 1 1 523 1
5 523B85048 27 1 1 523 1
6 523B85050 27 1 1 523 1
7 675B89002 27 1 8 675 1
8 556B95006 27 1 12 556 1
9 556B85030 27 1 12 556 1
10 556B85044 27 1 12 556 1
11 556B95035 27 1 12 556 1
12 556B95000 27 1 12 556 1
13 556B95004 27 1 12 556 1
14 556B95002 27 1 12 556 1
15 756Y81172 27 1 17 756 1
16 741B95022 27 1 99 741 1
17 741B95020 27 1 99 741 1
18 619B92008 28 1 7 619 1
19 552B89003 28 1 10 552 1
20 101B94097 28 1 99 101 1
21 101B94098 28 1 99 101 1
22 618C84582 29 1 23 618 1
23 618C84580 29 1 23 618 1
24 618C84581 29 1 23 618 1
25 730B90003 29 1 99 730 1
26 646B42015 34 1 4 646 1
27 671B60009 35 1 17 671 1
28 612C80247 35 1 21 612 1
29 700C64500 35 1 99 791 1
30 101B89052 40 1 99 101 1
31 101B85047 40 1 99 101 1
32 101B95068 40 1 99 101 1
33 538B70011 51 1 10 538 1
34 689C85036 57 1 1 689 1
35 689C95450 57 1 1 689 1
36 556B85050 62 1 12 556 1
37 636B80005 62 1 23 636 1
38 636B92002 62 1 23 636 1
39 630B30005 70 1 2 630 1
40 642B80021 78 1 4 642 1
41 101B79173 86 1 99 101 1
42 523B81007 106 0 1 523 1
43 620B88003 106 0 2 620 1
44 642B40002 106 1 4 642 1
45 642B40001 106 1 4 642 1
46 581B81002 106 0 5 581 1
47 581B81001 106 0 5 581 1
48 573B95000 106 0 8 573 1
49 589B80015 106 0 15 589 1
50 589B80016 106 0 15 589 1
51 657B50013 106 0 15 657 1
52 657B43004 106 0 15 657 1
53 459B85085 106 0 21 459 1
54 459Y81171 106 0 21 459 1
55 101B75006 106 0 99 101 1
56 101SC8023 106 0 99 101 1
57 101B85122 106 0 99 101 1
58 101B55116 106 0 99 101 1
59 101B79086 106 0 99 101 1
60 101B95066 106 0 99 101 1
61 730B97005 106 0 99 730 1
62 741B85045 106 0 99 741 1
63 777B96001 106 0 99 777 1
64 556B85077 1 1 12 556 2
65 636B92003 1 1 23 636 2
66 101B94137 1 1 99 101 2
67 700C64500 5 1 99 791 2
68 463Y91171 6 1 20 463 2
69 618C84319 6 1 23 618 2
70 776C93046 6 1 99 776 2
71 556B95042 7 1 12 556 2
72 556B95043 7 1 12 556 2
73 556B97000 7 1 12 556 2
74 549B80069 7 1 17 549 2
75 573B95000 22 1 8 573 2
76 580B90024 22 1 16 580 2
77 523B81007 28 1 1 523 2
78 520B60012 32 1 16 520 2
79 520B70011 32 1 16 520 2
80 586B70008 33 1 16 586 2
81 586B80006 33 1 16 586 2
82 586B80011 33 1 16 586 2
83 586B80015 33 1 16 586 2
84 657B43004 34 1 15 657 2
85 636B99009 35 1 23 636 2
86 691B68018 36 1 22 691 2
87 657B50013 41 1 15 657 2
88 741B95031 42 1 99 741 2
89 620B88003 46 0 2 620 2
90 620B90008 46 0 2 620 2
91 581B81001 46 0 5 581 2
92 581B81002 46 0 5 581 2
93 552B99002 46 0 10 552 2
94 459B85085 46 0 21 459 2
95 459B95055 46 0 21 459 2
96 101B75006 46 0 99 101 2
97 101B55060 46 0 99 101 2
98 101B79086 46 0 99 101 2
99 101B79058 46 0 99 101 2
100 101B85122 46 0 99 101 2
101 101B89115 46 0 99 101 2
102 101B85047 46 0 99 101 2
103 101B94123 46 0 99 101 2
104 101B95091 46 0 99 101 2
105 101B95038 46 0 99 101 2
106 101D98001 46 0 99 101 2
107 730B97005 46 0 99 730 2
108 741B85045 46 0 99 741 2
Here is my code for the Shiny App:
library(shiny)
library(ggplot2)
library(survival)
library(survminer)
library(dplyr)
attach(tdata)
studychoices=unique(tdata$Study)
groupchoices=unique(tdata$Group)
# Define UI
ui <- fluidPage(
titlePanel("Survival Data"),
selectInput(inputId = "studyselector",label="Select a Study:", choices=studychoices),
selectInput(inputId = "groupselector",label="Select a Group:", choices=groupchoices),
plotOutput("p1")
)
# Define server logic
server <- function(input, output) {
filter=reactive({
filteredData=tdata[tdata$Study==input$studyselector,]
return(filteredData)
})
output$p1=renderPlot({
fit=survfit(Surv(Time,Censored)~input$groupselector,data=filter())
ggsurvplot(fit,data=filter(),pval=TRUE,xlim=c(0,max(Time)+1),
title=paste("Study","INSERT HERE STUDY #", "Survival Plot for Group","INSERT HERE GROUP #"),
xlab="Time (Days)",
ggtheme=theme(plot.title=element_text(hjust=0.5)))
})
}
# Run the application
shinyApp(ui = ui, server = server)
I have the following two questions:
1.) When I run the App, I get an error in an external window that reads, "Error: variable lengths differ (found for'input$groupselector'). There are no NAs in this data and I specified the data to be used is the filter() dataset based on the Study selection so I'm not sure why this error is popping up.
2.) How would I be able to dynamically change the Study # and the Group # in the title? I understand how to do that with a normal R function, but I'm a little lost with the Shiny set up.
Any help would be appreciated! Thank you!
Edited solution
This should now work - have edited the Group column to be a binary in/out when passed to your reactive dataframe, which should colour the lines appropriately:
library(tidyverse)
library(survival)
library(survminer)
library(shiny)
ui <- fluidPage(
titlePanel("Survival Data"),
selectInput(inputId = "studyselector",label="Select a Study:", choices=studychoices),
selectInput(inputId = "groupselector",label="Select a Group:", choices=groupchoices),
plotOutput("p1")
)
# Define server logic
server <- function(input, output) {
filter=reactive({
filteredData=data[data$Study==input$studyselector,]
filteredData['Group'] = ifelse(filteredData$Group==input$groupselector,
input$groupselector,
"Others")
return(filteredData)
})
output$p1=renderPlot({
fit=survfit(Surv(Time,Censored)~Group,data=filter()) # `Group` as variable to stratify by?
ggsurvplot(fit,data=filter(),pval=TRUE,xlim=c(0,max(filter()$Time)+1),
title=paste("Study",
input$studyselector, # paste these bits straight in
"Survival Plot for Group",
input$groupselector), # here too
xlab="Time (Days)",
ggtheme=theme(plot.title=element_text(hjust=0.5)))
})
}
# Run the application
shinyApp(ui = ui, server = server)
Do let me know in comments if there are any mistakes or further ideas/questions!

How to annotate boxplots using svyboxplot library in R

I am trying to figure out how to label the boxplots that appear after I use the svyboxplot library for R.
I have tried the following:
svyboxplot(~ALCANYNO~factor(REGION), design=ihisDesign3, xlab='Region', ylab='Frequency', ylim=c(0,10), colnames=c("Northeast", "Midwest", "South", "West"));
SOLUTION: Add the following to factor:
labels = c('Northeast', 'Midwest', 'South', 'West')
This changes the example above to the following:
svyboxplot(~ALCANYNO~factor(REGION,
labels=c('Northeast', 'Midwest', 'South', 'West')),
design=ihisDesign3, xlab='Region', ylab='Frequency',
ylim =c (0, 10))
I am Creating a dataset to explain:
options(width = 120)
library (survey)
library (KernSmooth)
xd1<-
"xsmoke age_p psu stratum wt8
13601 3 22 2 20 356.5600
32966 3 38 2 45 434.3562
63493 1 32 1 87 699.9987
238175 3 46 1 338 982.8075
174162 3 40 1 240 273.6313
220206 3 33 2 308 1477.1688
118133 3 68 1 159 716.3012
142859 2 23 1 194 1100.9475
115253 2 35 2 155 444.3750
61675 3 31 1 85 769.5963
189813 3 37 1 263 328.5600
226274 1 47 2 318 605.8700
41969 3 71 2 58 597.0150
167667 3 40 2 230 1030.4637
225103 3 37 2 316 349.6825
49894 3 70 2 68 517.7862
98075 3 46 2 130 1428.7225
180771 3 50 1 250 652.4188
137057 3 42 1 186 590.2100
77705 2 23 1 105 1687.2450
89106 3 48 1 118 407.6513
208178 3 50 1 290 556.5000
100403 3 52 2 133 1481.8200
221571 1 27 2 310 833.5338
10823 2 72 1 16 1807.6425
108431 3 71 2 145 945.6263
68708 1 46 1 94 1989.3775
23874 3 23 2 33 1707.8775
150634 3 19 2 206 761.1500
231232 3 42 2 326 1487.4113
184654 2 42 2 255 1715.2375
215312 3 57 1 300 483.5663
40713 2 57 2 56 2042.2762
130309 3 23 1 177 948.5625
25515 2 55 1 35 2719.7525
235612 2 83 2 333 603.3537
13755 2 36 2 20 265.1938
2441 3 33 1 4 1062.1200
157327 3 77 1 215 2010.6600
66502 3 20 2 91 1122.9725
230778 1 55 2 325 1207.3025
74805 3 54 1 101 1028.5150
166556 1 50 1 229 1546.9450
91914 1 68 1 121 428.5350
89651 3 59 2 118 143.5437
149329 3 44 2 204 1064.7725
212700 2 59 2 295 1050.1163
454 1 79 1 1 275.5700
125639 1 27 1 170 785.1037
55442 3 47 1 76 950.3312
145132 3 77 1 197 1269.2287
123069 3 24 1 167 216.1937
188301 1 55 2 260 426.6313
852 2 66 2 1 1443.4887
3582 3 81 1 6 790.8412
235423 1 44 2 333 659.4238
42175 2 40 1 59 1089.6762
57033 3 43 1 78 226.8750
177273 2 85 1 244 392.7200
218558 3 40 2 305 1680.2700
27784 2 45 1 39 280.0550
81823 3 43 1 110 965.0438
76344 3 26 1 103 1095.6012
114916 3 56 2 154 436.8838
35563 3 78 1 49 333.2875
192279 3 30 2 267 722.0312
61315 1 48 2 84 1426.5725
219903 3 43 1 308 791.5738
42612 3 25 1 60 658.1387
178488 3 33 2 246 675.1912
9031 1 27 2 14 989.4863
145092 2 64 1 197 960.1912
71885 3 53 2 97 595.4050
38137 2 75 1 53 1004.0912
140149 1 21 1 190 1870.9350
162052 3 25 1 223 892.7775
89527 2 39 2 118 518.1050
59650 3 26 2 82 432.7837
24709 2 84 1 34 453.9013
18933 3 85 1 27 582.3288
24904 3 35 2 34 1027.5287
213668 3 39 1 298 3174.1925
110509 3 30 1 149 469.8188
72462 3 63 1 98 386.2163
152596 3 19 1 209 1328.2188
17014 4 62 1 24 294.9250
33467 2 50 1 46 1601.4575
5241 3 33 1 9 1651.0988
215094 3 23 1 300 427.6313
88885 1 21 1 118 1092.2613
204868 2 60 2 285 781.2325
157415 2 31 2 215 1323.5750
71081 2 44 2 96 1059.2088
25420 3 38 1 35 530.7413
144226 1 27 1 196 1126.3112
47888 3 46 2 66 965.4050
216179 3 29 2 301 1237.6463
29172 3 68 1 41 1025.9738
168786 1 47 1 232 680.6213
94035 2 23 2 124 330.4563
170542 1 25 2 234 757.2287
160331 2 33 2 220 636.3900
124163 3 80 2 167 287.6988
71442 2 37 1 97 442.2300
80191 2 74 2 107 871.0338
199309 3 29 2 277 485.2337
91293 3 35 2 120 138.3187
219524 2 68 1 307 609.5862
119336 3 85 2 160 149.7612
31814 3 68 1 44 396.6913
54920 1 28 2 75 532.7175
161034 3 29 2 221 791.0100
177037 1 50 1 244 626.2400
119963 1 54 1 162 374.1062
107972 2 58 1 145 944.8863
22932 3 60 1 32 310.6413
54197 3 23 2 74 931.2737
209598 3 23 1 292 1078.2950
213604 1 74 2 297 588.5000
146480 3 27 1 200 212.0588
162463 3 55 2 223 1202.0925
215534 3 33 2 300 430.3938
100703 1 53 1 134 463.6200
162588 3 27 1 224 612.0250
222676 1 35 1 312 292.7000
220052 3 84 1 308 1301.4738
131382 3 36 1 178 825.9512
102117 3 28 1 137 451.4075
70362 3 52 2 95 185.2562
188757 3 22 2 261 704.3913
215878 2 37 1 301 789.9837
45820 3 18 2 64 2019.4137
84860 3 47 1 113 149.0200
110581 3 37 1 149 526.0775
207650 3 51 2 289 688.0538
40723 3 59 2 56 497.6050
169663 3 19 2 233 845.0362
191955 1 36 1 267 735.7350
213816 3 18 2 298 2275.3513
120967 3 48 2 163 1055.3238
209430 2 42 2 291 1771.0225
21235 3 21 1 30 1204.5663
131326 3 29 1 178 331.9588
19667 1 57 1 28 638.9138
74743 2 48 1 101 1208.8763
178672 3 66 2 246 338.2013
100174 3 24 2 133 1733.6275
69046 3 24 2 94 542.4863
79960 1 41 2 107 567.6363
108591 2 42 1 146 978.3775
235635 3 24 1 334 1382.9437
187426 2 54 2 259 478.2362
28728 3 39 2 40 1165.6175
205348 3 32 2 286 1082.9913
218812 3 30 1 306 308.1037
168389 3 48 2 231 593.2475
145479 1 21 1 198 864.2663
105170 2 40 1 141 1016.7862
155753 2 78 2 212 1109.0025
169399 3 28 1 233 1467.1363
55664 1 63 1 76 904.3763
74024 2 51 1 100 547.5538
85558 1 25 1 114 893.8825
142684 3 54 2 193 1203.3212
198792 1 22 1 277 1800.3325
82603 3 70 2 110 827.3763
171036 2 50 2 235 2003.9725
1616 1 42 2 2 590.5662
57042 3 45 1 78 1021.7287
45100 2 38 2 63 1807.9288
134828 2 28 1 183 715.1187
91167 3 26 2 120 480.1950
170605 3 40 2 234 507.2763
175869 3 77 1 242 386.2987
81594 2 82 2 109 580.0838
37426 1 20 2 52 1159.1613
113799 3 85 1 153 459.5450
24721 3 18 2 34 2912.7575
26297 3 45 2 36 1304.4925
57074 1 51 1 78 602.2112
185000 3 34 1 256 583.5738
94196 3 44 2 124 2344.1087
80656 3 45 2 108 1340.9713
14849 1 46 1 22 967.2525
145730 2 73 1 198 418.8037
56633 3 34 2 77 1011.5488
273 2 54 1 1 786.2138
60567 1 40 2 83 315.2925
47788 1 38 2 66 1105.9188
76943 2 53 2 103 537.7062
165014 3 34 1 227 824.3125
188444 3 22 1 261 623.2225
29043 1 35 1 41 724.9025
165578 3 25 1 228 596.0275
50702 3 43 2 69 985.9662
197621 3 39 2 275 1310.1163
26267 3 41 2 36 1030.3900
29565 1 60 2 41 920.8550
20060 3 36 2 28 157.2188
119780 2 20 1 162 863.8100"
tor <- read.table(textConnection(xd1), header=TRUE, as.is=TRUE)
# Grouping variable "xsmoke" must be a factor
tor$xsmoke <- factor(tor$xsmoke,levels=c (1,2,3),
labels=c('Current SMK','Former SMK', 'Never Smk'), ordered=TRUE)
is.factor(tor$xsmoke)
# object with survey design variables and data
nhis <- svydesign (id=~psu,strat=~stratum, weights=~wt8, data=tor, nest=TRUE)
MyBreaks <- c(18, 25, 35, 45, 55, 65, 75, 85)
svyboxplot (age_p~xsmoke,
subset (nhis, age_p>=0),
col=c("red", "yellow", "green"), medcol="blue",
varwidth=TRUE, all.outliers=TRUE,
ylab="Age at Interview",
xlab=" "
)
The Factor variable xsmoke is coded as tor$xsmoke <- factor(tor$xsmoke,levels=c (1,2,3),
labels=c('Current SMK','Former SMK', 'Never Smk'), ordered=TRUE) which should be useful
__________________________________________enter code here

Resources