The Human Freedom Index is a report that attempts to summarize the idea of “freedom” through a bunch of different variables for many countries around the globe. It serves as a rough objective measure for the relationships between the different types of freedom - whether it’s political, religious, economical or personal freedom - and other social and economic circumstances. The Human Freedom Index is an annually co-published report by the Cato Institute, the Fraser Institute, and the Liberales Institut at the Friedrich Naumann Foundation for Freedom.

In this lab, you’ll be analyzing data from Human Freedom Index reports from 2008-2016. Your aim will be to summarize a few of the relationships within the data both graphically and numerically in order to find which variables can help tell a story about freedom.

Getting Started

Load packages

In this lab, you will explore and visualize the data using the tidyverse suite of packages. The data can be found in the companion package for OpenIntro resources, openintro.

Let’s load the packages.

library(tidyverse)
library(openintro)
library(devtools)
library(DATA606)
## 
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics 
## This package is designed to support this course. The text book used 
## is OpenIntro Statistics, 4th Edition. You can read this by typing 
## vignette('os4') or visit www.OpenIntro.org. 
##  
## The getLabs() function will return a list of the labs available. 
##  
## The demo(package='DATA606') will list the demos that are available.
library(dplyr)
library(ggplot2)
library(tidyr)
devtools::install_github("jbryer/DATA606")
data('hfi', package='openintro')

The data

The data we’re working with is in the openintro package and it’s called hfi, short for Human Freedom Index.

  1. What are the dimensions of the dataset?

(1458 x 123)

dim(hfi)
## [1] 1458  123
  1. What type of plot would you use to display the relationship between the personal freedom score, pf_score, and one of the other numerical variables? Plot this relationship using the variable pf_expression_control as the predictor. Does the relationship look linear? If you knew a country’s pf_expression_control, or its score out of 10, with 0 being the most, of political pressures and controls on media content, would you be comfortable using a linear model to predict the personal freedom score?

A scatterplot would be the best kind of plot to display the relationship between the personal freedom score and the personal control score. The relationship appears to be linear, as the data trends strongly positive.

In the case of 0 being the strong case of political pressure and control, I would not feel comfortable working with this set. This is because the data could be misleading when plotting this as a linear model.

hfi %>% ggplot(aes(x = pf_expression_control, y = pf_score)) + geom_point()

hfi$pf_score
##    [1] 7.596281 5.281772 6.111324 8.099696 6.912804 9.184438 9.246948 5.676553
##    [9] 7.454538 6.136070 5.302600 7.706894 6.059028 8.987179 7.430864 7.496976
##   [17] 6.600973 7.206770 7.861447 6.876334 6.665977 4.663700 8.155539 7.455340
##   [25] 4.414134 7.238448 5.330774 9.151727 7.986583 5.465632 5.509541 8.216035
##   [33] 5.350820 7.021513 4.947257 6.777868 8.165429 7.062542 8.456176 8.514394
##   [41] 9.029761 9.325640 6.942575 7.550555 3.894554 6.917902 9.013701 5.064090
##   [49] 7.792204 9.294368 8.766803 5.315575 5.302685 7.576536 9.235191 7.872178
##   [57] 7.946768 6.548246 5.371202 6.859722 7.041796 7.184443 6.386483 8.583680
##   [65] 8.258946 9.083506 6.195871 6.381312 4.532449 3.116028 8.939129 7.544916
##   [73] 8.690446 7.268416 8.733771 6.238667 6.375676 6.445537 8.766350 5.630574
##   [81] 6.247013 5.863586 8.851068 6.429994 6.688444 6.403402 3.880566 8.821339
##   [89] 9.257402 7.413658 6.835124 7.445214 5.902980 6.060929 8.977997 4.987750
##   [97] 7.717466 6.796020 7.059030 7.998313 7.630104 5.986757 6.659207 5.463361
##  [105] 7.394916 6.911611 9.398842 9.284819 6.432495 5.714520 5.823617 9.342481
##  [113] 5.509055 5.324592 7.715240 7.254949 6.974598 7.720787 6.497527 8.353120
##  [121] 9.043712 5.525553 8.653232 5.714382 6.467739 4.438732 6.774188 7.847943
##  [129] 7.372524 7.044659 7.479424 8.536500 8.822791 7.695144 8.758214 6.040722
##  [137] 4.246047 7.788563 6.020329 9.334750 9.185518 2.511654 9.041394 5.652325
##  [145] 6.126419 6.399563 6.186450 6.729938 6.920446 6.577472 6.092465 6.128889
##  [153] 6.586270 5.073350 8.995836 8.747310 8.296142 5.521449 5.968008 2.166555
##  [161] 6.007699 5.170726 7.587078 5.335310 6.132958 8.025096 6.871773 9.214745
##  [169] 9.379794 5.552377 7.502983 6.118902 5.292209 7.821851       NA 9.185713
##  [177] 7.094584 7.581713 6.662791 7.351932 8.003267 6.790132 6.704300 4.609116
##  [185] 8.238405 7.367716 4.241065 7.314548 5.127940 9.130112 8.361806 5.619156
##  [193] 5.634996 8.265002 5.374700 6.905774 4.819612 6.862956 8.255308 6.952270
##  [201] 8.521920 8.669154 9.038707 9.336046 7.023368 7.452677 3.852272 7.119972
##  [209] 8.963080 5.156317 7.701291 9.407869 8.658781 5.318685 5.460142 7.776962
##  [217] 9.290914 7.711281 8.071835 6.530605 5.560062 6.831116 6.949567 7.210987
##  [225] 6.397629 8.745162 8.442994 9.149527 6.204729 6.428120 4.492074       NA
##  [233] 9.041050 7.822677 8.717216 7.236812 8.706370 6.293274 6.421995 6.323389
##  [241] 8.796927 5.455369 6.211922 5.916014 8.816254 6.117908 6.681331 6.370389
##  [249] 3.974236 8.839368 9.329334 7.470899 6.885250 7.456857 5.970135 5.971989
##  [257] 9.041407 5.041825 7.781033 6.912513 7.101188 8.038873 7.982973 5.980245
##  [265] 6.779523 5.582742 7.331469 6.951082 9.366518 9.273780 6.362252 5.604470
##  [273] 5.630883 9.388992 5.592812 5.304687 7.706969 7.194145 6.999922 7.760073
##  [281] 6.675529 8.620769 9.041336 5.594931 8.714023 5.686519 6.551657 4.404408
##  [289] 6.842267 7.980960 7.746148 7.176240 7.613531 8.604980 8.955761 7.673184
##  [297] 8.821933 6.037550       NA 7.750407 6.129929 9.319149 9.258916 2.861653
##  [305] 9.053254 5.762052 6.191475 6.453236 5.610710 6.700934 7.039935 6.601278
##  [313] 6.473634 6.119589 6.288703 5.100455 9.047408 8.827570 8.390694 5.523500
##  [321] 6.014983 2.336884 6.128980 5.155279 7.750166 5.378657 6.107928 7.924290
##  [329] 6.915972 9.324684 9.380256 5.823368 7.422161 5.988579 5.494260 7.880064
##  [337]       NA 9.101087 6.983818 7.562763 6.722924 7.342117 8.042957 6.802452
##  [345] 6.959999 4.678174 8.327566 7.314257 4.638075 7.325478 5.543957 9.219105
##  [353] 8.006755 5.453477 5.994091 8.195178 5.519759 6.947464 4.827701 6.765420
##  [361] 8.617715 6.971078 8.478890 8.701781 9.019314 9.405681 7.291729 7.387582
##  [369] 4.301382 7.491165 9.000505 5.179382 7.477389 9.486705 8.934746 5.229237
##  [377] 5.777930 7.771882 9.302399 7.870900 8.143171 6.954735 5.368115 6.657558
##  [385] 6.945261 7.474011 6.553164 8.784911 8.567299 9.246634 6.279888 6.398716
##  [393] 4.231371       NA 8.795600 7.461034 8.703630 7.358998 8.771444 6.298162
##  [401] 6.469997 6.211527 8.855441 5.754312 6.254568 5.971988 8.711074 6.175405
##  [409] 6.563231 6.457114 4.216496 8.729086 9.362281 7.144729 7.221221 7.408412
##  [417] 5.839001 5.853400 8.871254 5.129175 7.844266 6.992210 7.126151 8.097245
##  [425] 8.101436 6.086609 6.709491 5.796951 7.258532 7.086550 9.211358 9.362772
##  [433] 6.649156 5.878961 5.723368 9.526564 5.705957 5.045778 7.687835 6.951363
##  [441] 7.180369 7.565960 6.608041 8.833323 9.029433 5.684795 8.902438 5.972597
##  [449] 6.479574 4.723066 6.809732 8.060048 6.650740 7.287357 7.589062 8.656840
##  [457] 8.936067 7.499558 8.944538 5.754403       NA 7.263678 5.954273 9.253566
##  [465] 9.303864 3.077190 8.785285 5.968551 6.359931 6.567701 6.315424 6.671558
##  [473] 7.057789 6.665387 6.642302 6.159892 6.160398 5.278573 9.155805 8.850570
##  [481] 8.333961 5.867460 6.020682 2.519069 5.672964 5.305365 7.509193 5.146842
##  [489] 6.739678 8.261689 7.103881 9.167331 9.320219 5.972940 7.417698 5.742265
##  [497] 5.292128 7.901771       NA 9.041067 7.140413 7.375620 6.819091 7.647877
##  [505] 8.012689 6.516080 7.404761 5.940890 8.347520 7.700455 6.357713 7.192898
##  [513] 6.222504 9.225504 8.006589 4.890353 5.560528 8.108076 5.737562 6.470821
##  [521] 4.455993 6.779646 8.233029 6.680421 8.481888 8.634588 9.105149 9.519350
##  [529] 7.111907 7.626139 4.699170 7.207116 8.971011 5.857614 7.284248 9.456231
##  [537] 8.769471 6.189146 5.929838 7.758326 9.220923 7.918135 8.093893 6.617232
##  [545] 5.882375 6.185571 7.633296 7.118479 5.955376 8.983629 8.557804 9.249103
##  [553] 6.965229 6.931741 4.087617       NA 9.015572 7.864782 8.760080 7.166775
##  [561] 8.852600 6.120075 6.348148 6.349646 8.666065 6.187585 6.145123       NA
##  [569] 8.752911 6.065344 6.436335       NA 4.995930 8.672215 9.188029 7.943612
##  [577] 7.068669 7.513179 6.134829 5.974948 8.930327 5.348084 8.343368 6.790791
##  [585] 7.453997 8.105190 8.051799 6.243817 7.047283 5.031095 7.376982 7.414771
##  [593] 9.186037 9.257169 6.879121 6.216772 5.445457 9.560169 5.793055 4.790842
##  [601] 7.718085 6.895944 7.289304 7.453131 6.067960 8.891007 8.929406 5.689483
##  [609] 8.615619 6.269067 6.522739 4.361519 6.735443 7.626731 6.812436 6.378484
##  [617] 7.296985 8.757185 8.920481 7.203877 8.661795 5.834280       NA 7.285546
##  [625] 5.839567 9.271152 9.210524 4.120057 8.678233 6.625175 6.672414 6.880345
##  [633] 6.494581 5.939951 7.065859 5.926390 7.315975 6.095985 7.491269 5.236195
##  [641] 9.065507 8.634731 8.493607 6.707714 6.248651 3.295410 6.440869 5.395069
##  [649] 7.670382 5.244807 6.384360 8.098573 7.368595 9.155873 9.268544 6.001311
##  [657] 7.851875 5.848527 5.477917 8.037217       NA 9.033781 7.225155 7.102856
##  [665]       NA 7.602511 7.874977 6.408080 7.420760 6.590635 8.266662 7.441721
##  [673] 6.027682 7.343626 6.130546 9.206788 8.198471 4.728113 5.545431 8.629822
##  [681] 5.636779 6.395642 4.421329 6.279521 8.251476 6.270804 8.422989 8.507043
##  [689] 9.030405 9.487167 7.032943 7.332286 5.202573 7.196339 8.967745 5.507089
##  [697] 7.349026 9.440905 8.752234 6.092342 6.080054 7.378219 9.206540 7.627061
##  [705] 8.595543 6.583208       NA 5.950756 7.712620 7.048531 5.877339 9.038171
##  [713] 8.528692 9.270262 7.204515 6.921252 3.923565       NA 8.985665 7.770850
##  [721] 8.725522 6.980532 8.886950 6.213783 6.375663 6.344946 8.663341 6.097100
##  [729] 6.269650       NA 8.723644 6.375842 6.473314       NA       NA 8.683015
##  [737] 9.169140 8.261328 7.010602 7.604950 6.278362 5.943164 8.844048 5.827101
##  [745] 8.582331 6.635227 7.460099 8.023393 8.077698 6.413817 6.742088 5.100395
##  [753] 7.147315 7.271136 9.158976 9.099278 6.638626 6.565336 5.227435 9.568154
##  [761] 5.805576 4.926955 7.728516 6.275805 7.336954 7.691982 6.900288 8.910093
##  [769] 8.949400 5.254456 8.522985 6.303112 6.320318 4.380158 6.431042 7.472898
##  [777]       NA 6.024225 7.163451 8.758805 8.911970 7.157849 8.683587 5.721443
##  [785]       NA 7.623914 5.414325 9.448184 9.320210 4.463307 8.522833 6.736704
##  [793] 6.540723 7.186706 6.644455 5.501812 6.989158 6.140156 7.189370 5.762716
##  [801] 7.613971 4.865616 9.034968 8.622446 8.578156 6.631301 6.291366 3.381012
##  [809] 6.577187 5.109619 7.764088 5.362339 5.549902 8.130653 7.410680 9.182501
##  [817] 9.176560 6.085698 7.942748 6.101845 5.616966 8.028648       NA 9.071863
##  [825] 7.395807 7.173281       NA 7.560832 7.993419 6.544255 7.631088 6.655830
##  [833] 8.359973 7.547067 6.113624 7.508288 6.128264 9.265986 8.251562 4.879459
##  [841] 5.655279 8.670305 5.738971 6.528019 4.796654 6.300028 8.269709 6.026988
##  [849] 8.477837 8.657318 9.019294 9.474775 6.971973 7.476097 5.291870 6.835688
##  [857] 8.956664 5.618283 7.355811 9.479371 8.764232 6.159744 6.193399 7.456488
##  [865] 9.244978 7.647471 8.727118 6.571394       NA 6.251189 7.851696 7.189368
##  [873] 6.074197 9.065782 8.715524 9.303933 7.287817 7.008588 4.054964       NA
##  [881] 9.061253 7.935495 8.746320 7.000108 8.896011 6.201937 6.386743 6.553068
##  [889] 8.630075 6.226252 6.190304       NA 8.802701 6.687906 6.571596       NA
##  [897]       NA 8.735453 9.204896 8.351364 7.368192 7.467499 6.485079 6.268739
##  [905] 9.004442 5.879231 8.581384 6.763573 7.463974 7.981010 8.149663 6.459090
##  [913] 6.880181 5.118472 7.317269 7.480462 9.217586 9.193461 6.737904 6.686819
##  [921] 5.340308 9.365398 5.866622 5.087155 7.726443 6.358829 7.268874 7.746133
##  [929] 7.026289 9.098542 9.000258 5.232194 8.576851 6.381338 6.349864 4.435566
##  [937] 6.458420 7.491859       NA 6.011410 7.211082 8.803042 8.870551 7.243796
##  [945] 8.800843 5.948040       NA 7.730232 5.471813 9.442889 9.324332 5.078535
##  [953] 8.605639 6.818178 6.675361 7.423516 6.800981 5.613306 7.013735 6.153702
##  [961] 7.407545 5.832225 7.701820 4.839355 9.088762 8.712934 8.692857 6.754916
##  [969] 6.415083 3.231188 6.640517 5.171471 7.757250 5.208712 5.175390 8.181316
##  [977] 7.416263 9.265386 9.023048 6.652655 7.894137 6.321461 5.538410 8.029702
##  [985]       NA 9.097983 7.379314 6.779762       NA 7.384301 8.204545 6.799652
##  [993] 7.768350 6.377936 8.299025 7.584140 6.352196 7.746637 5.698524 9.270230
## [1001] 8.312047 5.387513 5.699936 8.652734 5.563644 6.883967 5.515803 6.665292
## [1009] 8.084008 5.816050 8.148327 8.938169 9.091980 9.488762 6.971664 7.411652
## [1017] 5.538519 7.228299 9.066637 5.347053 7.563436 9.500482 8.784171 6.060273
## [1025] 5.576208 8.136609 9.106744 7.581635 8.619477 7.273042       NA 6.331051
## [1033] 7.194589 7.021834 6.488554 9.036896 8.882872 9.193639 6.574172 7.162843
## [1041] 4.489005       NA 9.069072 8.476937 8.762426 7.224410 8.789731 5.935704
## [1049] 6.910899 6.464775 8.833318 6.244275 6.188584       NA 8.657641 6.453066
## [1057] 5.687221       NA       NA 8.664016 9.202756 8.165920 6.822954 7.271030
## [1065] 6.304836 6.526071 9.032154 5.798658 8.506716 6.777854 7.450517 7.579833
## [1073] 8.208571 6.393932 6.399148 6.007950 7.242411 7.347654 9.197257 9.428153
## [1081] 7.400991 7.194329 5.881826 9.561851 5.433485 4.833934 7.683680 7.021329
## [1089] 7.318168 7.599118 7.319929 8.929675 8.999700 4.960796 8.749530 6.493733
## [1097] 6.710543 4.606060 6.460770 7.728383       NA 6.475251 7.445439 8.776370
## [1105] 8.620039 6.798161 9.172400 6.570650       NA 7.852177 5.852716 9.438605
## [1113] 9.336283 5.148592 8.294907 7.535268 6.479170 7.748105 7.487092 5.799787
## [1121] 6.840402 5.687479 7.152000 5.606187 7.696264 5.861769 9.012806 8.711692
## [1129] 8.817575 6.353238 6.679871 4.730448 6.087383 5.358830 7.860267 5.139881
## [1137] 5.273795 8.175462 7.408322 9.290626 9.031761 6.293471 8.000515 6.751321
## [1145] 5.533021 8.178830       NA 9.110864 7.359349 6.872947       NA 7.540680
## [1153] 8.226588 6.838069 7.751098       NA 8.406205 7.432165 6.804418       NA
## [1161] 5.737553 9.267999       NA 5.610116 5.545487 8.628272 5.565873 6.795143
## [1169] 5.608591 6.399806 8.265653 5.899114 8.409353 8.921275 9.153649 9.484657
## [1177] 7.038389 7.379937 5.551570 6.922049 9.057999 5.216424 7.556942 9.510538
## [1185] 8.999746 6.084612       NA 8.145426 9.142769 7.628736 8.766923 7.184947
## [1193]       NA 6.461521 7.335984 7.073770 6.556598 9.015306 8.898743 9.245503
## [1201] 6.591647 7.198304 4.353862       NA 9.045519 7.851206 8.792230 7.294271
## [1209] 8.862743 6.019213 6.768123 6.420323 8.753504 6.266073 6.641372       NA
## [1217] 8.451635       NA 5.705277       NA       NA 8.692407 8.982547 8.077733
## [1225] 6.819999 7.336294 6.548766 6.330020 9.080232 5.869709 8.360905 7.135360
## [1233] 7.727687 7.561840 8.000304 6.463867 6.243995 6.502495 7.187442 7.567099
## [1241] 9.222489 9.325434 7.308308 7.051861 5.888859 9.420202 5.585305 5.070443
## [1249] 7.787263 6.779836 7.140153 7.573309 7.240387 8.990015 8.860807       NA
## [1257] 8.668488 6.465113 7.574418       NA 6.616525 7.878666       NA 6.374351
## [1265] 7.619521 8.817191 8.632398 6.806230 8.932053 5.914972       NA       NA
## [1273]       NA 9.441831 9.324517 5.269188 8.236077       NA 6.455916 7.835613
## [1281]       NA 5.793766 6.919811 5.820344 7.137345 5.460617 7.771996 5.563103
## [1289] 9.100353 8.571688 8.800819 6.207029 6.680303       NA 6.110227 5.355347
## [1297] 7.777698 5.146131 5.237056 8.190240 7.320561 9.292040 9.027407 6.593344
## [1305] 8.146710 6.780238 5.526272 8.135015       NA 9.064172 7.350668 6.990275
## [1313]       NA 7.367289 8.004875 6.821652 7.718818       NA 8.242904 7.568461
## [1321] 6.282285       NA 5.804578 9.266003       NA 5.783164 5.585024 8.654430
## [1329] 5.721288 6.925207 5.543686 6.570636 8.295889 5.893142 8.076770 8.948103
## [1337] 9.131337 9.494348 6.878054 7.416109 5.579720 7.268472 9.039304 5.367709
## [1345] 7.786614 9.497741 8.969965 5.940088       NA 7.542609 9.144099 7.607686
## [1353] 8.771018 7.188651       NA 6.576988 6.954978 7.122098 6.730025 9.062626
## [1361] 8.894809 9.214496 6.569895 7.184828 4.882248       NA 9.059166 7.590547
## [1369] 8.793881 7.286042 8.828608 5.949624 6.868879 6.464595 8.838646 6.157210
## [1377] 6.782040       NA 8.644295       NA 5.837221       NA       NA 8.580363
## [1385] 9.108486 8.237437 7.041446 7.273545 6.296281 6.297124 9.035816 5.810717
## [1393] 8.469659 7.187200 7.507970 7.560718 8.149117 6.362837 6.393893 6.050606
## [1401] 7.179960 7.397345 9.241021 9.421085 7.406305 6.954829 6.272717 9.554284
## [1409] 5.854085 4.833701 7.748312 7.058135 7.301651 7.494997 7.391257 8.953865
## [1417] 8.986276       NA 8.737953 6.531378 7.149149       NA 6.527743 7.915282
## [1425]       NA 6.490277 7.439441 8.866865 8.641525 6.804749 9.124498 5.460755
## [1433]       NA       NA       NA 9.504694 9.308873 5.295031 8.094654       NA
## [1441] 6.477212 7.748565       NA 5.856729 6.871200 5.818116 6.976818 5.780542
## [1449] 7.727395 5.889732 9.082842 8.726531 8.775693 6.295759 6.650413       NA
## [1457] 6.145449 5.321141

If the relationship looks linear, we can quantify the strength of the relationship with the correlation coefficient.

hfi %>%
  summarise(cor(pf_expression_control, pf_score, use = "complete.obs"))
## # A tibble: 1 x 1
##   `cor(pf_expression_control, pf_score, use = "complete.obs")`
##                                                          <dbl>
## 1                                                        0.796

Here, we set the use argument to “complete.obs” since there are some observations of NA.

Sum of squared residuals

In this section, you will use an interactive function to investigate what we mean by “sum of squared residuals”. You will need to run this function in your console, not in your markdown document. Running the function also requires that the hfi dataset is loaded in your environment.

Think back to the way that we described the distribution of a single variable. Recall that we discussed characteristics such as center, spread, and shape. It’s also useful to be able to describe the relationship of two numerical variables, such as pf_expression_control and pf_score above.

  1. Looking at your plot from the previous exercise, describe the relationship between these two variables. Make sure to discuss the form, direction, and strength of the relationship as well as any unusual observations.

The relationship has a strong positive correlation. There are several outliers, but it is not a large amount that could sway the trendline.

Just as you’ve used the mean and standard deviation to summarize a single variable, you can summarize the relationship between these two variables by finding the line that best follows their association. Use the following interactive function to select the line that you think does the best job of going through the cloud of points.

# This will only work interactively (i.e. will not show in the knitted document)
DATA606::plot_ss(x = na.omit(hfi$pf_expression_control), y = na.omit(hfi$pf_score))

After running this command, you’ll be prompted to click two points on the plot to define a line. Once you’ve done that, the line you specified will be shown in black and the residuals in blue. Note that there are 30 residuals, one for each of the 30 observations. Recall that the residuals are the difference between the observed values and the values predicted by the line:

\[ e_i = y_i - \hat{y}_i \]

The most common way to do linear regression is to select the line that minimizes the sum of squared residuals. To visualize the squared residuals, you can rerun the plot command and add the argument showSquares = TRUE.

Note that the output from the plot_ss function provides you with the slope and intercept of your line as well as the sum of squares.

  1. Using plot_ss, choose a line that does a good job of minimizing the sum of squares. Run the function several times. What was the smallest sum of squares that you got? How does it compare to your neighbors?

The linear model

It is rather cumbersome to try to get the correct least squares line, i.e. the line that minimizes the sum of squared residuals, through trial and error. Instead, you can use the lm function in R to fit the linear model (a.k.a. regression line).

m1 <- lm(pf_score ~ pf_expression_control, data = hfi)

The first argument in the function lm is a formula that takes the form y ~ x. Here it can be read that we want to make a linear model of pf_score as a function of pf_expression_control. The second argument specifies that R should look in the hfi data frame to find the two variables.

The output of lm is an object that contains all of the information we need about the linear model that was just fit. We can access this information using the summary function.

summary(m1)
## 
## Call:
## lm(formula = pf_score ~ pf_expression_control, data = hfi)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.8467 -0.5704  0.1452  0.6066  3.2060 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)            4.61707    0.05745   80.36   <2e-16 ***
## pf_expression_control  0.49143    0.01006   48.85   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8318 on 1376 degrees of freedom
##   (80 observations deleted due to missingness)
## Multiple R-squared:  0.6342, Adjusted R-squared:  0.634 
## F-statistic:  2386 on 1 and 1376 DF,  p-value: < 2.2e-16

Let’s consider this output piece by piece. First, the formula used to describe the model is shown at the top. After the formula you find the five-number summary of the residuals. The “Coefficients” table shown next is key; its first column displays the linear model’s y-intercept and the coefficient of at_bats. With this table, we can write down the least squares regression line for the linear model:

\[ \hat{y} = 4.61707 + 0.49143 \times pf\_expression\_control \]

One last piece of information we will discuss from the summary output is the Multiple R-squared, or more simply, \(R^2\). The \(R^2\) value represents the proportion of variability in the response variable that is explained by the explanatory variable. For this model, 63.42% of the variability in runs is explained by at-bats.

  1. Fit a new model that uses pf_expression_control to predict hf_score, or the total human freedom score. Using the estimates from the R output, write the equation of the regression line. What does the slope tell us in the context of the relationship between human freedom and the amount of political pressure on media content?

As one can see, the slope tells us that there is a positive relationship/correlation between human freedom and politically heavy media.

m2 <- lm(hf_score ~ pf_expression_control, data = hfi)
summary(m2)
## 
## Call:
## lm(formula = hf_score ~ pf_expression_control, data = hfi)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6198 -0.4908  0.1031  0.4703  2.2933 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           5.153687   0.046070  111.87   <2e-16 ***
## pf_expression_control 0.349862   0.008067   43.37   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.667 on 1376 degrees of freedom
##   (80 observations deleted due to missingness)
## Multiple R-squared:  0.5775, Adjusted R-squared:  0.5772 
## F-statistic:  1881 on 1 and 1376 DF,  p-value: < 2.2e-16
ggplot(data = hfi, aes(x = pf_expression_control, y = pf_score)) +
  geom_point() +
  stat_smooth(method = "lm", se = FALSE)

Prediction and prediction errors

Let’s create a scatterplot with the least squares line for m1 laid on top.

ggplot(data = hfi, aes(x = pf_expression_control, y = pf_score)) +
  geom_point() +
  stat_smooth(method = "lm", se = FALSE)

Here, we are literally adding a layer on top of our plot. geom_smooth creates the line by fitting a linear model. It can also show us the standard error se associated with our line, but we’ll suppress that for now.

This line can be used to predict \(y\) at any value of \(x\). When predictions are made for values of \(x\) that are beyond the range of the observed data, it is referred to as extrapolation and is not usually recommended. However, predictions made within the range of the data are more reliable. They’re also used to compute the residuals.

  1. If someone saw the least squares regression line and not the actual data, how would they predict a country’s personal freedom school for one with a 6.7 rating for pf_expression_control? Is this an overestimate or an underestimate, and by how much? In other words, what is the residual for this prediction?

If somene predicted a country’s personal freedom score,they would have underestimated, because the pf_score is likely to be between 7.5 and 8. As far as residuals are concerned, they only apply to data points, whereas estimates are gathered by looking at the line.

Model diagnostics

To assess whether the linear model is reliable, we need to check for (1) linearity, (2) nearly normal residuals, and (3) constant variability.

Linearity: You already checked if the relationship between pf_score and `pf_expression_control’ is linear using a scatterplot. We should also verify this condition with a plot of the residuals vs. fitted (predicted) values.

ggplot(data = m1, aes(x = .fitted, y = .resid)) +
  geom_point() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab("Fitted values") +
  ylab("Residuals")

Notice here that m1 can also serve as a data set because stored within it are the fitted values (\(\hat{y}\)) and the residuals. Also note that we’re getting fancy with the code here. After creating the scatterplot on the first layer (first line of code), we overlay a horizontal dashed line at \(y = 0\) (to help us check whether residuals are distributed around 0), and we also reanme the axis labels to be more informative.

  1. Is there any apparent pattern in the residuals plot? What does this indicate about the linearity of the relationship between the two variables?

Looking at the residuals plot, there is no obvious pattern between the two variables. This gives one the impression that a fitted line would not have a slope, or, a very insignificant one. This indicates that there is most likely a linear relationship between the two variables.


Nearly normal residuals: To check this condition, we can look at a histogram

ggplot(data = m1, aes(x = .resid)) +
  geom_histogram(binwidth = .22) +
  xlab("Residuals")

or a normal probability plot of the residuals.

ggplot(data = m1, aes(sample = .resid)) +
  stat_qq()

Note that the syntax for making a normal probability plot is a bit different than what you’re used to seeing: we set sample equal to the residuals instead of x, and we set a statistical method qq, which stands for “quantile-quantile”, another name commonly used for normal probability plots.

  1. Based on the histogram and the normal probability plot, does the nearly normal residuals condition appear to be met?

According to the histogram and probability plot, the data follow a normal distribution.


Constant variability:

  1. Based on the residuals vs. fitted plot, does the constant variability condition appear to be met?

Looking at these plots, there is no obvious pattern. This indicates our assumption of constant variability is satisfied.


More Practice

  • Choose another freedom variable and a variable you think would strongly correlate with it.. Produce a scatterplot of the two variables and fit a linear model. At a glance, does there seem to be a linear relationship?

Yes, there appears to be a strong positive linear relationship

```r
hfi %>% ggplot(aes(x =pf_rol_civil, y = pf_ss )) +
  geom_point()
```

<img src="Data-606-Lab--8_files/figure-html/unnamed-chunk-6-1.png" width="672" />
  • How does this relationship compare to the relationship between pf_expression_control and pf_score? Use the \(R^2\) values from the two model summaries to compare. Does your independent variable seem to predict your dependent one better? Why or why not?

In this model, \(R^2 = 0.3316\). In M2, \(R^2 = 0.5775\). This means that the M3 model only explains 33.16% of the variability.

m3 <- lm(pf_ss~pf_rol_civil, data = hfi)
summary(m3)
## 
## Call:
## lm(formula = pf_ss ~ pf_rol_civil, data = hfi)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.7318 -0.6595  0.2051  0.6820  3.1515 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   5.45513    0.14332   38.06   <2e-16 ***
## pf_rol_civil  0.52869    0.02533   20.87   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.073 on 878 degrees of freedom
##   (578 observations deleted due to missingness)
## Multiple R-squared:  0.3316, Adjusted R-squared:  0.3308 
## F-statistic: 435.6 on 1 and 878 DF,  p-value: < 2.2e-16
summary(m2)
## 
## Call:
## lm(formula = hf_score ~ pf_expression_control, data = hfi)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6198 -0.4908  0.1031  0.4703  2.2933 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           5.153687   0.046070  111.87   <2e-16 ***
## pf_expression_control 0.349862   0.008067   43.37   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.667 on 1376 degrees of freedom
##   (80 observations deleted due to missingness)
## Multiple R-squared:  0.5775, Adjusted R-squared:  0.5772 
## F-statistic:  1881 on 1 and 1376 DF,  p-value: < 2.2e-16
  • What’s one freedom relationship you were most surprised about and why? Display the model diagnostics for the regression model analyzing this relationship.

In this relationship of homicide and criminal justice, \(R^2 = 0.1312\), which indicates there is not a strong correlation of homicides and criminal justice. Yet, looking at the graph, one can easily see there is a strong concentration of 0 homicides. This means there is missing data, or, certain countries simply don’t have homicides. But, ignoring this discrepency, there is a strong positive correlation between homicides and criminal justice.

hfi %>% ggplot(aes(x = pf_ss_homicide
, y = pf_rol_civil)) +
  geom_point()

m4 <- lm(pf_rol_civil~pf_ss_homicide, data = hfi)
summary(m4)
## 
## Call:
## lm(formula = pf_rol_civil ~ pf_ss_homicide, data = hfi)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.9008 -0.9041 -0.0633  0.9838  2.9020 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      4.1303     0.1251   33.01   <2e-16 ***
## pf_ss_homicide   0.1785     0.0155   11.51   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.332 on 878 degrees of freedom
##   (578 observations deleted due to missingness)
## Multiple R-squared:  0.1312, Adjusted R-squared:  0.1302 
## F-statistic: 132.5 on 1 and 878 DF,  p-value: < 2.2e-16