{"id":8088,"date":"2023-11-02T10:12:05","date_gmt":"2023-11-02T18:12:05","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=8088"},"modified":"2025-04-24T17:04:46","modified_gmt":"2025-04-24T17:04:46","slug":"how-to-write-your-comet-experiments-in-r","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r\/","title":{"rendered":"How to Write your Comet Experiments in R"},"content":{"rendered":"\n<div class=\"fl fm fn fo fp\">\n<div class=\"ab ca\">\n<div class=\"ch bg ex ey ez fa\">\n<figure class=\"mt mu mv mw mx my mq mr paragraph-image\">\n<div class=\"mz na ee nb bg nc\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg nd ne c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*C171C_IaEDAaS0gx\" alt=\"\" width=\"700\" height=\"465\"><\/figure><div class=\"mq mr ms\"><picture><\/picture><\/div>\n<\/div><figcaption class=\"nf ng nh mq mr ni nj be b bf z dw\" data-selectable-paragraph=\"\">Photo by <a class=\"af nk\" href=\"https:\/\/unsplash.com\/@olloweb?utm_source=medium&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener ugc nofollow\">Agence Olloweb<\/a> on <a class=\"af nk\" href=\"https:\/\/unsplash.com\/?utm_source=medium&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener ugc nofollow\">Unsplash<\/a><\/figcaption><\/figure>\n<p data-selectable-paragraph=\"\">\n<\/p><p id=\"45c6\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\"><a class=\"af nk\" href=\"https:\/\/www.comet.com\/site\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Comet<\/a> is an experimentation platform which permits you to track, monitor, and compare your Data Science experiments. So far, I have tested Comet using Python as a programming language. Recently, I have discovered that Comet also provides a plugin package for R and Java. This is very useful because you can continue to program using your preferred language and then track your experiments in Comet.<\/p>\n<p id=\"4dee\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">Comet provides an R package, named <code class=\"cw og oh oi oj b\">cometr<\/code>, which permits you to create and manage an experiment object. You can install the package as follows:<\/p>\n<pre class=\"mt mu mv mw mx ok oj ol om ax on bj\"><span id=\"c225\" class=\"oo op fs oj b ii oq or l jb os\" data-selectable-paragraph=\"\">install.packages(\"cometr\")<\/span><\/pre>\n<p id=\"87e1\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">For more details on the <code class=\"cw og oh oi oj b\">cometr<\/code> package, you can refer to the <a class=\"af nk\" href=\"https:\/\/www.comet.com\/docs\/r-sdk\/getting-started\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Comet official documentation<\/a>. To make your package work, you need to configure the KEY API, as described in the Comet official documentation.<\/p>\n<p id=\"e9b7\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">In this article, I describe how to run your Comet experiments in R through a practical example. The article is organized as follows:<\/p>\n<ul class=\"\">\n<li id=\"4306\" class=\"nl nm fs be b gv nn no np gy nq nr ns nt ot nv nw nx ou nz oa ob ov od oe of ow ox oy bj\" data-selectable-paragraph=\"\">Setup of the scenario<\/li>\n<li id=\"7794\" class=\"nl nm fs be b gv oz no np gy pa nr ns nt pb nv nw nx pc nz oa ob pd od oe of ow ox oy bj\" data-selectable-paragraph=\"\">Writing the experiments<\/li>\n<li id=\"fc9f\" class=\"nl nm fs be b gv oz no np gy pa nr ns nt pb nv nw nx pc nz oa ob pd od oe of ow ox oy bj\" data-selectable-paragraph=\"\">Comparing the experiments in Comet<\/li>\n<\/ul>\n<h2 id=\"1019\" class=\"pe op fs be pf pg ph gx pi pj pk ha pl pm pn po pp pq pr ps pt pu pv pw px py bj\">Setup of the Scenario<\/h2>\n<p id=\"3934\" class=\"pw-post-body-paragraph nl nm fs be b gv pz no np gy qa nr ns nt qb nv nw nx qc nz oa ob qd od oe of fl bj\" data-selectable-paragraph=\"\">As a use case, I will use the <a class=\"af nk\" href=\"https:\/\/www.kaggle.com\/datasets\/yasserh\/breast-cancer-dataset\" target=\"_blank\" rel=\"noopener ugc nofollow\">breast cancer dataset<\/a> available on Kaggle under the CC0 public license. The idea is to build a classification task, which returns the diagnosis (benign\/malignant), given some input features. We build two experiments and compare them in Comet. The first experiment uses a K-Nearest Neighbors classifier, the second one uses a decision tree classifier. I use the <code class=\"cw og oh oi oj b\">caret<\/code> library to implement the two classifiers.<\/p>\n<p id=\"973b\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">Firstly, I import all the required libraries:<\/p>\n<pre class=\"mt mu mv mw mx ok oj ol om ax on bj\"><span id=\"f029\" class=\"oo op fs oj b ii oq or l jb os\" data-selectable-paragraph=\"\">library(cometr)\nlibrary(caret)\nlibrary(Metrics)<\/span><\/pre>\n<p id=\"1fac\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">Then, I load the dataset:<\/p>\n<pre class=\"mt mu mv mw mx ok oj ol om ax on bj\"><span id=\"ae09\" class=\"oo op fs oj b ii oq or l jb os\" data-selectable-paragraph=\"\">df &lt;- read.csv('..\/source\/breast-cancer.csv')<\/span><\/pre>\n<p id=\"b067\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">The dataset contains 569 rows and 32 columns. The target class is named <code class=\"cw og oh oi oj b\">diagnosis<\/code>.<\/p>\n<p id=\"16bc\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">The following figure shows the list of columns available in the dataset:<\/p>\n<figure class=\"mt mu mv mw mx my mq mr paragraph-image\">\n<div class=\"mz na ee nb bg nc\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg nd ne c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*xCUyOhhn49VSnia8ByxEug.png\" alt=\"\" width=\"700\" height=\"235\"><\/figure><div class=\"mq mr qe\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*xCUyOhhn49VSnia8ByxEug.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*xCUyOhhn49VSnia8ByxEug.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*xCUyOhhn49VSnia8ByxEug.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*xCUyOhhn49VSnia8ByxEug.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*xCUyOhhn49VSnia8ByxEug.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*xCUyOhhn49VSnia8ByxEug.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*xCUyOhhn49VSnia8ByxEug.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*xCUyOhhn49VSnia8ByxEug.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*xCUyOhhn49VSnia8ByxEug.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*xCUyOhhn49VSnia8ByxEug.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*xCUyOhhn49VSnia8ByxEug.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*xCUyOhhn49VSnia8ByxEug.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*xCUyOhhn49VSnia8ByxEug.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*xCUyOhhn49VSnia8ByxEug.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"nf ng nh mq mr ni nj be b bf z dw\" data-selectable-paragraph=\"\">Image by Author<\/figcaption>\n<\/figure>\n<p id=\"604d\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">Now, I encode the target class:<\/p>\n<pre class=\"mt mu mv mw mx ok oj ol om ax on bj\"><span id=\"4fb1\" class=\"oo op fs oj b ii oq or l jb os\" data-selectable-paragraph=\"\">df$diagnosis &lt;- as.factor(df$diagnosis)<\/span><\/pre>\n<p id=\"5aa7\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">I configure the seed, to make the experiment reproducible:<\/p>\n<pre class=\"mt mu mv mw mx ok oj ol om ax on bj\"><span id=\"42be\" class=\"oo op fs oj b ii oq or l jb os\" data-selectable-paragraph=\"\">set.seed(30)<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"fl fm fn fo fp\">\n<div class=\"ab ca\">\n<div class=\"ch bg ex ey ez fa\">\n<blockquote class=\"qn\"><p id=\"e3d1\" class=\"qo qp fs be qq qr qs qt qu qv qw of dw\" data-selectable-paragraph=\"\">Most projects fail before they get to production. <a class=\"af nk\" href=\"https:\/\/go.comet.ml\/ebook-Building-Effective-ML-Teams.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">Check out our free ebook<\/a> to learn how to implement an MLOps lifecycle to better monitor, train, and deploy your machine learning models to increase output and iteration.<\/p><\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"fl fm fn fo fp\">\n<div class=\"ab ca\">\n<div class=\"ch bg ex ey ez fa\">\n<h2 id=\"9dca\" class=\"pe op fs be pf pg qx gx pi pj qy ha pl pm qz po pp pq ra ps pt pu rb pw px py bj\">Writing the Experiments<\/h2>\n<p id=\"6d99\" class=\"pw-post-body-paragraph nl nm fs be b gv pz no np gy qa nr ns nt qb nv nw nx qc nz oa ob qd od oe of fl bj\" data-selectable-paragraph=\"\">For each classifier, I create an experiment:<\/p>\n<pre class=\"mt mu mv mw mx ok oj ol om ax on bj\"><span id=\"ed3f\" class=\"oo op fs oj b ii oq or l jb os\" data-selectable-paragraph=\"\">exp &lt;- create_experiment()<\/span><\/pre>\n<p id=\"b9a8\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">I calculate an auxiliary variable, which stores the size of the dataset:<\/p>\n<pre class=\"mt mu mv mw mx ok oj ol om ax on bj\"><span id=\"680e\" class=\"oo op fs oj b ii oq or l jb os\" data-selectable-paragraph=\"\">n &lt;- dim(df)[1]<\/span><\/pre>\n<p id=\"2049\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">I train the classifier using different batch values, from 200 to the number of rows, and for each batch:<\/p>\n<ul class=\"\">\n<li id=\"e600\" class=\"nl nm fs be b gv nn no np gy nq nr ns nt ot nv nw nx ou nz oa ob ov od oe of ow ox oy bj\" data-selectable-paragraph=\"\">I split the dataset into training and test (through the <code class=\"cw og oh oi oj b\">createDataPartition()<\/code> function).<\/li>\n<li id=\"13ee\" class=\"nl nm fs be b gv oz no np gy pa nr ns nt pb nv nw nx pc nz oa ob pd od oe of ow ox oy bj\" data-selectable-paragraph=\"\">I perform hyperparameter tuning by specifying the train control (through the <code class=\"cw og oh oi oj b\">trainControl()<\/code> function.<\/li>\n<li id=\"41bc\" class=\"nl nm fs be b gv oz no np gy pa nr ns nt pb nv nw nx pc nz oa ob pd od oe of ow ox oy bj\" data-selectable-paragraph=\"\">I train the model through the <code class=\"cw og oh oi oj b\">train()<\/code> function which receives as input the training set.<\/li>\n<li id=\"151f\" class=\"nl nm fs be b gv oz no np gy pa nr ns nt pb nv nw nx pc nz oa ob pd od oe of ow ox oy bj\" data-selectable-paragraph=\"\">I test the model through the <code class=\"cw og oh oi oj b\">predict()<\/code> function which receives as input the test set.<\/li>\n<li id=\"2403\" class=\"nl nm fs be b gv oz no np gy pa nr ns nt pb nv nw nx pc nz oa ob pd od oe of ow ox oy bj\" data-selectable-paragraph=\"\">I calculate the model accuracy.<\/li>\n<li id=\"062a\" class=\"nl nm fs be b gv oz no np gy pa nr ns nt pb nv nw nx pc nz oa ob pd od oe of ow ox oy bj\" data-selectable-paragraph=\"\">I track the accuracy in Comet through the <code class=\"cw og oh oi oj b\">log_metric()<\/code> method provided by the experiment class.<\/li>\n<li id=\"6291\" class=\"nl nm fs be b gv oz no np gy pa nr ns nt pb nv nw nx pc nz oa ob pd od oe of ow ox oy bj\" data-selectable-paragraph=\"\">I add a tag that specifies the used classifier. I use the <code class=\"cw og oh oi oj b\">add_tags()<\/code> method provided by the experiment class.<\/li>\n<\/ul>\n<p id=\"46cd\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">The following code implements the previous steps:<\/p>\n<pre class=\"mt mu mv mw mx ok oj ol om ax on bj\"><span id=\"bd77\" class=\"oo op fs oj b ii oq or l jb os\" data-selectable-paragraph=\"\">for (i in seq(200, n+1, by=50)) {\n    if(i &gt; n)\n      i = n\n    dft &lt;- df[c(1:i),]\n\n    index &lt;- <strong class=\"oj gc\">createDataPartition<\/strong>(y = dft$diagnosis, times = 1, p = 0.8, list = FALSE)\n    training_set &lt;- dft[index,]\n    test_set &lt;- dft[-index,]\n    trControl &lt;- <strong class=\"oj gc\">trainControl<\/strong>(method = \"repeatedcv\",number = 10,repeats = 10)\n    model &lt;- <strong class=\"oj gc\">train<\/strong>(diagnosis ~ ., method=method, data = training_set, metric='Accuracy',\n                   preProcess = c(\"center\",\"scale\"), trControl=trControl)\n\n    test_set$pred &lt;- <strong class=\"oj gc\">predict<\/strong>(model, test_set)\n\n    acc &lt;- <strong class=\"oj gc\">accuracy<\/strong>(test_set$diagnosis, test_set$pred)\n\n    exp$<strong class=\"oj gc\">log_metric<\/strong>(\"accuracy\", acc, step=i)\n    exp$<strong class=\"oj gc\">add_tags<\/strong>(list(method))<\/span><\/pre>\n<p id=\"29b8\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">I can wrap the previous code into a for loop to work with both the classifiers:<\/p>\n<pre class=\"mt mu mv mw mx ok oj ol om ax on bj\"><span id=\"723b\" class=\"oo op fs oj b ii oq or l jb os\" data-selectable-paragraph=\"\">for (method in c('knn', 'rpart')){\n  exp &lt;- create_experiment()\n  # previous for loop\n  exp$stop()<\/span><span id=\"d656\" class=\"oo op fs oj b ii rc or l jb os\" data-selectable-paragraph=\"\">}<\/span><\/pre>\n<p id=\"29f7\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">Note that to stop an experiment, I use the <code class=\"cw og oh oi oj b\">stop()<\/code> method available for the experiment object.<\/p>\n<h2 id=\"dd60\" class=\"pe op fs be pf pg ph gx pi pj pk ha pl pm pn po pp pq pr ps pt pu pv pw px py bj\">Comparing the Experiments in Comet<\/h2>\n<p id=\"c184\" class=\"pw-post-body-paragraph nl nm fs be b gv pz no np gy qa nr ns nt qb nv nw nx qc nz oa ob qd od oe of fl bj\" data-selectable-paragraph=\"\">I run the code in R and then I can check the results in Comet. The Comet dashboard looks like this:<\/p>\n<figure class=\"mt mu mv mw mx my mq mr paragraph-image\">\n<div class=\"mz na ee nb bg nc\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg nd ne c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*2QYJoKpJujf783vmzGWShA.png\" alt=\"\" width=\"700\" height=\"283\"><\/figure><div class=\"mq mr rd\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*2QYJoKpJujf783vmzGWShA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*2QYJoKpJujf783vmzGWShA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*2QYJoKpJujf783vmzGWShA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*2QYJoKpJujf783vmzGWShA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*2QYJoKpJujf783vmzGWShA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*2QYJoKpJujf783vmzGWShA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*2QYJoKpJujf783vmzGWShA.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*2QYJoKpJujf783vmzGWShA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*2QYJoKpJujf783vmzGWShA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*2QYJoKpJujf783vmzGWShA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*2QYJoKpJujf783vmzGWShA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*2QYJoKpJujf783vmzGWShA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*2QYJoKpJujf783vmzGWShA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*2QYJoKpJujf783vmzGWShA.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"nf ng nh mq mr ni nj be b bf z dw\" data-selectable-paragraph=\"\">Image by Author<\/figcaption>\n<\/figure>\n<p id=\"c4e9\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">On the left, there are the two experiments, while on the right there is the chart showing the accuracy trend for the two experiments. To compare the two experiments, I select them on the left, and then I click the <strong class=\"be re\">Diff<\/strong> button. I select the Metrics menu item on the left, as shown in the following figure:<\/p>\n<figure class=\"mt mu mv mw mx my mq mr paragraph-image\">\n<div class=\"mz na ee nb bg nc\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg nd ne c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*hGOIm7wm15lt_YHpXidSsA.png\" alt=\"\" width=\"700\" height=\"280\"><\/figure><div class=\"mq mr rf\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*hGOIm7wm15lt_YHpXidSsA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*hGOIm7wm15lt_YHpXidSsA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*hGOIm7wm15lt_YHpXidSsA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*hGOIm7wm15lt_YHpXidSsA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*hGOIm7wm15lt_YHpXidSsA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*hGOIm7wm15lt_YHpXidSsA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*hGOIm7wm15lt_YHpXidSsA.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*hGOIm7wm15lt_YHpXidSsA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*hGOIm7wm15lt_YHpXidSsA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*hGOIm7wm15lt_YHpXidSsA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*hGOIm7wm15lt_YHpXidSsA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*hGOIm7wm15lt_YHpXidSsA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*hGOIm7wm15lt_YHpXidSsA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*hGOIm7wm15lt_YHpXidSsA.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"nf ng nh mq mr ni nj be b bf z dw\" data-selectable-paragraph=\"\">Image by Author<\/figcaption>\n<\/figure>\n<p data-selectable-paragraph=\"\">\n<\/p><p id=\"95ba\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">I note that KNN outperforms the Decision Tree algorithm.<\/p>\n<h2 id=\"f294\" class=\"pe op fs be pf pg ph gx pi pj pk ha pl pm pn po pp pq pr ps pt pu pv pw px py bj\">Summary<\/h2>\n<p id=\"8c6f\" class=\"pw-post-body-paragraph nl nm fs be b gv pz no np gy qa nr ns nt qb nv nw nx qc nz oa ob qd od oe of fl bj\" data-selectable-paragraph=\"\">Congratulations! You have just learned how to run your Comet experiments in R. The procedure is very simple: Firstly, you import the <code class=\"cw og oh oi oj b\">cometr<\/code> package, then you create an experiment through the <code class=\"cw og oh oi oj b\">create_experiment()<\/code> function. After, you log your metrics or objects, and, finally, you stop your experiment through the <code class=\"cw og oh oi oj b\">stop()<\/code> method.<\/p>\n<p id=\"3f99\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">Once you run your experiment, you can check the results in Comet. This is very useful!<\/p>\n<p id=\"2d83\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">You can download the code of this article from my <a class=\"af nk\" href=\"https:\/\/github.com\/alod83\/data-science-r\/blob\/main\/DataAnalysis\/CometClassification.R\" target=\"_blank\" rel=\"noopener ugc nofollow\">Github repository<\/a>, and you can see the live experiment directly in Comet at <a class=\"af nk\" href=\"https:\/\/www.comet.com\/comet-examples\/r-classification\" target=\"_blank\" rel=\"noopener ugc nofollow\">this link<\/a>.<\/p>\n<p id=\"d8c7\" class=\"pw-post-body-paragraph nl nm fs be b gv nn no np gy nq nr ns nt nu nv nw nx ny nz oa ob oc od oe of fl bj\" data-selectable-paragraph=\"\">Happy coding! Happy Comet!<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Photo by Agence Olloweb on Unsplash Comet is an experimentation platform which permits you to track, monitor, and compare your Data Science experiments. So far, I have tested Comet using Python as a programming language. Recently, I have discovered that Comet also provides a plugin package for R and Java. This is very useful because [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","footnotes":""},"categories":[9,7],"tags":[],"coauthors":[132],"class_list":["post-8088","post","type-post","status-publish","format-standard","hentry","category-product","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Use Comet and R to Write Experiments<\/title>\n<meta name=\"description\" content=\"Learn how to run your Comet experiments in R through a practical example. Follow the step by step tutorial in this article.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Write your Comet Experiments in R\" \/>\n<meta property=\"og:description\" content=\"Learn how to run your Comet experiments in R through a practical example. Follow the step by step tutorial in this article.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-02T18:12:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:04:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*C171C_IaEDAaS0gx\" \/>\n<meta name=\"author\" content=\"Angelica Lo Duca\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Angelica Lo Duca\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Use Comet and R to Write Experiments","description":"Learn how to run your Comet experiments in R through a practical example. Follow the step by step tutorial in this article.","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:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r","og_locale":"en_US","og_type":"article","og_title":"How to Write your Comet Experiments in R","og_description":"Learn how to run your Comet experiments in R through a practical example. Follow the step by step tutorial in this article.","og_url":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2023-11-02T18:12:05+00:00","article_modified_time":"2025-04-24T17:04:46+00:00","og_image":[{"url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*C171C_IaEDAaS0gx","type":"","width":"","height":""}],"author":"Angelica Lo Duca","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Angelica Lo Duca","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r\/"},"author":{"name":"Team Comet Digital","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/6266601170c60a7a82b3e0043fbe8ddf"},"headline":"How to Write your Comet Experiments in R","datePublished":"2023-11-02T18:12:05+00:00","dateModified":"2025-04-24T17:04:46+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r\/"},"wordCount":691,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*C171C_IaEDAaS0gx","articleSection":["Product","Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r\/","url":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r","name":"Use Comet and R to Write Experiments","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*C171C_IaEDAaS0gx","datePublished":"2023-11-02T18:12:05+00:00","dateModified":"2025-04-24T17:04:46+00:00","description":"Learn how to run your Comet experiments in R through a practical example. Follow the step by step tutorial in this article.","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r#primaryimage","url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*C171C_IaEDAaS0gx","contentUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*C171C_IaEDAaS0gx"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/how-to-write-your-comet-experiments-in-r#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"How to Write your Comet Experiments in R"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/6266601170c60a7a82b3e0043fbe8ddf","name":"Team Comet Digital","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/4f0c0a8cc7c0e87c636ff6a420a6647c","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/Screen-Shot-2023-08-12-at-8.58.50-AM-96x96.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/Screen-Shot-2023-08-12-at-8.58.50-AM-96x96.png","caption":"Team Comet Digital"},"sameAs":["https:\/\/www.comet.ml\/"],"url":"https:\/\/www.comet.com\/site\/blog\/author\/teamcometdigital\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=8088"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8088\/revisions"}],"predecessor-version":[{"id":15465,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8088\/revisions\/15465"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=8088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=8088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=8088"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=8088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}