Page 16 Exercises

1. Take the Galton dataset and find the mean, standard deviation and correlation between the parental and child heights.

library(UsingR)
## Loading required package: MASS
## Loading required package: HistData
## Loading required package: Hmisc
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
data(galton)
head(galton)
##   child parent
## 1  61.7   70.5
## 2  61.7   68.5
## 3  61.7   65.5
## 4  61.7   64.5
## 5  61.7   64.0
## 6  62.2   67.5
summary(galton)
##      child           parent     
##  Min.   :61.70   Min.   :64.00  
##  1st Qu.:66.20   1st Qu.:67.50  
##  Median :68.20   Median :68.50  
##  Mean   :68.09   Mean   :68.31  
##  3rd Qu.:70.20   3rd Qu.:69.50  
##  Max.   :73.70   Max.   :73.00
cor(galton)
##            child    parent
## child  1.0000000 0.4587624
## parent 0.4587624 1.0000000

2. Center the parent and child variables and verify that the centered variable means are 0.

galton
##     child parent
## 1    61.7   70.5
## 2    61.7   68.5
## 3    61.7   65.5
## 4    61.7   64.5
## 5    61.7   64.0
## 6    62.2   67.5
## 7    62.2   67.5
## 8    62.2   67.5
## 9    62.2   66.5
## 10   62.2   66.5
## 11   62.2   66.5
## 12   62.2   64.5
## 13   63.2   70.5
## 14   63.2   69.5
## 15   63.2   68.5
## 16   63.2   68.5
## 17   63.2   68.5
## 18   63.2   68.5
## 19   63.2   68.5
## 20   63.2   68.5
## 21   63.2   68.5
## 22   63.2   67.5
## 23   63.2   67.5
## 24   63.2   67.5
## 25   63.2   67.5
## 26   63.2   67.5
## 27   63.2   66.5
## 28   63.2   66.5
## 29   63.2   66.5
## 30   63.2   65.5
## 31   63.2   65.5
## 32   63.2   65.5
## 33   63.2   65.5
## 34   63.2   65.5
## 35   63.2   65.5
## 36   63.2   65.5
## 37   63.2   65.5
## 38   63.2   65.5
## 39   63.2   64.5
## 40   63.2   64.5
## 41   63.2   64.5
## 42   63.2   64.5
## 43   63.2   64.0
## 44   63.2   64.0
## 45   64.2   69.5
## 46   64.2   69.5
## 47   64.2   69.5
## 48   64.2   69.5
## 49   64.2   69.5
## 50   64.2   69.5
## 51   64.2   69.5
## 52   64.2   69.5
## 53   64.2   69.5
## 54   64.2   69.5
## 55   64.2   69.5
## 56   64.2   69.5
## 57   64.2   69.5
## 58   64.2   69.5
## 59   64.2   69.5
## 60   64.2   69.5
## 61   64.2   68.5
## 62   64.2   68.5
## 63   64.2   68.5
## 64   64.2   68.5
## 65   64.2   68.5
## 66   64.2   68.5
## 67   64.2   68.5
## 68   64.2   68.5
## 69   64.2   68.5
## 70   64.2   68.5
## 71   64.2   68.5
## 72   64.2   67.5
## 73   64.2   67.5
## 74   64.2   67.5
## 75   64.2   67.5
## 76   64.2   67.5
## 77   64.2   67.5
## 78   64.2   67.5
## 79   64.2   67.5
## 80   64.2   67.5
## 81   64.2   67.5
## 82   64.2   67.5
## 83   64.2   67.5
## 84   64.2   67.5
## 85   64.2   67.5
## 86   64.2   66.5
## 87   64.2   66.5
## 88   64.2   66.5
## 89   64.2   66.5
## 90   64.2   66.5
## 91   64.2   65.5
## 92   64.2   65.5
## 93   64.2   65.5
## 94   64.2   65.5
## 95   64.2   65.5
## 96   64.2   64.5
## 97   64.2   64.5
## 98   64.2   64.5
## 99   64.2   64.5
## 100  64.2   64.0
## 101  64.2   64.0
## 102  64.2   64.0
## 103  64.2   64.0
## 104  65.2   71.5
## 105  65.2   70.5
## 106  65.2   69.5
## 107  65.2   69.5
## 108  65.2   69.5
## 109  65.2   69.5
## 110  65.2   68.5
## 111  65.2   68.5
## 112  65.2   68.5
## 113  65.2   68.5
## 114  65.2   68.5
## 115  65.2   68.5
## 116  65.2   68.5
## 117  65.2   68.5
## 118  65.2   68.5
## 119  65.2   68.5
## 120  65.2   68.5
## 121  65.2   68.5
## 122  65.2   68.5
## 123  65.2   68.5
## 124  65.2   68.5
## 125  65.2   68.5
## 126  65.2   67.5
## 127  65.2   67.5
## 128  65.2   67.5
## 129  65.2   67.5
## 130  65.2   67.5
## 131  65.2   67.5
## 132  65.2   67.5
## 133  65.2   67.5
## 134  65.2   67.5
## 135  65.2   67.5
## 136  65.2   67.5
## 137  65.2   67.5
## 138  65.2   67.5
## 139  65.2   67.5
## 140  65.2   67.5
## 141  65.2   66.5
## 142  65.2   66.5
## 143  65.2   65.5
## 144  65.2   65.5
## 145  65.2   65.5
## 146  65.2   65.5
## 147  65.2   65.5
## 148  65.2   65.5
## 149  65.2   65.5
## 150  65.2   64.5
## 151  65.2   64.0
## 152  66.2   71.5
## 153  66.2   71.5
## 154  66.2   71.5
## 155  66.2   70.5
## 156  66.2   69.5
## 157  66.2   69.5
## 158  66.2   69.5
## 159  66.2   69.5
## 160  66.2   69.5
## 161  66.2   69.5
## 162  66.2   69.5
## 163  66.2   69.5
## 164  66.2   69.5
## 165  66.2   69.5
## 166  66.2   69.5
## 167  66.2   69.5
## 168  66.2   69.5
## 169  66.2   69.5
## 170  66.2   69.5
## 171  66.2   69.5
## 172  66.2   69.5
## 173  66.2   68.5
## 174  66.2   68.5
## 175  66.2   68.5
## 176  66.2   68.5
## 177  66.2   68.5
## 178  66.2   68.5
## 179  66.2   68.5
## 180  66.2   68.5
## 181  66.2   68.5
## 182  66.2   68.5
## 183  66.2   68.5
## 184  66.2   68.5
## 185  66.2   68.5
## 186  66.2   68.5
## 187  66.2   68.5
## 188  66.2   68.5
## 189  66.2   68.5
## 190  66.2   68.5
## 191  66.2   68.5
## 192  66.2   68.5
## 193  66.2   68.5
## 194  66.2   68.5
## 195  66.2   68.5
## 196  66.2   68.5
## 197  66.2   68.5
## 198  66.2   67.5
## 199  66.2   67.5
## 200  66.2   67.5
## 201  66.2   67.5
## 202  66.2   67.5
## 203  66.2   67.5
## 204  66.2   67.5
## 205  66.2   67.5
## 206  66.2   67.5
## 207  66.2   67.5
## 208  66.2   67.5
## 209  66.2   67.5
## 210  66.2   67.5
## 211  66.2   67.5
## 212  66.2   67.5
## 213  66.2   67.5
## 214  66.2   67.5
## 215  66.2   67.5
## 216  66.2   67.5
## 217  66.2   67.5
## 218  66.2   67.5
## 219  66.2   67.5
## 220  66.2   67.5
## 221  66.2   67.5
## 222  66.2   67.5
## 223  66.2   67.5
## 224  66.2   67.5
## 225  66.2   67.5
## 226  66.2   67.5
## 227  66.2   67.5
## 228  66.2   67.5
## 229  66.2   67.5
## 230  66.2   67.5
## 231  66.2   67.5
## 232  66.2   67.5
## 233  66.2   67.5
## 234  66.2   66.5
## 235  66.2   66.5
## 236  66.2   66.5
## 237  66.2   66.5
## 238  66.2   66.5
## 239  66.2   66.5
## 240  66.2   66.5
## 241  66.2   66.5
## 242  66.2   66.5
## 243  66.2   66.5
## 244  66.2   66.5
## 245  66.2   66.5
## 246  66.2   66.5
## 247  66.2   66.5
## 248  66.2   66.5
## 249  66.2   66.5
## 250  66.2   66.5
## 251  66.2   65.5
## 252  66.2   65.5
## 253  66.2   65.5
## 254  66.2   65.5
## 255  66.2   65.5
## 256  66.2   65.5
## 257  66.2   65.5
## 258  66.2   65.5
## 259  66.2   65.5
## 260  66.2   65.5
## 261  66.2   65.5
## 262  66.2   64.5
## 263  66.2   64.5
## 264  66.2   64.5
## 265  66.2   64.5
## 266  66.2   64.5
## 267  66.2   64.0
## 268  66.2   64.0
## 269  67.2   71.5
## 270  67.2   71.5
## 271  67.2   71.5
## 272  67.2   71.5
## 273  67.2   70.5
## 274  67.2   70.5
## 275  67.2   70.5
## 276  67.2   69.5
## 277  67.2   69.5
## 278  67.2   69.5
## 279  67.2   69.5
## 280  67.2   69.5
## 281  67.2   69.5
## 282  67.2   69.5
## 283  67.2   69.5
## 284  67.2   69.5
## 285  67.2   69.5
## 286  67.2   69.5
## 287  67.2   69.5
## 288  67.2   69.5
## 289  67.2   69.5
## 290  67.2   69.5
## 291  67.2   69.5
## 292  67.2   69.5
## 293  67.2   69.5
## 294  67.2   69.5
## 295  67.2   69.5
## 296  67.2   69.5
## 297  67.2   69.5
## 298  67.2   69.5
## 299  67.2   69.5
## 300  67.2   69.5
## 301  67.2   69.5
## 302  67.2   69.5
## 303  67.2   68.5
## 304  67.2   68.5
## 305  67.2   68.5
## 306  67.2   68.5
## 307  67.2   68.5
## 308  67.2   68.5
## 309  67.2   68.5
## 310  67.2   68.5
## 311  67.2   68.5
## 312  67.2   68.5
## 313  67.2   68.5
## 314  67.2   68.5
## 315  67.2   68.5
## 316  67.2   68.5
## 317  67.2   68.5
## 318  67.2   68.5
## 319  67.2   68.5
## 320  67.2   68.5
## 321  67.2   68.5
## 322  67.2   68.5
## 323  67.2   68.5
## 324  67.2   68.5
## 325  67.2   68.5
## 326  67.2   68.5
## 327  67.2   68.5
## 328  67.2   68.5
## 329  67.2   68.5
## 330  67.2   68.5
## 331  67.2   68.5
## 332  67.2   68.5
## 333  67.2   68.5
## 334  67.2   67.5
## 335  67.2   67.5
## 336  67.2   67.5
## 337  67.2   67.5
## 338  67.2   67.5
## 339  67.2   67.5
## 340  67.2   67.5
## 341  67.2   67.5
## 342  67.2   67.5
## 343  67.2   67.5
## 344  67.2   67.5
## 345  67.2   67.5
## 346  67.2   67.5
## 347  67.2   67.5
## 348  67.2   67.5
## 349  67.2   67.5
## 350  67.2   67.5
## 351  67.2   67.5
## 352  67.2   67.5
## 353  67.2   67.5
## 354  67.2   67.5
## 355  67.2   67.5
## 356  67.2   67.5
## 357  67.2   67.5
## 358  67.2   67.5
## 359  67.2   67.5
## 360  67.2   67.5
## 361  67.2   67.5
## 362  67.2   67.5
## 363  67.2   67.5
## 364  67.2   67.5
## 365  67.2   67.5
## 366  67.2   67.5
## 367  67.2   67.5
## 368  67.2   67.5
## 369  67.2   67.5
## 370  67.2   67.5
## 371  67.2   67.5
## 372  67.2   66.5
## 373  67.2   66.5
## 374  67.2   66.5
## 375  67.2   66.5
## 376  67.2   66.5
## 377  67.2   66.5
## 378  67.2   66.5
## 379  67.2   66.5
## 380  67.2   66.5
## 381  67.2   66.5
## 382  67.2   66.5
## 383  67.2   66.5
## 384  67.2   66.5
## 385  67.2   66.5
## 386  67.2   66.5
## 387  67.2   66.5
## 388  67.2   66.5
## 389  67.2   65.5
## 390  67.2   65.5
## 391  67.2   65.5
## 392  67.2   65.5
## 393  67.2   65.5
## 394  67.2   65.5
## 395  67.2   65.5
## 396  67.2   65.5
## 397  67.2   65.5
## 398  67.2   65.5
## 399  67.2   65.5
## 400  67.2   64.5
## 401  67.2   64.5
## 402  67.2   64.5
## 403  67.2   64.5
## 404  67.2   64.5
## 405  67.2   64.0
## 406  67.2   64.0
## 407  68.2   72.5
## 408  68.2   71.5
## 409  68.2   71.5
## 410  68.2   71.5
## 411  68.2   70.5
## 412  68.2   70.5
## 413  68.2   70.5
## 414  68.2   70.5
## 415  68.2   70.5
## 416  68.2   70.5
## 417  68.2   70.5
## 418  68.2   70.5
## 419  68.2   70.5
## 420  68.2   70.5
## 421  68.2   70.5
## 422  68.2   70.5
## 423  68.2   69.5
## 424  68.2   69.5
## 425  68.2   69.5
## 426  68.2   69.5
## 427  68.2   69.5
## 428  68.2   69.5
## 429  68.2   69.5
## 430  68.2   69.5
## 431  68.2   69.5
## 432  68.2   69.5
## 433  68.2   69.5
## 434  68.2   69.5
## 435  68.2   69.5
## 436  68.2   69.5
## 437  68.2   69.5
## 438  68.2   69.5
## 439  68.2   69.5
## 440  68.2   69.5
## 441  68.2   69.5
## 442  68.2   69.5
## 443  68.2   68.5
## 444  68.2   68.5
## 445  68.2   68.5
## 446  68.2   68.5
## 447  68.2   68.5
## 448  68.2   68.5
## 449  68.2   68.5
## 450  68.2   68.5
## 451  68.2   68.5
## 452  68.2   68.5
## 453  68.2   68.5
## 454  68.2   68.5
## 455  68.2   68.5
## 456  68.2   68.5
## 457  68.2   68.5
## 458  68.2   68.5
## 459  68.2   68.5
## 460  68.2   68.5
## 461  68.2   68.5
## 462  68.2   68.5
## 463  68.2   68.5
## 464  68.2   68.5
## 465  68.2   68.5
## 466  68.2   68.5
## 467  68.2   68.5
## 468  68.2   68.5
## 469  68.2   68.5
## 470  68.2   68.5
## 471  68.2   68.5
## 472  68.2   68.5
## 473  68.2   68.5
## 474  68.2   68.5
## 475  68.2   68.5
## 476  68.2   68.5
## 477  68.2   67.5
## 478  68.2   67.5
## 479  68.2   67.5
## 480  68.2   67.5
## 481  68.2   67.5
## 482  68.2   67.5
## 483  68.2   67.5
## 484  68.2   67.5
## 485  68.2   67.5
## 486  68.2   67.5
## 487  68.2   67.5
## 488  68.2   67.5
## 489  68.2   67.5
## 490  68.2   67.5
## 491  68.2   67.5
## 492  68.2   67.5
## 493  68.2   67.5
## 494  68.2   67.5
## 495  68.2   67.5
## 496  68.2   67.5
## 497  68.2   67.5
## 498  68.2   67.5
## 499  68.2   67.5
## 500  68.2   67.5
## 501  68.2   67.5
## 502  68.2   67.5
## 503  68.2   67.5
## 504  68.2   67.5
## 505  68.2   66.5
## 506  68.2   66.5
## 507  68.2   66.5
## 508  68.2   66.5
## 509  68.2   66.5
## 510  68.2   66.5
## 511  68.2   66.5
## 512  68.2   66.5
## 513  68.2   66.5
## 514  68.2   66.5
## 515  68.2   66.5
## 516  68.2   66.5
## 517  68.2   66.5
## 518  68.2   66.5
## 519  68.2   65.5
## 520  68.2   65.5
## 521  68.2   65.5
## 522  68.2   65.5
## 523  68.2   65.5
## 524  68.2   65.5
## 525  68.2   65.5
## 526  68.2   64.0
## 527  69.2   72.5
## 528  69.2   72.5
## 529  69.2   71.5
## 530  69.2   71.5
## 531  69.2   71.5
## 532  69.2   71.5
## 533  69.2   71.5
## 534  69.2   70.5
## 535  69.2   70.5
## 536  69.2   70.5
## 537  69.2   70.5
## 538  69.2   70.5
## 539  69.2   70.5
## 540  69.2   70.5
## 541  69.2   70.5
## 542  69.2   70.5
## 543  69.2   70.5
## 544  69.2   70.5
## 545  69.2   70.5
## 546  69.2   70.5
## 547  69.2   70.5
## 548  69.2   70.5
## 549  69.2   70.5
## 550  69.2   70.5
## 551  69.2   70.5
## 552  69.2   69.5
## 553  69.2   69.5
## 554  69.2   69.5
## 555  69.2   69.5
## 556  69.2   69.5
## 557  69.2   69.5
## 558  69.2   69.5
## 559  69.2   69.5
## 560  69.2   69.5
## 561  69.2   69.5
## 562  69.2   69.5
## 563  69.2   69.5
## 564  69.2   69.5
## 565  69.2   69.5
## 566  69.2   69.5
## 567  69.2   69.5
## 568  69.2   69.5
## 569  69.2   69.5
## 570  69.2   69.5
## 571  69.2   69.5
## 572  69.2   69.5
## 573  69.2   69.5
## 574  69.2   69.5
## 575  69.2   69.5
## 576  69.2   69.5
## 577  69.2   69.5
## 578  69.2   69.5
## 579  69.2   69.5
## 580  69.2   69.5
## 581  69.2   69.5
## 582  69.2   69.5
## 583  69.2   69.5
## 584  69.2   69.5
## 585  69.2   68.5
## 586  69.2   68.5
## 587  69.2   68.5
## 588  69.2   68.5
## 589  69.2   68.5
## 590  69.2   68.5
## 591  69.2   68.5
## 592  69.2   68.5
## 593  69.2   68.5
## 594  69.2   68.5
## 595  69.2   68.5
## 596  69.2   68.5
## 597  69.2   68.5
## 598  69.2   68.5
## 599  69.2   68.5
## 600  69.2   68.5
## 601  69.2   68.5
## 602  69.2   68.5
## 603  69.2   68.5
## 604  69.2   68.5
## 605  69.2   68.5
## 606  69.2   68.5
## 607  69.2   68.5
## 608  69.2   68.5
## 609  69.2   68.5
## 610  69.2   68.5
## 611  69.2   68.5
## 612  69.2   68.5
## 613  69.2   68.5
## 614  69.2   68.5
## 615  69.2   68.5
## 616  69.2   68.5
## 617  69.2   68.5
## 618  69.2   68.5
## 619  69.2   68.5
## 620  69.2   68.5
## 621  69.2   68.5
## 622  69.2   68.5
## 623  69.2   68.5
## 624  69.2   68.5
## 625  69.2   68.5
## 626  69.2   68.5
## 627  69.2   68.5
## 628  69.2   68.5
## 629  69.2   68.5
## 630  69.2   68.5
## 631  69.2   68.5
## 632  69.2   68.5
## 633  69.2   67.5
## 634  69.2   67.5
## 635  69.2   67.5
## 636  69.2   67.5
## 637  69.2   67.5
## 638  69.2   67.5
## 639  69.2   67.5
## 640  69.2   67.5
## 641  69.2   67.5
## 642  69.2   67.5
## 643  69.2   67.5
## 644  69.2   67.5
## 645  69.2   67.5
## 646  69.2   67.5
## 647  69.2   67.5
## 648  69.2   67.5
## 649  69.2   67.5
## 650  69.2   67.5
## 651  69.2   67.5
## 652  69.2   67.5
## 653  69.2   67.5
## 654  69.2   67.5
## 655  69.2   67.5
## 656  69.2   67.5
## 657  69.2   67.5
## 658  69.2   67.5
## 659  69.2   67.5
## 660  69.2   67.5
## 661  69.2   67.5
## 662  69.2   67.5
## 663  69.2   67.5
## 664  69.2   67.5
## 665  69.2   67.5
## 666  69.2   67.5
## 667  69.2   67.5
## 668  69.2   67.5
## 669  69.2   67.5
## 670  69.2   67.5
## 671  69.2   66.5
## 672  69.2   66.5
## 673  69.2   66.5
## 674  69.2   66.5
## 675  69.2   66.5
## 676  69.2   66.5
## 677  69.2   66.5
## 678  69.2   66.5
## 679  69.2   66.5
## 680  69.2   66.5
## 681  69.2   66.5
## 682  69.2   66.5
## 683  69.2   66.5
## 684  69.2   65.5
## 685  69.2   65.5
## 686  69.2   65.5
## 687  69.2   65.5
## 688  69.2   65.5
## 689  69.2   65.5
## 690  69.2   65.5
## 691  69.2   64.5
## 692  69.2   64.5
## 693  69.2   64.0
## 694  70.2   72.5
## 695  70.2   71.5
## 696  70.2   71.5
## 697  70.2   71.5
## 698  70.2   71.5
## 699  70.2   71.5
## 700  70.2   71.5
## 701  70.2   71.5
## 702  70.2   71.5
## 703  70.2   71.5
## 704  70.2   71.5
## 705  70.2   70.5
## 706  70.2   70.5
## 707  70.2   70.5
## 708  70.2   70.5
## 709  70.2   70.5
## 710  70.2   70.5
## 711  70.2   70.5
## 712  70.2   70.5
## 713  70.2   70.5
## 714  70.2   70.5
## 715  70.2   70.5
## 716  70.2   70.5
## 717  70.2   70.5
## 718  70.2   70.5
## 719  70.2   69.5
## 720  70.2   69.5
## 721  70.2   69.5
## 722  70.2   69.5
## 723  70.2   69.5
## 724  70.2   69.5
## 725  70.2   69.5
## 726  70.2   69.5
## 727  70.2   69.5
## 728  70.2   69.5
## 729  70.2   69.5
## 730  70.2   69.5
## 731  70.2   69.5
## 732  70.2   69.5
## 733  70.2   69.5
## 734  70.2   69.5
## 735  70.2   69.5
## 736  70.2   69.5
## 737  70.2   69.5
## 738  70.2   69.5
## 739  70.2   69.5
## 740  70.2   69.5
## 741  70.2   69.5
## 742  70.2   69.5
## 743  70.2   69.5
## 744  70.2   68.5
## 745  70.2   68.5
## 746  70.2   68.5
## 747  70.2   68.5
## 748  70.2   68.5
## 749  70.2   68.5
## 750  70.2   68.5
## 751  70.2   68.5
## 752  70.2   68.5
## 753  70.2   68.5
## 754  70.2   68.5
## 755  70.2   68.5
## 756  70.2   68.5
## 757  70.2   68.5
## 758  70.2   68.5
## 759  70.2   68.5
## 760  70.2   68.5
## 761  70.2   68.5
## 762  70.2   68.5
## 763  70.2   68.5
## 764  70.2   68.5
## 765  70.2   67.5
## 766  70.2   67.5
## 767  70.2   67.5
## 768  70.2   67.5
## 769  70.2   67.5
## 770  70.2   67.5
## 771  70.2   67.5
## 772  70.2   67.5
## 773  70.2   67.5
## 774  70.2   67.5
## 775  70.2   67.5
## 776  70.2   67.5
## 777  70.2   67.5
## 778  70.2   67.5
## 779  70.2   67.5
## 780  70.2   67.5
## 781  70.2   67.5
## 782  70.2   67.5
## 783  70.2   67.5
## 784  70.2   66.5
## 785  70.2   66.5
## 786  70.2   66.5
## 787  70.2   66.5
## 788  70.2   65.5
## 789  70.2   65.5
## 790  70.2   65.5
## 791  70.2   65.5
## 792  70.2   65.5
## 793  71.2   72.5
## 794  71.2   72.5
## 795  71.2   71.5
## 796  71.2   71.5
## 797  71.2   71.5
## 798  71.2   71.5
## 799  71.2   70.5
## 800  71.2   70.5
## 801  71.2   70.5
## 802  71.2   70.5
## 803  71.2   70.5
## 804  71.2   70.5
## 805  71.2   70.5
## 806  71.2   69.5
## 807  71.2   69.5
## 808  71.2   69.5
## 809  71.2   69.5
## 810  71.2   69.5
## 811  71.2   69.5
## 812  71.2   69.5
## 813  71.2   69.5
## 814  71.2   69.5
## 815  71.2   69.5
## 816  71.2   69.5
## 817  71.2   69.5
## 818  71.2   69.5
## 819  71.2   69.5
## 820  71.2   69.5
## 821  71.2   69.5
## 822  71.2   69.5
## 823  71.2   69.5
## 824  71.2   69.5
## 825  71.2   69.5
## 826  71.2   68.5
## 827  71.2   68.5
## 828  71.2   68.5
## 829  71.2   68.5
## 830  71.2   68.5
## 831  71.2   68.5
## 832  71.2   68.5
## 833  71.2   68.5
## 834  71.2   68.5
## 835  71.2   68.5
## 836  71.2   68.5
## 837  71.2   68.5
## 838  71.2   68.5
## 839  71.2   68.5
## 840  71.2   68.5
## 841  71.2   68.5
## 842  71.2   68.5
## 843  71.2   68.5
## 844  71.2   67.5
## 845  71.2   67.5
## 846  71.2   67.5
## 847  71.2   67.5
## 848  71.2   67.5
## 849  71.2   67.5
## 850  71.2   67.5
## 851  71.2   67.5
## 852  71.2   67.5
## 853  71.2   67.5
## 854  71.2   67.5
## 855  71.2   65.5
## 856  71.2   65.5
## 857  72.2   73.0
## 858  72.2   72.5
## 859  72.2   72.5
## 860  72.2   72.5
## 861  72.2   72.5
## 862  72.2   72.5
## 863  72.2   72.5
## 864  72.2   72.5
## 865  72.2   71.5
## 866  72.2   71.5
## 867  72.2   71.5
## 868  72.2   71.5
## 869  72.2   71.5
## 870  72.2   71.5
## 871  72.2   71.5
## 872  72.2   71.5
## 873  72.2   71.5
## 874  72.2   70.5
## 875  72.2   70.5
## 876  72.2   70.5
## 877  72.2   70.5
## 878  72.2   69.5
## 879  72.2   69.5
## 880  72.2   69.5
## 881  72.2   69.5
## 882  72.2   69.5
## 883  72.2   69.5
## 884  72.2   69.5
## 885  72.2   69.5
## 886  72.2   69.5
## 887  72.2   69.5
## 888  72.2   69.5
## 889  72.2   68.5
## 890  72.2   68.5
## 891  72.2   68.5
## 892  72.2   68.5
## 893  72.2   67.5
## 894  72.2   67.5
## 895  72.2   67.5
## 896  72.2   67.5
## 897  72.2   65.5
## 898  73.2   73.0
## 899  73.2   73.0
## 900  73.2   73.0
## 901  73.2   72.5
## 902  73.2   72.5
## 903  73.2   71.5
## 904  73.2   71.5
## 905  73.2   70.5
## 906  73.2   70.5
## 907  73.2   70.5
## 908  73.2   69.5
## 909  73.2   69.5
## 910  73.2   69.5
## 911  73.2   69.5
## 912  73.2   68.5
## 913  73.2   68.5
## 914  73.2   68.5
## 915  73.7   72.5
## 916  73.7   72.5
## 917  73.7   72.5
## 918  73.7   72.5
## 919  73.7   71.5
## 920  73.7   71.5
## 921  73.7   70.5
## 922  73.7   70.5
## 923  73.7   70.5
## 924  73.7   69.5
## 925  73.7   69.5
## 926  73.7   69.5
## 927  73.7   69.5
## 928  73.7   69.5
x = galton$parent
y = galton$parent
x[1 : 10]
##  [1] 70.5 68.5 65.5 64.5 64.0 67.5 67.5 67.5 66.5 66.5
y[1 : 10]
##  [1] 70.5 68.5 65.5 64.5 64.0 67.5 67.5 67.5 66.5 66.5
xc = x - mean(x)
xc = x - mean(x, na.rm = TRUE)
mean(xc)
## [1] 9.775954e-16

3. Rescale the parent and child variables and verify that the scaled variable standard deviations are 1.

x/sd(x)
##   [1] 39.44424 38.32525 36.64677 36.08728 35.80753 37.76576 37.76576 37.76576
##   [9] 37.20626 37.20626 37.20626 36.08728 39.44424 38.88474 38.32525 38.32525
##  [17] 38.32525 38.32525 38.32525 38.32525 38.32525 37.76576 37.76576 37.76576
##  [25] 37.76576 37.76576 37.20626 37.20626 37.20626 36.64677 36.64677 36.64677
##  [33] 36.64677 36.64677 36.64677 36.64677 36.64677 36.64677 36.08728 36.08728
##  [41] 36.08728 36.08728 35.80753 35.80753 38.88474 38.88474 38.88474 38.88474
##  [49] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
##  [57] 38.88474 38.88474 38.88474 38.88474 38.32525 38.32525 38.32525 38.32525
##  [65] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 37.76576
##  [73] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
##  [81] 37.76576 37.76576 37.76576 37.76576 37.76576 37.20626 37.20626 37.20626
##  [89] 37.20626 37.20626 36.64677 36.64677 36.64677 36.64677 36.64677 36.08728
##  [97] 36.08728 36.08728 36.08728 35.80753 35.80753 35.80753 35.80753 40.00373
## [105] 39.44424 38.88474 38.88474 38.88474 38.88474 38.32525 38.32525 38.32525
## [113] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [121] 38.32525 38.32525 38.32525 38.32525 38.32525 37.76576 37.76576 37.76576
## [129] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [137] 37.76576 37.76576 37.76576 37.76576 37.20626 37.20626 36.64677 36.64677
## [145] 36.64677 36.64677 36.64677 36.64677 36.64677 36.08728 35.80753 40.00373
## [153] 40.00373 40.00373 39.44424 38.88474 38.88474 38.88474 38.88474 38.88474
## [161] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [169] 38.88474 38.88474 38.88474 38.88474 38.32525 38.32525 38.32525 38.32525
## [177] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [185] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [193] 38.32525 38.32525 38.32525 38.32525 38.32525 37.76576 37.76576 37.76576
## [201] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [209] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [217] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [225] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [233] 37.76576 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626
## [241] 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626
## [249] 37.20626 37.20626 36.64677 36.64677 36.64677 36.64677 36.64677 36.64677
## [257] 36.64677 36.64677 36.64677 36.64677 36.64677 36.08728 36.08728 36.08728
## [265] 36.08728 36.08728 35.80753 35.80753 40.00373 40.00373 40.00373 40.00373
## [273] 39.44424 39.44424 39.44424 38.88474 38.88474 38.88474 38.88474 38.88474
## [281] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [289] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [297] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.32525 38.32525
## [305] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [313] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [321] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [329] 38.32525 38.32525 38.32525 38.32525 38.32525 37.76576 37.76576 37.76576
## [337] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [345] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [353] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [361] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [369] 37.76576 37.76576 37.76576 37.20626 37.20626 37.20626 37.20626 37.20626
## [377] 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626
## [385] 37.20626 37.20626 37.20626 37.20626 36.64677 36.64677 36.64677 36.64677
## [393] 36.64677 36.64677 36.64677 36.64677 36.64677 36.64677 36.64677 36.08728
## [401] 36.08728 36.08728 36.08728 36.08728 35.80753 35.80753 40.56322 40.00373
## [409] 40.00373 40.00373 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424
## [417] 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424 38.88474 38.88474
## [425] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [433] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [441] 38.88474 38.88474 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [449] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [457] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [465] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [473] 38.32525 38.32525 38.32525 38.32525 37.76576 37.76576 37.76576 37.76576
## [481] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [489] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [497] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [505] 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626
## [513] 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 36.64677 36.64677
## [521] 36.64677 36.64677 36.64677 36.64677 36.64677 35.80753 40.56322 40.56322
## [529] 40.00373 40.00373 40.00373 40.00373 40.00373 39.44424 39.44424 39.44424
## [537] 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424
## [545] 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424 38.88474
## [553] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [561] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [569] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [577] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [585] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [593] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [601] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [609] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [617] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [625] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [633] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [641] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [649] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [657] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [665] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.20626 37.20626
## [673] 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626 37.20626
## [681] 37.20626 37.20626 37.20626 36.64677 36.64677 36.64677 36.64677 36.64677
## [689] 36.64677 36.64677 36.08728 36.08728 35.80753 40.56322 40.00373 40.00373
## [697] 40.00373 40.00373 40.00373 40.00373 40.00373 40.00373 40.00373 40.00373
## [705] 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424
## [713] 39.44424 39.44424 39.44424 39.44424 39.44424 39.44424 38.88474 38.88474
## [721] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [729] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [737] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.32525
## [745] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [753] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [761] 38.32525 38.32525 38.32525 38.32525 37.76576 37.76576 37.76576 37.76576
## [769] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576
## [777] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 37.20626
## [785] 37.20626 37.20626 37.20626 36.64677 36.64677 36.64677 36.64677 36.64677
## [793] 40.56322 40.56322 40.00373 40.00373 40.00373 40.00373 39.44424 39.44424
## [801] 39.44424 39.44424 39.44424 39.44424 39.44424 38.88474 38.88474 38.88474
## [809] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [817] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [825] 38.88474 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [833] 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525 38.32525
## [841] 38.32525 38.32525 38.32525 37.76576 37.76576 37.76576 37.76576 37.76576
## [849] 37.76576 37.76576 37.76576 37.76576 37.76576 37.76576 36.64677 36.64677
## [857] 40.84297 40.56322 40.56322 40.56322 40.56322 40.56322 40.56322 40.56322
## [865] 40.00373 40.00373 40.00373 40.00373 40.00373 40.00373 40.00373 40.00373
## [873] 40.00373 39.44424 39.44424 39.44424 39.44424 38.88474 38.88474 38.88474
## [881] 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474 38.88474
## [889] 38.32525 38.32525 38.32525 38.32525 37.76576 37.76576 37.76576 37.76576
## [897] 36.64677 40.84297 40.84297 40.84297 40.56322 40.56322 40.00373 40.00373
## [905] 39.44424 39.44424 39.44424 38.88474 38.88474 38.88474 38.88474 38.32525
## [913] 38.32525 38.32525 40.56322 40.56322 40.56322 40.56322 40.00373 40.00373
## [921] 39.44424 39.44424 39.44424 38.88474 38.88474 38.88474 38.88474 38.88474
xs = x/sd(x)
sd(xs)
## [1] 1

4. Normalize the parental and child heights. Verify that the normalized variables have mean 0 and standard deviation 1 and take the correlation between them.

x[1:10]
##  [1] 70.5 68.5 65.5 64.5 64.0 67.5 67.5 67.5 66.5 66.5
y[1:10]
##  [1] 70.5 68.5 65.5 64.5 64.0 67.5 67.5 67.5 66.5 66.5
xn = (x - mean(x))/sd(x)
yn = (y - mean(y))/sd(y)
mean(xn)
## [1] 5.501733e-16
mean(yn)
## [1] 5.501733e-16
sd(xn)
## [1] 1
sd(yn)
## [1] 1
cor(xn, yn)
## [1] 1
cor(x, y)
## [1] 1
xn[1]
## [1] 1.226302
yn[1]
## [1] 1.226302

Pages 21 and 22

1. Install and load the package UsingR and load the father.son data with data(father.son). Get the linear regression fit where the son’s height is the outcome and the father’s height is the predictor. Give the intercept and the slope, plot the data and overlay the fitted regression line.

library(UsingR)
data(father.son)
fit = lm(sheight ~ fheight, data = father.son)
y = father.son$sheight
x = father.son$fheight
b1 = cor(y,x) * sd(y) / sd(x)
b0 = mean(y) - b1 * mean(x)
rbind(coef(fit), c(b0,b1))
##      (Intercept)  fheight
## [1,]     33.8866 0.514093
## [2,]     33.8866 0.514093
library(ggplot2)
g = ggplot(father.son, aes(x=fheight, y=sheight))
g = g + geom_point()
g = g + geom_smooth(method = lm, se = FALSE, lwd = 2)
g
## `geom_smooth()` using formula = 'y ~ x'

#### 2. Refer to problem 1. Center the father and son variables and refit the model omitting the intercept. Verify that the slope estimate is the same as the linear regression fit from problem 1.

fit = lm(sheight ~ fheight, data = father.son)
summary(fit)
## 
## Call:
## lm(formula = sheight ~ fheight, data = father.son)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.8772 -1.5144 -0.0079  1.6285  8.9685 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 33.88660    1.83235   18.49   <2e-16 ***
## fheight      0.51409    0.02705   19.01   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.437 on 1076 degrees of freedom
## Multiple R-squared:  0.2513, Adjusted R-squared:  0.2506 
## F-statistic: 361.2 on 1 and 1076 DF,  p-value: < 2.2e-16
xc = x  - mean(x)
yc = y - mean(y)
sum(xc*yc)/sum(xc^2)
## [1] 0.514093
lm(yc~xc - 1)
## 
## Call:
## lm(formula = yc ~ xc - 1)
## 
## Coefficients:
##     xc  
## 0.5141

3. Refer to problem 1. Normalize the father and son data and see that the fitted slope is the correlation.

xn = (x - mean(x))/sd(x)
yn = (y - mean(y))/sd(y)
lm(yn~xn)
## 
## Call:
## lm(formula = yn ~ xn)
## 
## Coefficients:
## (Intercept)           xn  
##   1.820e-15    5.013e-01
cor(xn, yn)
## [1] 0.5013383
lm(xn~yn)
## 
## Call:
## lm(formula = xn ~ yn)
## 
## Coefficients:
## (Intercept)           yn  
##  -2.216e-15    5.013e-01

4. Go back to the linear regression line from Problem 1. If a father’s height was 63 inches, what would you predict the son’s height to be?

fit = lm(sheight ~ fheight, data = father.son)
summary(fit)
## 
## Call:
## lm(formula = sheight ~ fheight, data = father.son)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.8772 -1.5144 -0.0079  1.6285  8.9685 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 33.88660    1.83235   18.49   <2e-16 ***
## fheight      0.51409    0.02705   19.01   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.437 on 1076 degrees of freedom
## Multiple R-squared:  0.2513, Adjusted R-squared:  0.2506 
## F-statistic: 361.2 on 1 and 1076 DF,  p-value: < 2.2e-16
predict(fit, newdata = data.frame(fheight = 63))
##        1 
## 66.27447
coef(fit)
## (Intercept)     fheight 
##   33.886604    0.514093
b0 = coef(fit)[1]; b1 = coef(fit)[2]
b0 + b1 * 63
## (Intercept) 
##    66.27447

5. Consider a data set where the standard deviation of the outcome variable is double that of the predictor. Also, the variables have a correlation of 0.3. If you fit a linear regression model, what would be the estimate of the slope?

Cor(y,x) = 0.3
sd(y)= 2sd(x) = sd(y)/sd(x) = 2
Cor(y,x)sd(y)/sd(x) = (0.3)2 = 0.6

6. Consider the previous problem. The outcome variable has a mean of 1 and the predictor has a mean of 0.5. What would be the intercept?

B₀ = Ȳ-B₁X̄ = 1-(0.6)(0.5)=0.7

7. True or false, if the predictor variable has mean 0, the estimated intercept from linear regression will be the mean of the outcome?

B̂₀ = Ȳ-B̂₁X̄=Ȳ

8. Consider problem 5 again. What would be the estimated slope if the predictor and outcome were reversed?

Cor(y,x) = sd(x)/sd(y) = (0.3)1/2 = 0.15

Page 26

1. You have two noisy scales and a bunch of people that you’d like to weigh. You weigh each person on both scales. The correlation was 0.75. If you normalized each set of weights, what would you have to multiply the weight on one scale to get a good estimate of the weight on the other scale?

2. Consider the previous problem. Someone’s weight was 2 standard deviations above the mean of the group on the first scale. How many standard deviations above the mean would you estimate them to be on the second?

2 x 0.75 = 1.5

3. You ask a collection of husbands and wives to guess how many jellybeans are in a jar. The correlation is 0.2. The standard deviation for the husbands is 10 beans while the standard deviation for wives is 8 beans. Assume that the data were centered so that 0 is the mean for each. The centered guess for a husband was 30 beans (above the mean). What would be your best estimate of the wife’s guess?

(0.2)8/10 = 0.16
0.16 x 30 = 4.18

Pages 32 and 33

1. Fit a linear regression model to the father.son dataset with the father as the predictor and the son as the outcome. Give a p-value for the slope coefficient and perform the relevant hypothesis test.

fit = lm(sheight ~ fheight, data = father.son)
summary(fit)$coef
##              Estimate Std. Error  t value     Pr(>|t|)
## (Intercept) 33.886604 1.83235382 18.49348 1.604044e-66
## fheight      0.514093 0.02704874 19.00618 1.121268e-69
SH = B₀ +B₁ FH+ε
H₀:B₁ = 0
✔Hₐ:B₁ ≠ 0

2. Refer to question 1. Interpret both parameters. Recenter for the intercept if necessary.

fit = lm(sheight ~ I(fheight - mean(fheight)), data = father.son)
summary(fit)$coef
##                             Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)                68.684070 0.07421078 925.52689 0.000000e+00
## I(fheight - mean(fheight))  0.514093 0.02704874  19.00618 1.121268e-69

3. Refer to question 1. Predict the son’s height if the father’s height is 80 inches. Would you recommend this prediction? Why or why not?

fit = lm(sheight ~ fheight, data = father.son)
predict(fit, newdata = data.frame(fheight = 80))
##        1 
## 75.01405
summary(father.son)
##     fheight         sheight     
##  Min.   :59.01   Min.   :58.51  
##  1st Qu.:65.79   1st Qu.:66.93  
##  Median :67.77   Median :68.62  
##  Mean   :67.69   Mean   :68.68  
##  3rd Qu.:69.60   3rd Qu.:70.47  
##  Max.   :75.43   Max.   :78.36

4. Load the mtcars dataset. Fit a linear regression with miles per gallon as the outcome and horsepower as the predictor. Interpret your coefficients, recenter for the intercept if necessary.

data(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
summary(lm(mpg ~ hp, data = mtcars))$coef
##                Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 30.09886054  1.6339210 18.421246 6.642736e-18
## hp          -0.06822828  0.0101193 -6.742389 1.787835e-07
summary(lm(mpg ~ I(hp - mean(hp)), data = mtcars))$coef
##                     Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)      20.09062500  0.6828817 29.420360 1.101810e-23
## I(hp - mean(hp)) -0.06822828  0.0101193 -6.742389 1.787835e-07

5. Refer to question 4. Overlay the fit onto a scatterplot.

library(ggplot2)
g = ggplot(mtcars, aes(x=hp, y=mpg))
g = g + geom_point(cex = 5, alpha = 0.5)
g = g + geom_smooth(method = lm, se = FALSE, lwd = 2)
g
## `geom_smooth()` using formula = 'y ~ x'

6. Refer to question 4. Test the hypothesis of no linear relationship between horsepower and miles per gallon.

summary(lm(mpg ~ hp, data = mtcars))$coef
##                Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 30.09886054  1.6339210 18.421246 6.642736e-18
## hp          -0.06822828  0.0101193 -6.742389 1.787835e-07

7. Refer to question 4. Predict the miles per gallon for a horsepower of 111.

fit = lm(mpg ~ hp, data = mtcars)
summary(fit)
## 
## Call:
## lm(formula = mpg ~ hp, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7121 -2.1122 -0.8854  1.5819  8.2360 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 30.09886    1.63392  18.421  < 2e-16 ***
## hp          -0.06823    0.01012  -6.742 1.79e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.863 on 30 degrees of freedom
## Multiple R-squared:  0.6024, Adjusted R-squared:  0.5892 
## F-statistic: 45.46 on 1 and 30 DF,  p-value: 1.788e-07
predict(fit, newdata = data.frame(hp = 111))
##        1 
## 22.52552
30.09886 + -0.06823 * 111
## [1] 22.52533

Page 45

1. Fit a linear regression model to the father.son dataset with the father as the predictor and the son as the outcome. Plot the son’s height (horizontal axis) versus the residuals (vertical axis).

fit = lm(sheight ~ fheight, data = father.son)
plot(fit)

plot(father.son$fheight, resid(fit))
abline(v = 0, col = "red")

####2. Refer to question 1. Directly estimate the residual variance and compare this estimate to the output of lm.

fit = lm(sheight ~ fheight, data = father.son)
sum(resid(fit))
## [1] 1.148526e-13
sum(resid(fit)^2)/(nrow(father.son)-2)
## [1] 5.936804
summary(fit)$sigma^2
## [1] 5.936804

3. Refer to question 1. Give the R squared for this model.

fit = lm(sheight ~ fheight, data = father.son)
summary(fit)
## 
## Call:
## lm(formula = sheight ~ fheight, data = father.son)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.8772 -1.5144 -0.0079  1.6285  8.9685 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 33.88660    1.83235   18.49   <2e-16 ***
## fheight      0.51409    0.02705   19.01   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.437 on 1076 degrees of freedom
## Multiple R-squared:  0.2513, Adjusted R-squared:  0.2506 
## F-statistic: 361.2 on 1 and 1076 DF,  p-value: < 2.2e-16
summary(fit)$r.squared
## [1] 0.2513401

4. Load the mtcars dataset. Fit a linear regression with miles per gallon as the outcome and horsepower as the predictor. Plot horsepower versus the residuals.

fit = lm(mpg ~ hp, data = mtcars)
temp = mtcars; temp$resid = resid(fit)
g = ggplot(temp, aes(x = hp, y = resid))
g = g + geom_hline(yintercept = 0, col = "red")
g = g + geom_point(alpha =  0.5, cex = 5)
g 

qplot(hp, mpg, data = mtcars)
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#### 5. Refer to question 4. Directly estimate the residual variance and compare this estimate to the output of lm.

fit = lm(mpg ~ hp, data = mtcars)
sum(resid(fit)^2)/(nrow(mtcars)-2)
## [1] 14.92248
summary(fit)$sigma^2
## [1] 14.92248

6. Refer to question 4. Give the R squared for this model.

summary(fit)
## 
## Call:
## lm(formula = mpg ~ hp, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7121 -2.1122 -0.8854  1.5819  8.2360 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 30.09886    1.63392  18.421  < 2e-16 ***
## hp          -0.06823    0.01012  -6.742 1.79e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.863 on 30 degrees of freedom
## Multiple R-squared:  0.6024, Adjusted R-squared:  0.5892 
## F-statistic: 45.46 on 1 and 30 DF,  p-value: 1.788e-07