library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
First, set up your working directory where you have your data file and working R file.
setwd("/Users/se776257/OneDrive - University of Central Florida/Desktop/Prof. An/02 Teaching/2024 Spring/PAD 7754 Quantitative Methods/R script/")
Import data from your working folder.
db <- read.csv("2022_HIC.csv", stringsAsFactors = F)
If you want to only select cases in Florida
df <- db %>%
filter(CocState == "FL")
df <- df %>%
filter(!is.na(PIT.Count))
You want to examine the relationship between PIT count (# of sheltered people) and # of beds
summary(df$PIT.Count)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 7.00 16.00 32.82 36.00 850.00
summary(df$Total.Beds)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 10.00 20.00 39.69 44.00 1010.00
ggplot(data = df) +
geom_point(mapping = aes(x = PIT.Count, y = Total.Beds))
Let’s add a regression line
From the scatterplots, we suspect there is a significant linear relationship between the two variables. Now we want to calcaulte the correlation
cor(df$PIT.Count, df$Total.Beds)
## [1] 0.9640077
You will notice that when running a cor function with a variable that includes missing data which is very common, the result will appear as “NA.” To correct that, add the following option.
cor(df$PIT.Count, df$Total.Beds, use ="complete.obs")
## [1] 0.9640077
Let’s call our fitted model model1. Then we have:
model1=lm(data=df, formula= Total.Beds ~ PIT.Count)
summary(model1)
##
## Call:
## lm(formula = Total.Beds ~ PIT.Count, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -84.144 -5.604 -4.230 0.312 309.260
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.863633 0.599650 6.443 1.67e-10 ***
## PIT.Count 1.091589 0.008539 127.832 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 18.7 on 1243 degrees of freedom
## Multiple R-squared: 0.9293, Adjusted R-squared: 0.9293
## F-statistic: 1.634e+04 on 1 and 1243 DF, p-value: < 2.2e-16
“lm” stands for linear model, so the first letter is an L. The first line calculates the results of the linear regression.The second line prints the results.
#Residuals and plots of the residuals # We can create the residuals after fitting the model
residuals(model1)
## 1 2 3 4 5 6
## -10.69540931 -4.13839949 0.31206763 11.28963679 1.20552493 -4.13839949
## 7 8 9 10 11 12
## -4.22998830 -4.50475474 -4.87110999 -4.13839949 -5.42064287 -4.04681067
## 13 14 15 16 17 18
## -3.95522186 -3.95522186 -6.61129745 -4.13839949 -4.32157711 -4.32157711
## 19 20 21 22 23 24
## -5.51223169 17.93075849 -4.41316593 3.94571238 16.83916967 11.76253475
## 25 26 27 28 29 30
## -4.77952118 -3.95522186 -4.50475474 -3.22998830 -6.70288626 -5.32905406
## 31 32 33 34 35 36
## -5.60382050 -7.34400795 -16.59447810 48.56440323 -6.97765270 -5.51223169
## 37 38 39 40 41 42
## -4.50475474 -3.86363305 -4.41316593 -3.95522186 -4.41316593 -5.14587643
## 43 44 45 46 47 48
## -5.60382050 -4.41316593 -4.22998830 6.20552493 20.22047882 -6.15335338
## 49 50 51 52 53 54
## 11.49524526 -5.14587643 -4.50475474 -7.61877439 -7.61877439 -5.32905406
## 55 56 57 58 59 60
## -5.32905406 -4.87110999 -4.59634355 -6.42811982 -13.66363608 -4.22998830
## 61 62 63 64 65 66
## -8.16830727 -4.13839949 -4.59634355 -5.42064287 -5.78699813 -4.59634355
## 67 68 69 70 71 72
## -3.95522186 -4.68793237 7.35131782 -4.13839949 -4.68793237 -5.42064287
## 73 74 75 76 77 78
## -7.80195202 -5.51223169 -5.05428762 -12.56457032 -4.15335338 17.95318933
## 79 80 81 82 83 84
## -5.51223169 -9.26737303 -39.94962552 -3.95522186 -4.32157711 -3.95522186
## 85 86 87 88 89 90
## 13.78121954 -4.32157711 12.12141306 -46.08607601 -2.80195202 4.77935410
## 91 92 93 94 95 96
## -5.60382050 35.12141306 -3.16083033 -4.59634355 -66.96832546 0.47281442
## 97 98 99 100 101 102
## 0.22047882 -2.84681370 -6.06176457 -3.95522186 4.57188018 -5.14587643
## 103 104 105 106 107 108
## -2.04681067 -4.77952118 -5.42064287 -6.70288626 -4.96269881 -6.97765270
## 109 110 111 112 113 114
## 61.55692629 -2.78699813 -7.98512965 -5.32905406 -4.77952118 -7.16083033
## 115 116 117 118 119 120
## 20.95318933 21.57188018 -4.59634355 -3.95522186 -5.97017575 -4.59634355
## 121 122 123 124 125 126
## -6.15335338 -5.23746525 30.21300187 -4.22998830 -5.42064287 0.11393611
## 127 128 129 130 131 132
## -6.24494219 32.40365645 -5.23746525 -6.88606389 -5.78699813 -4.77952118
## 133 134 135 136 137 138
## -5.05428762 -4.13839949 -5.60382050 -5.51223169 -5.32905406 20.85412357
## 139 140 141 142 143 144
## -4.04681067 -4.68793237 -5.05428762 -4.04681067 -3.22998830 20.40365645
## 145 146 147 148 149 150
## -5.69540931 -4.50475474 -4.13839949 -4.14587643 -6.37952724 -4.59634355
## 151 152 153 154 155 156
## -5.32905406 -5.14587643 -4.32157711 -5.97017575 -3.95522186 28.84664662
## 157 158 159 160 161 162
## 31.86160051 108.74758086 -4.13839949 16.29711374 0.12889001 21.86160051
## 163 164 165 166 167 168
## 6.62608426 -25.11223775 -5.87858694 -5.24494219 -5.32905406 -4.87110999
## 169 170 171 172 173 174
## -6.79447507 -10.64120524 -6.89354083 1.57188018 -4.32157711 -5.78699813
## 175 176 177 178 179 180
## 11.57935713 -2.26737303 -4.13839949 -3.95522186 -5.42064287 -4.41316593
## 181 182 183 184 185 186
## -3.95522186 -1.87110999 5.67094594 -4.32157711 6.22047882 -3.96269881
## 187 188 189 190 191 192
## 42.48776831 17.81112733 25.89337375 -10.73279405 -5.05428762 -8.90101778
## 193 194 195 196 197 198
## 6.29711374 6.30459069 -7.71036321 3.28963679 -5.14587643 -2.77952118
## 199 200 201 202 203 204
## 7.85412357 9.45038358 -4.13839949 -4.68793237 -5.87858694 -5.52718558
## 205 206 207 208 209 210
## -1.88606389 -2.41316593 -2.59634355 -4.87110999 -4.04681067 0.86160051
## 211 212 213 214 215 216
## -4.04681067 -10.42064287 -13.87858694 -3.32157711 -2.59634355 -4.59634355
## 217 218 219 220 221 222
## -4.13839949 -4.22998830 -4.22998830 -4.22998830 -5.23746525 -1.68793237
## 223 224 225 226 227 228
## -11.78699813 12.92328154 -5.05428762 -4.50475474 -4.77952118 -3.95522186
## 229 230 231 232 233 234
## -4.96269881 -4.68793237 -2.95522186 -4.13839949 -0.59634355 -3.95522186
## 235 236 237 238 239 240
## -4.96269881 -14.57952421 -3.86363305 -11.37391574 -4.96269881 -4.15335338
## 241 242 243 244 245 246
## -2.68793237 -6.42811982 -1.05428762 50.84851207 -0.96269881 6.29711374
## 247 248 249 250 251 252
## 2.49524526 0.84664662 -14.08419541 -5.23746525 -8.71784015 -3.95522186
## 253 254 255 256 257 258
## -4.77952118 -8.44307371 -3.68793237 16.80926188 -3.68793237 -4.13839949
## 259 260 261 262 263 264
## -4.77952118 -6.15335338 -12.10662625 -3.68793237 -4.50475474 -2.42064287
## 265 266 267 268 269 270
## -5.60382050 8.85412357 0.03730119 10.22047882 11.95318933 1.40365645
## 271 272 273 274 275 276
## -4.04681067 0.67842289 -4.22998830 -4.22998830 -4.32157711 -4.32157711
## 277 278 279 280 281 282
## 0.95318933 -1.04681067 -4.50475474 -4.32157711 -0.41316593 -4.04681067
## 283 284 285 286 287 288
## -5.05428762 -1.15335338 -5.05428762 -3.41316593 -4.96269881 -5.97017575
## 289 290 291 292 293 294
## -6.33653101 -4.04681067 -3.04681067 -4.77952118 -1.13839949 -2.13839949
## 295 296 297 298 299 300
## -4.04681067 -4.13839949 1.31206763 -1.95522186 -5.23746525 0.86160051
## 301 302 303 304 305 306
## -4.97017575 -10.36643879 -5.23746525 -5.51223169 -2.59634355 -0.22998830
## 307 308 309 310 311 312
## 2.31206763 -3.90101778 44.76253475 44.76253475 7.74010391 -5.05428762
## 313 314 315 316 317 318
## -7.61877439 -5.78699813 -5.14587643 -5.05428762 -4.96269881 -5.05428762
## 319 320 321 322 323 324
## -4.68793237 -5.42064287 -5.05428762 -7.25241914 -5.60382050 -3.87858694
## 325 326 327 328 329 330
## -4.87110999 -5.51223169 -6.15335338 -8.53466253 -8.16830727 -5.97017575
## 331 332 333 334 335 336
## -17.69354386 -4.50475474 -5.62625134 0.66346899 -4.41316593 -4.41316593
## 337 338 339 340 341 342
## -8.07671846 -2.59634355 -5.23746525 -0.69540931 -6.82438286 -4.96269881
## 343 344 345 346 347 348
## -4.87110999 -3.22998830 -2.87110999 -4.42064287 -1.50475474 -2.87858694
## 349 350 351 352 353 354
## -4.32157711 -1.70288626 7.12889001 -11.92344862 -13.20569201 -6.33653101
## 355 356 357 358 359 360
## -2.51223169 -7.16083033 -13.57204726 -11.65615913 -5.42064287 1.46533747
## 361 362 363 364 365 366
## 17.38870255 13.67094594 -1.50475474 -2.91597168 12.48776831 1.02982425
## 367 368 369 370 371 372
## 101.10645917 18.49524526 18.86160051 -7.71036321 -5.14587643 -2.51223169
## 373 374 375 376 377 378
## -3.71036321 96.23729816 -7.25241914 126.59617647 42.92328154 -4.71036321
## 379 380 381 382 383 384
## -4.50475474 9.22047882 -4.05428762 -5.51223169 -3.95522186 1.20552493
## 385 386 387 388 389 390
## -1.41316593 -3.79447507 -4.04681067 -6.15335338 -7.52718558 -4.22998830
## 391 392 393 394 395 396
## -5.97017575 -15.67858997 -5.32905406 9.48776831 -7.43559677 -7.52718558
## 397 398 399 400 401 402
## -5.51223169 -2.98512965 -6.15335338 -5.87858694 -24.19634961 -4.87110999
## 403 404 405 406 407 408
## -4.13839949 -6.42811982 -5.87858694 -6.97765270 -6.33653101 5.03730119
## 409 410 411 412 413 414
## 9.93075849 -9.81690591 -11.37391574 -9.54213947 -5.32905406 -1.98512965
## 415 416 417 418 419 420
## 1.86160051 -5.23746525 -12.74774794 -4.41316593 54.13636695 -14.85429065
## 421 422 423 424 425 426
## -5.32905406 -1.68793237 -5.15335338 -60.55710852 -7.34400795 -4.13839949
## 427 428 429 430 431 432
## -4.68793237 2.31206763 2.67842289 -4.87110999 -4.32157711 -5.51223169
## 433 434 435 436 437 438
## -0.49354690 -6.51970863 5.48776831 26.13636695 5.93823543 -4.68793237
## 439 440 441 442 443 444
## -6.15335338 -6.70288626 21.77001170 -2.96269881 -4.13839949 0.22047882
## 445 446 447 448 449 450
## -1.68793237 -4.41316593 -4.87110999 -5.14587643 -5.23746525 -7.06924151
## 451 452 453 454 455 456
## -4.32157711 -4.68793237 -4.32157711 -4.60382050 -9.55709337 -6.06176457
## 457 458 459 460 461 462
## -6.43559677 -9.54213947 -5.05428762 -15.86176760 -7.34400795 -4.87110999
## 463 464 465 466 467 468
## -7.98512965 -4.77952118 -5.24494219 6.83916967 5.85412357 17.75505781
## 469 470 471 472 473 474
## 22.31206763 -2.52718558 -7.18326117 9.39617950 -4.87110999 1.31206763
## 475 476 477 478 479 480
## -5.51223169 5.48029137 4.10645917 -0.22998830 -4.96269881 9.77001170
## 481 482 483 484 485 486
## -3.95522186 3.58683407 -7.98512965 39.38870255 8.02234730 -2.14587643
## 487 488 489 490 491 492
## -5.05428762 -3.96269881 -6.15335338 -7.80195202 -0.22998830 -4.04681067
## 493 494 495 496 497 498
## -4.13839949 9.76253475 0.31206763 -6.71784015 29.01487035 3.76253475
## 499 500 501 502 503 504
## -0.06176457 -2.70288626 -0.25241914 10.11393611 -3.32905406 -43.06364517
## 505 506 507 508 509 510
## -0.68793237 2.12889001 2.95318933 4.22047882 -3.14587643 -1.87110999
## 511 512 513 514 515 516
## -3.05428762 -4.75522489 -8.16830727 -1.13839949 -8.35148490 -0.05428762
## 517 518 519 520 521 522
## 6.89337375 19.38870255 -3.88606389 13.57935713 81.20552493 53.02234730
## 523 524 525 526 527 528
## 5.57188018 -4.96269881 -7.98512965 -8.99260659 -4.59634355 -3.95522186
## 529 530 531 532 533 534
## -4.59634355 9.46533747 -4.60382050 8.77001170 -4.41316593 -5.23746525
## 535 536 537 538 539 540
## -4.96269881 -9.88606389 -3.87110999 -2.60382050 -1.50475474 -0.95522186
## 541 542 543 544 545 546
## -3.05428762 1.58683407 -2.95522186 -3.44307371 -2.79447507 -11.28232693
## 547 548 549 550 551 552
## -4.35896185 -2.13839949 -3.17578422 -3.88606389 -4.44307371 -1.61129745
## 553 554 555 556 557 558
## -8.05428762 -4.41316593 15.58683407 -6.15335338 5.92328154 -1.95522186
## 559 560 561 562 563 564
## -14.18326117 -6.33653101 -4.77952118 -10.18326117 -3.41316593 -3.83185981
## 565 566 567 568 569 570
## -1.13839949 2.67842289 -2.77952118 -5.60382050 -1.04681067 -4.04681067
## 571 572 573 574 575 576
## 0.04477814 -6.42811982 0.22047882 -4.04681067 -1.32905406 11.76253475
## 577 578 579 580 581 582
## -24.20382656 -22.11971470 -1.04681067 -4.32157711 8.86160051 12.28215985
## 583 584 585 586 587 588
## -4.96269881 -7.35148490 20.59617647 0.46533747 -3.51223169 3.84664662
## 589 590 591 592 593 594
## 0.93823543 -8.44307371 -2.60382050 -8.96269881 -7.80195202 -3.32157711
## 595 596 597 598 599 600
## -6.24494219 -10.61877439 -0.70288626 -7.33653101 -11.09914930 -13.42811982
## 601 602 603 604 605 606
## 0.86160051 5.02982425 -8.91597168 1.12889001 12.48029137 -6.52718558
## 607 608 609 610 611 612
## 1.94571238 1.56440323 -3.72531710 -2.26737303 -0.61129745 -4.25241914
## 613 614 615 616 617 618
## -11.55709337 -5.60382050 -18.40382353 11.20552493 8.49524526 117.35504871
## 619 620 621 622 623 624
## 101.83355817 -10.82438286 -11.46550456 -7.61877439 16.66346899 -6.42811982
## 625 626 627 628 629 630
## -7.43559677 -7.52718558 -8.07671846 -7.61877439 4.75505781 -6.31971166
## 631 632 633 634 635 636
## -3.86176760 -7.71036321 -2.78699813 -61.79260963 -4.50475474 3.94571238
## 637 638 639 640 641 642
## 1.40365645 -2.42064287 -4.22998830 -0.77952118 -3.95522186 -4.50475474
## 643 644 645 646 647 648
## 5.04477814 30.74010391 80.32888698 -0.04681067 -5.69540931 11.31206763
## 649 650 651 652 653 654
## -28.93840252 11.27468290 0.39617950 -4.50475474 2.64851510 -10.06176457
## 655 656 657 658 659 660
## -24.28980388 6.67842289 -14.27673059 -1.15335338 3.46533747 8.40552190
## 661 662 663 664 665 666
## -2.40382353 -4.68793237 -4.22998830 -4.32157711 -4.32157711 -3.32157711
## 667 668 669 670 671 672
## -4.77952118 -3.04681067 -4.87110999 -4.41316593 -6.68606692 15.13636695
## 673 674 675 676 677 678
## -4.97017575 1.38870255 -4.22998830 0.62608426 -2.41316593 -4.05428762
## 679 680 681 682 683 684
## -5.23746525 -3.87110999 -6.33653101 -1.97765270 -5.69540931 -4.97765270
## 685 686 687 688 689 690
## 0.22047882 2.40365645 -4.22998830 -4.13839949 -4.13839949 -20.25803064
## 691 692 693 694 695 696
## -8.44307371 -6.06176457 -4.41316593 -10.05428762 -5.42064287 -5.42064287
## 697 698 699 700 701 702
## -2.06924151 -9.45055066 -7.71036321 -6.14587643 -4.50475474 0.49524526
## 703 704 705 706 707 708
## -0.22998830 -2.04681067 2.12889001 0.12889001 1.58683407 10.66533444
## 709 710 711 712 713 714
## -1.14587643 19.73262697 0.22047882 -5.05428762 -2.04681067 -0.04681067
## 715 716 717 718 719 720
## 9.13636695 -4.59634355 -3.68793237 -4.59634355 -3.86363305 -10.97017575
## 721 722 723 724 725 726
## 9.22047882 25.12141306 -8.44307371 -4.04681067 -0.86363305 -4.42811982
## 727 728 729 730 731 732
## -9.74774794 -0.06924151 18.40365645 9.75505781 15.04477814 -5.51223169
## 733 734 735 736 737 738
## -4.68793237 -4.96269881 -10.09167235 -2.04681067 -4.04681067 -15.71036321
## 739 740 741 742 743 744
## -19.16083033 1.21300187 -4.50475474 139.22047882 -3.95522186 36.20552493
## 745 746 747 748 749 750
## 2.66346899 -12.19821506 -7.34400795 -5.32905406 -10.91597168 17.22047882
## 751 752 753 754 755 756
## 52.57374563 1.44477208 26.13636695 -7.06924151 -7.61877439 -6.24494219
## 757 758 759 760 761 762
## -1.13839949 -2.59634355 8.12889001 -5.05428762 4.12889001 -5.05428762
## 763 764 765 766 767 768
## -6.79447507 -6.33653101 -6.70288626 10.48776831 -4.87110999 -4.04681067
## 769 770 771 772 773 774
## -6.06176457 -6.79447507 -9.63372829 -6.25989609 -4.76270184 -26.68420147
## 775 776 777 778 779 780
## -0.97017575 -2.06924151 -3.97017575 -4.68793237 -19.99260659 -4.53466253
## 781 782 783 784 785 786
## -11.06924151 -4.04681067 14.99243951 -56.98326420 -32.35709640 -7.70288626
## 787 788 789 790 791 792
## -0.06176457 -3.95522186 -5.51223169 1.22047882 -29.18326117 2.22047882
## 793 794 795 796 797 798
## 27.20552493 -8.25989609 46.85412357 -11.81690591 -9.36643879 -8.25989609
## 799 800 801 802 803 804
## 56.69524223 -22.22813801 -4.22998830 -5.97017575 43.77001170 -4.78699813
## 805 806 807 808 809 810
## -6.06176457 19.57935713 2.12889001 -4.22998830 22.85412357 32.48029137
## 811 812 813 814 815 816
## 0.85412357 -3.16830727 19.58683407 -9.79447507 -4.59634355 10.04477814
## 817 818 819 820 821 822
## 17.40365645 -4.04681067 -5.78699813 -4.77952118 -1.16830727 -4.41316593
## 823 824 825 826 827 828
## -10.64120524 -4.22998830 15.02982425 3.94571238 -7.05428762 -5.14587643
## 829 830 831 832 833 834
## -2.68793237 5.95318933 -7.80195202 -12.05428762 -0.35896185 0.02234730
## 835 836 837 838 839 840
## -4.59634355 -5.32905406 -7.35148490 -8.87858694 -17.41877742 -84.14401098
## 841 842 843 844 845 846
## -17.14401098 -3.41316593 10.12141306 3.77001170 -4.59634355 -4.77952118
## 847 848 849 850 851 852
## -4.22998830 -5.22998830 -3.00756049 -4.68793237 -10.45802761 -10.00008354
## 853 854 855 856 857 858
## -3.86363305 -5.78699813 -3.86363305 -4.13839949 -4.77952118 -4.87110999
## 859 860 861 862 863 864
## -8.16830727 19.04477814 -10.91597168 -2.95522186 -4.22998830 -5.87858694
## 865 866 867 868 869 870
## 0.74758086 5.12889001 -4.59634355 -4.59634355 -2.51223169 -11.23746525
## 871 872 873 874 875 876
## -0.87110999 -2.59634355 -12.65615913 47.65599205 -3.70288626 10.86160051
## 877 878 879 880 881 882
## 20.48776831 -7.69540931 -6.42811982 -5.87858694 -14.81690591 8.49524526
## 883 884 885 886 887 888
## -10.18326117 17.02982425 -3.86363305 -15.97017575 -10.36643879 -2.51970863
## 889 890 891 892 893 894
## -0.95522186 -1.13839949 5.04477814 -2.50475474 -4.77952118 -4.04681067
## 895 896 897 898 899 900
## -9.35896185 -5.05428762 4.86160051 -3.41316593 -3.51223169 -4.32157711
## 901 902 903 904 905 906
## 7.67842289 21.31206763 10.30459069 -4.22998830 -2.77952118 13.99991646
## 907 908 909 910 911 912
## -1.32157711 -7.71036321 309.25972900 100.13636695 23.77374260 1.20552493
## 913 914 915 916 917 918
## -14.94587946 -5.32905406 -4.22998830 1.40365645 -9.54213947 -5.78699813
## 919 920 921 922 923 924
## 2.12141306 -3.22998830 19.95318933 15.58683407 -7.89354083 -4.96269881
## 925 926 927 928 929 930
## 1.31206763 3.13636695 -4.87110999 -4.32157711 -4.13839949 4.13636695
## 931 932 933 934 935 936
## -1.32157711 -5.60382050 -5.32905406 16.13636695 7.13636695 0.49524526
## 937 938 939 940 941 942
## -6.70288626 31.13636695 -6.43559677 -4.22998830 -4.68793237 -2.86363305
## 943 944 945 946 947 948
## 24.58683407 12.13636695 7.31206763 -5.51223169 -4.50475474 18.13636695
## 949 950 951 952 953 954
## -2.07671846 -11.74027100 -10.82438286 -13.72345165 -2.24494219 -11.19073812
## 955 956 957 958 959 960
## 10.86160051 13.58683407 -3.68793237 -8.80942897 -10.54961642 -5.42064287
## 961 962 963 964 965 966
## -5.23746525 -8.53466253 -5.05428762 -7.61877439 1.57935713 12.13636695
## 967 968 969 970 971 972
## -4.68793237 7.38870255 -0.23746525 12.86160051 5.77001170 8.13636695
## 973 974 975 976 977 978
## 8.12889001 -2.42064287 0.36627171 -3.78699813 25.81673883 -1.32157711
## 979 980 981 982 983 984
## 9.22047882 9.58683407 -5.14587643 6.48776831 0.31206763 2.49524526
## 985 986 987 988 989 990
## -6.42811982 7.19057103 -3.68793237 -1.13839949 114.28215985 6.31206763
## 991 992 993 994 995 996
## 1.94571238 38.03730119 -0.41316593 -4.32157711 55.93075849 0.04477814
## 997 998 999 1000 1001 1002
## 3.75505781 -10.00008354 -5.32905406 -4.60382050 -3.32905406 3.94571238
## 1003 1004 1005 1006 1007 1008
## -3.86363305 -1.61877439 2.76253475 -6.33653101 -3.86363305 -3.86363305
## 1009 1010 1011 1012 1013 1014
## -3.86363305 7.77001170 18.11393611 -5.42064287 -4.22998830 -2.32157711
## 1015 1016 1017 1018 1019 1020
## -3.95522186 -1.68793237 -1.32157711 -2.59634355 -4.32905406 -4.13839949
## 1021 1022 1023 1024 1025 1026
## -3.13839949 -3.04681067 -4.22998830 1.58683407 -3.87110999 -3.22998830
## 1027 1028 1029 1030 1031 1032
## -4.59634355 -2.41316593 -4.62625134 3.03730119 -4.32157711 -3.87110999
## 1033 1034 1035 1036 1037 1038
## -3.22998830 0.12889001 -2.41316593 -4.04681067 9.12889001 2.92328154
## 1039 1040 1041 1042 1043 1044
## -0.41316593 -0.98512965 -10.36643879 -4.87110999 -5.61877439 -3.50475474
## 1045 1046 1047 1048 1049 1050
## -4.13839949 -2.77952118 -4.23746525 8.95318933 -4.22998830 -5.32905406
## 1051 1052 1053 1054 1055 1056
## 6.19243648 -10.09914930 -3.14587643 -0.87110999 -6.89354083 5.04477814
## 1057 1058 1059 1060 1061 1062
## -5.87858694 -6.42811982 -4.04681067 -7.06924151 7.04477814 5.95318933
## 1063 1064 1065 1066 1067 1068
## 1.11393611 -1.32157711 29.74758086 0.12141306 -9.17578422 17.57935713
## 1069 1070 1071 1072 1073 1074
## -5.23746525 1.22047882 -5.07671846 6.48776831 -0.27484998 12.48776831
## 1075 1076 1077 1078 1079 1080
## 97.58683407 25.91392399 -12.65615913 96.38309105 -3.51223169 1.20552493
## 1081 1082 1083 1084 1085 1086
## -3.04681067 -5.51223169 22.55692629 -4.32157711 -4.13839949 7.03730119
## 1087 1088 1089 1090 1091 1092
## 17.02982425 71.86160051 101.94010088 3.83916967 -0.17578422 -7.98512965
## 1093 1094 1095 1096 1097 1098
## -10.36643879 -9.72531710 -10.09167235 -1.32905406 1.48776831 0.04477814
## 1099 1100 1101 1102 1103 1104
## -7.24494219 12.37374866 -6.06176457 26.20552493 -8.16830727 -5.51223169
## 1105 1106 1107 1108 1109 1110
## -2.96269881 8.61860731 4.86160051 2.31206763 -4.41316593 -4.04681067
## 1111 1112 1113 1114 1115 1116
## 112.61299581 2.75505781 5.56440323 -7.53466253 -5.69540931 -5.51223169
## 1117 1118 1119 1120 1121 1122
## -4.87110999 -4.59634355 -5.87858694 -5.87858694 0.07655138 -4.41316593
## 1123 1124 1125 1126 1127 1128
## -0.51223169 1.57188018 -8.62625134 -4.41316593 -4.22998830 -12.10662625
## 1129 1130 1131 1132 1133 1134
## -4.59634355 -5.05428762 -4.68793237 1.49524526 -11.00756049 -8.16830727
## 1135 1136 1137 1138 1139 1140
## -4.87110999 -10.82438286 -5.23746525 -3.50475474 -4.68793237 -6.24494219
## 1141 1142 1143 1144 1145 1146
## -9.90849473 -5.32905406 -10.82438286 -5.60382050 -5.05428762 -4.13839949
## 1147 1148 1149 1150 1151 1152
## -4.87110999 -12.83933676 -0.77952118 -6.24494219 -4.13839949 -4.87110999
## 1153 1154 1155 1156 1157 1158
## -1.68793237 -5.32905406 -5.23746525 -7.98512965 -5.42064287 78.28587559
## 1159 1160 1161 1162 1163 1164
## -4.22998830 -2.22998830 -6.51970863 -1.60382050 -12.83933676 -12.35148490
## 1165 1166 1167 1168 1169 1170
## -4.77952118 -9.26737303 -23.46363911 -4.41316593 -10.54961642 -8.90101778
## 1171 1172 1173 1174 1175 1176
## -10.18326117 -8.99260659 -9.81690591 -8.35148490 -9.06176457 -5.97017575
## 1177 1178 1179 1180 1181 1182
## -10.45802761 -8.07671846 -4.77952118 4.30459069 -14.12158014 -8.25989609
## 1183 1184 1185 1186 1187 1188
## -4.22998830 -10.18326117 0.04477814 -4.13839949 -3.22998830 -11.97017575
## 1189 1190 1191 1192 1193 1194
## -4.60382050 -11.82438286 -5.41316593 -3.87110999 -4.41316593 -6.33653101
## 1195 1196 1197 1198 1199 1200
## -6.33653101 -7.43559677 -4.41316593 -12.74774794 -8.53466253 -9.72531710
## 1201 1202 1203 1204 1205 1206
## 29.16814019 8.29711374 8.48029137 1.95318933 -4.68793237 -10.73279405
## 1207 1208 1209 1210 1211 1212
## 9.02234730 -4.96269881 -3.87110999 2.95318933 -4.32905406 3.76253475
## 1213 1214 1215 1216 1217 1218
## 27.71767307 7.01487035 8.04477814 0.67842289 -7.34400795 -4.68793237
## 1219 1220 1221 1222 1223 1224
## -6.80942897 28.83916967 -5.23746525 -4.32157711 -5.87858694 -5.05428762
## 1225 1226 1227 1228 1229 1230
## -4.87110999 28.99991646 -1.59634355 -5.42064287 -5.23746525 -3.22998830
## 1231 1232 1233 1234 1235 1236
## -3.50475474 -4.77952118 -4.68793237 -5.23746525 30.99991646 -6.97765270
## 1237 1238 1239 1240 1241 1242
## 1.86160051 -0.77952118 -0.87110999 -4.59634355 -2.59634355 9.94571238
## 1243 1244 1245
## -5.78699813 -0.68793237 28.84663146
df$ResidBeds <- residuals(model1)
We can plot the residuals as a function of ‘PIT count’ to check for linearity, and for common standard deviation (i.e. that the spread of the residuals about 0 is similar for all values of PIT count):
ggplot(data = df) +
geom_point(mapping = aes(x = PIT.Count, y = ResidBeds)) +
geom_hline(yintercept=0,color="red") +
geom_smooth(mapping = aes(x = PIT.Count, y = ResidBeds),method=loess,se=FALSE,color="blue")
## `geom_smooth()` using formula = 'y ~ x'
geom_point above creates a scatter plot. The geom_hline adds a
horizontal line for y=0, and the color is by default black, so I
switched it to red to make it more salient. Finally, geom_smooth adds a
smooth fit (not necessarily linear) using the loess smoothing method
(method=loess).
Finally, we can do a QQ plot of the residuals to check that the response varies normally about the regression line:
qqnorm(df$ResidBeds, main = "Q-Q Plot of Rediduals")