传播数据并计算RMSE

时间:2019-02-10 14:41:31

标签: r

我正在尝试创建RMSE函数。我的数据具有“实际”值和“预测”值。我可以使用以下方法绘制值:

data %>%
  ggplot(aes(x = WEEK)) +
  geom_line(aes(y = value, color = key))

enter image description here

但是,当我尝试应用存储在列表中的函数时,出现此错误:

> sample_rmse_tbl <- sample_predictions_XGB_tbl %>%
+   mutate(rmse = map_dbl(predict, calc_rmse)) %>%
+   select(id, rmse) %>%
+   print()
Error in mutate_impl(.data, dots) : 
  Evaluation error: object 'value' not found.

因此,我分解了功能以尝试找出问题所在。当我到达这一行时:

data%>%
  spread(key, value)

我收到此错误:

Error in synthetic.instrument(primary_id = primary_id, currency = currency,  : 
  object 'value' not found
In addition: Warning message:
Unknown or uninitialised column: 'UNITS'. 

编辑:似乎与另一个使用以下软件包的冲突:tidyr::spread(….)解决了该问题。

我正在尝试计算我已进行预测的几周的“实际”和“预测”的均方根误差。

本质上,这就是我想要我的代码执行的事情:

calc_rmse <- function(prediction_tbl) {
  rmse_calculation <- function(data) {
    data %>%
      #fill(IRI_KEY) %>%
      spread(key, value) %>%
      filter(complete.cases(actual, predict)) %>%
      with(., ModelMetrics::rmse(actual, predict))
  }
  rmse_calculation(prediction_tbl)
}



sample_rmse_tbl <- sample_predictions_tbl %>%
  mutate(rmse = map_dbl(predict, calc_rmse)) %>%
  select(id, rmse) %>%
  print()

创建一个函数来传播数据并计算RMSE。使用map_dbl应用此功能-因为我有与此类似的数据帧列表。但是,我想先将其用于一个数据帧,然后再将其应用于更多数据。

数据:

data <- structure(list(value = c(1.68249754931008, 1.3773881016757, 1.73127655564675, 
1.64164659032462, 1.62525633104759, 1.57081870252295, 1.52976740117506, 
1.38159108164287, 1.69456489311165, 1.70857557015324, 1.62550896016911, 
1.70395991828564, 1.74848746064726, 1.60716937665098, 1.6503274497958, 
1.60715142890414, 1.51006184469724, 1.59101819589116, 1.47145479621209, 
1.807832548351, 1.88238018665382, 1.6847570961895, 1.54448117025255, 
1.54550877609031, 1.50679452076707, 1.74490984077029, 1.63017966656427, 
1.54876260039133, 1.50080593389507, 1.61338125645549, 1.58102748592166, 
2.21895413669185, 1.74135631555296, 1.79200450772336, 1.75255617521159, 
1.74074785655497, 1.75937163064538, 1.56472068304409, 1.63178694522919, 
1.47235001481839, 1.69021038681392, 1.81692516593091, 1.53019963669236, 
1.47732811481282, 1.69348786267125, 1.70678696717445, 1.69879285461694, 
1.66798216966774, 1.63119155274524, 1.67442778059381, 1.68912567748478, 
1.77821919960956, 1.61635533039127, 1.83799388503246, 1.91289818480626, 
1.79087410802717, 1.43217503644745, 1.60960056207603, 1.55482615645367, 
1.63042347794729, 1.41456068972624, 1.57883280201476, 1.79867207687568, 
1.78235762430476, 1.69524882795482, 1.75903811557747, 1.64195411205064, 
1.48938568215594, 1.64538350057091, 1.66430764720985, 1.63574885140424, 
1.81530392137519, 1.73676171804612, 1.69548183379717, 1.78308469276707, 
1.751128562897, 1.67759226700679, 1.86987660088991, 1.69964018670804, 
1.62922029741533, 1.63734911010712, 1.66988694473212, 1.67092894856568, 
2.44914975248695, 1.75637638934015, 1.93179127343957, 1.77087133865682, 
1.6869271416981, 1.61589333842489, 1.82248999635966, 1.79167305263457, 
1.64749375447026, 1.74530528115849, 1.81784476623349, 1.7463946210047, 
1.54714538207303, 1.51714712249699, 1.71487593939636, 1.49484462514138, 
1.56785346132361, 1.71383832360239, 1.75737562385996, 1.60340108989314, 
1.57129900272006, 1.44957662294027, 1.57202817368926, 1.59084572454443, 
1.44037188323936, 1.53716516642982, 1.49699929192734, 1.43766640502566, 
1.54113107680902, 1.33006992875969, 1.40567546513146, 1.54315146808789, 
1.41036306251124, 1.51451455367316, 1.51345402737158, 1.5205986242358, 
1.58147725906913, 1.54159830157023, 1.3876234758156, 1.46389506368138, 
1.56285937838487, 1.51410862208213, 1.50367243641386, 1.56139493275278, 
1.49104351346731, 1.41014401400783, 1.49889681660757, 1.34041209247165, 
1.42205116215392, 1.36950558354429, 1.41027795521969, 1.37464813658801, 
2.49106682289769, 1.71061867827708, 1.63070123655746, 1.71174484856285, 
1.59620994331684, 1.44954843416392, 1.539011523027, 1.51519095066453, 
1.4656332310528, 1.42905971531356, 1.6640586427151, 1.54233071401629, 
1.45076891503735, 1.53377738496914, 1.52554312015618, 1.42180030584458, 
1.47193618462568, 1.41175039113236, 1.50334417416672, 1.43039801312571, 
1.4598923351229, 1.29041517388662, 1.50306427983598, 1.50041065448728, 
1.38217252688794, 1.39398798252723, 1.43761461278735, 1.56644624176978, 
1.35006839566629, 1.29800067845516, 1.21771832241593, 1.65444585166229, 
1.36349548591942, 1.42953547183492, 1.34313522511867, 1.24310770081259, 
1.43536954696695, 1.3817893816059, 1.48266831972615, 1.38748179128218, 
1.31310215755671, 1.43192495846516, 1.50249098591771, 1.55723407053227, 
1.49850499113045, 1.53094929370581, 1.67915366401504, 1.79477647998702, 
1.52215423228312, 1.55692869735814, 1.58515796561194, 1.75882650759676, 
1.47615631169642, 2.47784050059889, 1.63547149973475, 1.68119578112547, 
1.52340723035678, 1.4863930093781, 1.52162889454811, 1.4062295928061, 
1.43050816404494, 1.60971497838266, 1.41331349109383, 1.33609982809505, 
1.58199523705326, 1.53624435516486, 1.49482116925997, 1.47666540952947, 
1.37753427902501, 1.39633000184869, 1.33956662049738, 1.39960468999806, 
1.4422545454172, 1.34095203471389, 1.46338249356994, 1.67396948706621, 
1.62675553527423, 1.50184401364278, 1.35531415924625, 1.57740824164861, 
1.45393647653576, 1.50483557909664, 1.31231723880218, 1.58380875243292, 
1.41795601550546, 1.51811670703729, 1.5469654575278, 1.40700839400062, 
1.39773997966331, 1.48566118816054, 1.30519674828076, 1.29541059189359, 
1.39388071017628, 1.47683911975003, 1.57842281601588, 1.474740664501, 
1.39040599297286, 1.44480101492875, 1.53949478393003, 1.62043687009918, 
1.44004413207613, 1.42326015560623, 1.52649908173561, 1.47657001122015, 
1.45697763576672, 2.315362098968, 1.4895353750789, 1.54194814556338, 
1.46148291572849, 1.49333652490128, 1.57321884569939, 1.59552754210274, 
1.44852227700215, 1.37192433595377, 1.43253204732305, 1.35378532046931, 
1.50987979619152, 1.31913995631763, 1.37511932213644, 1.40240627825577, 
1.66257446017493, 1.48766906121991, 1.32726370420464, 1.45663570883313, 
1.50736662690743, 1.33140596562706, 1.68460880792832, 1.69434847798237, 
1.54777499298008, 1.55880456550123, 1.52350919658822, 1.57771404655133, 
1.34361281481031, 1.29217821523941, 1.24364288877433, 1.7316509920069, 
1.47465225156474, 1.6155648554527, 1.48118431484749, 1.33697182559051, 
1.48680734825687, 1.53284431031927, 1.63114140995442, 1.40393805410254, 
1.40377357959565, 1.59907571544276, 1.53885030005043, 1.48365804407904, 
1.51693090348844, 1.3694344375816, 1.63086388551996, 1.45746833284969, 
1.3471194142225, 1.6067123364899, 1.39579205159823, 1.5302983739695, 
1.51553066191837, 2.63558111595972, 1.84091875216258, 1.58933271230692, 
1.61348773461894, 1.65841092149162, 1.47626583770387, 1.49739059293494, 
1.49259721343736, 1.80207444014884, 1.46196152400589, 1.85699449916459, 
1.40871828918604, 1.7875511902171, 1.54186763996258, 1.35592733753932, 
1.75909544763252, 1.78393381231045, 1.53748875744489, 1.46828731346078, 
1.42709112090469, 1.31662392616272, 1.45476961135864, 1.66966092586517, 
1.60142409801483, 1.50503647327423, 1.35559320449829, 1.51329791545868, 
1.44091081619263, 1.50782096385956, 1.31523466110229, 1.51839280128479, 
1.4072687625885, 1.51538097858429, 1.5423789024353, 1.40655064582825, 
1.41274774074554, 1.47834539413452, 1.25591278076172, 1.29949343204498, 
1.40869092941284, 1.47546911239624, 1.57634508609772, 1.46414303779602, 
1.390469789505, 1.44607317447662, 1.53944540023804, 1.62032628059387, 
1.44083118438721, 1.42385101318359, 1.52643978595734, 1.5058708190918, 
1.45733332633972, 2.3126699924469, 1.49019253253937, 1.55842363834381, 
1.46187388896942, 1.49001717567444, 1.56597626209259, 1.49563848972321, 
1.44788336753845, 1.37008190155029, 1.43171572685242, 1.36814773082733, 
1.50062358379364, 1.32583212852478, 1.37599742412567, 1.40903007984161, 
1.63710296154022, 1.48639678955078, 1.32994318008423, 1.4562349319458, 
1.42629456520081), WEEK = structure(c(11323, 11330, 11337, 11344, 
11351, 11358, 11365, 11372, 11379, 11386, 11393, 11400, 11407, 
11414, 11421, 11435, 11442, 11449, 11456, 11463, 11470, 11477, 
11484, 11491, 11498, 11505, 11512, 11519, 11526, 11533, 11540, 
11547, 11554, 11561, 11568, 11575, 11582, 11589, 11596, 11603, 
11610, 11617, 11624, 11631, 11638, 11645, 11652, 11659, 11666, 
11673, 11680, 11687, 11694, 11701, 11708, 11715, 11722, 11729, 
11736, 11743, 11750, 11757, 11764, 11771, 11778, 11785, 11792, 
11799, 11806, 11813, 11820, 11827, 11834, 11841, 11848, 11855, 
11862, 11869, 11876, 11883, 11890, 11897, 11904, 11911, 11918, 
11925, 11932, 11939, 11946, 11953, 11960, 11967, 11974, 11981, 
11988, 11995, 12002, 12009, 12016, 12023, 12030, 12037, 12044, 
12051, 12058, 12065, 12072, 12079, 12086, 12093, 12100, 12107, 
12114, 12121, 12128, 12135, 12142, 12149, 12156, 12163, 12170, 
12177, 12184, 12191, 12198, 12205, 12212, 12219, 12226, 12233, 
12240, 12247, 12254, 12261, 12268, 12275, 12282, 12289, 12296, 
12303, 12310, 12317, 12324, 12331, 12338, 12345, 12352, 12359, 
12366, 12373, 12380, 12387, 12394, 12401, 12408, 12415, 12422, 
12429, 12436, 12443, 12450, 12457, 12464, 12471, 12478, 12485, 
12492, 12499, 12506, 12513, 12520, 12527, 12534, 12541, 12548, 
12555, 12562, 12569, 12576, 12583, 12590, 12597, 12604, 12611, 
12618, 12625, 12632, 12639, 12646, 12653, 12660, 12667, 12674, 
12681, 12688, 12695, 12702, 12709, 12716, 12723, 12730, 12737, 
12744, 12751, 12758, 12765, 12772, 12779, 12786, 12793, 12800, 
12807, 12814, 12821, 12828, 12835, 12842, 12849, 12856, 12863, 
12870, 12877, 12884, 12891, 12898, 12905, 12912, 12919, 12926, 
12933, 12940, 12947, 12954, 12961, 12968, 12975, 12982, 12989, 
12996, 13003, 13010, 13017, 13024, 13031, 13038, 13045, 13052, 
13059, 13066, 13073, 13080, 13087, 13094, 13101, 13108, 13115, 
13122, 13129, 13136, 13143, 13150, 13157, 13164, 13171, 13178, 
13185, 13192, 13199, 13206, 13213, 13220, 13227, 13234, 13241, 
13248, 13255, 13262, 13269, 13276, 13283, 13290, 13297, 13304, 
13311, 13318, 13325, 13332, 13339, 13346, 13353, 13360, 13367, 
13374, 13381, 13388, 13395, 13402, 13409, 13416, 13423, 13430, 
13437, 13444, 13451, 13458, 13465, 13472, 13479, 13486, 13493, 
13500, 13507, 13150, 13157, 13164, 13171, 13178, 13185, 13192, 
13199, 13206, 13213, 13220, 13227, 13234, 13241, 13248, 13255, 
13262, 13269, 13276, 13283, 13290, 13297, 13304, 13311, 13318, 
13325, 13332, 13339, 13346, 13353, 13360, 13367, 13374, 13381, 
13388, 13395, 13402, 13409, 13416, 13423, 13430, 13437, 13444, 
13451, 13458, 13465, 13472, 13479, 13486, 13493, 13500, 13507
), class = "Date"), key = c("Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Actual", "Actual", "Actual", "Actual", "Actual", "Actual", "Actual", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction")), row.names = c(NA, -364L), index_quo = ~WEEK, index_time_zone = "UTC", class = c("tbl_time", 
"tbl_df", "tbl", "data.frame"))

sample_predictions_tbl的外观如下:

enter image description here

为了在此处创建data,我使用了以下行:

data <- sample_predictions_tbl$predict[[1]]

0 个答案:

没有答案