{"id":917,"date":"2023-07-28T07:59:26","date_gmt":"2023-07-28T07:59:26","guid":{"rendered":"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/"},"modified":"2023-07-28T07:59:26","modified_gmt":"2023-07-28T07:59:26","slug":"mahalanobis-afstand-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/","title":{"rendered":"Hoe de mahalanobis-afstand in r te berekenen"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">De <strong>Mahalanobis-afstand<\/strong> is de afstand tussen twee punten in een multivariate ruimte.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Het wordt vaak gebruikt om uitschieters te detecteren in statistische analyses waarbij meerdere variabelen betrokken zijn.<\/span><\/p>\n<p> <span style=\"color: #000000;\">In deze tutorial wordt uitgelegd hoe u de Mahalanobis-afstand in R kunt berekenen.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld: Mahalanobis-afstand in R<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Gebruik de volgende stappen om de Mahalanobis-afstand voor elke <a href=\"https:\/\/statorials.org\/nl\/observatie-in-de-statistiek\/\" target=\"_blank\" rel=\"noopener\">waarneming<\/a> in een dataset in R te berekenen.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Stap 1: Maak de gegevensset.<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Eerst gaan we een dataset maken die de examenscores van 20 studenten weergeeft, samen met het aantal uren dat ze hebben gestudeerd, het aantal oefenexamens dat ze hebben afgelegd en hun huidige cijfer voor de cursus:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create data<\/span>\ndf = data.frame(score = c(91, 93, 72, 87, 86, 73, 68, 87, 78, 99, 95, 76, 84, 96, 76, 80, 83, 84, 73, 74) ,\n        hours = c(16, 6, 3, 1, 2, 3, 2, 5, 2, 5, 2, 3, 4, 3, 3, 3, 4, 3, 4, 4),\n        prep = c(3, 4, 0, 3, 4, 0, 1, 2, 1, 2, 3, 3, 3, 2, 2, 2, 3, 3, 2, 2),\n        grade = c(70, 88, 80, 83, 88, 84, 78, 94, 90, 93, 89, 82, 95, 94, 81, 93, 93, 90, 89, 89))\n\n<span style=\"color: #008080;\">#view first six rows of data\n<\/span>head(df)\n\n  score hours prep grade\n1 91 16 3 70\n2 93 6 4 88\n3 72 3 0 80\n4 87 1 3 83\n5 86 2 4 88\n6 73 3 0 84\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Stap 2: Bereken de Mahalanobis-afstand voor elke waarneming.<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Vervolgens zullen we de in R ingebouwde functie <a href=\"https:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/stats\/html\/mahalanobis.html\" target=\"_blank\" rel=\"noopener noreferrer\">mahalanobis()<\/a> gebruiken om de Mahalanobis-afstand voor elke waarneming te berekenen, waarbij de volgende syntaxis wordt gebruikt:<\/span><\/p>\n<p> <strong><span style=\"color: #000000;\">mahalanobis (x, midden, cov)<\/span><\/strong><\/p>\n<p> <span style=\"color: #000000;\">Goud:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>x:<\/strong> gegevensmatrix<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>midden:<\/strong> gemiddelde vector van de verdeling<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>cov:<\/strong> verdelingscovariantiematrix<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u deze functie voor onze dataset implementeert:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#calculate Mahalanobis distance for each observation<\/span>\nmahalanobis(df, colMeans(df), cov(df))\n\n [1] 16.5019630 2.6392864 4.8507973 5.2012612 3.8287341 4.0905633\n [7] 4.2836303 2.4198736 1.6519576 5.6578253 3.9658770 2.9350178\n[13] 2.8102109 4.3682945 1.5610165 1.4595069 2.0245748 0.7502536\n[19] 2.7351292 2.2642268\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Stap 3: Bereken de p-waarde voor elke Mahalanobis-afstand.<\/strong><\/span> <\/p>\n<div id=\"mv-first-video\" class=\"mv-video-target mv-video__featured mv-video__optimized mv-video__embedded\" data-video-id=\"tmdycgxksxqpmqpu0vbx\" data-autoplay=\"true\" data-automobile=\"true\" data-muted=\"true\" data-sticky=\"true\" data-autoinsert=\"true\" data-disable-jsonld=\"true\" data-video-placement-inited=\"true\">\n<div id=\"mediavine-video__target-div__8330b88d-7ed9-463c-85d3-dc370a4c9ab3\" class=\"mediavine-video__target-div\">\n<div class=\"mediavine-video__root-container\">\n<div class=\"mediavine-video__placement-container mediavine-video__bottom mediavine-video__right mediavine-video__dark-theme\">\n<div class=\"mediavine-video__title-bar\">\n<p> <span style=\"color: #000000;\">We kunnen zien dat sommige Mahalanobis-afstanden veel groter zijn dan andere.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Om te bepalen of een van de afstanden statistisch significant is, moeten we hun <a href=\"https:\/\/statorials.org\/nl\/p-waarden-statistische-significantie\/\" target=\"_blank\" rel=\"noopener\">p-waarden<\/a> berekenen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">De p-waarde voor elke afstand wordt berekend als de p-waarde die overeenkomt met de chi-kwadraatstatistiek van de Mahalanobis-afstand met k-1 vrijheidsgraden, waarbij k = aantal variabelen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">In dit geval gebruiken we dus vrijheidsgraden van 4-1 = 3.<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create new column in data frame to hold Mahalanobis distances<\/span>\ndf$mahal &lt;- mahalanobis(df, colMeans(df), cov(df))\n\n<span style=\"color: #008080;\">#create new column in data frame to hold p-value for each Mahalanobis distance\n<\/span>df$p &lt;- <span style=\"color: #3366ff;\">pchisq<\/span> (df$mahal, df= <span style=\"color: #339966;\">3<\/span> , lower.tail=FALSE)\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n   score hours prep grade mahal p\n1 91 16 3 70 16.5019630 0.0008945642\n2 93 6 4 88 2.6392864 0.4506437265\n3 72 3 0 80 4.8507973 0.1830542407\n4 87 1 3 83 5.2012612 0.1576392526\n5 86 2 4 88 3.8287341 0.2805615121\n6 73 3 0 84 4.0905633 0.2518495222\n7 68 2 1 78 4.2836303 0.2324211504\n8 87 5 2 94 2.4198736 0.4899458807\n9 78 2 1 90 1.6519576 0.6476670033\n10 99 5 2 93 5.6578253 0.1294978092\n11 95 2 3 89 3.9658770 0.2651724541\n12 76 3 3 82 2.9350178 0.4017530495\n13 84 4 3 95 2.8102109 0.4218217836\n14 96 3 2 94 4.3682945 0.2243432904\n15 76 3 2 81 1.5610165 0.6682610031\n16 80 3 2 93 1.4595069 0.6916471506\n17 83 4 3 93 2.0245748 0.5673218169\n18 84 3 3 90 0.7502536 0.8613248635\n19 73 4 2 89 2.7351292 0.4342904353\n20 74 4 2 89 2.2642268 0.5194087143\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Over het algemeen wordt een p-waarde <strong>kleiner dan 0,001<\/strong> als een uitbijter beschouwd.<\/span><\/p>\n<p> <span style=\"color: #000000;\">We kunnen zien dat de eerste waarneming een uitbijter in de dataset is, omdat deze een p-waarde van minder dan 0,001 heeft.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Afhankelijk van de context van het probleem kunt u besluiten deze waarneming uit de dataset te verwijderen, omdat deze een uitschieter is en de analyseresultaten zou kunnen be\u00efnvloeden.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\"><b>Gerelateerd:<\/b><\/span> <a href=\"https:\/\/statorials.org\/nl\/multivariate-normaliteitstest-r\/\" target=\"_blank\" rel=\"noopener noreferrer\">Multivariate normaliteitstests uitvoeren in R<\/a><\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>De Mahalanobis-afstand is de afstand tussen twee punten in een multivariate ruimte. Het wordt vaak gebruikt om uitschieters te detecteren in statistische analyses waarbij meerdere variabelen betrokken zijn. In deze tutorial wordt uitgelegd hoe u de Mahalanobis-afstand in R kunt berekenen. Voorbeeld: Mahalanobis-afstand in R Gebruik de volgende stappen om de Mahalanobis-afstand voor elke waarneming [&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-917","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 de Mahalanobis-afstand te berekenen in R - Statorials<\/title>\n<meta name=\"description\" content=\"Een eenvoudige uitleg over het berekenen van de Mahalanobis-afstand in R, met een 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\/mahalanobis-afstand-r\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe de Mahalanobis-afstand te berekenen in R - Statorials\" \/>\n<meta property=\"og:description\" content=\"Een eenvoudige uitleg over het berekenen van de Mahalanobis-afstand in R, met een voorbeeld.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T07:59:26+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=\"2\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/\",\"url\":\"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/\",\"name\":\"Hoe de Mahalanobis-afstand te berekenen in R - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-28T07:59:26+00:00\",\"dateModified\":\"2023-07-28T07:59:26+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"Een eenvoudige uitleg over het berekenen van de Mahalanobis-afstand in R, met een voorbeeld.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoe de mahalanobis-afstand in r te berekenen\"}]},{\"@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 de Mahalanobis-afstand te berekenen in R - Statorials","description":"Een eenvoudige uitleg over het berekenen van de Mahalanobis-afstand in R, met een 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\/mahalanobis-afstand-r\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe de Mahalanobis-afstand te berekenen in R - Statorials","og_description":"Een eenvoudige uitleg over het berekenen van de Mahalanobis-afstand in R, met een voorbeeld.","og_url":"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/","og_site_name":"Statorials","article_published_time":"2023-07-28T07:59:26+00:00","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\/mahalanobis-afstand-r\/","url":"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/","name":"Hoe de Mahalanobis-afstand te berekenen in R - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-28T07:59:26+00:00","dateModified":"2023-07-28T07:59:26+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"Een eenvoudige uitleg over het berekenen van de Mahalanobis-afstand in R, met een voorbeeld.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/mahalanobis-afstand-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Hoe de mahalanobis-afstand in r te berekenen"}]},{"@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\/917","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=917"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/917\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}