{"id":2041,"date":"2023-07-23T23:32:33","date_gmt":"2023-07-23T23:32:33","guid":{"rendered":"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/"},"modified":"2023-07-23T23:32:33","modified_gmt":"2023-07-23T23:32:33","slug":"randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/","title":{"rendered":"Come risolvere il problema: randomforest.default(m, y, \u2026): na\/nan\/inf nella chiamata di funzione esterna"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Un errore che potresti riscontrare in R \u00e8:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>Error in randomForest.default(m, y, ...): \n  NA\/NaN\/Inf in foreign function call (arg 1)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo errore pu\u00f2 verificarsi per due motivi:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Nel set di dati sono presenti valori NA, NaN o Inf<\/span><\/li>\n<li> <span style=\"color: #000000;\">Una delle variabili nel set di dati \u00e8 un carattere<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Il modo pi\u00f9 semplice per correggere questo errore \u00e8 rimuovere le righe con dati mancanti e convertire le variabili carattere in variabili fattore:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#remove rows with missing values \n<\/span>df &lt;- na. <span style=\"color: #3366ff;\">omitted<\/span> (df)\n\n<span style=\"color: #008080;\">#convert all character variables to factor variables<\/span>\n<span style=\"color: #993300;\">library<\/span> (dplyr)\ndf %&gt;% mutate_if(is. <span style=\"color: #3366ff;\">character<\/span> , as. <span style=\"color: #3366ff;\">factor<\/span> )<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo tutorial condivide un esempio di come correggere questo errore nella pratica.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Correlati:<\/strong><\/span> <a href=\"https:\/\/statorials.org\/it\/esercitazione-casuale-in-r\/\" target=\"_blank\" rel=\"noopener\">Come creare foreste casuali in R (passo dopo passo)<\/a><\/p>\n<h3> <strong>Come riprodurre l&#8217;errore<\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Supponiamo di provare ad adattare una foresta casuale al seguente frame di dati in R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #993300;\">library<\/span> (randomForest)\n\n<span style=\"color: #008080;\">#create data frame\n<\/span>df &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (y &lt;- c(30, 29, 30, 45, 23, 19, 9, 8, 11, 14),\n                 x1 &lt;- c('A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C'),\n                 x2 &lt;- c(4, 4, 5, 7, 8, 7, 9, 6, 13, 15))\n\n<span style=\"color: #008080;\">#attempt to fit random forest model\n<\/span>model &lt;- randomForest(formula = y ~ ., data = df)\n\nError in randomForest.default(m, y, ...):\n  NA\/NaN\/Inf in foreign function call (arg 1)<\/span><\/span><\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Riceviamo un errore perch\u00e9 x1 \u00e8 una variabile di carattere nel frame di dati.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Possiamo confermarlo utilizzando la funzione <strong>str()<\/strong> per visualizzare la struttura del frame di dati:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #000000;\"><span style=\"color: #993300;\"><span style=\"color: #000000;\">str(df)<\/span>\n\n<\/span>'data.frame': 10 obs. of 3 variables:\n $ y....c.30..29..30..45: num 30 29 30 45 23 19 9 8 11 14\n $ x1....c..A....A....B....B.... : chr \"A\" \"A\" \"B\" \"B\"\n $ x2....c.4..4..5..7..: num 4 4 5 7 8 7 9 6 13 15<\/span><\/strong><\/span>\n<\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Come correggere l&#8217;errore<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Per correggere questo errore, possiamo utilizzare la funzione <strong>mutate_if()<\/strong> di <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/mutate_all.html\" target=\"_blank\" rel=\"noopener\">dplyr<\/a> per convertire ciascuna colonna di caratteri in una colonna di fattori:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #993300;\">library<\/span> (dplyr)\n\n<span style=\"color: #008080;\">#convert each character column to factor\n<\/span>df = df %&gt;% mutate_if(is. <span style=\"color: #3366ff;\">character<\/span> , as. <span style=\"color: #3366ff;\">factor<\/span> )\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Possiamo quindi adattare il modello della foresta casuale al frame di dati:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#fit random forest model<\/span>\nmodel &lt;- randomForest(formula = y ~ ., data = df)\n\n<span style=\"color: #008080;\">#view summary of model<\/span>\nmodel\n\nCall:\n randomForest(formula = y ~ ., data = df) \n               Type of random forest: regression\n                     Number of trees: 500\nNo. of variables tried at each split: 1\n\n          Mean of squared residuals: 65.0047\n                    % Var explained: 48.64\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Questa volta non riceviamo alcun errore perch\u00e9 non ci sono pi\u00f9 variabili di carattere nel dataframe.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come risolvere altri errori comuni in R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/la-condizione-r-ha-lunghezza-1-verra-utilizzato-solo-il-primo-elemento\/\" target=\"_blank\" rel=\"noopener\">Come risolvere il problema: la condizione ha lunghezza &gt; 1 e verr\u00e0 utilizzato solo il primo elemento<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/r-error-dim-deve-avere-una-lunghezza-positiva\/\" target=\"_blank\" rel=\"noopener\">Come risolvere in R: dim(X) deve avere una lunghezza positiva<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/r-valore-mancante-o-vero-falso-necessario\/\" target=\"_blank\" rel=\"noopener\">Come risolvere in R: valore mancante dove \u00e8 necessario vero\/falso<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/nas-introdotto-con-la-coercizione-in-r\/\" target=\"_blank\" rel=\"noopener\">Come risolvere: NA introdotte dalla coercizione<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Un errore che potresti riscontrare in R \u00e8: Error in randomForest.default(m, y, &#8230;): NA\/NaN\/Inf in foreign function call (arg 1) Questo errore pu\u00f2 verificarsi per due motivi: Nel set di dati sono presenti valori NA, NaN o Inf Una delle variabili nel set di dati \u00e8 un carattere Il modo pi\u00f9 semplice per correggere questo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come risolvere il problema: randomForest.default(m, y, ...): Na\/NaN\/Inf nella chiamata di funzione esterna - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come correggere il seguente errore in R: errore in randomforest.default(m, y, ...): na\/nan\/inf nella chiamata di funzione esterna (arg 1).\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come risolvere il problema: randomForest.default(m, y, ...): Na\/NaN\/Inf nella chiamata di funzione esterna - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come correggere il seguente errore in R: errore in randomforest.default(m, y, ...): na\/nan\/inf nella chiamata di funzione esterna (arg 1).\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-23T23:32:33+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/\",\"url\":\"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/\",\"name\":\"Come risolvere il problema: randomForest.default(m, y, ...): Na\/NaN\/Inf nella chiamata di funzione esterna - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-23T23:32:33+00:00\",\"dateModified\":\"2023-07-23T23:32:33+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come correggere il seguente errore in R: errore in randomforest.default(m, y, ...): na\/nan\/inf nella chiamata di funzione esterna (arg 1).\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come risolvere il problema: randomforest.default(m, y, \u2026): na\/nan\/inf nella chiamata di funzione esterna\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come risolvere il problema: randomForest.default(m, y, ...): Na\/NaN\/Inf nella chiamata di funzione esterna - Statorials","description":"Questo tutorial spiega come correggere il seguente errore in R: errore in randomforest.default(m, y, ...): na\/nan\/inf nella chiamata di funzione esterna (arg 1).","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/","og_locale":"it_IT","og_type":"article","og_title":"Come risolvere il problema: randomForest.default(m, y, ...): Na\/NaN\/Inf nella chiamata di funzione esterna - Statorials","og_description":"Questo tutorial spiega come correggere il seguente errore in R: errore in randomforest.default(m, y, ...): na\/nan\/inf nella chiamata di funzione esterna (arg 1).","og_url":"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/","og_site_name":"Statorials","article_published_time":"2023-07-23T23:32:33+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/","url":"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/","name":"Come risolvere il problema: randomForest.default(m, y, ...): Na\/NaN\/Inf nella chiamata di funzione esterna - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-23T23:32:33+00:00","dateModified":"2023-07-23T23:32:33+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come correggere il seguente errore in R: errore in randomforest.default(m, y, ...): na\/nan\/inf nella chiamata di funzione esterna (arg 1).","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/randomforest-na-nan-inf-nella-chiamata-di-funzione-esterna\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come risolvere il problema: randomforest.default(m, y, \u2026): na\/nan\/inf nella chiamata di funzione esterna"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2041"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=2041"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2041\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}