Создание растра из фрейма данных создает ошибку

#r

#r

Вопрос:

Я запускаю случайный лес по сравнению PC_Count с несколькими переменными

 rf_h_count_km lt;- randomForest (  formula = PC_count ~NLCD Pop_Focal NLUD Population Urban Slope NLCD21_f NLCD22_f NLCD23_f NLCD24_f Density PC,  data = training,  method = 'rf',   tuneGrid = tuneGrid,  ntree = 100,  nodesize = 5,  importance = TRUE,  trControl = ctrl,  metric = "ROC" )  

Случайный лес прошел успешно, но теперь я пытаюсь преобразовать выходные данные в растр прогнозирования. Видя, что переменная предиктора получена из фрейма данных, а независимые переменные из стека растров, я не нашел способа сделать это напрямую

 #predict the response variable with training data  llt;-raster::predict(rf_h_count_km, type="response", data=testing, index=2, na.rm=TRUE, progress="window", overwrite=TRUE)   

Я преобразовал выходной массив в datframe формат

 xlt;-as.data.frame(l) names(x) lt;- c("try")  

Во-вторых, я создал пустой растр с необходимыми размерами

 r = raster(ncols=225, nrows=176, xmn=-1788075, xmx=-1584465, ymn=1481625, ymx=1717755, res=1000)  

В-третьих, я попытался добавить значения из фрейма данных в растр. Однако это приводит к ошибке

 gt; raster::values(r)lt;-x$try Error in setValues(x, value) :   length(values) is not equal to ncell(x), or to 1  

Есть ли какой-нибудь способ добавить эти значения в растр без возникновения ошибки?

Ниже приведен сегмент фрейма данных с 1 столбцом

 gt; dput(head(x,500)) structure(list(try = c(1.25482243953606, 1.29704685733086, 1.59604755116753,  1.27323310015838, 1.65523336838096, 1.16055734781969, 1.91659455992709,  1.52214228365861, 1.39167234672577, 1.39345512204005, 1.14707832355435,  1.38027338524104, 1.43416846811582, 1.19860901400402, 1.36468712553493,  1.39169436843932, 1.331203829109, 1.92116276432308, 1.76695170198865,  1.29033362603409, 1.49713229698021, 1.43769464696184, 1.68270039388572,  1.44802123045305, 1.48484344231137, 1.5923432415293, 1.89790742744141,  1.32407450302128, 1.41150242816569, 1.56883137580953, 1.39132737542988,  1.34693405858518, 1.70515171668636, 1.787306308579, 1.53811871225996,  1.54649976632651, 1.37557485902264, 1.42460519475178, 1.84602421808187,  1.46107494748112, 1.48574351291447, 1.55515920286892, 1.47435149169411,  2.06774644973887, 1.67011263561363, 1.53676870286262, 1.51437261683124,  1.65344434977873, 1.52521033303975, 1.33428311000463, 1.86538942543929,  1.1728691498302, 1.17687652159234, 1.80537815643604, 1.49692701357964,  1.00050456141199, 1.47365424021922, 1.47114102470081, 1.39277787775097,  0.865697705973826, 1.50096649740468, 1.13385260841033, 1.65271338239792,  1.46884690867827, 1.481906677787, 1.14199273373277, 1.68774387988497,  1.51872838325436, 1.38328649616044, 1.67114602131701, 1.54848407844392,  1.3996624303754, 1.71789148484286, 1.33823346156167, 1.25566170818395,  1.55020641523366, 1.17128373170123, 1.26915332769989, 1.66256952986062,  1.50038471501031, 1.30659761867545, 1.38389413400123, 1.59603869890816,  1.28059405159913, 1.49876041289129, 1.45692568883059, 1.82472848165875,  1.23544716197641, 1.30844258992342, 1.62576923439109, 1.26939004412273,  1.58507687431416, 1.31481901026701, 1.4575809162353, 1.53809208050283,  1.16766113344713, 1.56314563435862, 1.55411214306813, 1.78357145366888,  1.4329858089636, 1.43396598308919, 1.39465154299149, 1.48451992338695,  1.24684337200367, 1.96110469737173, 1.16625138690017, 0.775810330702323,  1.00045993259336, 1.37686548806939, 1.62733354682162, 1.68759141765908,  1.628166880637, 1.94206520355245, 1.61238598818564, 1.96885931093973,  1.34625059573652, 1.31831686519609, 1.53290631646635, 1.57775601413376,  1.58363330493889, 1.89319841817133, 0.782837422055985, 1.64643379323501,  1.66712161497969, 1.29699891021702, 1.47429558125218, 1.68691877809511,  1.74608775523785, 1.9991622606389, 1.56607510448966, 1.6309907225318,  1.46078033728118, 1.17142033440308, 1.40255145198544, 1.33112985551097,  1.15166500669548, 1.80419274569205, 1.62729203127811, 1.83685487185426,  1.58635321656375, 1.82779066393067, 1.34477410566094, 1.49887518851983,  1.50264666884424, 1.30559742049033, 1.70529841717443, 1.36461045525143,  1.35656649147087, 1.37880956124176, 1.32825774615472, 1.4743468228756,  1.68848953739118, 1.5714969472275, 1.28051735582015, 1.70382287292112,  1.48697666114366, 1.52509854662891, 2.00215110778257, 1.51573583601317,  1.52652734904295, 1.83725972310248, 1.43565914024433, 1.65558396263679,  1.28093702179809, 1.34220737362985, 1.45510583039298, 1.34188715043272,  1.59560616172484, 1.18911884147528, 1.76384130089723, 1.14960238421033,  1.36430236202792, 1.62519404962719, 1.41158597502356, 0.797289908270849,  1.15520086855207, 1.42517561255296, 1.00047571138336, 1.31365223086707,  1.4129217354065, 1.29535822063539, 1.67078827797151, 1.54029946373479,  1.84666725485803, 1.31394353069208, 1.30563028604674, 1.81450886504348,  1.19491662297257, 1.27229869878179, 1.28963013410991, 1.43762367763379,  1.16928002123748, 0.76442403537918, 1.28823684878266, 1.74785996977029,  1.64959703066888, 1.5509430052318, 1.45843245579434, 1.63183302686946,  1.59733661443473, 0.760843648977963, 1.36684005161429, 1.84519682070654,  1.50049023315098, 1.7050215606835, 1.62763853632365, 1.56200633371077,  1.51191419294519, 1.6116950079444, 1.76167964082101, 1.6283596872148,  1.60698192017333, 1.66810316469347, 1.16126887133296, 1.38968215164831,  1.48455441438511, 0.660013152279427, 1.53085391999465, 1.82842047029258,  1.1661862475647, 1.00044570589583, 1.70599622205831, 1.38431550482051,  1.50410961983431, 1.61241963890188, 1.68592745635435, 1.36708568021482,  1.49941207893968, 1.48543479320907, 1.35809437513195, 1.55296348681227,  1.59819516191763, 1.48481302388378, 1.4061746887853, 1.36929246790332,  1.39266730855954, 1.46027138849061, 1.53273782134419, 1.16136716380526,  1.56032756345299, 1.76303556273634, 1.30009474488364, 0.713431047919119,  1.27230670052624, 1.74416452489221, 1.61122408130062, 1.89634699686347,  1.47106580278593, 1.28149781048158, 1.55039187691515, 1.70574909049325,  1.30713212157724, 1.52846086526416, 1.67346330500358, 1.43438661455238,  1.18645458508454, 1.63936712727192, 1.41112353469103, 1.2761061429259,  1.27580024677754, 1.3072009173607, 0.76482905669465, 1.48493548485036,  1.62951834284185, 1.26147044940371, 1.51789060097793, 1.41333216708036,  1.33639249077728, 1.45542697933075, 1.43760708672506, 1.71199034891044,  1.26027699097399, 1.32370075969687, 1.14957580510731, 1.42015421688412,  1.80770319259332, 1.56027185359019, 1.26335891477022, 1.14435742981936,  1.21496018704641, 1.1581564288297, 1.53379063258365, 1.51294803803159,  1.30732487742837, 1.43717349783508, 1.43486086288793, 1.50187858120187,  1.15120725634633, 1.35400674186641, 1.55364027339135, 1.94536872847578,  1.710630763992, 1.71391827834412, 1.00050653319808, 1.30922140163975,  1.66640189638562, 1.70458770847952, 1.41382683700093, 1.54886023547989,  1.62626167586269, 1.91473409741524, 1.63101761509542, 1.67304098596854,  1.43182019801906, 1.14567627381978, 1.35238386442408, 1.64751122114616,  1.34873996399922, 1.37023850292472, 1.53564601207952, 1.57505619181632,  1.36708995300055, 1.56941164647469, 1.16925475135763, 0.842871188459787,  1.3812699305909, 1.41386414196727, 1.16059409444905, 1.76201853912225,  1.44222009921562, 1.64997144444006, 1.38376159924388, 1.61003199654573,  1.57499790571938, 1.15201686470987, 1.76676689613511, 1.8096908759832,  1.20486311868743, 1.68728039392101, 1.42500419002418, 1.44665607038376,  1.58123291895195, 1.14005656747451, 1.48678222431433, 1.44661082228836,  1.61171967442807, 1.30729777317031, 1.42286763782785, 1.40553596608522,  1.55064563919909, 1.70051648043645, 1.69303294109564, 1.5515686820675,  1.49226483353159, 1.43554567483609, 1.30857962097702, 1.41632801810122,  1.66756491810872, 1.44526232593268, 1.48719674319771, 1.42309996893368,  1.6580257935528, 1.5829194496966, 1.64235861450852, 1.44895677424997,  1.61443646370592, 1.68792404047021, 1.56680875163758, 1.63772239523653,  1.38406187171508, 1.18445033651953, 1.80761413246068, 1.52670027322229,  1.13013642070425, 1.56261574480113, 1.6888386513478, 1.00051404040559,  1.53839461415883, 1.58065043297887, 1.45576137913913, 1.49989463209582,  1.28055237635367, 1.50010654014454, 1.42137860072691, 1.56511565854225,  1.70639749591321, 1.41508801317881, 1.73233307347998, 1.13841982707047,  1.4400662466764, 1.80565275760731, 0.77520490363639, 1.61527499647451,  1.32672943129696, 1.76607190217432, 1.27583861087918, 1.73820202046061,  1.51335423345056, 1.30921314993083, 1.89626987520185, 1.4473406270278,  1.35322135964277, 2.01512208326191, 1.50393203460877, 1.68770896622325,  0.805436684457629, 1.37740579297073, 1.76790174733803, 1.6562649800791,  1.34223556479203, 1.15246311383752, 1.68485931618636, 1.40568909806569,  0.784724015738059, 1.49886040116948, 1.8272016947891, 1.76469931390133,  1.26688645784432, 1.72643807856103, 1.43467491769798, 1.26459859241559,  1.68797176508759, 1.46051637830131, 1.49869552936372, 1.14441765225327,  1.57745957616188, 1.28307917058032, 1.74574595151118, 1.35334508600179,  1.59272719674702, 1.19935222312585, 1.35880915261704, 1.15239419992794,  1.4044441578425, 1.39219305326424, 1.44629956051605, 2.01162723040148,  1.4711194451495, 1.62984928328262, 1.29120536667572, 1.7437705740116,  1.32490447606648, 1.73704376707315, 1.60847808850637, 1.38597284699774,  1.59628605075663, 1.43444007060914, 1.3244765426713, 1.36864321432182,  1.60706449020901, 1.14400820678228, 1.28098051291041, 1.59293724647213,  1.20693559821733, 0.762694890744079, 1.00051265006564, 1.15238864138768,  1.94340119252715, 1.18820627573647, 1.36578984019797, 1.78610658908017,  1.6139282437022, 1.56664750472243, 1.28225859203438, 1.351183496751,  1.74883140168085, 1.98997982109868, 1.36718566083602, 1.81044434104092,  1.27607390816209, 1.45917517513078, 1.46192984151842, 1.72610404482554,  1.6265388077098, 1.55430788004514, 1.44485886502457, 1.73699539353876,  1.48594712331721, 1.42357556702341, 1.32457308337899, 1.55064010993579,  1.42291188618139, 1.64818926255211, 1.13841846135019, 1.95209192401635,  1.66937085634394, 1.55376682098252, 1.624705399401, 1.30640121517372,  1.28092432425333, 1.42589541938036, 1.41146701872041, 1.14830789689793,  1.3160788417034, 1.58225727505092, 1.16056978361285, 1.4883577330535,  1.41232141834121, 1.52493787348832, 1.62487746013324, 1.43269182322572,  1.55049696656887, 1.53525920970974, 1.31609276967797, 1.68663626612582,  1.46992801989381, 1.51568282463528, 1.56295971478745, 1.43171059023851,  1.48486531481598)), row.names = c("4", "5", "8", "9", "10", "13",  "15", "16", "17", "18", "19", "20", "21", "22", "23", "26", "27",  "28", "29", "31", "34", "35", "37", "39", "40", "41", "42", "43",  "44", "45", "46", "47", "48", "49", "50", "51", "54", "55", "56",  "57", "59", "60", "61", "62", "63", "64", "66", "67", "68", "71",  "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82",  "83", "84", "85", "86", "87", "88", "89", "90", "92", "93", "94",  "95", "96", "97", "98", "99", "100", "102", "103", "104", "105",  "106", "107", "108", "109", "111", "112", "113", "114", "115",  "117", "118", "119", "120", "121", "123", "124", "125", "126",  "127", "128", "130", "131", "132", "133", "134", "135", "136",  "137", "138", "139", "141", "143", "144", "145", "147", "148",  "150", "152", "153", "154", "156", "158", "159", "162", "163",  "165", "168", "169", "172", "174", "175", "176", "177", "178",  "179", "180", "182", "183", "184", "185", "186", "187", "188",  "190", "191", "194", "196", "197", "198", "199", "202", "204",  "205", "206", "208", "209", "211", "212", "213", "214", "215",  "216", "217", "218", "219", "220", "221", "223", "224", "227",  "228", "229", "230", "231", "232", "233", "236", "238", "239",  "240", "241", "242", "243", "244", "245", "248", "251", "254",  "255", "257", "258", "259", "260", "262", "263", "264", "265",  "266", "268", "270", "271", "272", "273", "274", "276", "278",  "279", "280", "281", "282", "283", "284", "285", "286", "287",  "288", "289", "290", "292", "293", "295", "296", "297", "300",  "301", "304", "307", "309", "310", "312", "314", "315", "316",  "317", "318", "319", "320", "321", "322", "323", "324", "325",  "326", "327", "328", "330", "331", "332", "333", "334", "336",  "339", "340", "345", "346", "349", "350", "351", "352", "353",  "355", "356", "358", "359", "360", "362", "363", "364", "365",  "366", "367", "368", "369", "370", "371", "372", "373", "376",  "377", "378", "380", "381", "382", "383", "384", "385", "386",  "391", "392", "395", "396", "398", "399", "400", "403", "404",  "405", "406", "407", "408", "409", "410", "411", "413", "414",  "415", "417", "418", "419", "420", "421", "422", "424", "426",  "427", "428", "429", "430", "431", "433", "434", "435", "436",  "437", "441", "442", "443", "446", "447", "449", "450", "451",  "453", "454", "457", "458", "459", "460", "463", "465", "466",  "467", "468", "469", "470", "471", "472", "473", "475", "476",  "477", "478", "479", "480", "481", "482", "483", "484", "486",  "487", "489", "490", "491", "493", "494", "495", "498", "499",  "500", "502", "503", "506", "508", "509", "510", "511", "512",  "513", "514", "515", "516", "517", "520", "521", "522", "523",  "524", "525", "526", "527", "528", "529", "530", "531", "532",  "533", "534", "537", "539", "541", "543", "545", "546", "548",  "549", "550", "551", "552", "554", "555", "556", "558", "559",  "560", "561", "562", "563", "564", "565", "566", "567", "568",  "569", "570", "571", "572", "573", "574", "575", "576", "577",  "579", "580", "581", "582", "583", "584", "585", "586", "588",  "589", "591", "592", "593", "596", "598", "599", "601", "602",  "604", "606", "608", "609", "610", "611", "614", "615", "616",  "617", "618", "619", "620", "621", "623", "626", "627", "628",  "629", "630", "632", "633", "634", "635", "636", "637", "638",  "639", "640", "641", "642", "644", "645", "646", "647", "649",  "650", "651", "653", "654", "655", "656", "657", "658", "659",  "660", "662", "663", "664"), class = "data.frame")  

Ответ №1:

Вот как вы можете это сделать с terra помощью (замены raster )

 library(terra) logo lt;- rast(system.file("ex/logo.tif", package="terra"))  names(logo) lt;- c("red", "green", "blue") p lt;- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85,   66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31,   22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)  a lt;- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,  99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,  37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)  xy lt;- rbind(cbind(1, p), cbind(0, a)) e lt;- extract(logo, xy[,2:3]) v lt;- data.frame(cbind(pa=xy[,1], e[,-1]))  library(randomForest) rfm lt;- randomForest(formula=pa~., data=v) p lt;- predict(logo, rfm)  

Я скопировал это из файла справки ?terra::predict . Тот же пример для растра доступен в raster::predict .

Обратите внимание, что первым аргументом для predict должен быть a SpatRaster (или Растр* для растрового пакета). Что ты делаешь:

 l lt;- raster::predict(rf_h_count_km,...)  

это неправильно, потому что это просто дает вам прогнозы для обучающих данных модели, поскольку вы не предоставляете растровые данные для прогнозирования.

((и raster::predict является универсальной функцией, которая выберет соответствующую реализацию на основе первого аргумента. В этом случае он просто отправится в randomForest::predict ))

Если вы не обучили свою модель всем своим растровым данным, в этом случае вы могли бы использовать что-то вроде

 rfma lt;- randomForest(formula=red~., data=as.data.frame(logo))  r lt;- rast(logo, nlyr=1)  values(r) lt;- predict(rfma)