使用 map() 在 lm 中未识别的因素
Posted
技术标签:
【中文标题】使用 map() 在 lm 中未识别的因素【英文标题】:Factors not being recognised in a lm using map() 【发布时间】:2018-12-04 05:14:10 【问题描述】:目标:我想使用 map() 函数来做一个涉及分类变量的线性模型。
问题:我收到以下错误,但我知道包含的分类变量,borrower_genders
有五个级别。
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
我做了什么:以下代码在删除 borrower_genders
时工作得很好,而不是它,为什么?
kiva_country%>%
dplyr::group_by(country_code)%>%
tidyr::nest(-country_code) %>%
mutate(model=map(data, ~lm(loan_usd~
lender_count+
borrower_genders,
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, 4L,
2L, 4L, 5L, 5L, 4L, 4L, 4L, 4L, 4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L, 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"))
【问题讨论】:
你想映射什么?通常,这种方法对于制作由某个变量嵌套的多个数据模型很有用。除非我遗漏了什么,否则在您的数据集上调用lm
应该更有意义。但是您的公式中也有两次lender_count
嗨@camille,正确!我有大约 86 个国家。完整的数据集有 680,000 行。我包括的是一个小样本来重现错误。
我没有收到那个错误;我得到的错误是基于这样一个事实,即当我使用这些数据运行您的代码时,您在 lm
调用中没有使用名为 data
的列。 data
列通常是来自嵌套的默认值。如果那是您正在做的事情,请将其包含在您的问题中;否则,它是不可重现的
但是如果您按国家/地区嵌套,您是否拥有所有国家/地区的所有borrower_genders
级别?该错误表明某些国家/地区仅存在一个级别。在您提供的样本中,只有 6 个国家有 2 个或更多级别,没有一个国家超过 3 个
@camille,是的,我的数据有误。我刚刚改了。
【参考方案1】:
错误消息表明至少有一个模型中的borrower_genders
少于2 个级别。首先要做的是梳理数据以查看发生这种情况的位置。
如果您计算每个国家/地区不同 borrower_genders
级别的数量,然后计算每个国家/地区的数量(它得到元数据),您将看到您的 47 个不同国家/地区的数量,其中 19 个仅存在一个级别,并且没有一个具有全部 5 个级别。
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
此时您可能需要重新评估您的方法 - 仅过滤特定样本量或特定种类的性别,按大洲而不是国家/地区分组等。我将把这些决定留给您,但为了说明,我只过滤级别超过 1 的国家/地区(其中有 28 个)。即便如此,其中一些样本还是非常小的样本,正如稍后的警告消息会指出的那样;我假设您的完整数据集中有更好的样本量。
select_countries <- kiva_country %>%
distinct(country_code, borrower_genders) %>%
count(country_code) %>%
filter(n > 1) %>%
pull(country_code)
现在除了关于样本大小的警告之外,您的嵌套/地图/模型工作流程工作正常。
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>
由reprex package (v0.2.0) 于 2018 年 6 月 25 日创建。
【讨论】:
谢谢卡米尔!我将使用更大的数据集重新评估我的方法。 我遵循了这个确切的模式,这个答案肯定比非常好的答案更准确,但非常笼统***.com/a/44201384/1585486以上是关于使用 map() 在 lm 中未识别的因素的主要内容,如果未能解决你的问题,请参考以下文章
VMware Virtual Ethernet Adapter for VMnet1和和VMnet8 显示未识别的网络,如何解决?