{"id":4441,"date":"2023-07-11T02:36:29","date_gmt":"2023-07-11T02:36:29","guid":{"rendered":"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/"},"modified":"2023-07-11T02:36:29","modified_gmt":"2023-07-11T02:36:29","slug":"r-logistische-regressievoorspelling","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/","title":{"rendered":"Hoe predict() te gebruiken met een logistisch regressiemodel in r"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Zodra we een <a href=\"https:\/\/statorials.org\/nl\/logistische-regressie-1\/\" target=\"_blank\" rel=\"noopener\">logistisch regressiemodel<\/a> in R hebben ge\u00efnstalleerd, kunnen we de functie <strong>voorspellen()<\/strong> gebruiken om de responswaarde van een nieuwe waarneming te voorspellen die het model nog nooit eerder heeft gezien.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Deze functie gebruikt de volgende syntaxis:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>voorspellen(object, nieuwedata, type = \u201cantwoord\u201d)<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Goud:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>object:<\/strong> de naam van het logistische regressiemodel<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>newdata:<\/strong> de naam van het nieuwe dataframe waarvoor voorspellingen moeten worden gedaan<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>type:<\/strong> Het type voorspelling dat moet worden gedaan<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Het volgende voorbeeld laat zien hoe u deze functie in de praktijk kunt gebruiken.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld: Predict() gebruiken met een logistiek regressiemodel in R<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Voor dit voorbeeld gebruiken we de ingebouwde R-dataset genaamd <a href=\"https:\/\/statorials.org\/nl\/mtcars-r-gegevensset\/\" target=\"_blank\" rel=\"noopener\">mtcars<\/a> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#view first six rows of <em>mtcars<\/em> dataset<\/span>\nhead(mtcars)\n\n                   mpg cyl disp hp drat wt qsec vs am gear carb\nMazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4\nMazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4\nDatsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1\nHornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1\nHornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2\nValiant 18.1 6 225 105 2.76 3,460 20.22 1 0 3 1<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">We zullen het volgende logistische regressiemodel passen waarin we de variabelen <strong>disp<\/strong> en <strong>hp<\/strong> gebruiken om de responsvariabele <strong>am<\/strong> te voorspellen (het transmissietype van de auto: 0 = automatisch, 1 = handgeschakeld):<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#fit logistic regression model<\/span>\nmodel &lt;- glm(am ~ disp + hp, data=mtcars, family=binomial)\n\n<span style=\"color: #008080;\">#view model summary\n<\/span>summary(model)\n\nCall:\nglm(formula = am ~ disp + hp, family = binomial, data = mtcars)\n\nDeviance Residuals: \n    Min 1Q Median 3Q Max  \n-1.9665 -0.3090 -0.0017 0.3934 1.3682  \n\nCoefficients:\n            Estimate Std. Error z value Pr(&gt;|z|)  \n(Intercept) 1.40342 1.36757 1.026 0.3048  \navailable -0.09518 0.04800 -1.983 0.0474 *\nhp 0.12170 0.06777 1.796 0.0725 .\n---\nSignificant. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\n(Dispersion parameter for binomial family taken to be 1)\n\n    Null deviance: 43,230 on 31 degrees of freedom\nResidual deviance: 16,713 on 29 degrees of freedom\nAIC: 22,713\n\nNumber of Fisher Scoring iterations: 8\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">We kunnen dan een nieuw dataframe maken met informatie over acht auto&#8217;s die het model nog nooit eerder heeft gezien en de functie <strong>voorspellen()<\/strong> gebruiken om de waarschijnlijkheid te voorspellen dat een nieuwe auto een automatische transmissie (am=0) of een handgeschakelde transmissie zal hebben ( ben =1):<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define new data frame\n<span style=\"color: #000000;\">newdata = data. <span style=\"color: #3366ff;\">frame<\/span> (disp=c(200, 180, 160, 140, 120, 120, 100, 160),\n                     hp=c(100, 90, 108, 90, 80, 90, 80, 90),\n                     am=c(0, 0, 0, 1, 0, 1, 1, 1))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>newdata\n\n<span style=\"color: #008080;\">#use model to predict value of am for all new cars\n<\/span>newdata$am_prob &lt;- predict(model, newdata, type=\" <span style=\"color: #ff0000;\">response<\/span> \")\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>newdata\n\n  disp hp am am_prob\n1 200 100 0 0.004225640\n2 180 90 0 0.008361069\n3 160 108 0 0.335916069\n4 140 90 1 0.275162866\n5 120 80 0 0.429961894\n6 120 90 1 0.718090728\n7 100 80 1 0.835013994\n8 160 90 1 0.053546152<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Zo interpreteert u het resultaat:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">De kans dat auto 1 een handgeschakelde versnellingsbak heeft is <strong>0,004<\/strong> .<\/span><\/li>\n<li> <span style=\"color: #000000;\">De kans dat auto 2 een handgeschakelde versnellingsbak heeft is <strong>0,008<\/strong> .<\/span><\/li>\n<li> <span style=\"color: #000000;\">De kans dat auto 3 een handgeschakelde versnellingsbak heeft is <strong>0,336<\/strong> .<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Enzovoort.<\/span><\/p>\n<p> <span style=\"color: #000000;\">We kunnen ook de functie <strong>table()<\/strong> gebruiken om een verwarringsmatrix te maken die de werkelijke am-waarden weergeeft versus de waarden die door het model worden voorspeld:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create vector that contains 0 or 1 depending on predicted value of am\n<span style=\"color: #000000;\">am_pred = rep(0, dim(newdata)[1])\nam_pred[newdata$am_prob &gt; .5] = 1\n\n<span style=\"color: #008080;\">#create confusion matrix\n<\/span>table(am_pred, newdata$am)\n\nam_pred 0 1\n      0 4 2\n      1 0 2\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Ten slotte kunnen we de functie <strong>Mean()<\/strong> gebruiken om het percentage waarnemingen in de nieuwe database te berekenen waarvoor het model de waarde van <strong>am<\/strong> correct voorspelde:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#calculate percentage of observations the model correctly predicted response value for\n<span style=\"color: #000000;\">mean(am_pred == newdata$am)\n\n[1] 0.75\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">We kunnen zien dat het model de <strong>am-<\/strong> waarde correct voorspelde voor <strong>75%<\/strong> van de auto&#8217;s in de nieuwe database.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Aanvullende bronnen<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">In de volgende tutorials wordt uitgelegd hoe u andere veelvoorkomende taken in R kunt uitvoeren:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/eenvoudige-lineaire-regressie-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe eenvoudige lineaire regressie uit te voeren in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/meervoudige-lineaire-regressie-r\/\" target=\"_blank\" rel=\"noopener\">Hoe meervoudige lineaire regressie uit te voeren in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/polynomiale-regressie-r\/\" target=\"_blank\" rel=\"noopener\">Hoe polynomiale regressie uit te voeren in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/voorspellingsinterval-r\/\" target=\"_blank\" rel=\"noopener\">Een voorspellingsinterval maken in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Zodra we een logistisch regressiemodel in R hebben ge\u00efnstalleerd, kunnen we de functie voorspellen() gebruiken om de responswaarde van een nieuwe waarneming te voorspellen die het model nog nooit eerder heeft gezien. Deze functie gebruikt de volgende syntaxis: voorspellen(object, nieuwedata, type = \u201cantwoord\u201d) Goud: object: de naam van het logistische regressiemodel newdata: de naam van [&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":[],"class_list":["post-4441","post","type-post","status-publish","format-standard","hentry","category-gids"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Hoe voorspellen() te gebruiken met het logistische regressiemodel in R - Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u voorspellingen kunt doen over nieuwe gegevens met behulp van een logistisch regressiemodel in R.\" \/>\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\/nl\/r-logistische-regressievoorspelling\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe voorspellen() te gebruiken met het logistische regressiemodel in R - Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u voorspellingen kunt doen over nieuwe gegevens met behulp van een logistisch regressiemodel in R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-11T02:36:29+00:00\" \/>\n<meta name=\"author\" content=\"Dr.benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Verfasst von\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr.benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/\",\"url\":\"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/\",\"name\":\"Hoe voorspellen() te gebruiken met het logistische regressiemodel in R - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-11T02:36:29+00:00\",\"dateModified\":\"2023-07-11T02:36:29+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u voorspellingen kunt doen over nieuwe gegevens met behulp van een logistisch regressiemodel in R.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoe predict() te gebruiken met een logistisch regressiemodel in r\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/nl\/#website\",\"url\":\"https:\/\/statorials.org\/nl\/\",\"name\":\"Statorials\",\"description\":\"Uw gids voor statistische competentie\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/nl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"de\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\",\"name\":\"Dr.benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr.benjamin anderson\"},\"description\":\"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder\",\"sameAs\":[\"http:\/\/statorials.org\/nl\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hoe voorspellen() te gebruiken met het logistische regressiemodel in R - Statorials","description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u voorspellingen kunt doen over nieuwe gegevens met behulp van een logistisch regressiemodel in R.","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\/nl\/r-logistische-regressievoorspelling\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe voorspellen() te gebruiken met het logistische regressiemodel in R - Statorials","og_description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u voorspellingen kunt doen over nieuwe gegevens met behulp van een logistisch regressiemodel in R.","og_url":"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/","og_site_name":"Statorials","article_published_time":"2023-07-11T02:36:29+00:00","author":"Dr.benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr.benjamin anderson","Gesch\u00e4tzte Lesezeit":"3\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/","url":"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/","name":"Hoe voorspellen() te gebruiken met het logistische regressiemodel in R - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-11T02:36:29+00:00","dateModified":"2023-07-11T02:36:29+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u voorspellingen kunt doen over nieuwe gegevens met behulp van een logistisch regressiemodel in R.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/r-logistische-regressievoorspelling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Hoe predict() te gebruiken met een logistisch regressiemodel in r"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/nl\/#website","url":"https:\/\/statorials.org\/nl\/","name":"Statorials","description":"Uw gids voor statistische competentie","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/nl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"de"},{"@type":"Person","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219","name":"Dr.benjamin anderson","image":{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/","url":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr.benjamin anderson"},"description":"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder","sameAs":["http:\/\/statorials.org\/nl"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/4441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/comments?post=4441"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/4441\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=4441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=4441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=4441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}