{"id":1576,"date":"2023-07-25T19:42:15","date_gmt":"2023-07-25T19:42:15","guid":{"rendered":"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/"},"modified":"2023-07-25T19:42:15","modified_gmt":"2023-07-25T19:42:15","slug":"matrixverwarring-in-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/","title":{"rendered":"Een verwarringsmatrix cre\u00ebren in r (stap voor stap)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\"><a href=\"https:\/\/statorials.org\/nl\/logistische-regressie-1\/\" target=\"_blank\" rel=\"noopener\">Logistische regressie<\/a> is een type regressie dat we kunnen gebruiken als de responsvariabele binair is.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Een gebruikelijke manier om de kwaliteit van een logistisch regressiemodel te beoordelen, is door een <strong>verwarringsmatrix<\/strong> te maken, een tabel van 2 \u00d7 2 die de voorspelde waarden van het model weergeeft versus de werkelijke waarden van de testdataset.<\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-15654 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/confusionr1.png\" alt=\"\" width=\"292\" height=\"129\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">Het volgende stapsgewijze voorbeeld laat zien hoe u een verwarringsmatrix in R maakt.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Stap 1: Pas het logistische regressiemodel toe<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Voor dit voorbeeld gebruiken we de <strong>standaarddataset<\/strong> uit het <strong>ISLR-<\/strong> pakket. We zullen de studentenstatus, het banksaldo en het jaarinkomen gebruiken om de waarschijnlijkheid te voorspellen dat een bepaalde persoon zijn lening niet kan afbetalen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u een logistisch regressiemodel aan deze gegevensset kunt aanpassen:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#load necessary packages\n<\/span><span style=\"color: #993300;\">library<\/span> (caret)\n<span style=\"color: #993300;\">library<\/span> (InformationValue)\n<span style=\"color: #993300;\">library<\/span> (ISLR)\n\n<span style=\"color: #008080;\">#load dataset\n<\/span>data &lt;-Default\n\n<span style=\"color: #008080;\">#split dataset into training and testing set\n<\/span>set. <span style=\"color: #3366ff;\">seeds<\/span> (1)\nsample &lt;- sample(c(TRUE, FALSE), nrow(data), replace= <span style=\"color: #008000;\">TRUE<\/span> , prob=c(0.7,0.3))\ntrain &lt;- data[sample, ]\ntest &lt;- data[!sample, ]\n\n<span style=\"color: #008080;\">#fit logistic regression model\n<\/span>model &lt;- glm(default~student+balance+income, family=\" <span style=\"color: #008000;\">binomial<\/span> \", data=train)\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Stap 2: Cre\u00eber de verwarringsmatrix<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Vervolgens zullen we de functie <strong>verwarringMatrix()<\/strong> uit het <strong>dakje-<\/strong> pakket gebruiken om een verwarringsmatrix te maken:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#use model to predict probability of default\n<span style=\"color: #000000;\">predicted &lt;- predict(model, test, type=\"response\")\n<\/span>\n#convert defaults from \"Yes\" and \"No\" to 1's and 0's\n<span style=\"color: #000000;\">test$default &lt;- ifelse(test$default==\" <span style=\"color: #008000;\">Yes<\/span> \", 1, 0)\n<\/span>\n#find optimal cutoff probability to use to maximize accuracy\n<span style=\"color: #000000;\">optimal &lt;- optimalCutoff(test$default, predicted)[1]\n<\/span>\n#create confusion matrix\n<span style=\"color: #000000;\">confusionMatrix(test$default, predicted)\n\n     0 1\n0 2912 64\n1 21 39\n<\/span><\/span><\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Stap 3: Evalueer de verwarringsmatrix<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">We kunnen ook de volgende statistieken berekenen met behulp van de verwarringsmatrix:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>Gevoeligheid:<\/strong> Het \u2018echte positiviteitspercentage\u2019 \u2013 het percentage individuen waarvan het model correct voorspelde dat ze in gebreke zouden blijven.<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>Specificiteit:<\/strong> Het \u201cechte negatieve percentage\u201d \u2013 het percentage individuen waarvan het model correct voorspelde dat ze <em>niet<\/em> in gebreke zouden blijven.<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>Totaal percentage misclassificaties:<\/strong> percentage van het totale aantal misclassificaties dat door het model is gemaakt.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u deze statistieken berekent:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#calculate sensitivity<\/span>\nsensitivity(test$default, predicted)\n\n[1] 0.3786408\n\n<span style=\"color: #008080;\">#calculate specificity\n<\/span>specificity(test$default, predicted)\n\n[1] 0.9928401\n\n<span style=\"color: #008080;\">#calculate total misclassification error rate\n<\/span>misClassError(test$default, predicted, <span style=\"color: #3366ff;\">threshold<\/span> =optimal)\n\n[1] 0.027<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Het totale classificatiefoutenpercentage bedraagt voor dit model <strong>2,7%<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Over het algemeen geldt: hoe lager dit percentage, hoe beter het model de resultaten kan voorspellen. Dit specifieke model blijkt daarom zeer effectief te zijn bij het voorspellen of een individu in gebreke zal blijven of niet.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Logistische regressie is een type regressie dat we kunnen gebruiken als de responsvariabele binair is. Een gebruikelijke manier om de kwaliteit van een logistisch regressiemodel te beoordelen, is door een verwarringsmatrix te maken, een tabel van 2 \u00d7 2 die de voorspelde waarden van het model weergeeft versus de werkelijke waarden van de testdataset. Het [&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-1576","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>Een verwarringsmatrix cre\u00ebren in R (stap voor stap)<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt uitgelegd hoe u een verwarringsmatrix maakt in R, inclusief een stapsgewijs voorbeeld.\" \/>\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\/matrixverwarring-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Een verwarringsmatrix cre\u00ebren in R (stap voor stap)\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt uitgelegd hoe u een verwarringsmatrix maakt in R, inclusief een stapsgewijs voorbeeld.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-25T19:42:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/confusionr1.png\" \/>\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=\"2\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/\",\"url\":\"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/\",\"name\":\"Een verwarringsmatrix cre\u00ebren in R (stap voor stap)\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-25T19:42:15+00:00\",\"dateModified\":\"2023-07-25T19:42:15+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt uitgelegd hoe u een verwarringsmatrix maakt in R, inclusief een stapsgewijs voorbeeld.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Een verwarringsmatrix cre\u00ebren in r (stap voor stap)\"}]},{\"@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":"Een verwarringsmatrix cre\u00ebren in R (stap voor stap)","description":"In deze tutorial wordt uitgelegd hoe u een verwarringsmatrix maakt in R, inclusief een stapsgewijs voorbeeld.","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\/matrixverwarring-in-r\/","og_locale":"de_DE","og_type":"article","og_title":"Een verwarringsmatrix cre\u00ebren in R (stap voor stap)","og_description":"In deze tutorial wordt uitgelegd hoe u een verwarringsmatrix maakt in R, inclusief een stapsgewijs voorbeeld.","og_url":"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/","og_site_name":"Statorials","article_published_time":"2023-07-25T19:42:15+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/confusionr1.png"}],"author":"Dr.benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr.benjamin anderson","Gesch\u00e4tzte Lesezeit":"2\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/","url":"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/","name":"Een verwarringsmatrix cre\u00ebren in R (stap voor stap)","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-25T19:42:15+00:00","dateModified":"2023-07-25T19:42:15+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt uitgelegd hoe u een verwarringsmatrix maakt in R, inclusief een stapsgewijs voorbeeld.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/matrixverwarring-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Een verwarringsmatrix cre\u00ebren in r (stap voor stap)"}]},{"@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\/1576","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=1576"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/1576\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=1576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=1576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=1576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}