1

Aim: I want to use the map() function to do a linear model that involves categorical variables.

Problem: I am getting the following error but I know the categorical variable included, borrower_genders has five levels.

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

What I've done: The following code works perfectly fine when removing borrower_genders, not with it, why?

kiva_country%>%
 dplyr::group_by(country_code)%>%
 tidyr::nest(-country_code) %>%   
       mutate(model=map(data, ~lm(loan_usd~                        
                                 lender_count+
                                 borrower_genders,
                                  data=.)))

Data:

kiva_country<- structure(list(loan_usd = c(0.913621225, 0.085394175, 6.079311875, 
12.626263125, 0.024824985, 6.54695125, 0.1793277675, 0.21334368, 
0.12996942, 6.079311875, 14.496820625, 0.10343936, 650, 87.632824, 
6.079311875, 0.0597759225, 4.208754375, 0.9948269, 2.48706725, 
1.9896538, 4.208754375, 1.8705575, 2.338196875, 0.0939335925, 
5.144033125, 6.54695125, 0.1337112975, 3.741115, 6.079311875, 
4.47672105, 0.2228521625, 1.9896538, 0.224215244, 231.40113, 
0.6284916, 0.04964997, 0.3119930275, 0.029483707725, 2.48706725, 
14.42499005, 2.80583625, 4.208754375, 0.489196896, 200, 6.079311875, 
0.2793296, 0.36544849, 0.65066387, 2.80583625, 41.07788625, 1300, 
173.5508475, 0.02585984, 0.04149558865, 1000, 0.7576973525, 4000, 
0.308370055, 6.54695125, 137.4675435, 800, 9.948269, 0.13576416, 
0.2418007625, 112.27955575, 4.67639375, 30.12378325, 2.80583625, 
0.9948269, 0.10990432, 4.208754375, 6.079311875, 2.238360525, 
4.725427775, 20.108493125, 4.208754375, 6.217668125, 3.741115, 
0.13663068, 3.48189415, 11.690984375, 8.885148125, 1.9896538, 
10.28806625, 2500, 9.699562275, 4.9741345, 7.48223, 0.04964997, 
8.41750875, 3.2932654, 139.66481325, 7.014590625, 1800, 5.144033125, 
4.208754375, 7.014590625, 7.014590625, 35.60083475, 3.741115, 
1000, 54.770515, 1.6486415625, 0.04344372375, 0.108106928325, 
3.1130664, 2.338196875, 6.54695125, 4.208754375, 3.74391633, 
3.704923575, 1000, 3.704923575, 1.9896538, 1500, 9.699562275, 
5.144033125, 2.46994905, 89.12655875, 2.48706725, 4.67639375, 
4.208754375, 0.53484519, 8.41750875, 8.885148125, 3.48189415, 
19.16968025, 4.9741345, 500, 1.9896538, 0.1117124325, 7.949869375, 
4.208754375, 2.338196875, 2.058290875, 1100, 4.11658175, 6.54695125, 
8.41750875, 7.48223, 0.1004630041, 19.16968025, 2.238360525, 
0.445704325, 4.208754375, 0.33617792, 6.54695125, 424.235405, 
2.338196875, 3.741115, 0.2094972, 0.817726215, 0.61674011, 5.6116725, 
0.24824985, 9.820426875, 1.9896538, 0.2134854375, 0.0646496, 
0.3119930275, 19.173214375, 4.208754375, 0.024824985, 7.014590625, 
4.9741345, 0.11575085255, 1147.95914625, 0.9715994275, 1.243533625, 
2.222982255, 2.80583625, 3.667749, 1.6466327, 2.62710048, 3.741115, 
11.223345, 0.51762116375, 0.36544849, 1500, 2.5674243, 4.67639375, 
500, 5.96049055, 6.54695125, 0.038219621125, 2.9862189775, 0.024824985, 
139.875587, 0.294315025, 4.1507552, 6.9637883, 4.9741345, 2.34777983, 
0.1337112975, 2.48706725, 0.1538732125, 2.238360525, 4.3224108375, 
0.0646496, 15.2741715, 65.724618, 3.741115, 1175, 5.6116725, 
0.17078835, 0.2134854375, 250.6845575, 3.273475625, 575, 7.014590625, 
4.208754375, 183.290058, 3.48189415, 1000, 1.76407096, 2.238360525, 
3.273475625, 0.20494602, 5.6116725, 0.03878976, 500, 9.820426875, 
440.96208475, 1351.76417775, 4.208754375, 1500, 0.93527875, 328.39468725, 
0.802267785, 800, 4.67639375, 10.28806625, 2.48706725, 0.5379833025, 
0.4189944, 26.0410179, 0.10247301, 0.43963775, 5.6116725, 0.0620624625, 
0.29738816, 91.645029, 420.03971625, 106.80250425, 3.002586925, 
6.079311875, 5.5573853625, 800, 1.0376888, 0.153709515, 0.06826870875, 
4.208754375, 1.02990029, 190.92714375, 4.208754375, 4.208754375, 
4.9741345, 1.2008166, 2.48706725, 1657.24760775, 4.208754375, 
5.13693565, 0.6828194075, 5.222841225, 83.88382, 1.196990445, 
4.208754375, 1.243533625, 4.208754375, 4.208754375, 1.8705575, 
0.15515904, 2.80583625, 2.46994905, 6.54695125, 6.079311875, 
1.243533625, 43.816412, 16.5048705, 500, 0.21721861875, 7.705403475, 
4.208754375, 0.01861873875, 800, 0.2094972, 0.746120175, 5200, 
2.338196875, 7.48223, 1000, 0.119551845, 20.108493125, 0.124124925, 
0.1337112975, 5.720254675, 2.39398089, 0.256182525, 0.05171968, 
0.09050944, 1.02422911125, 0.548172735, 670.4304375, 1.243533625, 
0.10990432, 54.66472125, 4.11658175, 4.208754375, 1.8159554, 
4.9741345, 39.2449143, 8.207321925, 0.0388692568, 0.6828194075, 
0.47840704, 104.0639785, 0.5379833025, 5.9689614, 110.09751375, 
12.15862375, 2.80583625, 2.058290875, 49.2934635, 0.04699248, 
0.1626659675, 6.54695125, 2500, 160.37880075, 4.4012988, 1500, 
0.1793277675, 2.9844807, 8.885148125, 5.144033125, 5.144033125, 
0.35656346, 10.28806625, 0.035335688, 2.46994905, 106.9192005, 
0.26431719, 4.228014325, 5.144033125, 4.208754375, 3.233187425, 
0.9948269, 1400, 0.5583399425, 7.9586152, 6.079311875, 0.9948269, 
0.119551845, 2500, 1.158831245, 511.01082875, 1.515394705, 0.17078835, 
0.16808896, 4.208754375, 0.17828173, 6.079311875, 4.67639375, 
4.9741345, 3.48189415, 336.031773, 1800, 2.735773975, 1200, 4.228014325, 
72.666295, 1.9896538, 0.07111456, 2.338196875, 4.208754375, 1000, 
0.1861873875, 625, 0.35656346, 600, 19.64085375, 2.338196875, 
1145.5628625, 1.243533625, 11.9379228, 3.741115, 0.1861873875, 
0.051236505, 0.24824985, 8.5129526075, 0.024824985, 5.5573853625, 
0.17078835, 5.144033125, 500, 3000, 0.91724418, 8.41750875, 3.741115, 
0.1451700975, 0.9364284075, 2.338196875, 0.325331935, 3.741115, 
8.207321925, 3.2932654, 10.755705625, 0.5055834125, 7.949869375, 
0.51762116375, 3.741115, 4.208754375, 0.21980864, 0.0372374775, 
0.93527875, 800, 21.28159055, 1.36798908, 2000, 4.208754375, 
676.6696025, 0.04964997, 4.208754375, 1.8705575, 6.715081575, 
3.48189415, 3.741115, 4.3224108375, 1.2925425425, 0.02585984, 
2.058290875, 16.367378125, 2.80583625, 0.18101888, 1.297111, 
11.00771925, 0.37573437, 3.741115, 8.41750875, 4.67639375, 0.2793296, 
1.196990445, 0.75617693, 2.48706725, 6.079311875, 4.208754375, 
0.1793277675, 2.338196875, 0.09174312, 6.9637883, 600, 5.6116725, 
0.256182525, 32.14086125, 1061.55491925, 3.233187425, 1750, 3.741115, 
0.04964997, 8.940735825, 1160.837034, 2.884287245, 147.8803905, 
21.51141125, 8.41750875, 800, 0.04344372375, 154.26742, 5.6116725, 
462.80226, 0.49741345, 2875, 7.014590625, 7.48223, 99.28211475, 
0.5794156225, 0.128446112, 68.46314375, 4.67639375, 2900, 0.21721861875, 
0.9948269, 0.903179175, 3.273475625, 4.67639375, 1175, 11.689216075, 
0.623986055, 17.245426825, 8.41750875, 5.1457271875, 11.223345, 
800, 11.690984375, 300, 1000, 0.0764526, 2.9844807, 7.949869375, 
137.4675435, 10.755705625, 0.04964997, 3.273475625, 2.9844807, 
4.9741345, 6.54695125, 4.208754375, 2.48706725, 2.338196875, 
3.233187425, 6.079311875, 1.880984985, 3.9793076, 699.708432, 
0.5892198075, 0.034461152, 24.64673175, 1100, 0.71761056, 221.75941625, 
878.339758, 7.949869375, 1000, 0.31678304, 0.029483707725, 1.9896538, 
3000, 0.119551845, 0.05585621625, 2.80583625, 0.0597759225, 2.80583625
), lender_count = c(49L, 10L, 8L, 8L, 4L, 11L, 21L, 27L, 17L, 
13L, 30L, 9L, 13L, 13L, 10L, 7L, 9L, 3L, 27L, 6L, 9L, 1L, 4L, 
6L, 8L, 13L, 2L, 8L, 10L, 6L, 5L, 8L, 16L, 24L, 5L, 8L, 7L, 11L, 
4L, 52L, 6L, 5L, 42L, 7L, 11L, 4L, 22L, 32L, 4L, 15L, 41L, 11L, 
4L, 36L, 31L, 11L, 138L, 27L, 2L, 18L, 23L, 34L, 20L, 24L, 28L, 
10L, 10L, 5L, 4L, 8L, 2L, 6L, 8L, 1L, 25L, 8L, 25L, 7L, 16L, 
14L, 24L, 1L, 8L, 18L, 93L, 36L, 12L, 16L, 8L, 11L, 16L, 34L, 
14L, 69L, 6L, 8L, 15L, 12L, 11L, 8L, 32L, 16L, 274L, 4L, 86L, 
57L, 1L, 14L, 4L, 46L, 18L, 32L, 11L, 3L, 44L, 25L, 6L, 10L, 
12L, 12L, 10L, 8L, 6L, 12L, 16L, 14L, 7L, 11L, 18L, 6L, 18L, 
13L, 1L, 4L, 6L, 38L, 17L, 8L, 13L, 15L, 64L, 6L, 4L, 1L, 9L, 
31L, 12L, 41L, 5L, 1L, 3L, 27L, 50L, 12L, 19L, 18L, 8L, 25L, 
8L, 6L, 38L, 9L, 4L, 14L, 15L, 53L, 159L, 12L, 3L, 13L, 6L, 10L, 
8L, 167L, 7L, 1L, 14L, 18L, 52L, 7L, 5L, 17L, 14L, 7L, 31L, 57L, 
4L, 67L, 10L, 73L, 24L, 19L, 62L, 2L, 14L, 31L, 8L, 9L, 9L, 2L, 
12L, 8L, 28L, 12L, 14L, 9L, 22L, 4L, 23L, 6L, 8L, 18L, 14L, 22L, 
26L, 1L, 6L, 15L, 11L, 6L, 12L, 17L, 79L, 59L, 8L, 55L, 2L, 2L, 
17L, 28L, 10L, 15L, 6L, 2L, 5L, 55L, 12L, 5L, 8L, 6L, 40L, 2L, 
12L, 17L, 8L, 3L, 27L, 28L, 18L, 16L, 10L, 6L, 53L, 1L, 9L, 1L, 
13L, 10L, 19L, 20L, 8L, 14L, 36L, 19L, 16L, 2L, 1L, 10L, 9L, 
8L, 1L, 4L, 1L, 7L, 11L, 10L, 7L, 12L, 40L, 19L, 28L, 2L, 8L, 
3L, 20L, 1L, 3L, 43L, 4L, 14L, 22L, 14L, 12L, 19L, 3L, 20L, 9L, 
27L, 8L, 10L, 86L, 18L, 16L, 4L, 10L, 15L, 11L, 9L, 27L, 19L, 
103L, 13L, 20L, 53L, 58L, 12L, 9L, 16L, 14L, 14L, 4L, 7L, 18L, 
15L, 36L, 1L, 50L, 20L, 12L, 44L, 13L, 9L, 19L, 1L, 4L, 7L, 15L, 
19L, 9L, 13L, 17L, 12L, 9L, 1L, 7L, 1L, 39L, 8L, 25L, 7L, 4L, 
7L, 85L, 24L, 50L, 25L, 20L, 22L, 2L, 4L, 13L, 9L, 18L, 9L, 21L, 
22L, 4L, 41L, 12L, 50L, 8L, 11L, 3L, 9L, 25L, 27L, 23L, 8L, 24L, 
19L, 1L, 64L, 5L, 45L, 2L, 22L, 6L, 27L, 96L, 3L, 23L, 16L, 1L, 
20L, 77L, 83L, 16L, 1L, 16L, 1L, 5L, 72L, 8L, 17L, 12L, 16L, 
33L, 5L, 44L, 6L, 6L, 28L, 5L, 2L, 20L, 50L, 7L, 73L, 9L, 26L, 
2L, 9L, 4L, 27L, 28L, 8L, 21L, 15L, 4L, 9L, 14L, 6L, 23L, 24L, 
26L, 31L, 8L, 13L, 10L, 1L, 6L, 14L, 6L, 11L, 9L, 21L, 1L, 11L, 
27L, 21L, 12L, 30L, 19L, 63L, 13L, 66L, 7L, 8L, 21L, 43L, 92L, 
44L, 29L, 18L, 13L, 7L, 10L, 11L, 41L, 2L, 73L, 14L, 15L, 13L, 
12L, 33L, 24L, 7L, 66L, 17L, 4L, 31L, 1L, 8L, 17L, 40L, 13L, 
44L, 12L, 19L, 13L, 23L, 2L, 12L, 34L, 10L, 4L, 11L, 2L, 10L, 
8L, 1L, 12L, 19L, 8L, 1L, 10L, 5L, 12L, 11L, 10L, 12L, 82L, 47L, 
9L, 7L, 14L, 74L, 19L, 54L, 11L, 17L, 16L, 11L, 7L, 59L, 12L, 
7L, 6L, 7L, 6L), borrower_genders = structure(c(4L, 4L, 4L, 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5L, 2L, 4L, 4L, 4L, 1L, 5L, 4L, 4L, 
4L, 1L, 4L, 5L, 4L, 5L, 4L, 4L, 5L, 4L, 4L, 1L, 4L, 4L, 5L), .Label = c("mixed_genders", 
"mult_females", "mult_males", "single_female", "single_male"), class = "factor"), 
    country_code = c("LB", "CO", "PH", "PH", "KH", "PH", "CO", 
    "UG", "MG", "PH", "PH", "UG", "KH", "TJ", "PH", "CO", "PH", 
    "KE", "KE", "KE", "PH", "PH", "PH", "CO", "PH", "PH", "TG", 
    "PH", "PH", "KE", "ML", "KE", "MN", "WS", "NG", "KH", "TG", 
    "VN", "KE", "KE", "PH", "PH", "MN", "TJ", "PH", "NG", "LB", 
    "PY", "PH", "TJ", "EC", "WS", "UG", "VN", "KH", "ML", "CD", 
    "TZ", "PH", "PE", "PS", "KE", "UG", "PY", "TJ", "PH", "TJ", 
    "PH", "KE", "UG", "PH", "PH", "KE", "KE", "PH", "PH", "KE", 
    "PH", "CO", "KE", "PH", "PH", "KE", "PH", "AM", "KE", "KE", 
    "PH", "KH", "PH", "PK", "TJ", "PH", "KE", "PH", "PH", "PH", 
    "PH", "TJ", "PH", "EC", "TJ", "PY", "KH", "VN", "AM", "PH", 
    "PH", "PH", "SN", "PK", "EC", "PK", "KE", "EC", "KE", "PH", 
    "PK", "GH", "KE", "PH", "PH", "SN", "PH", "PH", "KE", "TJ", 
    "KE", "ZW", "KE", "KH", "PH", "PH", "PH", "PK", "EC", "PK", 
    "PH", "PH", "PH", "VN", "TJ", "KE", "TG", "PH", "UG", "PH", 
    "WS", "PH", "PH", "NG", "PY", "TZ", "PH", "KH", "PH", "KE", 
    "CO", "UG", "CM", "PH", "PH", "KH", "PH", "KE", "VN", "BO", 
    "PY", "KE", "LR", "PH", "IN", "PK", "BI", "PH", "PH", "TZ", 
    "LB", "TL", "IN", "PH", "EC", "MZ", "PH", "VN", "ML", "KH", 
    "ZA", "RW", "AM", "KE", "KE", "MW", "TG", "KE", "PY", "KE", 
    "PK", "UG", "PE", "TJ", "PH", "EC", "PH", "CO", "CO", "WS", 
    "PH", "LB", "PH", "PH", "PE", "KE", "EC", "AM", "KE", "PH", 
    "CO", "PH", "UG", "ZW", "PH", "BO", "PE", "PH", "BO", "PH", 
    "PE", "CM", "TL", "PH", "PH", "KE", "CO", "NG", "IN", "CO", 
    "PY", "PH", "KH", "UG", "PE", "PE", "TJ", "NP", "PH", "PK", 
    "PS", "AM", "CO", "KH", "PH", "LB", "PE", "PH", "PH", "KE", 
    "YE", "KE", "PE", "PH", "KG", "TZ", "KE", "GH", "LR", "PH", 
    "KE", "PH", "PH", "PH", "UG", "PH", "PK", "PH", "PH", "KE", 
    "TJ", "IN", "SO", "KH", "KG", "PH", "KH", "TL", "NG", "KE", 
    "CD", "PH", "PH", "PS", "CO", "PH", "KH", "TG", "KE", "LR", 
    "CO", "UG", "UG", "TZ", "LB", "JO", "KE", "UG", "BO", "PK", 
    "PH", "AM", "KE", "IN", "KE", "ID", "TZ", "UG", "TJ", "CO", 
    "KE", "GH", "PH", "PH", "PK", "TJ", "SL", "PY", "PH", "LB", 
    "PE", "IN", "EC", "CO", "KE", "PH", "PH", "PH", "ML", "PH", 
    "ID", "PK", "PE", "TZ", "KE", "PH", "PH", "KE", "KE", "LB", 
    "PY", "KE", "PH", "KE", "CO", "PS", "TG", "WS", "ML", "CO", 
    "UG", "PH", "TG", "PH", "PH", "KE", "KE", "PE", "LB", "KE", 
    "ZW", "KE", "EG", "KE", "UG", "PH", "PH", "PS", "KH", "PS", 
    "TG", "KH", "PH", "PH", "PE", "KE", "KE", "PH", "KH", "CO", 
    "KH", "BF", "KH", "PK", "CO", "PH", "ZW", "AM", "MN", "PH", 
    "PH", "CO", "PY", "PH", "PY", "PH", "KE", "PK", "PH", "PY", 
    "PH", "TZ", "PH", "PH", "UG", "KH", "PH", "EC", "KG", "LR", 
    "PS", "PH", "AZ", "KH", "PH", "PH", "KE", "KE", "PH", "PK", 
    "ML", "UG", "PK", "PH", "PH", "UG", "AM", "KG", "CO", "PH", 
    "PH", "PH", "NG", "LR", "PY", "KE", "PH", "PH", "CO", "PH", 
    "MG", "KE", "KH", "PH", "CO", "EG", "PE", "KE", "CD", "PH", 
    "KH", "MZ", "PE", "RW", "TJ", "PH", "PH", "EC", "KH", "WS", 
    "PH", "WS", "KE", "KH", "PH", "PH", "PE", "SN", "SL", "TJ", 
    "PH", "LB", "KH", "KE", "MM", "PH", "PH", "EC", "KE", "ML", 
    "KG", "PH", "PK", "PH", "PS", "PH", "KH", "TL", "MG", "KE", 
    "PH", "PE", "PH", "KH", "PH", "KE", "KE", "PH", "PH", "KE", 
    "PH", "KE", "PH", "LR", "KE", "BO", "CO", "SL", "TJ", "EC", 
    "UG", "WS", "GE", "PH", "EC", "UG", "VN", "KE", "GE", "CO", 
    "KH", "PH", "CO", "PH")), row.names = c(NA, -531L), class = c("tbl_df", 
"tbl", "data.frame"), na.action = structure(c(`9` = 9L, `30` = 30L, 
`32` = 32L, `102` = 102L, `104` = 104L, `118` = 118L, `119` = 119L, 
`164` = 164L, `166` = 166L, `169` = 169L, `196` = 196L, `224` = 224L, 
`231` = 231L, `255` = 255L, `264` = 264L, `268` = 268L, `274` = 274L, 
`280` = 280L, `285` = 285L, `327` = 327L, `338` = 338L, `377` = 377L, 
`385` = 385L, `386` = 386L, `390` = 390L, `402` = 402L, `420` = 420L, 
`441` = 441L, `445` = 445L, `446` = 446L, `473` = 473L, `528` = 528L, 
`533` = 533L, `541` = 541L, `544` = 544L, `546` = 546L, `555` = 555L
), class = "omit"))
camille
  • 16,432
  • 18
  • 38
  • 60
delcast
  • 496
  • 1
  • 5
  • 16
  • 2
    What are you trying to map over? Generally this approach is useful for making several models of data that's been nested by some variable. Unless I'm missing something, it should make more sense to just call `lm` on your dataset. But you also have `lender_count` twice in your formula – camille Jun 25 '18 at 15:20
  • Hi @camille, correct! I have about 86 countries. The full data set has 680,000 rows. What I'm including is a small sample to reproduce the error. – delcast Jun 25 '18 at 15:21
  • I'm not getting that error; the error I get is based on the fact that when I run your code with this data, there's no column called `data` that you're using in your `lm` call. A `data` column would generally be the default coming from nesting. If that's what you're doing, please include it in your question; otherwise, it isn't reproducible – camille Jun 25 '18 at 15:24
  • But if you're nesting by country, do you have all the levels of `borrower_genders` present for all the countries? That error suggests that some countries only have one level present. In the sample you gave, only 6 countries have 2 or more levels, and none have more than 3 – camille Jun 25 '18 at 15:29
  • @camille, yes, my data is wrong. I've changed it just now. – delcast Jun 25 '18 at 15:33
  • Okay, but please complete the code also, i.e. if you're nesting the data IRL, nest it the same way here. Otherwise there's no column `data` – camille Jun 25 '18 at 15:34
  • I have changed the data and added two lines of code to include the nesting. Does this work for you? @camille – delcast Jun 25 '18 at 15:40

1 Answers1

2

The error message says that there are fewer than 2 levels of borrower_genders in at least one of the models. The first thing to do is comb through the data to see where this happens.

If you count up the number of distinct borrower_genders levels for each country, then county the number of countries with each of those counts (it gets meta), you'll see that of your 47 distinct countries, 19 of them have only one level present, and none of them have all 5 levels.

library(tidyverse)

kiva_country %>%
  distinct(country_code, borrower_genders) %>%
  count(country_code) %>%
  count(n)
#> # A tibble: 4 x 2
#>       n    nn
#>   <int> <int>
#> 1     1    19
#> 2     2    19
#> 3     3     5
#> 4     4     4

This is a point where you might want to reevaluate your approach—filter for only certain sample sizes or certain variety of genders, group by continent instead of country, etc. I'll leave those decisions up to you, but to illustrate, I'm filtering for only the countries that have more than 1 level (there are 28 of these). Even still, some of these are very small samples, as the warning message later will point out; I'll assume there are better sample sizes in your full dataset.

select_countries <- kiva_country %>%
  distinct(country_code, borrower_genders) %>%
  count(country_code) %>%
  filter(n > 1) %>%
  pull(country_code)

Now your nest / map / model workflow works fine, aside from the warning about sample sizes.

kiva_country %>%
  filter(country_code %in% select_countries) %>%
  group_by(country_code) %>%
  nest(-country_code) %>%
  mutate(model = map(data, ~lm(loan_usd ~ lender_count + borrower_genders, data = .))) %>%
  mutate(glnc = map(model, broom::glance)) %>%
  unnest(glnc)
#> Warning in stats::summary.lm(x): essentially perfect fit: summary may be
#> unreliable
#> # A tibble: 28 x 14
#>    country_code data      model r.squared adj.r.squared    sigma statistic
#>    <chr>        <list>    <lis>     <dbl>         <dbl>    <dbl>     <dbl>
#>  1 LB           <tibble … <S3:…     0.573         0.451 847.          4.69
#>  2 CO           <tibble … <S3:…     0.195         0.127   0.135       2.90
#>  3 PH           <tibble … <S3:…     0.532         0.526   2.55       88.2 
#>  4 KH           <tibble … <S3:…     0.836         0.810 246.         32.0 
#>  5 UG           <tibble … <S3:…     0.903         0.887   0.0562     55.9 
#>  6 TJ           <tibble … <S3:…     0.324         0.234  44.6         3.59
#>  7 KE           <tibble … <S3:…     0.297         0.256 180.          7.29
#>  8 MN           <tibble … <S3:…     1           NaN     NaN         NaN   
#>  9 WS           <tibble … <S3:…     0.976         0.967  25.6       102.  
#> 10 NG           <tibble … <S3:…     0.615         0.358   0.130       2.39
#> # ... with 18 more rows, and 7 more variables: p.value <dbl>, df <int>,
#> #   logLik <dbl>, AIC <dbl>, BIC <dbl>, deviance <dbl>, df.residual <int>

Created on 2018-06-25 by the reprex package (v0.2.0).

camille
  • 16,432
  • 18
  • 38
  • 60
  • Thanks Camille! I'll reevaluate my approach with the larger dataset. – delcast Jun 25 '18 at 16:25
  • I was following this exact pattern and this answer definitely is more on spot than the very good, but very general https://stackoverflow.com/a/44201384/1585486 – Pablo Adames May 18 '20 at 01:00