Home > Mobile >  Using a list of models to make predictions over a list of results using lapply in R
Using a list of models to make predictions over a list of results using lapply in R

Time:02-18

I have a large list of models that I built using lapply with the following code (these lists are too long to show the whole data but I used the corresponding code to set the models up):

table_list <- raw2 %>%
  group_by(Lake_name) %>%
  group_split()

model_list <- lapply(table_list, function(x) gam(surface_area~s(percent, k=10), data=x,family=scat))

I am trying to take a list of results that looks like this:

list(structure(list(percent = c(0.950517843, 0.982918911, 0.94428041, 
0.940079126, 0.780998003, 0.620697483), Lake_name = c("AlanHenry", 
"AlanHenry", "AlanHenry", "AlanHenry", "AlanHenry", "AlanHenry"
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(percent = c(0.795161993, 0.795161993, 0.421236243, 
0.994041331, 0.994041331, 0.421236243, 0.805093669, 0.805093669, 
0.573425366, 0.573425366), Lake_name = c("Amistad", "Amistad", 
"Amistad", "Amistad", "Amistad", "Amistad", "Amistad", "Amistad", 
"Amistad", "Amistad")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.879544275, 
0.933534634, 0.723783507, 0.885373648, 1.035273336, 0.945443902, 
1.035273336, 1.16011839, 1.16011839), Lake_name = c("AmonGCarter", 
"AmonGCarter", "AmonGCarter", "AmonGCarter", "AmonGCarter", "AmonGCarter", 
"AmonGCarter", "AmonGCarter", "AmonGCarter")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.750046962, 0.750046962), Lake_name = c("Aquilla", 
    "Aquilla")), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.881318271, 
0.8814417, 0.881318271, 0.881318271), Lake_name = c("Arlington", 
"Arlington", "Arlington", "Arlington")), row.names = c(NA, -4L
), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.980618735, 0.980618735, 0.980618735, 0.987737842, 
    0.936128978, 0.987737842, 0.987737842, 0.943553617), Lake_name = c("Arrowhead", 
    "Arrowhead", "Arrowhead", "Arrowhead", "Arrowhead", "Arrowhead", 
    "Arrowhead", "Arrowhead")), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.858796281, 
1.0220055, 1.039047613, 1.0220055, 1.0220055, 1.034844283, 1.034844283
), Lake_name = c("Athens", "Athens", "Athens", "Athens", "Athens", 
"Athens", "Athens")), row.names = c(NA, -7L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.97113492, 
1.01757844), Lake_name = c("Bardwell", "Bardwell")), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.963092828, 1.014093347, 1.014093347, 0.953013783, 
    0.805597973, 1.005629119, 1.005629119, 0.963092828, 0.752357351, 
    0.963092828, 0.752357351, 0.752357351), Lake_name = c("Belton", 
    "Belton", "Belton", "Belton", "Belton", "Belton", "Belton", 
    "Belton", "Belton", "Belton", "Belton", "Belton")), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.791489458, 0.791489458, 0.791489458, 1.139593985
    ), Lake_name = c("Benbrook", "Benbrook", "Benbrook", "Benbrook"
    )), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(percent = 0.646778371, Lake_name = "BradyCreek"), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.551283842, 0.551283842), Lake_name = c("Bridgeport", 
    "Bridgeport")), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.930093, 0.776171528, 
0.982543677, 0.76651093, 0.982543677), Lake_name = c("Buchanan", 
"Buchanan", "Buchanan", "Buchanan", "Buchanan")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.972444388, 0.861133914, 0.861133914, 0.819707966, 
    0.872433988, 0.972444388, 0.972444388, 0.990635195, 1.000751124, 
    0.990635195, 0.990635195, 1.007893833), Lake_name = c("CedarCreek", 
    "CedarCreek", "CedarCreek", "CedarCreek", "CedarCreek", "CedarCreek", 
    "CedarCreek", "CedarCreek", "CedarCreek", "CedarCreek", "CedarCreek", 
    "CedarCreek")), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.547229246, 
0.782389745, 0.770380962, 0.946435035, 0.946435035, 0.859004388, 
0.782389745, 0.77512134, 0.77512134, 0.547229246, 0.782389745, 
0.768274562, 0.415392286, 0.415392286, 0.770380962, 0.946435035, 
0.77512134, 0.77512134, 0.946435035, 0.252023748, 0.388922578, 
0.388922578, 0.859004388, 0.770380962, 0.415392286, 0.252023748, 
0.314899005, 0.593407265, 0.859004388, 1.000273732, 0.770380962, 
0.252023748), Lake_name = c("ChokeCanyon", "ChokeCanyon", "ChokeCanyon", 
"ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", 
"ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", 
"ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", 
"ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", 
"ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon", 
"ChokeCanyon", "ChokeCanyon", "ChokeCanyon", "ChokeCanyon")), row.names = c(NA, 
-32L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.484808543, 0.329173594, 0.484808543, 0.484808543
    ), Lake_name = c("Cisco", "Cisco", "Cisco", "Cisco")), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(1.022886318, 1.022721734, 1.022721734, 0.879750882, 
    0.948863318, 0.879750882, 0.879750882, 1.007645815, 1.007645815, 
    1.022721734), Lake_name = c("ColetoCreek", "ColetoCreek", 
    "ColetoCreek", "ColetoCreek", "ColetoCreek", "ColetoCreek", 
    "ColetoCreek", "ColetoCreek", "ColetoCreek", "ColetoCreek"
    )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(percent = c(0.915823603, 0.996244047, 1.006961999, 
1.006961999, 1.006961999, 0.843606075, 1.000495295, 0.843606075, 
0.843606075, 1.003553071, 1.001489881, 1.001489881, 1.001489881, 
1.00980109, 0.886847141, 0.886847141, 0.886847141, 1.014695685, 
1.014695685, 1.014695685), Lake_name = c("Conroe", "Conroe", 
"Conroe", "Conroe", "Conroe", "Conroe", "Conroe", "Conroe", "Conroe", 
"Conroe", "Conroe", "Conroe", "Conroe", "Conroe", "Conroe", "Conroe", 
"Conroe", "Conroe", "Conroe", "Conroe")), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = 0.160326098, Lake_name = "CorpusChristi"), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(1.008881286, 0.911694145, 1.002308211, 0.911694145, 
    0.911694145, 0.921063112, 1.008881286, 1.008881286), Lake_name = c("CypressSprings", 
    "CypressSprings", "CypressSprings", "CypressSprings", "CypressSprings", 
    "CypressSprings", "CypressSprings", "CypressSprings")), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(1.015119258, 0.714775128, 1.015119258, 1.015119258
    ), Lake_name = c("EagleMountain", "EagleMountain", "EagleMountain", 
    "EagleMountain")), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.269020478, 
0.269020478), Lake_name = c("EVSpence", "EVSpence")), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.436313018, 0.587352116, 0.587352116, 0.690227218, 
    0.690227218, 0.436313018, 0.385240855, 0.366479802), Lake_name = c("Falcon", 
    "Falcon", "Falcon", "Falcon", "Falcon", "Falcon", "Falcon", 
    "Falcon")), row.names = c(NA, -8L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(percent = c(1.060156044, 0.968670765, 
0.968670765, 0.988310095, 0.988310095, 0.988310095, 0.894380128, 
0.988310095, 0.894380128, 0.894380128, 0.945974263, 0.894380128, 
0.945974263, 0.945974263, 1.004911859, 0.945974263, 1.004911859, 
1.001320618, 1.004911859, 1.004911859, 1.001320618, 1.001320618, 
1.060156044, 1.001320618, 0.896955772, 0.972470508, 1.060156044, 
0.915504441, 1.060156044, 0.915504441, 0.915504441, 0.915504441, 
0.943737383, 0.943737383, 0.943737383, 0.871279058, 0.896955772, 
1.038550874, 1.051715519, 0.896955772, 0.896955772, 0.972470508, 
0.972470508, 0.972470508, 1.051791311, 1.051791311, 1.051791311, 
1.038550874, 1.01644421, 1.038550874, 1.051715519), Lake_name = c("Fork", 
"Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", 
"Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", 
"Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", 
"Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", 
"Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", 
"Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", "Fork", 
"Fork", "Fork")), row.names = c(NA, -51L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.671113156, 
0.341205012, 0.576030459, 1.031849704, 0.341205012, 1.031849704
), Lake_name = c("FortPhantomHill", "FortPhantomHill", "FortPhantomHill", 
"FortPhantomHill", "FortPhantomHill", "FortPhantomHill")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.705600581, 0.985366361, 0.967061051, 0.967061051, 
    0.826323366, 0.967061051, 0.705600581, 0.705600581), Lake_name = c("Graham", 
    "Graham", "Graham", "Graham", "Graham", "Graham", "Graham", 
    "Graham")), row.names = c(NA, -8L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(percent = c(0.397747096, 0.266384826, 
0.247195243), Lake_name = c("Greenbelt", "Greenbelt", "Greenbelt"
)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(percent = c(1.047145701, 1.012189143, NA, 
1.047145701, 1.047145701, 1.044004839, 1.001766481, 1.001766481, 
1.001766481), Lake_name = c("Houston", "Houston", "Houston", 
"Houston", "Houston", "Houston", "Houston", "Houston", "Houston"
)), row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(percent = c(1.049085491, 1.050248476), Lake_name = c("HoustonCounty", 
"HoustonCounty")), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(1.020781541, 
0.980551056, 0.980551056), Lake_name = c("HubbardCreek", "HubbardCreek", 
"HubbardCreek")), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.987948205, 
1.008921766, 1.010649379, 0.942955319), Lake_name = c("Jacksonville", 
"Jacksonville", "Jacksonville", "Jacksonville")), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.96276138, 1.036625798, 0.96276138, 0.96276138
    ), Lake_name = c("JoePool", "JoePool", "JoePool", "JoePool"
    )), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(percent = c(1.007395106, 1.116069186, 1.007395106, 
1.151743129, 1.007395106, 1.316776555, 1.316776555, 1.316776555
), Lake_name = c("LakeO'thePines", "LakeO'thePines", "LakeO'thePines", 
"LakeO'thePines", "LakeO'thePines", "LakeO'thePines", "LakeO'thePines", 
"LakeO'thePines")), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(1.074877778, 
1.021140446, 1.021140446, 1.074877778, 1.064486164, 1.074877778
), Lake_name = c("Lewisville", "Lewisville", "Lewisville", "Lewisville", 
"Lewisville", "Lewisville")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(1.022041792, 
1.030232129, 1.007150505, 1.007150505, 1.007150505), Lake_name = c("Livingston", 
"Livingston", "Livingston", "Livingston", "Livingston")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.161027261, 0.275899609, 0.347375022, 0.347375022, 
    0.263324239, 0.263324239, 0.226113739, 0.161027261, 0.226113739, 
    0.138466, 0.190260152, 0.190260152, 0.138466), Lake_name = c("Meredith", 
    "Meredith", "Meredith", "Meredith", "Meredith", "Meredith", 
    "Meredith", "Meredith", "Meredith", "Meredith", "Meredith", 
    "Meredith", "Meredith")), row.names = c(NA, -13L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.693098021, 
0.698086537, 0.693098021), Lake_name = c("MillersCreek", "MillersCreek", 
"MillersCreek")), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.959910265, 
1.016191084, 1.018648156), Lake_name = c("Monticello", "Monticello", 
"Monticello")), row.names = c(NA, -3L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(percent = c(1.039809268, 0.988916081, 
1.015347423), Lake_name = c("Murvaul", "Murvaul", "Murvaul")), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.992337784, 0.980260856, 0.981598856, 0.803433641, 
    1.009299994), Lake_name = c("Nacogdoches", "Nacogdoches", 
    "Nacogdoches", "Nacogdoches", "Nacogdoches")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.859451943, 0.840810328, 0.879181453, 0.879181453, 
    0.879181453), Lake_name = c("Nasworthy", "Nasworthy", "Nasworthy", 
    "Nasworthy", "Nasworthy")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.595828759, 
0.396544803, 0.595828759, 0.595828759), Lake_name = c("OakCreek", 
"OakCreek", "OakCreek", "OakCreek")), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.676099721, 
0.676099721, 0.676099721, 0.52233859, 0.52233859, 0.676099721, 
0.52233859, 0.429286551, 0.52233859, 0.429286551, 0.429286551, 
0.293295435, 0.293295435, 0.429286551, 0.171035317, 0.293295435, 
0.171035317, 0.293295435, 0.171035317, 0.200646127, 0.122963952, 
0.200646127, 0.18082718, 0.57717558, 0.57717558, 0.57717558, 
0.41221331, 0.34807764, 0.564015531, 0.564015531, 0.564015531, 
0.511473358, 0.511473358, 0.564015531, 0.41221331, 0.41221331, 
0.511473358, 0.511473358, 0.41221331), Lake_name = c("OHIvie", 
"OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", 
"OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", 
"OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", 
"OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", 
"OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", "OHIvie", 
"OHIvie", "OHIvie", "OHIvie")), row.names = c(NA, -39L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(1.018889765, 
1.005572269, 1.005572269, 1.005572269, 1.029045379, 1.03165015, 
1.029045379, 1.029045379, 0.985153538, 0.978887795, 1.005152363
), Lake_name = c("Palestine", "Palestine", "Palestine", "Palestine", 
"Palestine", "Palestine", "Palestine", "Palestine", "Palestine", 
"Palestine", "Palestine")), row.names = c(NA, -11L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.87959486, 
1.014397808, 1.014397808), Lake_name = c("PaloPinto", "PaloPinto", 
"PaloPinto")), row.names = c(NA, -3L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(percent = 0.860171369, Lake_name = "PatCleburne"), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.927998999, 0.927998999, 0.927998999, 0.920527951, 
    0.990555668), Lake_name = c("PossumKingdom", "PossumKingdom", 
    "PossumKingdom", "PossumKingdom", "PossumKingdom")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.938790449, 1.007077401, 1.003690153, 1.012294735
    ), Lake_name = c("RayRoberts", "RayRoberts", "RayRoberts", 
    "RayRoberts")), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.816532402, 
0.816532402, 0.816532402, 1.026305383, 1.026305383, 1.026305383, 
0.74906376, 0.74906376, 0.74906376, 1.017855893, 1.017855893, 
1.017855893), Lake_name = c("RichlandChambers", "RichlandChambers", 
"RichlandChambers", "RichlandChambers", "RichlandChambers", "RichlandChambers", 
"RichlandChambers", "RichlandChambers", "RichlandChambers", "RichlandChambers", 
"RichlandChambers", "RichlandChambers")), row.names = c(NA, -12L
), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(1.115899872, 1.04142091, 1.04142091, 1.04142091, 
    1.04142091, 0.946349991, 0.946349991, 0.946349991, 0.946349991, 
    1.006902848, 1.006902848, 1.006902848, 1.006902848, 0.96617729, 
    0.96617729, 0.96617729, 0.889117026, 0.889117026, 0.889117026, 
    1.004503763, 1.004503763, 1.004503763, 0.717543326, 0.938848253, 
    0.938848253, 0.932363178, 0.920428481, 0.920428481, 0.920428481, 
    1.058408932, 0.950432907, 1.058408932, 1.058408932), Lake_name = c("SamRayburn", 
    "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", 
    "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", 
    "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", 
    "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", 
    "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", 
    "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", "SamRayburn", 
    "SamRayburn", "SamRayburn")), row.names = c(NA, -33L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.850517699, 
1.016267772, 0.850517699, 0.850517699, 0.858128973, 0.840538076, 
1.013782498, 1.475488097, 1.013782498), Lake_name = c("Somerville", 
"Somerville", "Somerville", "Somerville", "Somerville", "Somerville", 
"Somerville", "Somerville", "Somerville")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.599141918, 0.692387849, 0.599141918, 0.399200223
    ), Lake_name = c("Sweetwater", "Sweetwater", "Sweetwater", 
    "Sweetwater")), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(0.730412693, 
1.0312829, 1.0312829, 1.0312829, 0.793778928, 0.951172803, 0.793778928, 
0.793778928, 0.89825862, 0.89825862, 1.036546746, 0.89825862), 
    Lake_name = c("Tawakoni", "Tawakoni", "Tawakoni", "Tawakoni", 
    "Tawakoni", "Tawakoni", "Tawakoni", "Tawakoni", "Tawakoni", 
    "Tawakoni", "Tawakoni", "Tawakoni")), row.names = c(NA, -12L
), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    percent = c(0.779311185, 0.962372813, 1.094582091, NA, 1.094582091, 
    1.094582091, 1.041470417, 1.041470417), Lake_name = c("Texoma", 
    "Texoma", "Texoma", "Texoma", "Texoma", "Texoma", "Texoma", 
    "Texoma")), row.names = c(NA, -8L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(percent = c(0.980009412, 0.936288268, 
0.936288268, 0.936288268, 0.936288268, 0.844365179, 0.844365179, 
0.844365179, 0.844365179, 0.952313816, 0.952313816, 0.952313816, 
0.952313816, 0.989684275, 0.989684275, 0.989684275, 0.989684275, 
0.970103592, 0.970103592, 0.970103592, 0.970103592, 0.961245469, 
0.961245469, 0.961245469, 0.721018438, 0.721018438, 0.923647617, 
0.923084916, 0.923084916, 0.923084916, 0.944379741, 1.008805296, 
1.008805296, 1.008805296, 0.986546519, 0.969414714, 0.969414714, 
0.969414714), Lake_name = c("ToledoBend", "ToledoBend", "ToledoBend", 
"ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", 
"ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", 
"ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", 
"ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", 
"ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", 
"ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", 
"ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend", "ToledoBend"
)), row.names = c(NA, -38L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(percent = c(1.029229532, 1.015364781, 1.015364781, 
0.810192038), Lake_name = c("Travis", "Travis", "Travis", "Travis"
)), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(percent = c(0.440960582, 0.286229799, 0.286229799, 
0.440960582), Lake_name = c("TwinButtes", "TwinButtes", "TwinButtes", 
"TwinButtes")), row.names = c(NA, -4L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(percent = c(0.978479283, 0.813063289, 
0.978479283, 0.978479283, 1.010656877, 0.962413365, 0.962413365, 
0.962413365), Lake_name = c("Waco", "Waco", "Waco", "Waco", "Waco", 
"Waco", "Waco", "Waco")), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = 0.977303985, 
    Lake_name = "Waxahachie"), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = 0.162477446, 
    Lake_name = "WhiteRiver"), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(percent = c(1.071594286, 
1.420510935, 1.420510935, 1.420510935), Lake_name = c("Worth", 
"Worth", "Worth", "Worth")), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame")))

and plug these results into the list of models to get predictions for each Lake_name at each corresponding percent value using lapply like this (the name of my list of results is result):

pred1 <- lapply(model_list, function(x) predict(x, newdata=result, type = "response"))

I am getting an error each time I try to run the predictions off of this list. Error is too long to list completely but it starts with:

Called from: (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, 
    fix.empty.names = TRUE, stringsAsFactors = default.stringsAsFactors()) 
{
    data.row.names <- if (check.rows && is.null(row.names)) 
        function(current, new, i) {
            if (is.character(current)) 
                new <- as.character(new)
            if (is.character(new)) 
                current <- as.character(current)
            if (anyDuplicated(new)) 
                return(current)
            if (is.null(current)) 
                return(new)
            if (all(current == new) || all(current == "")) 
                return(new)
            stop(gettextf("mismatch of row names in arguments of 'data.frame', item %d", 
                i), domain = NA)
        }
    else function(current, new, i) {
        if (is.null(current)) {
            if (anyDuplicated(new)) {
                warning(gettextf("some row.names duplicated: %s --> row.names NOT used", 
                  paste(which(duplicated(new)), collapse = ",")), 
                  domain = NA)
                current
            }
            else new
        }
        else current
    }
    object <- as.list(substitute(list(...)))[-1L]
    mirn <- missing(row.names)
    mrn <- is.null(row.names)
    x <- list(...)
    n <- length(x)
    if (n < 1L)

How do I set up a list of models for each Lake_name to run predictions based off another corresponding list of results within lapply?

CodePudding user response:

You need an iterator to move through both the models and the new data. Instead of moving through the models, make it an iterator.

pred1 <- lapply(1:length(model_list), 
                function(x) predict(model_list[[x]], 
                                    newdata = result[[x]], 
                                    type = "response"))
  • Related