These exercises cover the sections of Data wrangling with tidy.

All files can be found in the “dataset” directory.

 

Exercise 8

 

  1. Create a nested dataframe of df1. Specifically only of the two largest groups by common_name, and nest by the common_name.
  2. Create a linear model between length_mm and IGF1_ng_ml using purrr over the nested dataframe. Add the results back to the dataframe
  3. Create a new nested variable containing projections for length_mm based on your linear model (the function predict will be helpful here).

ANSWERS

Answer 1

## # A tibble: 2 x 2
## # Groups:   common_name [2]
##   common_name              data
##   <chr>          <list<df[,4]>>
## 1 Chinook salmon       [46 × 4]
## 2 Steelhead            [38 × 4]

Answer 2

## # A tibble: 2 x 3
## # Groups:   common_name [2]
##   common_name              data my_model
##   <chr>          <list<df[,4]>> <list>  
## 1 Chinook salmon       [46 × 4] <lm>    
## 2 Steelhead            [38 × 4] <lm>

Answer 3

## # A tibble: 2 x 4
## # Groups:   common_name [2]
##   common_name              data my_model predictions
##   <chr>          <list<df[,4]>> <list>   <list>     
## 1 Chinook salmon       [46 × 4] <lm>     <dbl [42]> 
## 2 Steelhead            [38 × 4] <lm>     <dbl [35]>
## [[1]]
##        1        2        3        4        5        6        7        8        9       10       11 
## 174.0853 204.0953 210.3557 231.1932 148.3513 259.7978 190.8976 171.3871 190.7028 191.2548 181.2579 
##       12       13       14       15       16       17       18       19       20       22       23 
## 122.7105 139.3573 147.1669 205.1610 178.0563 211.4999 232.4803 146.3425 176.3637 231.4895 232.0093 
##       24       25       26       27       28       30       31       34       35       36       37 
## 153.6838 244.9919 135.1426 181.6404 191.7068 202.8107 189.6438 219.2654 156.6304 180.8808 160.2568 
##       38       39       40       41       42       43       44       45       46 
## 177.5445 144.6843 131.2548 177.3148 176.9896 127.9072 196.2106 143.8471 178.5774 
## 
## [[2]]
##        1        3        5        6        7        8        9       10       11       12       13 
## 214.2574 213.1801 215.4682 215.3350 213.5525 214.6490 215.0501 215.0165 215.1124 212.9443 216.4120 
##       14       15       16       17       18       19       20       21       22       23       24 
## 216.0293 214.1118 214.5615 212.1104 214.9008 212.3449 213.9893 214.4727 214.2505 213.1433 212.0261 
##       26       27       28       29       30       31       32       33       34       35       36 
## 214.7772 215.5983 214.5010 215.3785 214.8688 215.1779 215.5824 212.6976 214.1116 214.7586 216.2151 
##       37       38 
## 215.3987 214.0160