Chapter 6.2.1.1 Exercises

LR
In [31]:
y<- rnorm(100, 2, 1)  
x <- rnorm(100, 4,20)
mydata <- data.frame(x,y)

# The following two calls are equivalent:

lm(data = mydata, y ~ x) # this should still be the same as the line below - you can add the other stuff to double check
lm(y ~ x, mydata, 1:100, model = FALSE)

# a linear model requires a dataframe; the former code only created a vector of numerics - not a DF.
Call:
lm(formula = y ~ x, data = mydata)

Coefficients:
(Intercept)            x  
   1.856881     0.000469  
Call:
lm(formula = y ~ x, data = mydata, subset = 1:100, model = FALSE)

Coefficients:
(Intercept)            x  
   1.856881     0.000469  
In [51]:
# You might be able to puzzle out that this rescales each column to have a range from 0 to 1. But did you spot the mistake? I made an error when copying-and-pasting the code for df$b: I forgot to change an a to a b. Extracting repeated code out into a function is a good idea because it prevents you from making this type of mistake.

# To write a function you need to first analyse the code. How many inputs does it have?

(df$a - min(df$a, na.rm = TRUE)) /
  (max(df$a, na.rm = TRUE) - min(df$a, na.rm = TRUE))
  1. 0.863086319110583
  2. 0.777154459053692
  3. 0.872831463744569
  4. 1
  5. 0
  6. 0.874617255465876
  7. 0.151935036054834
  8. 0.536186660528693
  9. 0.505301370778135
  10. 0.67278687859053
In [5]:
# Last, let’s recall the regressions we fit. First, the regression with no interaction effect: note the use of + in the formula.

score_model_2 <- lm(score ~ age + gender, data = evals_multiple)
get_regression_table(score_model_2)

# Second, the regression with an interaction effect: note the use of * in the formula.

score_model_3 <- lm(score ~ age * gender, data = evals_multiple)
get_regression_table(score_model_3)
termestimatestd_errorstatisticp_valuelower_ciupper_ci
intercept 4.484 0.125 35.792 0.000 4.238 4.730
age -0.009 0.003 -3.280 0.001 -0.014 -0.003
gendermale 0.191 0.052 3.632 0.000 0.087 0.294
termestimatestd_errorstatisticp_valuelower_ciupper_ci
intercept 4.883 0.205 23.795 0.000 4.480 5.286
age -0.018 0.004 -3.919 0.000 -0.026 -0.009
gendermale -0.446 0.265 -1.681 0.094 -0.968 0.076
age:gendermale 0.014 0.006 2.446 0.015 0.003 0.024
In [2]:
library(ggplot2)
library(dplyr)
library(moderndive)
library(infer)
In [3]:
# Our data is stored in evals and we are focused on the measurements of the score and bty_avg variables there. Note that we don’t choose a subset of variables here since we will specify() the variables of interest using infer.

evals %>% 
  specify(score ~ bty_avg)
scorebty_avg
4.7 5.000
4.1 5.000
3.9 5.000
4.8 5.000
4.6 3.000
4.3 3.000
2.8 3.000
4.1 3.333
3.4 3.333
4.5 3.167
3.8 3.167
4.5 3.167
4.6 3.167
3.9 3.167
3.9 3.167
4.3 3.167
4.5 3.167
4.8 7.333
4.6 7.333
4.6 7.333
4.9 7.333
4.6 7.333
4.5 7.333
4.4 5.500
4.6 5.500
4.7 5.500
4.5 5.500
4.8 5.500
4.9 5.500
4.5 5.500
2.8 2.000
3.1 2.000
4.2 2.000
3.4 2.000
3.0 2.000
3.3 7.833
3.6 7.833
3.7 7.833
3.6 3.333
4.3 3.333
4.1 4.500
4.9 4.500
4.8 4.500
3.7 4.333
3.9 4.333
4.5 4.333
3.6 4.333
4.4 4.333
3.4 4.333
4.4 4.333
4.5 6.833
4.5 6.833
4.5 6.833
4.6 6.833
4.1 6.833
4.5 6.833
3.5 5.333
4.4 5.333
4.4 5.333
4.1 5.333
In [5]:
# We can use the specify() %>% calculate() shortcut here to determine the slope value seen in our observed data:

slope_obs <- evals %>% 
  specify(score ~ bty_avg) %>% 
  calculate(stat = "slope")
slope_obs
stat
0.06663704
In [6]:
# We can build our null distribution in much the same way we did in Chapter 10 using the generate() and calculate() functions. Note also the addition of the hypothesize() function, which lets generate() know to perform the permuting instead of bootstrapping.

null_slope_distn <- evals %>% 
  specify(score ~ bty_avg) %>%
  hypothesize(null = "independence") %>% 
  generate(reps = 10000) %>% 
  calculate(stat = "slope")

null_slope_distn %>% 
  visualize(obs_stat = slope_obs, direction = "greater")
In [27]:
library(ggplot2)
library(dplyr)
library(moderndive)
library(gapminder)
library(skimr)
In [28]:
# Let’s load the gapminder data and filter() for only observations in 2007. Next we select() only the variables we’ll need along with gdpPercap, which is each country’s gross domestic product per capita (GDP). GDP is a rough measure of that country’s economic performance. (This will be used for the upcoming Learning Check). Lastly, we save this in a data frame with name gapminder2007:

gapminder2007 <- gapminder %>%
  filter(year == 2007) %>% 
  select(country, continent, lifeExp, gdpPercap)
In [15]:
gapminder2007
A tibble: 142 × 4
countrycontinentlifeExpgdpPercap
<fct><fct><dbl><dbl>
Afghanistan Asia 43.828 974.5803
Albania Europe 76.423 5937.0295
Algeria Africa 72.301 6223.3675
Angola Africa 42.731 4797.2313
Argentina Americas75.32012779.3796
Australia Oceania 81.23534435.3674
Austria Europe 79.82936126.4927
Bahrain Asia 75.63529796.0483
Bangladesh Asia 64.062 1391.2538
Belgium Europe 79.44133692.6051
Benin Africa 56.728 1441.2849
Bolivia Americas65.554 3822.1371
Bosnia and Herzegovina Europe 74.852 7446.2988
Botswana Africa 50.72812569.8518
Brazil Americas72.390 9065.8008
Bulgaria Europe 73.00510680.7928
Burkina Faso Africa 52.295 1217.0330
Burundi Africa 49.580 430.0707
Cambodia Asia 59.723 1713.7787
Cameroon Africa 50.430 2042.0952
Canada Americas80.65336319.2350
Central African RepublicAfrica 44.741 706.0165
Chad Africa 50.651 1704.0637
Chile Americas78.55313171.6388
China Asia 72.961 4959.1149
Colombia Americas72.889 7006.5804
Comoros Africa 65.152 986.1479
Congo, Dem. Rep. Africa 46.462 277.5519
Congo, Rep. Africa 55.322 3632.5578
Costa Rica Americas78.782 9645.0614
Sierra Leone Africa 42.568 862.5408
Singapore Asia 79.97247143.1796
Slovak Republic Europe 74.66318678.3144
Slovenia Europe 77.92625768.2576
Somalia Africa 48.159 926.1411
South Africa Africa 49.339 9269.6578
Spain Europe 80.94128821.0637
Sri Lanka Asia 72.396 3970.0954
Sudan Africa 58.556 2602.3950
Swaziland Africa 39.613 4513.4806
Sweden Europe 80.88433859.7484
Switzerland Europe 81.70137506.4191
Syria Asia 74.143 4184.5481
Taiwan Asia 78.40028718.2768
Tanzania Africa 52.517 1107.4822
Thailand Asia 70.616 7458.3963
Togo Africa 58.420 882.9699
Trinidad and TobagoAmericas69.81918008.5092
Tunisia Africa 73.923 7092.9230
Turkey Europe 71.777 8458.2764
Uganda Africa 51.542 1056.3801
United Kingdom Europe 79.42533203.2613
United States Americas78.24242951.6531
Uruguay Americas76.38410611.4630
Venezuela Americas73.74711415.8057
Vietnam Asia 74.249 2441.5764
West Bank and Gaza Asia 73.422 3025.3498
Yemen, Rep. Asia 62.698 2280.7699
Zambia Africa 42.384 1271.2116
Zimbabwe Africa 43.487 469.7093
In [21]:
glimpse(gapminder2007)
Rows: 142
Columns: 4
$ country   <fct> Afghanistan, Albania, Algeria, Angola, Argentina, Australia…
$ continent <fct> Asia, Europe, Africa, Africa, Americas, Oceania, Europe, As…
$ lifeExp   <dbl> 43.828, 76.423, 72.301, 42.731, 75.320, 81.235, 79.829, 75.…
$ gdpPercap <dbl> 974.5803, 5937.0295, 6223.3675, 4797.2313, 12779.3796, 3443…
In [22]:
# We see that the variable continent is indeed categorical, as it is encoded as fct which stands for “factor.” This is R’s way of storing categorical variables. Let’s once again apply the skim() function from the skimr package to our two variables of interest: continent and lifeExp:

gapminder2007 %>% 
  select(continent, lifeExp) %>%  
  skim()
── Data Summary ────────────────────────
                           Values    
Name                       Piped data
Number of rows             142       
Number of columns          2         
_______________________              
Column type frequency:               
  factor                   1         
  numeric                  1         
________________________             
Group variables            None      

── Variable type: factor ───────────────────────────────────────────────────────
  skim_variable n_missing complete_rate ordered n_unique
1 continent             0             1 FALSE          5
  top_counts                        
1 Afr: 52, Asi: 33, Eur: 30, Ame: 25

── Variable type: numeric ──────────────────────────────────────────────────────
  skim_variable n_missing complete_rate  mean    sd    p0   p25   p50   p75
1 lifeExp               0             1  67.0  12.1  39.6  57.2  71.9  76.4
   p100 hist 
1  82.6 ▂▃▃▆▇
In [19]:
skim(gapminder2007)
── Data Summary ────────────────────────
                           Values       
Name                       gapminder2007
Number of rows             142          
Number of columns          4            
_______________________                 
Column type frequency:                  
  factor                   2            
  numeric                  2            
________________________                
Group variables            None         

── Variable type: factor ───────────────────────────────────────────────────────
  skim_variable n_missing complete_rate ordered n_unique
1 country               0             1 FALSE        142
2 continent             0             1 FALSE          5
  top_counts                        
1 Afg: 1, Alb: 1, Alg: 1, Ang: 1    
2 Afr: 52, Asi: 33, Eur: 30, Ame: 25

── Variable type: numeric ──────────────────────────────────────────────────────
  skim_variable n_missing complete_rate    mean      sd    p0    p25    p50
1 lifeExp               0             1    67.0    12.1  39.6   57.2   71.9
2 gdpPercap             0             1 11680.  12860.  278.  1625.  6124. 
      p75    p100 hist 
1    76.4    82.6 ▂▃▃▆▇
2 18009.  49357.  ▇▂▁▂▁
In [23]:
dplyr::group_by(gapminder2007, country) %>% skim()

# or: gapminder2007 %>% group_by(country) %>% skim()
── Data Summary ────────────────────────
                           Values    
Name                       Piped data
Number of rows             142       
Number of columns          4         
_______________________              
Column type frequency:               
  factor                   1         
  numeric                  2         
________________________             
Group variables            country   

── Variable type: factor ───────────────────────────────────────────────────────
    skim_variable country                  n_missing complete_rate ordered
  1 continent     Afghanistan                      0             1 FALSE  
  2 continent     Albania                          0             1 FALSE  
  3 continent     Algeria                          0             1 FALSE  
  4 continent     Angola                           0             1 FALSE  
  5 continent     Argentina                        0             1 FALSE  
  6 continent     Australia                        0             1 FALSE  
  7 continent     Austria                          0             1 FALSE  
  8 continent     Bahrain                          0             1 FALSE  
  9 continent     Bangladesh                       0             1 FALSE  
 10 continent     Belgium                          0             1 FALSE  
 11 continent     Benin                            0             1 FALSE  
 12 continent     Bolivia                          0             1 FALSE  
 13 continent     Bosnia and Herzegovina           0             1 FALSE  
 14 continent     Botswana                         0             1 FALSE  
 15 continent     Brazil                           0             1 FALSE  
 16 continent     Bulgaria                         0             1 FALSE  
 17 continent     Burkina Faso                     0             1 FALSE  
 18 continent     Burundi                          0             1 FALSE  
 19 continent     Cambodia                         0             1 FALSE  
 20 continent     Cameroon                         0             1 FALSE  
 21 continent     Canada                           0             1 FALSE  
 22 continent     Central African Republic         0             1 FALSE  
 23 continent     Chad                             0             1 FALSE  
 24 continent     Chile                            0             1 FALSE  
 25 continent     China                            0             1 FALSE  
 26 continent     Colombia                         0             1 FALSE  
 27 continent     Comoros                          0             1 FALSE  
 28 continent     Congo, Dem. Rep.                 0             1 FALSE  
 29 continent     Congo, Rep.                      0             1 FALSE  
 30 continent     Costa Rica                       0             1 FALSE  
 31 continent     Cote d'Ivoire                    0             1 FALSE  
 32 continent     Croatia                          0             1 FALSE  
 33 continent     Cuba                             0             1 FALSE  
 34 continent     Czech Republic                   0             1 FALSE  
 35 continent     Denmark                          0             1 FALSE  
 36 continent     Djibouti                         0             1 FALSE  
 37 continent     Dominican Republic               0             1 FALSE  
 38 continent     Ecuador                          0             1 FALSE  
 39 continent     Egypt                            0             1 FALSE  
 40 continent     El Salvador                      0             1 FALSE  
 41 continent     Equatorial Guinea                0             1 FALSE  
 42 continent     Eritrea                          0             1 FALSE  
 43 continent     Ethiopia                         0             1 FALSE  
 44 continent     Finland                          0             1 FALSE  
 45 continent     France                           0             1 FALSE  
 46 continent     Gabon                            0             1 FALSE  
 47 continent     Gambia                           0             1 FALSE  
 48 continent     Germany                          0             1 FALSE  
 49 continent     Ghana                            0             1 FALSE  
 50 continent     Greece                           0             1 FALSE  
 51 continent     Guatemala                        0             1 FALSE  
 52 continent     Guinea                           0             1 FALSE  
 53 continent     Guinea-Bissau                    0             1 FALSE  
 54 continent     Haiti                            0             1 FALSE  
 55 continent     Honduras                         0             1 FALSE  
 56 continent     Hong Kong, China                 0             1 FALSE  
 57 continent     Hungary                          0             1 FALSE  
 58 continent     Iceland                          0             1 FALSE  
 59 continent     India                            0             1 FALSE  
 60 continent     Indonesia                        0             1 FALSE  
 61 continent     Iran                             0             1 FALSE  
 62 continent     Iraq                             0             1 FALSE  
 63 continent     Ireland                          0             1 FALSE  
 64 continent     Israel                           0             1 FALSE  
 65 continent     Italy                            0             1 FALSE  
 66 continent     Jamaica                          0             1 FALSE  
 67 continent     Japan                            0             1 FALSE  
 68 continent     Jordan                           0             1 FALSE  
 69 continent     Kenya                            0             1 FALSE  
 70 continent     Korea, Dem. Rep.                 0             1 FALSE  
 71 continent     Korea, Rep.                      0             1 FALSE  
 72 continent     Kuwait                           0             1 FALSE  
 73 continent     Lebanon                          0             1 FALSE  
 74 continent     Lesotho                          0             1 FALSE  
 75 continent     Liberia                          0             1 FALSE  
 76 continent     Libya                            0             1 FALSE  
 77 continent     Madagascar                       0             1 FALSE  
 78 continent     Malawi                           0             1 FALSE  
 79 continent     Malaysia                         0             1 FALSE  
 80 continent     Mali                             0             1 FALSE  
 81 continent     Mauritania                       0             1 FALSE  
 82 continent     Mauritius                        0             1 FALSE  
 83 continent     Mexico                           0             1 FALSE  
 84 continent     Mongolia                         0             1 FALSE  
 85 continent     Montenegro                       0             1 FALSE  
 86 continent     Morocco                          0             1 FALSE  
 87 continent     Mozambique                       0             1 FALSE  
 88 continent     Myanmar                          0             1 FALSE  
 89 continent     Namibia                          0             1 FALSE  
 90 continent     Nepal                            0             1 FALSE  
 91 continent     Netherlands                      0             1 FALSE  
 92 continent     New Zealand                      0             1 FALSE  
 93 continent     Nicaragua                        0             1 FALSE  
 94 continent     Niger                            0             1 FALSE  
 95 continent     Nigeria                          0             1 FALSE  
 96 continent     Norway                           0             1 FALSE  
 97 continent     Oman                             0             1 FALSE  
 98 continent     Pakistan                         0             1 FALSE  
 99 continent     Panama                           0             1 FALSE  
100 continent     Paraguay                         0             1 FALSE  
101 continent     Peru                             0             1 FALSE  
102 continent     Philippines                      0             1 FALSE  
103 continent     Poland                           0             1 FALSE  
104 continent     Portugal                         0             1 FALSE  
105 continent     Puerto Rico                      0             1 FALSE  
106 continent     Reunion                          0             1 FALSE  
107 continent     Romania                          0             1 FALSE  
108 continent     Rwanda                           0             1 FALSE  
109 continent     Sao Tome and Principe            0             1 FALSE  
110 continent     Saudi Arabia                     0             1 FALSE  
111 continent     Senegal                          0             1 FALSE  
112 continent     Serbia                           0             1 FALSE  
113 continent     Sierra Leone                     0             1 FALSE  
114 continent     Singapore                        0             1 FALSE  
115 continent     Slovak Republic                  0             1 FALSE  
116 continent     Slovenia                         0             1 FALSE  
117 continent     Somalia                          0             1 FALSE  
118 continent     South Africa                     0             1 FALSE  
119 continent     Spain                            0             1 FALSE  
120 continent     Sri Lanka                        0             1 FALSE  
121 continent     Sudan                            0             1 FALSE  
122 continent     Swaziland                        0             1 FALSE  
123 continent     Sweden                           0             1 FALSE  
124 continent     Switzerland                      0             1 FALSE  
125 continent     Syria                            0             1 FALSE  
126 continent     Taiwan                           0             1 FALSE  
127 continent     Tanzania                         0             1 FALSE  
128 continent     Thailand                         0             1 FALSE  
129 continent     Togo                             0             1 FALSE  
130 continent     Trinidad and Tobago              0             1 FALSE  
131 continent     Tunisia                          0             1 FALSE  
132 continent     Turkey                           0             1 FALSE  
133 continent     Uganda                           0             1 FALSE  
134 continent     United Kingdom                   0             1 FALSE  
135 continent     United States                    0             1 FALSE  
136 continent     Uruguay                          0             1 FALSE  
137 continent     Venezuela                        0             1 FALSE  
138 continent     Vietnam                          0             1 FALSE  
139 continent     West Bank and Gaza               0             1 FALSE  
140 continent     Yemen, Rep.                      0             1 FALSE  
141 continent     Zambia                           0             1 FALSE  
142 continent     Zimbabwe                         0             1 FALSE  
    n_unique top_counts                    
  1        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
  2        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
  3        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
  4        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
  5        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
  6        1 Oce: 1, Afr: 0, Ame: 0, Asi: 0
  7        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
  8        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
  9        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 10        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 11        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 12        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 13        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 14        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 15        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 16        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 17        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 18        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 19        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 20        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 21        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 22        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 23        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 24        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 25        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 26        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 27        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 28        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 29        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 30        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 31        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 32        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 33        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 34        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 35        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 36        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 37        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 38        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 39        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 40        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 41        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 42        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 43        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 44        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 45        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 46        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 47        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 48        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 49        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 50        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 51        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 52        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 53        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 54        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 55        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 56        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 57        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 58        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 59        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 60        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 61        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 62        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 63        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 64        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 65        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 66        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 67        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 68        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 69        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 70        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 71        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 72        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 73        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 74        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 75        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 76        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 77        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 78        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 79        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 80        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 81        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 82        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 83        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 84        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 85        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 86        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 87        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 88        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 89        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 90        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 91        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 92        1 Oce: 1, Afr: 0, Ame: 0, Asi: 0
 93        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
 94        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 95        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
 96        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
 97        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 98        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
 99        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
100        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
101        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
102        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
103        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
104        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
105        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
106        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
107        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
108        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
109        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
110        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
111        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
112        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
113        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
114        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
115        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
116        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
117        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
118        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
119        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
120        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
121        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
122        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
123        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
124        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
125        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
126        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
127        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
128        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
129        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
130        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
131        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
132        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
133        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
134        1 Eur: 1, Afr: 0, Ame: 0, Asi: 0
135        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
136        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
137        1 Ame: 1, Afr: 0, Asi: 0, Eur: 0
138        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
139        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
140        1 Asi: 1, Afr: 0, Ame: 0, Eur: 0
141        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0
142        1 Afr: 1, Ame: 0, Asi: 0, Eur: 0

── Variable type: numeric ──────────────────────────────────────────────────────
    skim_variable country                  n_missing complete_rate    mean    sd
  1 lifeExp       Afghanistan                      0             1    43.8    NA
  2 lifeExp       Albania                          0             1    76.4    NA
  3 lifeExp       Algeria                          0             1    72.3    NA
  4 lifeExp       Angola                           0             1    42.7    NA
  5 lifeExp       Argentina                        0             1    75.3    NA
  6 lifeExp       Australia                        0             1    81.2    NA
  7 lifeExp       Austria                          0             1    79.8    NA
  8 lifeExp       Bahrain                          0             1    75.6    NA
  9 lifeExp       Bangladesh                       0             1    64.1    NA
 10 lifeExp       Belgium                          0             1    79.4    NA
 11 lifeExp       Benin                            0             1    56.7    NA
 12 lifeExp       Bolivia                          0             1    65.6    NA
 13 lifeExp       Bosnia and Herzegovina           0             1    74.9    NA
 14 lifeExp       Botswana                         0             1    50.7    NA
 15 lifeExp       Brazil                           0             1    72.4    NA
 16 lifeExp       Bulgaria                         0             1    73.0    NA
 17 lifeExp       Burkina Faso                     0             1    52.3    NA
 18 lifeExp       Burundi                          0             1    49.6    NA
 19 lifeExp       Cambodia                         0             1    59.7    NA
 20 lifeExp       Cameroon                         0             1    50.4    NA
 21 lifeExp       Canada                           0             1    80.7    NA
 22 lifeExp       Central African Republic         0             1    44.7    NA
 23 lifeExp       Chad                             0             1    50.7    NA
 24 lifeExp       Chile                            0             1    78.6    NA
 25 lifeExp       China                            0             1    73.0    NA
 26 lifeExp       Colombia                         0             1    72.9    NA
 27 lifeExp       Comoros                          0             1    65.2    NA
 28 lifeExp       Congo, Dem. Rep.                 0             1    46.5    NA
 29 lifeExp       Congo, Rep.                      0             1    55.3    NA
 30 lifeExp       Costa Rica                       0             1    78.8    NA
 31 lifeExp       Cote d'Ivoire                    0             1    48.3    NA
 32 lifeExp       Croatia                          0             1    75.7    NA
 33 lifeExp       Cuba                             0             1    78.3    NA
 34 lifeExp       Czech Republic                   0             1    76.5    NA
 35 lifeExp       Denmark                          0             1    78.3    NA
 36 lifeExp       Djibouti                         0             1    54.8    NA
 37 lifeExp       Dominican Republic               0             1    72.2    NA
 38 lifeExp       Ecuador                          0             1    75.0    NA
 39 lifeExp       Egypt                            0             1    71.3    NA
 40 lifeExp       El Salvador                      0             1    71.9    NA
 41 lifeExp       Equatorial Guinea                0             1    51.6    NA
 42 lifeExp       Eritrea                          0             1    58.0    NA
 43 lifeExp       Ethiopia                         0             1    52.9    NA
 44 lifeExp       Finland                          0             1    79.3    NA
 45 lifeExp       France                           0             1    80.7    NA
 46 lifeExp       Gabon                            0             1    56.7    NA
 47 lifeExp       Gambia                           0             1    59.4    NA
 48 lifeExp       Germany                          0             1    79.4    NA
 49 lifeExp       Ghana                            0             1    60.0    NA
 50 lifeExp       Greece                           0             1    79.5    NA
 51 lifeExp       Guatemala                        0             1    70.3    NA
 52 lifeExp       Guinea                           0             1    56.0    NA
 53 lifeExp       Guinea-Bissau                    0             1    46.4    NA
 54 lifeExp       Haiti                            0             1    60.9    NA
 55 lifeExp       Honduras                         0             1    70.2    NA
 56 lifeExp       Hong Kong, China                 0             1    82.2    NA
 57 lifeExp       Hungary                          0             1    73.3    NA
 58 lifeExp       Iceland                          0             1    81.8    NA
 59 lifeExp       India                            0             1    64.7    NA
 60 lifeExp       Indonesia                        0             1    70.6    NA
 61 lifeExp       Iran                             0             1    71.0    NA
 62 lifeExp       Iraq                             0             1    59.5    NA
 63 lifeExp       Ireland                          0             1    78.9    NA
 64 lifeExp       Israel                           0             1    80.7    NA
 65 lifeExp       Italy                            0             1    80.5    NA
 66 lifeExp       Jamaica                          0             1    72.6    NA
 67 lifeExp       Japan                            0             1    82.6    NA
 68 lifeExp       Jordan                           0             1    72.5    NA
 69 lifeExp       Kenya                            0             1    54.1    NA
 70 lifeExp       Korea, Dem. Rep.                 0             1    67.3    NA
 71 lifeExp       Korea, Rep.                      0             1    78.6    NA
 72 lifeExp       Kuwait                           0             1    77.6    NA
 73 lifeExp       Lebanon                          0             1    72.0    NA
 74 lifeExp       Lesotho                          0             1    42.6    NA
 75 lifeExp       Liberia                          0             1    45.7    NA
 76 lifeExp       Libya                            0             1    74.0    NA
 77 lifeExp       Madagascar                       0             1    59.4    NA
 78 lifeExp       Malawi                           0             1    48.3    NA
 79 lifeExp       Malaysia                         0             1    74.2    NA
 80 lifeExp       Mali                             0             1    54.5    NA
 81 lifeExp       Mauritania                       0             1    64.2    NA
 82 lifeExp       Mauritius                        0             1    72.8    NA
 83 lifeExp       Mexico                           0             1    76.2    NA
 84 lifeExp       Mongolia                         0             1    66.8    NA
 85 lifeExp       Montenegro                       0             1    74.5    NA
 86 lifeExp       Morocco                          0             1    71.2    NA
 87 lifeExp       Mozambique                       0             1    42.1    NA
 88 lifeExp       Myanmar                          0             1    62.1    NA
 89 lifeExp       Namibia                          0             1    52.9    NA
 90 lifeExp       Nepal                            0             1    63.8    NA
 91 lifeExp       Netherlands                      0             1    79.8    NA
 92 lifeExp       New Zealand                      0             1    80.2    NA
 93 lifeExp       Nicaragua                        0             1    72.9    NA
 94 lifeExp       Niger                            0             1    56.9    NA
 95 lifeExp       Nigeria                          0             1    46.9    NA
 96 lifeExp       Norway                           0             1    80.2    NA
 97 lifeExp       Oman                             0             1    75.6    NA
 98 lifeExp       Pakistan                         0             1    65.5    NA
 99 lifeExp       Panama                           0             1    75.5    NA
100 lifeExp       Paraguay                         0             1    71.8    NA
101 lifeExp       Peru                             0             1    71.4    NA
102 lifeExp       Philippines                      0             1    71.7    NA
103 lifeExp       Poland                           0             1    75.6    NA
104 lifeExp       Portugal                         0             1    78.1    NA
105 lifeExp       Puerto Rico                      0             1    78.7    NA
106 lifeExp       Reunion                          0             1    76.4    NA
107 lifeExp       Romania                          0             1    72.5    NA
108 lifeExp       Rwanda                           0             1    46.2    NA
109 lifeExp       Sao Tome and Principe            0             1    65.5    NA
110 lifeExp       Saudi Arabia                     0             1    72.8    NA
111 lifeExp       Senegal                          0             1    63.1    NA
112 lifeExp       Serbia                           0             1    74.0    NA
113 lifeExp       Sierra Leone                     0             1    42.6    NA
114 lifeExp       Singapore                        0             1    80.0    NA
115 lifeExp       Slovak Republic                  0             1    74.7    NA
116 lifeExp       Slovenia                         0             1    77.9    NA
117 lifeExp       Somalia                          0             1    48.2    NA
118 lifeExp       South Africa                     0             1    49.3    NA
119 lifeExp       Spain                            0             1    80.9    NA
120 lifeExp       Sri Lanka                        0             1    72.4    NA
121 lifeExp       Sudan                            0             1    58.6    NA
122 lifeExp       Swaziland                        0             1    39.6    NA
123 lifeExp       Sweden                           0             1    80.9    NA
124 lifeExp       Switzerland                      0             1    81.7    NA
125 lifeExp       Syria                            0             1    74.1    NA
126 lifeExp       Taiwan                           0             1    78.4    NA
127 lifeExp       Tanzania                         0             1    52.5    NA
128 lifeExp       Thailand                         0             1    70.6    NA
129 lifeExp       Togo                             0             1    58.4    NA
130 lifeExp       Trinidad and Tobago              0             1    69.8    NA
131 lifeExp       Tunisia                          0             1    73.9    NA
132 lifeExp       Turkey                           0             1    71.8    NA
133 lifeExp       Uganda                           0             1    51.5    NA
134 lifeExp       United Kingdom                   0             1    79.4    NA
135 lifeExp       United States                    0             1    78.2    NA
136 lifeExp       Uruguay                          0             1    76.4    NA
137 lifeExp       Venezuela                        0             1    73.7    NA
138 lifeExp       Vietnam                          0             1    74.2    NA
139 lifeExp       West Bank and Gaza               0             1    73.4    NA
140 lifeExp       Yemen, Rep.                      0             1    62.7    NA
141 lifeExp       Zambia                           0             1    42.4    NA
142 lifeExp       Zimbabwe                         0             1    43.5    NA
143 gdpPercap     Afghanistan                      0             1   975.     NA
144 gdpPercap     Albania                          0             1  5937.     NA
145 gdpPercap     Algeria                          0             1  6223.     NA
146 gdpPercap     Angola                           0             1  4797.     NA
147 gdpPercap     Argentina                        0             1 12779.     NA
148 gdpPercap     Australia                        0             1 34435.     NA
149 gdpPercap     Austria                          0             1 36126.     NA
150 gdpPercap     Bahrain                          0             1 29796.     NA
151 gdpPercap     Bangladesh                       0             1  1391.     NA
152 gdpPercap     Belgium                          0             1 33693.     NA
153 gdpPercap     Benin                            0             1  1441.     NA
154 gdpPercap     Bolivia                          0             1  3822.     NA
155 gdpPercap     Bosnia and Herzegovina           0             1  7446.     NA
156 gdpPercap     Botswana                         0             1 12570.     NA
157 gdpPercap     Brazil                           0             1  9066.     NA
158 gdpPercap     Bulgaria                         0             1 10681.     NA
159 gdpPercap     Burkina Faso                     0             1  1217.     NA
160 gdpPercap     Burundi                          0             1   430.     NA
161 gdpPercap     Cambodia                         0             1  1714.     NA
162 gdpPercap     Cameroon                         0             1  2042.     NA
163 gdpPercap     Canada                           0             1 36319.     NA
164 gdpPercap     Central African Republic         0             1   706.     NA
165 gdpPercap     Chad                             0             1  1704.     NA
166 gdpPercap     Chile                            0             1 13172.     NA
167 gdpPercap     China                            0             1  4959.     NA
168 gdpPercap     Colombia                         0             1  7007.     NA
169 gdpPercap     Comoros                          0             1   986.     NA
170 gdpPercap     Congo, Dem. Rep.                 0             1   278.     NA
171 gdpPercap     Congo, Rep.                      0             1  3633.     NA
172 gdpPercap     Costa Rica                       0             1  9645.     NA
173 gdpPercap     Cote d'Ivoire                    0             1  1545.     NA
174 gdpPercap     Croatia                          0             1 14619.     NA
175 gdpPercap     Cuba                             0             1  8948.     NA
176 gdpPercap     Czech Republic                   0             1 22833.     NA
177 gdpPercap     Denmark                          0             1 35278.     NA
178 gdpPercap     Djibouti                         0             1  2082.     NA
179 gdpPercap     Dominican Republic               0             1  6025.     NA
180 gdpPercap     Ecuador                          0             1  6873.     NA
181 gdpPercap     Egypt                            0             1  5581.     NA
182 gdpPercap     El Salvador                      0             1  5728.     NA
183 gdpPercap     Equatorial Guinea                0             1 12154.     NA
184 gdpPercap     Eritrea                          0             1   641.     NA
185 gdpPercap     Ethiopia                         0             1   691.     NA
186 gdpPercap     Finland                          0             1 33207.     NA
187 gdpPercap     France                           0             1 30470.     NA
188 gdpPercap     Gabon                            0             1 13206.     NA
189 gdpPercap     Gambia                           0             1   753.     NA
190 gdpPercap     Germany                          0             1 32170.     NA
191 gdpPercap     Ghana                            0             1  1328.     NA
192 gdpPercap     Greece                           0             1 27538.     NA
193 gdpPercap     Guatemala                        0             1  5186.     NA
194 gdpPercap     Guinea                           0             1   943.     NA
195 gdpPercap     Guinea-Bissau                    0             1   579.     NA
196 gdpPercap     Haiti                            0             1  1202.     NA
197 gdpPercap     Honduras                         0             1  3548.     NA
198 gdpPercap     Hong Kong, China                 0             1 39725.     NA
199 gdpPercap     Hungary                          0             1 18009.     NA
200 gdpPercap     Iceland                          0             1 36181.     NA
201 gdpPercap     India                            0             1  2452.     NA
202 gdpPercap     Indonesia                        0             1  3541.     NA
203 gdpPercap     Iran                             0             1 11606.     NA
204 gdpPercap     Iraq                             0             1  4471.     NA
205 gdpPercap     Ireland                          0             1 40676.     NA
206 gdpPercap     Israel                           0             1 25523.     NA
207 gdpPercap     Italy                            0             1 28570.     NA
208 gdpPercap     Jamaica                          0             1  7321.     NA
209 gdpPercap     Japan                            0             1 31656.     NA
210 gdpPercap     Jordan                           0             1  4519.     NA
211 gdpPercap     Kenya                            0             1  1463.     NA
212 gdpPercap     Korea, Dem. Rep.                 0             1  1593.     NA
213 gdpPercap     Korea, Rep.                      0             1 23348.     NA
214 gdpPercap     Kuwait                           0             1 47307.     NA
215 gdpPercap     Lebanon                          0             1 10461.     NA
216 gdpPercap     Lesotho                          0             1  1569.     NA
217 gdpPercap     Liberia                          0             1   415.     NA
218 gdpPercap     Libya                            0             1 12057.     NA
219 gdpPercap     Madagascar                       0             1  1045.     NA
220 gdpPercap     Malawi                           0             1   759.     NA
221 gdpPercap     Malaysia                         0             1 12452.     NA
222 gdpPercap     Mali                             0             1  1043.     NA
223 gdpPercap     Mauritania                       0             1  1803.     NA
224 gdpPercap     Mauritius                        0             1 10957.     NA
225 gdpPercap     Mexico                           0             1 11978.     NA
226 gdpPercap     Mongolia                         0             1  3096.     NA
227 gdpPercap     Montenegro                       0             1  9254.     NA
228 gdpPercap     Morocco                          0             1  3820.     NA
229 gdpPercap     Mozambique                       0             1   824.     NA
230 gdpPercap     Myanmar                          0             1   944      NA
231 gdpPercap     Namibia                          0             1  4811.     NA
232 gdpPercap     Nepal                            0             1  1091.     NA
233 gdpPercap     Netherlands                      0             1 36798.     NA
234 gdpPercap     New Zealand                      0             1 25185.     NA
235 gdpPercap     Nicaragua                        0             1  2749.     NA
236 gdpPercap     Niger                            0             1   620.     NA
237 gdpPercap     Nigeria                          0             1  2014.     NA
238 gdpPercap     Norway                           0             1 49357.     NA
239 gdpPercap     Oman                             0             1 22316.     NA
240 gdpPercap     Pakistan                         0             1  2606.     NA
241 gdpPercap     Panama                           0             1  9809.     NA
242 gdpPercap     Paraguay                         0             1  4173.     NA
243 gdpPercap     Peru                             0             1  7409.     NA
244 gdpPercap     Philippines                      0             1  3190.     NA
245 gdpPercap     Poland                           0             1 15390.     NA
246 gdpPercap     Portugal                         0             1 20510.     NA
247 gdpPercap     Puerto Rico                      0             1 19329.     NA
248 gdpPercap     Reunion                          0             1  7670.     NA
249 gdpPercap     Romania                          0             1 10808.     NA
250 gdpPercap     Rwanda                           0             1   863.     NA
251 gdpPercap     Sao Tome and Principe            0             1  1598.     NA
252 gdpPercap     Saudi Arabia                     0             1 21655.     NA
253 gdpPercap     Senegal                          0             1  1712.     NA
254 gdpPercap     Serbia                           0             1  9787.     NA
255 gdpPercap     Sierra Leone                     0             1   863.     NA
256 gdpPercap     Singapore                        0             1 47143.     NA
257 gdpPercap     Slovak Republic                  0             1 18678.     NA
258 gdpPercap     Slovenia                         0             1 25768.     NA
259 gdpPercap     Somalia                          0             1   926.     NA
260 gdpPercap     South Africa                     0             1  9270.     NA
261 gdpPercap     Spain                            0             1 28821.     NA
262 gdpPercap     Sri Lanka                        0             1  3970.     NA
263 gdpPercap     Sudan                            0             1  2602.     NA
264 gdpPercap     Swaziland                        0             1  4513.     NA
265 gdpPercap     Sweden                           0             1 33860.     NA
266 gdpPercap     Switzerland                      0             1 37506.     NA
267 gdpPercap     Syria                            0             1  4185.     NA
268 gdpPercap     Taiwan                           0             1 28718.     NA
269 gdpPercap     Tanzania                         0             1  1107.     NA
270 gdpPercap     Thailand                         0             1  7458.     NA
271 gdpPercap     Togo                             0             1   883.     NA
272 gdpPercap     Trinidad and Tobago              0             1 18009.     NA
273 gdpPercap     Tunisia                          0             1  7093.     NA
274 gdpPercap     Turkey                           0             1  8458.     NA
275 gdpPercap     Uganda                           0             1  1056.     NA
276 gdpPercap     United Kingdom                   0             1 33203.     NA
277 gdpPercap     United States                    0             1 42952.     NA
278 gdpPercap     Uruguay                          0             1 10611.     NA
279 gdpPercap     Venezuela                        0             1 11416.     NA
280 gdpPercap     Vietnam                          0             1  2442.     NA
281 gdpPercap     West Bank and Gaza               0             1  3025.     NA
282 gdpPercap     Yemen, Rep.                      0             1  2281.     NA
283 gdpPercap     Zambia                           0             1  1271.     NA
284 gdpPercap     Zimbabwe                         0             1   470.     NA
         p0     p25     p50     p75    p100 hist 
  1    43.8    43.8    43.8    43.8    43.8 ▁▁▇▁▁
  2    76.4    76.4    76.4    76.4    76.4 ▁▁▇▁▁
  3    72.3    72.3    72.3    72.3    72.3 ▁▁▇▁▁
  4    42.7    42.7    42.7    42.7    42.7 ▁▁▇▁▁
  5    75.3    75.3    75.3    75.3    75.3 ▁▁▇▁▁
  6    81.2    81.2    81.2    81.2    81.2 ▁▁▇▁▁
  7    79.8    79.8    79.8    79.8    79.8 ▁▁▇▁▁
  8    75.6    75.6    75.6    75.6    75.6 ▁▁▇▁▁
  9    64.1    64.1    64.1    64.1    64.1 ▁▁▇▁▁
 10    79.4    79.4    79.4    79.4    79.4 ▁▁▇▁▁
 11    56.7    56.7    56.7    56.7    56.7 ▁▁▇▁▁
 12    65.6    65.6    65.6    65.6    65.6 ▁▁▇▁▁
 13    74.9    74.9    74.9    74.9    74.9 ▁▁▇▁▁
 14    50.7    50.7    50.7    50.7    50.7 ▁▁▇▁▁
 15    72.4    72.4    72.4    72.4    72.4 ▁▁▇▁▁
 16    73.0    73.0    73.0    73.0    73.0 ▁▁▇▁▁
 17    52.3    52.3    52.3    52.3    52.3 ▁▁▇▁▁
 18    49.6    49.6    49.6    49.6    49.6 ▁▁▇▁▁
 19    59.7    59.7    59.7    59.7    59.7 ▁▁▇▁▁
 20    50.4    50.4    50.4    50.4    50.4 ▁▁▇▁▁
 21    80.7    80.7    80.7    80.7    80.7 ▁▁▇▁▁
 22    44.7    44.7    44.7    44.7    44.7 ▁▁▇▁▁
 23    50.7    50.7    50.7    50.7    50.7 ▁▁▇▁▁
 24    78.6    78.6    78.6    78.6    78.6 ▁▁▇▁▁
 25    73.0    73.0    73.0    73.0    73.0 ▁▁▇▁▁
 26    72.9    72.9    72.9    72.9    72.9 ▁▁▇▁▁
 27    65.2    65.2    65.2    65.2    65.2 ▁▁▇▁▁
 28    46.5    46.5    46.5    46.5    46.5 ▁▁▇▁▁
 29    55.3    55.3    55.3    55.3    55.3 ▁▁▇▁▁
 30    78.8    78.8    78.8    78.8    78.8 ▁▁▇▁▁
 31    48.3    48.3    48.3    48.3    48.3 ▁▁▇▁▁
 32    75.7    75.7    75.7    75.7    75.7 ▁▁▇▁▁
 33    78.3    78.3    78.3    78.3    78.3 ▁▁▇▁▁
 34    76.5    76.5    76.5    76.5    76.5 ▁▁▇▁▁
 35    78.3    78.3    78.3    78.3    78.3 ▁▁▇▁▁
 36    54.8    54.8    54.8    54.8    54.8 ▁▁▇▁▁
 37    72.2    72.2    72.2    72.2    72.2 ▁▁▇▁▁
 38    75.0    75.0    75.0    75.0    75.0 ▁▁▇▁▁
 39    71.3    71.3    71.3    71.3    71.3 ▁▁▇▁▁
 40    71.9    71.9    71.9    71.9    71.9 ▁▁▇▁▁
 41    51.6    51.6    51.6    51.6    51.6 ▁▁▇▁▁
 42    58.0    58.0    58.0    58.0    58.0 ▁▁▇▁▁
 43    52.9    52.9    52.9    52.9    52.9 ▁▁▇▁▁
 44    79.3    79.3    79.3    79.3    79.3 ▁▁▇▁▁
 45    80.7    80.7    80.7    80.7    80.7 ▁▁▇▁▁
 46    56.7    56.7    56.7    56.7    56.7 ▁▁▇▁▁
 47    59.4    59.4    59.4    59.4    59.4 ▁▁▇▁▁
 48    79.4    79.4    79.4    79.4    79.4 ▁▁▇▁▁
 49    60.0    60.0    60.0    60.0    60.0 ▁▁▇▁▁
 50    79.5    79.5    79.5    79.5    79.5 ▁▁▇▁▁
 51    70.3    70.3    70.3    70.3    70.3 ▁▁▇▁▁
 52    56.0    56.0    56.0    56.0    56.0 ▁▁▇▁▁
 53    46.4    46.4    46.4    46.4    46.4 ▁▁▇▁▁
 54    60.9    60.9    60.9    60.9    60.9 ▁▁▇▁▁
 55    70.2    70.2    70.2    70.2    70.2 ▁▁▇▁▁
 56    82.2    82.2    82.2    82.2    82.2 ▁▁▇▁▁
 57    73.3    73.3    73.3    73.3    73.3 ▁▁▇▁▁
 58    81.8    81.8    81.8    81.8    81.8 ▁▁▇▁▁
 59    64.7    64.7    64.7    64.7    64.7 ▁▁▇▁▁
 60    70.6    70.6    70.6    70.6    70.6 ▁▁▇▁▁
 61    71.0    71.0    71.0    71.0    71.0 ▁▁▇▁▁
 62    59.5    59.5    59.5    59.5    59.5 ▁▁▇▁▁
 63    78.9    78.9    78.9    78.9    78.9 ▁▁▇▁▁
 64    80.7    80.7    80.7    80.7    80.7 ▁▁▇▁▁
 65    80.5    80.5    80.5    80.5    80.5 ▁▁▇▁▁
 66    72.6    72.6    72.6    72.6    72.6 ▁▁▇▁▁
 67    82.6    82.6    82.6    82.6    82.6 ▁▁▇▁▁
 68    72.5    72.5    72.5    72.5    72.5 ▁▁▇▁▁
 69    54.1    54.1    54.1    54.1    54.1 ▁▁▇▁▁
 70    67.3    67.3    67.3    67.3    67.3 ▁▁▇▁▁
 71    78.6    78.6    78.6    78.6    78.6 ▁▁▇▁▁
 72    77.6    77.6    77.6    77.6    77.6 ▁▁▇▁▁
 73    72.0    72.0    72.0    72.0    72.0 ▁▁▇▁▁
 74    42.6    42.6    42.6    42.6    42.6 ▁▁▇▁▁
 75    45.7    45.7    45.7    45.7    45.7 ▁▁▇▁▁
 76    74.0    74.0    74.0    74.0    74.0 ▁▁▇▁▁
 77    59.4    59.4    59.4    59.4    59.4 ▁▁▇▁▁
 78    48.3    48.3    48.3    48.3    48.3 ▁▁▇▁▁
 79    74.2    74.2    74.2    74.2    74.2 ▁▁▇▁▁
 80    54.5    54.5    54.5    54.5    54.5 ▁▁▇▁▁
 81    64.2    64.2    64.2    64.2    64.2 ▁▁▇▁▁
 82    72.8    72.8    72.8    72.8    72.8 ▁▁▇▁▁
 83    76.2    76.2    76.2    76.2    76.2 ▁▁▇▁▁
 84    66.8    66.8    66.8    66.8    66.8 ▁▁▇▁▁
 85    74.5    74.5    74.5    74.5    74.5 ▁▁▇▁▁
 86    71.2    71.2    71.2    71.2    71.2 ▁▁▇▁▁
 87    42.1    42.1    42.1    42.1    42.1 ▁▁▇▁▁
 88    62.1    62.1    62.1    62.1    62.1 ▁▁▇▁▁
 89    52.9    52.9    52.9    52.9    52.9 ▁▁▇▁▁
 90    63.8    63.8    63.8    63.8    63.8 ▁▁▇▁▁
 91    79.8    79.8    79.8    79.8    79.8 ▁▁▇▁▁
 92    80.2    80.2    80.2    80.2    80.2 ▁▁▇▁▁
 93    72.9    72.9    72.9    72.9    72.9 ▁▁▇▁▁
 94    56.9    56.9    56.9    56.9    56.9 ▁▁▇▁▁
 95    46.9    46.9    46.9    46.9    46.9 ▁▁▇▁▁
 96    80.2    80.2    80.2    80.2    80.2 ▁▁▇▁▁
 97    75.6    75.6    75.6    75.6    75.6 ▁▁▇▁▁
 98    65.5    65.5    65.5    65.5    65.5 ▁▁▇▁▁
 99    75.5    75.5    75.5    75.5    75.5 ▁▁▇▁▁
100    71.8    71.8    71.8    71.8    71.8 ▁▁▇▁▁
101    71.4    71.4    71.4    71.4    71.4 ▁▁▇▁▁
102    71.7    71.7    71.7    71.7    71.7 ▁▁▇▁▁
103    75.6    75.6    75.6    75.6    75.6 ▁▁▇▁▁
104    78.1    78.1    78.1    78.1    78.1 ▁▁▇▁▁
105    78.7    78.7    78.7    78.7    78.7 ▁▁▇▁▁
106    76.4    76.4    76.4    76.4    76.4 ▁▁▇▁▁
107    72.5    72.5    72.5    72.5    72.5 ▁▁▇▁▁
108    46.2    46.2    46.2    46.2    46.2 ▁▁▇▁▁
109    65.5    65.5    65.5    65.5    65.5 ▁▁▇▁▁
110    72.8    72.8    72.8    72.8    72.8 ▁▁▇▁▁
111    63.1    63.1    63.1    63.1    63.1 ▁▁▇▁▁
112    74.0    74.0    74.0    74.0    74.0 ▁▁▇▁▁
113    42.6    42.6    42.6    42.6    42.6 ▁▁▇▁▁
114    80.0    80.0    80.0    80.0    80.0 ▁▁▇▁▁
115    74.7    74.7    74.7    74.7    74.7 ▁▁▇▁▁
116    77.9    77.9    77.9    77.9    77.9 ▁▁▇▁▁
117    48.2    48.2    48.2    48.2    48.2 ▁▁▇▁▁
118    49.3    49.3    49.3    49.3    49.3 ▁▁▇▁▁
119    80.9    80.9    80.9    80.9    80.9 ▁▁▇▁▁
120    72.4    72.4    72.4    72.4    72.4 ▁▁▇▁▁
121    58.6    58.6    58.6    58.6    58.6 ▁▁▇▁▁
122    39.6    39.6    39.6    39.6    39.6 ▁▁▇▁▁
123    80.9    80.9    80.9    80.9    80.9 ▁▁▇▁▁
124    81.7    81.7    81.7    81.7    81.7 ▁▁▇▁▁
125    74.1    74.1    74.1    74.1    74.1 ▁▁▇▁▁
126    78.4    78.4    78.4    78.4    78.4 ▁▁▇▁▁
127    52.5    52.5    52.5    52.5    52.5 ▁▁▇▁▁
128    70.6    70.6    70.6    70.6    70.6 ▁▁▇▁▁
129    58.4    58.4    58.4    58.4    58.4 ▁▁▇▁▁
130    69.8    69.8    69.8    69.8    69.8 ▁▁▇▁▁
131    73.9    73.9    73.9    73.9    73.9 ▁▁▇▁▁
132    71.8    71.8    71.8    71.8    71.8 ▁▁▇▁▁
133    51.5    51.5    51.5    51.5    51.5 ▁▁▇▁▁
134    79.4    79.4    79.4    79.4    79.4 ▁▁▇▁▁
135    78.2    78.2    78.2    78.2    78.2 ▁▁▇▁▁
136    76.4    76.4    76.4    76.4    76.4 ▁▁▇▁▁
137    73.7    73.7    73.7    73.7    73.7 ▁▁▇▁▁
138    74.2    74.2    74.2    74.2    74.2 ▁▁▇▁▁
139    73.4    73.4    73.4    73.4    73.4 ▁▁▇▁▁
140    62.7    62.7    62.7    62.7    62.7 ▁▁▇▁▁
141    42.4    42.4    42.4    42.4    42.4 ▁▁▇▁▁
142    43.5    43.5    43.5    43.5    43.5 ▁▁▇▁▁
143   975.    975.    975.    975.    975.  ▁▁▇▁▁
144  5937.   5937.   5937.   5937.   5937.  ▁▁▇▁▁
145  6223.   6223.   6223.   6223.   6223.  ▁▁▇▁▁
146  4797.   4797.   4797.   4797.   4797.  ▁▁▇▁▁
147 12779.  12779.  12779.  12779.  12779.  ▁▁▇▁▁
148 34435.  34435.  34435.  34435.  34435.  ▁▁▇▁▁
149 36126.  36126.  36126.  36126.  36126.  ▁▁▇▁▁
150 29796.  29796.  29796.  29796.  29796.  ▁▁▇▁▁
151  1391.   1391.   1391.   1391.   1391.  ▁▁▇▁▁
152 33693.  33693.  33693.  33693.  33693.  ▁▁▇▁▁
153  1441.   1441.   1441.   1441.   1441.  ▁▁▇▁▁
154  3822.   3822.   3822.   3822.   3822.  ▁▁▇▁▁
155  7446.   7446.   7446.   7446.   7446.  ▁▁▇▁▁
156 12570.  12570.  12570.  12570.  12570.  ▁▁▇▁▁
157  9066.   9066.   9066.   9066.   9066.  ▁▁▇▁▁
158 10681.  10681.  10681.  10681.  10681.  ▁▁▇▁▁
159  1217.   1217.   1217.   1217.   1217.  ▁▁▇▁▁
160   430.    430.    430.    430.    430.  ▁▁▇▁▁
161  1714.   1714.   1714.   1714.   1714.  ▁▁▇▁▁
162  2042.   2042.   2042.   2042.   2042.  ▁▁▇▁▁
163 36319.  36319.  36319.  36319.  36319.  ▁▁▇▁▁
164   706.    706.    706.    706.    706.  ▁▁▇▁▁
165  1704.   1704.   1704.   1704.   1704.  ▁▁▇▁▁
166 13172.  13172.  13172.  13172.  13172.  ▁▁▇▁▁
167  4959.   4959.   4959.   4959.   4959.  ▁▁▇▁▁
168  7007.   7007.   7007.   7007.   7007.  ▁▁▇▁▁
169   986.    986.    986.    986.    986.  ▁▁▇▁▁
170   278.    278.    278.    278.    278.  ▁▁▇▁▁
171  3633.   3633.   3633.   3633.   3633.  ▁▁▇▁▁
172  9645.   9645.   9645.   9645.   9645.  ▁▁▇▁▁
173  1545.   1545.   1545.   1545.   1545.  ▁▁▇▁▁
174 14619.  14619.  14619.  14619.  14619.  ▁▁▇▁▁
175  8948.   8948.   8948.   8948.   8948.  ▁▁▇▁▁
176 22833.  22833.  22833.  22833.  22833.  ▁▁▇▁▁
177 35278.  35278.  35278.  35278.  35278.  ▁▁▇▁▁
178  2082.   2082.   2082.   2082.   2082.  ▁▁▇▁▁
179  6025.   6025.   6025.   6025.   6025.  ▁▁▇▁▁
180  6873.   6873.   6873.   6873.   6873.  ▁▁▇▁▁
181  5581.   5581.   5581.   5581.   5581.  ▁▁▇▁▁
182  5728.   5728.   5728.   5728.   5728.  ▁▁▇▁▁
183 12154.  12154.  12154.  12154.  12154.  ▁▁▇▁▁
184   641.    641.    641.    641.    641.  ▁▁▇▁▁
185   691.    691.    691.    691.    691.  ▁▁▇▁▁
186 33207.  33207.  33207.  33207.  33207.  ▁▁▇▁▁
187 30470.  30470.  30470.  30470.  30470.  ▁▁▇▁▁
188 13206.  13206.  13206.  13206.  13206.  ▁▁▇▁▁
189   753.    753.    753.    753.    753.  ▁▁▇▁▁
190 32170.  32170.  32170.  32170.  32170.  ▁▁▇▁▁
191  1328.   1328.   1328.   1328.   1328.  ▁▁▇▁▁
192 27538.  27538.  27538.  27538.  27538.  ▁▁▇▁▁
193  5186.   5186.   5186.   5186.   5186.  ▁▁▇▁▁
194   943.    943.    943.    943.    943.  ▁▁▇▁▁
195   579.    579.    579.    579.    579.  ▁▁▇▁▁
196  1202.   1202.   1202.   1202.   1202.  ▁▁▇▁▁
197  3548.   3548.   3548.   3548.   3548.  ▁▁▇▁▁
198 39725.  39725.  39725.  39725.  39725.  ▁▁▇▁▁
199 18009.  18009.  18009.  18009.  18009.  ▁▁▇▁▁
200 36181.  36181.  36181.  36181.  36181.  ▁▁▇▁▁
201  2452.   2452.   2452.   2452.   2452.  ▁▁▇▁▁
202  3541.   3541.   3541.   3541.   3541.  ▁▁▇▁▁
203 11606.  11606.  11606.  11606.  11606.  ▁▁▇▁▁
204  4471.   4471.   4471.   4471.   4471.  ▁▁▇▁▁
205 40676.  40676.  40676.  40676.  40676.  ▁▁▇▁▁
206 25523.  25523.  25523.  25523.  25523.  ▁▁▇▁▁
207 28570.  28570.  28570.  28570.  28570.  ▁▁▇▁▁
208  7321.   7321.   7321.   7321.   7321.  ▁▁▇▁▁
209 31656.  31656.  31656.  31656.  31656.  ▁▁▇▁▁
210  4519.   4519.   4519.   4519.   4519.  ▁▁▇▁▁
211  1463.   1463.   1463.   1463.   1463.  ▁▁▇▁▁
212  1593.   1593.   1593.   1593.   1593.  ▁▁▇▁▁
213 23348.  23348.  23348.  23348.  23348.  ▁▁▇▁▁
214 47307.  47307.  47307.  47307.  47307.  ▁▁▇▁▁
215 10461.  10461.  10461.  10461.  10461.  ▁▁▇▁▁
216  1569.   1569.   1569.   1569.   1569.  ▁▁▇▁▁
217   415.    415.    415.    415.    415.  ▁▁▇▁▁
218 12057.  12057.  12057.  12057.  12057.  ▁▁▇▁▁
219  1045.   1045.   1045.   1045.   1045.  ▁▁▇▁▁
220   759.    759.    759.    759.    759.  ▁▁▇▁▁
221 12452.  12452.  12452.  12452.  12452.  ▁▁▇▁▁
222  1043.   1043.   1043.   1043.   1043.  ▁▁▇▁▁
223  1803.   1803.   1803.   1803.   1803.  ▁▁▇▁▁
224 10957.  10957.  10957.  10957.  10957.  ▁▁▇▁▁
225 11978.  11978.  11978.  11978.  11978.  ▁▁▇▁▁
226  3096.   3096.   3096.   3096.   3096.  ▁▁▇▁▁
227  9254.   9254.   9254.   9254.   9254.  ▁▁▇▁▁
228  3820.   3820.   3820.   3820.   3820.  ▁▁▇▁▁
229   824.    824.    824.    824.    824.  ▁▁▇▁▁
230   944     944     944     944     944   ▁▁▇▁▁
231  4811.   4811.   4811.   4811.   4811.  ▁▁▇▁▁
232  1091.   1091.   1091.   1091.   1091.  ▁▁▇▁▁
233 36798.  36798.  36798.  36798.  36798.  ▁▁▇▁▁
234 25185.  25185.  25185.  25185.  25185.  ▁▁▇▁▁
235  2749.   2749.   2749.   2749.   2749.  ▁▁▇▁▁
236   620.    620.    620.    620.    620.  ▁▁▇▁▁
237  2014.   2014.   2014.   2014.   2014.  ▁▁▇▁▁
238 49357.  49357.  49357.  49357.  49357.  ▁▁▇▁▁
239 22316.  22316.  22316.  22316.  22316.  ▁▁▇▁▁
240  2606.   2606.   2606.   2606.   2606.  ▁▁▇▁▁
241  9809.   9809.   9809.   9809.   9809.  ▁▁▇▁▁
242  4173.   4173.   4173.   4173.   4173.  ▁▁▇▁▁
243  7409.   7409.   7409.   7409.   7409.  ▁▁▇▁▁
244  3190.   3190.   3190.   3190.   3190.  ▁▁▇▁▁
245 15390.  15390.  15390.  15390.  15390.  ▁▁▇▁▁
246 20510.  20510.  20510.  20510.  20510.  ▁▁▇▁▁
247 19329.  19329.  19329.  19329.  19329.  ▁▁▇▁▁
248  7670.   7670.   7670.   7670.   7670.  ▁▁▇▁▁
249 10808.  10808.  10808.  10808.  10808.  ▁▁▇▁▁
250   863.    863.    863.    863.    863.  ▁▁▇▁▁
251  1598.   1598.   1598.   1598.   1598.  ▁▁▇▁▁
252 21655.  21655.  21655.  21655.  21655.  ▁▁▇▁▁
253  1712.   1712.   1712.   1712.   1712.  ▁▁▇▁▁
254  9787.   9787.   9787.   9787.   9787.  ▁▁▇▁▁
255   863.    863.    863.    863.    863.  ▁▁▇▁▁
256 47143.  47143.  47143.  47143.  47143.  ▁▁▇▁▁
257 18678.  18678.  18678.  18678.  18678.  ▁▁▇▁▁
258 25768.  25768.  25768.  25768.  25768.  ▁▁▇▁▁
259   926.    926.    926.    926.    926.  ▁▁▇▁▁
260  9270.   9270.   9270.   9270.   9270.  ▁▁▇▁▁
261 28821.  28821.  28821.  28821.  28821.  ▁▁▇▁▁
262  3970.   3970.   3970.   3970.   3970.  ▁▁▇▁▁
263  2602.   2602.   2602.   2602.   2602.  ▁▁▇▁▁
264  4513.   4513.   4513.   4513.   4513.  ▁▁▇▁▁
265 33860.  33860.  33860.  33860.  33860.  ▁▁▇▁▁
266 37506.  37506.  37506.  37506.  37506.  ▁▁▇▁▁
267  4185.   4185.   4185.   4185.   4185.  ▁▁▇▁▁
268 28718.  28718.  28718.  28718.  28718.  ▁▁▇▁▁
269  1107.   1107.   1107.   1107.   1107.  ▁▁▇▁▁
270  7458.   7458.   7458.   7458.   7458.  ▁▁▇▁▁
271   883.    883.    883.    883.    883.  ▁▁▇▁▁
272 18009.  18009.  18009.  18009.  18009.  ▁▁▇▁▁
273  7093.   7093.   7093.   7093.   7093.  ▁▁▇▁▁
274  8458.   8458.   8458.   8458.   8458.  ▁▁▇▁▁
275  1056.   1056.   1056.   1056.   1056.  ▁▁▇▁▁
276 33203.  33203.  33203.  33203.  33203.  ▁▁▇▁▁
277 42952.  42952.  42952.  42952.  42952.  ▁▁▇▁▁
278 10611.  10611.  10611.  10611.  10611.  ▁▁▇▁▁
279 11416.  11416.  11416.  11416.  11416.  ▁▁▇▁▁
280  2442.   2442.   2442.   2442.   2442.  ▁▁▇▁▁
281  3025.   3025.   3025.   3025.   3025.  ▁▁▇▁▁
282  2281.   2281.   2281.   2281.   2281.  ▁▁▇▁▁
283  1271.   1271.   1271.   1271.   1271.  ▁▁▇▁▁
284   470.    470.    470.    470.    470.  ▁▁▇▁▁
In [30]:
# In Figure 6.14, the variable we facet by is continent, which is categorical with five levels, each corresponding to the five continents of the world.

ggplot(gapminder2007, aes(x = lifeExp)) +
  geom_histogram(binwidth = 5, color = "white") +
  labs(x = "Life expectancy", y = "Number of countries", 
       title = "Life expectancy by continent") +
  facet_wrap(~ continent, nrow = 2)
In [9]:
lifeExp_model <- lm(lifeExp ~ continent, data = gapminder2007)
get_regression_table(lifeExp_model)
A tibble: 5 × 7
termestimatestd_errorstatisticp_valuelower_ciupper_ci
<chr><dbl><dbl><dbl><dbl><dbl><dbl>
intercept 54.8061.02553.446052.77856.834
continentAmericas18.8021.80010.448015.24322.361
continentAsia 15.9221.646 9.675012.66819.177
continentEurope 22.8431.69513.474019.49026.195
continentOceania 25.9135.328 4.863015.37736.450
In [11]:
regression_points <- get_regression_points(lifeExp_model)
regression_points
A tibble: 142 × 5
IDlifeExpcontinentlifeExp_hatresidual
<int><dbl><fct><dbl><dbl>
143.828Asia 70.728-26.900
276.423Europe 77.649 -1.226
372.301Africa 54.806 17.495
442.731Africa 54.806-12.075
575.320Americas73.608 1.712
681.235Oceania 80.719 0.516
779.829Europe 77.649 2.180
875.635Asia 70.728 4.907
964.062Asia 70.728 -6.666
1079.441Europe 77.649 1.792
1156.728Africa 54.806 1.922
1265.554Americas73.608 -8.054
1374.852Europe 77.649 -2.797
1450.728Africa 54.806 -4.078
1572.390Americas73.608 -1.218
1673.005Europe 77.649 -4.644
1752.295Africa 54.806 -2.511
1849.580Africa 54.806 -5.226
1959.723Asia 70.728-11.005
2050.430Africa 54.806 -4.376
2180.653Americas73.608 7.045
2244.741Africa 54.806-10.065
2350.651Africa 54.806 -4.155
2478.553Americas73.608 4.945
2572.961Asia 70.728 2.233
2672.889Americas73.608 -0.719
2765.152Africa 54.806 10.346
2846.462Africa 54.806 -8.344
2955.322Africa 54.806 0.516
3078.782Americas73.608 5.174
11342.568Africa 54.806-12.238
11479.972Asia 70.728 9.244
11574.663Europe 77.649 -2.986
11677.926Europe 77.649 0.277
11748.159Africa 54.806 -6.647
11849.339Africa 54.806 -5.467
11980.941Europe 77.649 3.292
12072.396Asia 70.728 1.668
12158.556Africa 54.806 3.750
12239.613Africa 54.806-15.193
12380.884Europe 77.649 3.235
12481.701Europe 77.649 4.052
12574.143Asia 70.728 3.415
12678.400Asia 70.728 7.672
12752.517Africa 54.806 -2.289
12870.616Asia 70.728 -0.112
12958.420Africa 54.806 3.614
13069.819Americas73.608 -3.789
13173.923Africa 54.806 19.117
13271.777Europe 77.649 -5.872
13351.542Africa 54.806 -3.264
13479.425Europe 77.649 1.776
13578.242Americas73.608 4.634
13676.384Americas73.608 2.776
13773.747Americas73.608 0.139
13874.249Asia 70.728 3.521
13973.422Asia 70.728 2.694
14062.698Asia 70.728 -8.030
14142.384Africa 54.806-12.422
14243.487Africa 54.806-11.319
In [14]:
ggplot(elms, aes(x=family_income, y=gift_aid)) + geom_point() +  geom_smooth(method = "lm", se=FALSE)
`geom_smooth()` using formula 'y ~ x'

In [15]:
giftinc <- lm(family_income~gift_aid, data = elms); giftinc
get_regression_table(giftinc) 
rgiftinc <- get_regression_points(giftinc); rgiftinc
Call:
lm(formula = family_income ~ gift_aid, data = elms)

Coefficients:
(Intercept)     gift_aid  
    216.823       -5.771  
A tibble: 2 × 7
termestimatestd_errorstatisticp_valuelower_ciupper_ci
<chr><dbl><dbl><dbl><dbl><dbl><dbl>
intercept216.82329.915 7.2480156.675276.970
gift_aid -5.771 1.448-3.9850 -8.683 -2.859
A tibble: 50 × 5
IDfamily_incomegift_aidfamily_income_hatresidual
<int><dbl><dbl><dbl><dbl>
1 92.92221.720 91.481 1.441
2 0.25027.470 58.299-58.049
3 53.09227.750 56.683 -3.591
4 50.20027.220 59.741 -9.541
5137.61318.000112.948 24.665
6 47.95718.520109.947-61.990
7113.53413.000141.802-28.268
8168.57913.000141.802 26.777
9208.11514.000136.031 72.084
10 12.52325.470 69.840-57.317
11119.82221.000 95.636 24.186
12 50.56317.476115.972-65.409
13 16.12022.470 87.153-71.033
14206.93211.000153.344 53.588
15 68.67825.720 68.398 0.280
16 73.59832.720 28.002 45.596
17218.12023.000 84.094134.026
18 89.98316.000124.490-34.507
19271.97420.000101.407170.567
20118.16524.000 78.323 39.842
21108.39515.500127.375-18.980
22235.522 7.000176.427 59.095
23 78.92620.000101.407-22.481
24 76.85423.520 81.093 -4.239
25 98.49614.000136.031-37.535
26134.58610.000159.115-24.529
27 75.15721.120 94.943-19.786
28135.85721.000 95.636 40.221
29 79.44827.500 58.126 21.322
30 80.85820.550 98.233-17.375
31 86.14014.300134.300-48.160
32 40.49018.320111.102-70.612
33143.33718.000112.948 30.389
34 97.66410.000159.115-61.451
35 74.71321.000 95.636-20.923
36178.79513.600138.340 40.455
37 71.55020.470 98.694-27.144
38 92.60521.000 95.636 -3.031
39 62.54621.600 92.173-29.627
40 0.00027.470 58.299-58.299
41159.98125.814 67.855 92.126
42 40.39725.970 66.955-26.558
43 85.20325.558 69.332 15.871
44 27.16420.470 98.694-71.530
45146.39717.000118.719 27.678
46 14.08920.420 98.983-84.894
47217.44320.000101.407116.036
48140.09315.000130.261 9.832
49104.14717.560115.487-11.340
50 83.33323.500 81.209 2.124
In [8]:
ggplot(regression_points, aes(x = continent, y = residual)) +
  geom_jitter(width = 0.1) + 
  labs(x = "Continent", y = "Residual") +
  geom_hline(yintercept = 0, col = "blue")
# There seems to be a rough balance of both positive and negative residuals for all 5 continents. However, there is one clear outlier in Asia. It has the smallest residual, hence also has the smallest life expectancy in Asia.
In [10]:
# Second, let’s look at a histogram of all 142 values of residuals in Figure 6.17. In this case, the residuals form a rather nice bell-shape, although there are a couple of very low and very high values at the tails. As we said previously, searching for patterns in residuals can be somewhat subjective, but ideally we hope there are no “drastic” patterns.

ggplot(regression_points, aes(x = residual)) +
  geom_histogram(binwidth = 5, color = "white") +
  labs(x = "Residual")
In [2]:
evals
IDscoreagebty_avggenderethnicitylanguagerankpic_outfitpic_colorcls_did_evalcls_studentscls_level
1 4.7 36 5.000 female minority english tenure tracknot formal color 24 43 upper
2 4.1 36 5.000 female minority english tenure tracknot formal color 86 125 upper
3 3.9 36 5.000 female minority english tenure tracknot formal color 76 125 upper
4 4.8 36 5.000 female minority english tenure tracknot formal color 77 123 upper
5 4.6 59 3.000 male not minorityenglish tenured not formal color 17 20 upper
6 4.3 59 3.000 male not minorityenglish tenured not formal color 35 40 upper
7 2.8 59 3.000 male not minorityenglish tenured not formal color 39 44 upper
8 4.1 51 3.333 male not minorityenglish tenured not formal color 55 55 upper
9 3.4 51 3.333 male not minorityenglish tenured not formal color 111 195 upper
10 4.5 40 3.167 female not minorityenglish tenured not formal color 40 46 upper
11 3.8 40 3.167 female not minorityenglish tenured not formal color 24 27 upper
12 4.5 40 3.167 female not minorityenglish tenured not formal color 24 25 upper
13 4.6 40 3.167 female not minorityenglish tenured not formal color 17 20 upper
14 3.9 40 3.167 female not minorityenglish tenured not formal color 14 25 upper
15 3.9 40 3.167 female not minorityenglish tenured not formal color 37 42 upper
16 4.3 40 3.167 female not minorityenglish tenured not formal color 18 20 upper
17 4.5 40 3.167 female not minorityenglish tenured not formal color 15 18 upper
18 4.8 31 7.333 female not minorityenglish tenure tracknot formal color 42 48 upper
19 4.6 31 7.333 female not minorityenglish tenure tracknot formal color 40 44 upper
20 4.6 31 7.333 female not minorityenglish tenure tracknot formal color 38 48 upper
21 4.9 31 7.333 female not minorityenglish tenure tracknot formal color 40 45 upper
22 4.6 31 7.333 female not minorityenglish tenure tracknot formal color 52 59 upper
23 4.5 31 7.333 female not minorityenglish tenure tracknot formal color 49 87 upper
24 4.4 62 5.500 male not minorityenglish tenured formal color 182 282 upper
25 4.6 62 5.500 male not minorityenglish tenured formal color 160 292 upper
26 4.7 62 5.500 male not minorityenglish tenured formal color 79 130 upper
27 4.5 62 5.500 male not minorityenglish tenured formal color 176 285 upper
28 4.8 62 5.500 male not minorityenglish tenured formal color 155 272 upper
29 4.9 62 5.500 male not minorityenglish tenured formal color 166 286 upper
30 4.5 62 5.500 male not minorityenglish tenured formal color 186 302 upper
434 2.8 62 2.000 male not minorityenglish tenured not formal color 61 149 lower
435 3.1 62 2.000 male not minorityenglish tenured not formal color 49 137 lower
436 4.2 62 2.000 male not minorityenglish tenured not formal color 13 29 upper
437 3.4 62 2.000 male not minorityenglish tenured not formal color 28 55 lower
438 3.0 62 2.000 male not minorityenglish tenured not formal color 67 136 lower
439 3.3 35 7.833 female minority english tenure tracknot formal color 60 96 lower
440 3.6 35 7.833 female minority english tenure tracknot formal color 20 60 lower
441 3.7 35 7.833 female minority english tenure tracknot formal color 43 108 lower
442 3.6 61 3.333 male not minorityenglish tenured not formal color 27 39 lower
443 4.3 61 3.333 male not minorityenglish tenured not formal color 13 15 lower
444 4.1 52 4.500 female not minorityenglish tenured not formal color 61 111 lower
445 4.9 52 4.500 female not minorityenglish tenured not formal color 14 17 lower
446 4.8 52 4.500 female not minorityenglish tenured not formal color 19 19 lower
447 3.7 60 4.333 female not minority non-english tenure track formal black&white 23 27 upper
448 3.9 60 4.333 female not minority non-english tenure track formal black&white 18 19 upper
449 4.5 60 4.333 female not minority non-english tenure track formal black&white 11 13 upper
450 3.6 60 4.333 female not minority non-english tenure track formal black&white 18 19 upper
451 4.4 60 4.333 female not minority non-english tenure track formal black&white 11 22 upper
452 3.4 60 4.333 female not minority non-english tenure track formal black&white 7 20 upper
453 4.4 60 4.333 female not minority non-english tenure track formal black&white 24 27 upper
454 4.5 32 6.833 male not minorityenglish tenure tracknot formal color 98 132 lower
455 4.5 32 6.833 male not minorityenglish tenure tracknot formal color 111 127 lower
456 4.5 32 6.833 male not minorityenglish tenure tracknot formal color 62 85 upper
457 4.6 32 6.833 male not minorityenglish tenure tracknot formal color 76 101 lower
458 4.1 32 6.833 male not minorityenglish tenure tracknot formal color 9 21 lower
459 4.5 32 6.833 male not minorityenglish tenure tracknot formal color 52 86 upper
460 3.5 42 5.333 female minority non-english tenure tracknot formal color 48 84 upper
461 4.4 42 5.333 female minority non-english tenure tracknot formal color 52 67 upper
462 4.4 42 5.333 female minority non-english tenure tracknot formal color 54 66 upper
463 4.1 42 5.333 female minority non-english tenure tracknot formal color 28 35 lower
In [3]:
# Let’s load the data, select only a subset of the variables, and look at the raw values. 
# Recall you can look at the raw values by running View() in the console in RStudio to pop-up the spreadsheet 
# viewer with the data frame of interest as the argument to View(). Here, however, we present only a snapshot of 
# five randomly chosen rows:

evals_ch6 <- evals %>%
  select(score, bty_avg, age)
evals_ch6 %>% 
  sample_n(5)
# sample_n() and sample_frac() have been superseded in favour of slice_sample().
scorebty_avgage
4.7 8.16739
3.3 5.50037
3.8 4.83342
3.9 4.33357
4.2 4.16745
In [6]:
evals_ch6 %>% 
  get_correlation(formula = score ~ bty_avg)
correlation
0.1871424
In [7]:
ggplot(data.frame(evals$score), aes(x = evals$score)) + geom_density()
In [8]:
# The correlation coefficient can also be computed using the cor() function, where in this case the inputs to the function are the two numerical variables from which we want to calculate the correlation coefficient. Recall from Subsection 2.4.3 that the $ pulls out specific variables from a data frame:

cor(x = evals_ch6$bty_avg, y = evals_ch6$score)
0.187142354868474
In [9]:
# Let’s now proceed by visualizing this data. Since both the score and bty_avg variables are numerical, a scatterplot is an appropriate graph to visualize this data. Let’s do this using geom_point() and set informative axes labels and title and display the result in Figure 6.2.

ggplot(evals_ch6, aes(x = bty_avg, y = score)) +
  geom_point() +
  labs(x = "Beauty Score", y = "Teaching Score", 
       title = "Relationship of teaching and beauty scores")
In [10]:
# adding a “regression line” is easily done by adding a new layer to the ggplot code that created Figure 6.3: + geom_smooth(method = "lm"). A regression line is a “best fitting” line in that of all possible lines you could draw on this plot, it is “best” in terms of some mathematical criteria. 

ggplot(evals_ch6, aes(x = bty_avg, y = score)) +
  geom_point() +
  labs(x = "Beauty Score", y = "Teaching Score", 
       title = "Relationship of teaching and beauty scores") +  
  geom_smooth(method = "lm")
In [11]:
ggplot(evals_ch6, aes(x = bty_avg, y = score)) +
  geom_point() +
  labs(x = "Beauty Score", y = "Teaching Score", 
       title = "Relationship of teaching and beauty scores") +
  geom_smooth(method = "lm", se = FALSE)
In [18]:
# Here is what the regression table from Subsection 6.1.2 looks like:

score_model <- lm(score ~ bty_avg, data = evals_ch6)
get_regression_table(score_model)
termestimatestd_errorstatisticp_valuelower_ciupper_ci
intercept3.880 0.076 50.961 0 3.731 4.030
bty_avg 0.067 0.016 4.090 0 0.035 0.099
In [13]:
# The get_regression_table() function takes the above two functions that already existed in other R packages, uses them, and hides the details as seen below. This was on the editorial decision on our part as we felt the following code was unfortunately out of the reach for some new coders, so the following wrapper function was written so that users need only focus on the output.

library(broom)
library(janitor)
score_model %>% 
  tidy(conf.int = TRUE) %>% 
  mutate_if(is.numeric, round, digits = 3) %>%
  clean_names() %>% 
  rename(lower_ci = conf_low,
         upper_ci = conf_high)

# # Note that the mutate_if() function is from the dplyr package and applies the round() function with 3 significant digits precision only to those variables that are numerical.
termestimatestd_errorstatisticp_valuelower_ciupper_ci
(Intercept)3.880 0.076 50.961 0 3.731 4.030
bty_avg 0.067 0.016 4.090 0 0.035 0.099
In [14]:
# Similarly, the second get_regression_points() function is another wrapper function, but this time returning information about the points in a regression rather than the regression table. It uses the augment() function in the broom package instead of tidy() as with get_regression_points().

library(broom)
library(janitor)
score_model %>% 
  augment() %>% 
  mutate_if(is.numeric, round, digits = 3) %>%
  clean_names() %>% 
  select(-c("se_fit", "hat", "sigma", "cooksd", "std_resid"))

# In this case, it outputs only variables of interest to us as new regression modelers: the outcome variable y (score), all explanatory/predictor variables (bty_avg), all resulting fitted values ^y used by applying the equation of the regression line to bty_avg, and the residual y−^y.
scorebty_avgfittedresid
4.7 5.000 4.214 0.486
4.1 5.000 4.214 -0.114
3.9 5.000 4.214 -0.314
4.8 5.000 4.214 0.586
4.6 3.000 4.080 0.520
4.3 3.000 4.080 0.220
2.8 3.000 4.080 -1.280
4.1 3.333 4.102 -0.002
3.4 3.333 4.102 -0.702
4.5 3.167 4.091 0.409
3.8 3.167 4.091 -0.291
4.5 3.167 4.091 0.409
4.6 3.167 4.091 0.509
3.9 3.167 4.091 -0.191
3.9 3.167 4.091 -0.191
4.3 3.167 4.091 0.209
4.5 3.167 4.091 0.409
4.8 7.333 4.369 0.431
4.6 7.333 4.369 0.231
4.6 7.333 4.369 0.231
4.9 7.333 4.369 0.531
4.6 7.333 4.369 0.231
4.5 7.333 4.369 0.131
4.4 5.500 4.247 0.153
4.6 5.500 4.247 0.353
4.7 5.500 4.247 0.453
4.5 5.500 4.247 0.253
4.8 5.500 4.247 0.553
4.9 5.500 4.247 0.653
4.5 5.500 4.247 0.253
2.8 2.000 4.014 -1.214
3.1 2.000 4.014 -0.914
4.2 2.000 4.014 0.186
3.4 2.000 4.014 -0.614
3.0 2.000 4.014 -1.014
3.3 7.833 4.402 -1.102
3.6 7.833 4.402 -0.802
3.7 7.833 4.402 -0.702
3.6 3.333 4.102 -0.502
4.3 3.333 4.102 0.198
4.1 4.500 4.180 -0.080
4.9 4.500 4.180 0.720
4.8 4.500 4.180 0.620
3.7 4.333 4.169 -0.469
3.9 4.333 4.169 -0.269
4.5 4.333 4.169 0.331
3.6 4.333 4.169 -0.569
4.4 4.333 4.169 0.231
3.4 4.333 4.169 -0.769
4.4 4.333 4.169 0.231
4.5 6.833 4.336 0.164
4.5 6.833 4.336 0.164
4.5 6.833 4.336 0.164
4.6 6.833 4.336 0.264
4.1 6.833 4.336 -0.236
4.5 6.833 4.336 0.164
3.5 5.333 4.236 -0.736
4.4 5.333 4.236 0.164
4.4 5.333 4.236 0.164
4.1 5.333 4.236 -0.136
In [8]:
score_model <- lm(score ~ bty_avg, data = evals_ch6)
score_model
Call:
lm(formula = score ~ bty_avg, data = evals_ch6)

Coefficients:
(Intercept)      bty_avg  
    3.88034      0.06664  
In [9]:
# Fit regression model:

score_model <- lm(score ~ bty_avg, data = evals_ch6)

# Get regression table:

get_regression_table(score_model)
termestimatestd_errorstatisticp_valuelower_ciupper_ci
intercept3.880 0.076 50.961 0 3.731 4.030
bty_avg 0.067 0.016 4.090 0 0.035 0.099
In [10]:
regression_points <- get_regression_points(score_model)
regression_points
IDscorebty_avgscore_hatresidual
1 4.7 5.000 4.214 0.486
2 4.1 5.000 4.214 -0.114
3 3.9 5.000 4.214 -0.314
4 4.8 5.000 4.214 0.586
5 4.6 3.000 4.080 0.520
6 4.3 3.000 4.080 0.220
7 2.8 3.000 4.080 -1.280
8 4.1 3.333 4.102 -0.002
9 3.4 3.333 4.102 -0.702
10 4.5 3.167 4.091 0.409
11 3.8 3.167 4.091 -0.291
12 4.5 3.167 4.091 0.409
13 4.6 3.167 4.091 0.509
14 3.9 3.167 4.091 -0.191
15 3.9 3.167 4.091 -0.191
16 4.3 3.167 4.091 0.209
17 4.5 3.167 4.091 0.409
18 4.8 7.333 4.369 0.431
19 4.6 7.333 4.369 0.231
20 4.6 7.333 4.369 0.231
21 4.9 7.333 4.369 0.531
22 4.6 7.333 4.369 0.231
23 4.5 7.333 4.369 0.131
24 4.4 5.500 4.247 0.153
25 4.6 5.500 4.247 0.353
26 4.7 5.500 4.247 0.453
27 4.5 5.500 4.247 0.253
28 4.8 5.500 4.247 0.553
29 4.9 5.500 4.247 0.653
30 4.5 5.500 4.247 0.253
434 2.8 2.000 4.014 -1.214
435 3.1 2.000 4.014 -0.914
436 4.2 2.000 4.014 0.186
437 3.4 2.000 4.014 -0.614
438 3.0 2.000 4.014 -1.014
439 3.3 7.833 4.402 -1.102
440 3.6 7.833 4.402 -0.802
441 3.7 7.833 4.402 -0.702
442 3.6 3.333 4.102 -0.502
443 4.3 3.333 4.102 0.198
444 4.1 4.500 4.180 -0.080
445 4.9 4.500 4.180 0.720
446 4.8 4.500 4.180 0.620
447 3.7 4.333 4.169 -0.469
448 3.9 4.333 4.169 -0.269
449 4.5 4.333 4.169 0.331
450 3.6 4.333 4.169 -0.569
451 4.4 4.333 4.169 0.231
452 3.4 4.333 4.169 -0.769
453 4.4 4.333 4.169 0.231
454 4.5 6.833 4.336 0.164
455 4.5 6.833 4.336 0.164
456 4.5 6.833 4.336 0.164
457 4.6 6.833 4.336 0.264
458 4.1 6.833 4.336 -0.236
459 4.5 6.833 4.336 0.164
460 3.5 5.333 4.236 -0.736
461 4.4 5.333 4.236 0.164
462 4.4 5.333 4.236 0.164
463 4.1 5.333 4.236 -0.136
In [2]:
ggplot(regression_points, aes(x = bty_avg, y = residual)) +
  geom_point() +
  labs(x = "Beauty Score", y = "Residual") +
  geom_hline(yintercept = 0, col = "blue", size = 1)
In [3]:
# The second way to perform a residual analysis is to look at the histogram of the residuals:

ggplot(regression_points, aes(x = residual)) +
  geom_histogram(binwidth = 0.25, color = "white") +
  labs(x = "Residual")
In [1]:
require(graphics)

## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
lm.D90 <- lm(weight ~ group - 1) # omitting intercept
In [2]:
anova(lm.D9)
summary(lm.D90)

opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))
plot(lm.D9, las = 1) # Residuals, Fitted, ...
par(opar)

### less simple examples in "See Also" above
DfSum SqMean SqF valuePr(>F)
group 1 0.688205 0.68820501.419101 0.2490232
Residuals18 8.729250 0.4849583 NA NA
Call:
lm(formula = weight ~ group - 1)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.0710 -0.4938  0.0685  0.2462  1.3690 

Coefficients:
         Estimate Std. Error t value Pr(>|t|)    
groupCtl   5.0320     0.2202   22.85 9.55e-15 ***
groupTrt   4.6610     0.2202   21.16 3.62e-14 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.6964 on 18 degrees of freedom
Multiple R-squared:  0.9818,	Adjusted R-squared:  0.9798 
F-statistic: 485.1 on 2 and 18 DF,  p-value: < 2.2e-16
In [ ]: